MediaWiki master
TrackingCategories.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Category;
23
33use Psr\Log\LoggerInterface;
34use Wikimedia\Parsoid\Core\ContentMetadataCollector;
35
43
47 public const CONSTRUCTOR_OPTIONS = [
50 ];
51
53 private $options;
54
56 private $namespaceInfo;
57
59 private $titleParser;
60
62 private $extensionRegistry;
63
65 private $logger;
66
70 private const CORE_TRACKING_CATEGORIES = [
71 'broken-file-category',
72 'duplicate-args-category',
73 'expansion-depth-exceeded-category',
74 'expensive-parserfunction-category',
75 'hidden-category-category',
76 'index-category',
77 'node-count-exceeded-category',
78 'noindex-category',
79 'nonnumeric-formatnum',
80 'post-expand-template-argument-category',
81 'post-expand-template-inclusion-category',
82 'restricted-displaytitle-ignored',
83 # template-equals-category is unused in MW>=1.39, but the category
84 # can be left around for a major release or so for an easier
85 # transition for anyone who didn't do the cleanup. T91154
86 'template-equals-category',
87 'template-loop-category',
88 'unstrip-depth-category',
89 'unstrip-size-category',
90 'bad-language-code-category',
91 'double-px-category',
92 ];
93
94 public function __construct(
95 ServiceOptions $options,
96 NamespaceInfo $namespaceInfo,
97 TitleParser $titleParser,
98 LoggerInterface $logger
99 ) {
100 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
101 $this->options = $options;
102 $this->namespaceInfo = $namespaceInfo;
103 $this->titleParser = $titleParser;
104 $this->logger = $logger;
105
106 // TODO convert ExtensionRegistry to a service and inject it
107 $this->extensionRegistry = ExtensionRegistry::getInstance();
108 }
109
120 public function getTrackingCategories() {
121 $categories = array_merge(
122 self::CORE_TRACKING_CATEGORIES,
123 $this->extensionRegistry->getAttribute( MainConfigNames::TrackingCategories ),
124 $this->options->get( MainConfigNames::TrackingCategories ) // deprecated
125 );
126
127 // Only show magic link tracking categories if they are enabled
128 $enableMagicLinks = $this->options->get( MainConfigNames::EnableMagicLinks );
129 if ( $enableMagicLinks['ISBN'] ) {
130 $categories[] = 'magiclink-tracking-isbn';
131 }
132 if ( $enableMagicLinks['RFC'] ) {
133 $categories[] = 'magiclink-tracking-rfc';
134 }
135 if ( $enableMagicLinks['PMID'] ) {
136 $categories[] = 'magiclink-tracking-pmid';
137 }
138
139 $trackingCategories = [];
140 foreach ( $categories as $catMsg ) {
141 /*
142 * Check if the tracking category varies by namespace
143 * Otherwise only pages in the current namespace will be displayed
144 * If it does vary, show pages considering all namespaces
145 *
146 * TODO replace uses of wfMessage with an injected service once that is available
147 */
148 $msgObj = wfMessage( $catMsg )->inContentLanguage();
149 $allCats = [];
150 $catMsgTitle = $this->titleParser->makeTitleValueSafe( NS_MEDIAWIKI, $catMsg );
151 if ( !$catMsgTitle ) {
152 continue;
153 }
154
155 // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
156 // False positives are ok, this is just an efficiency shortcut
157 if ( str_contains( $msgObj->plain(), '{{' ) ) {
158 $ns = $this->namespaceInfo->getValidNamespaces();
159 foreach ( $ns as $namesp ) {
160 $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
161 if ( !$tempTitle ) {
162 continue;
163 }
164 // XXX: should be a better way to convert a TitleValue
165 // to a PageReference!
166 $tempTitle = Title::newFromLinkTarget( $tempTitle );
167 $allCats[] = $msgObj->page( $tempTitle )->text();
168 }
169 } else {
170 $allCats[] = $msgObj->text();
171 }
172 $titles = [];
173 foreach ( $allCats as $catName ) {
174 // Extra check in case a message does fancy stuff with {{#if:… and such
175 if ( $catName !== '-' ) {
176 $catTitle = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $catName );
177 if ( $catTitle ) {
178 $titles[] = $catTitle;
179 }
180 }
181 }
182 $trackingCategories[$catMsg] = [
183 'cats' => $titles,
184 'msg' => $catMsgTitle,
185 ];
186 }
187
188 return $trackingCategories;
189 }
190
199 public function resolveTrackingCategory( string $msg, ?PageReference $contextPage ): ?LinkTarget {
200 if ( !$contextPage ) {
201 $this->logger->debug( "Not adding tracking category $msg to missing page!" );
202 return null;
203 }
204
205 if ( $contextPage->getNamespace() === NS_SPECIAL ) {
206 $this->logger->debug( "Not adding tracking category $msg to special page!" );
207 return null;
208 }
209
210 // Important to parse with correct title (T33469)
211 // TODO replace uses of wfMessage with an injected service once that is available
212 $cat = wfMessage( $msg )
213 ->page( $contextPage )
214 ->inContentLanguage()
215 ->text();
216
217 # Allow tracking categories to be disabled by setting them to "-"
218 if ( $cat === '-' ) {
219 return null;
220 }
221
222 $containerCategory = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $cat );
223 if ( $containerCategory === null ) {
224 $this->logger->debug( "[[MediaWiki:$msg]] is not a valid title!" );
225 return null;
226 }
227 return $containerCategory;
228 }
229
247 public function addTrackingCategory(
248 ContentMetadataCollector $parserOutput,
249 string $msg,
250 ?PageReference $contextPage
251 ): bool {
252 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
253 if ( $categoryPage === null ) {
254 return false;
255 }
256 $parserOutput->addCategory( $categoryPage );
257 return true;
258 }
259}
260
262class_alias( TrackingCategories::class, 'TrackingCategories' );
const NS_MEDIAWIKI
Definition Defines.php:73
const NS_SPECIAL
Definition Defines.php:54
const NS_CATEGORY
Definition Defines.php:79
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
This class performs some operations related to tracking categories, such as adding a tracking categor...
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
A class containing constants representing the names of configuration variables.
const EnableMagicLinks
Name constant for the EnableMagicLinks setting, for use with Config::get()
const TrackingCategories
Name constant for the TrackingCategories setting, for use with Config::get()
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:145
Load JSON files, and uses a Processor to extract information.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a title within MediaWiki.
Definition Title.php:78
Represents the target of a wiki link.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
A title parser service for MediaWiki.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...