MediaWiki master
TrackingCategories.php
Go to the documentation of this file.
1<?php
22namespace MediaWiki\Category;
23
34use Psr\Log\LoggerInterface;
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
72 private const CORE_TRACKING_CATEGORIES = [
73 'broken-file-category',
74 'duplicate-args-category',
75 'expansion-depth-exceeded-category',
76 'expensive-parserfunction-category',
77 'hidden-category-category',
78 'index-category',
79 'node-count-exceeded-category',
80 'noindex-category',
81 'nonnumeric-formatnum',
82 'post-expand-template-argument-category',
83 'post-expand-template-inclusion-category',
84 'restricted-displaytitle-ignored',
85 # template-equals-category is unused in MW>=1.39, but the category
86 # can be left around for a major release or so for an easier
87 # transition for anyone who didn't do the cleanup. T91154
88 'template-equals-category',
89 'template-loop-category',
90 'unstrip-depth-category',
91 'unstrip-size-category',
92 ];
93
100 public function __construct(
101 ServiceOptions $options,
102 NamespaceInfo $namespaceInfo,
103 TitleParser $titleParser,
104 LoggerInterface $logger
105 ) {
106 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
107 $this->options = $options;
108 $this->namespaceInfo = $namespaceInfo;
109 $this->titleParser = $titleParser;
110 $this->logger = $logger;
111
112 // TODO convert ExtensionRegistry to a service and inject it
113 $this->extensionRegistry = ExtensionRegistry::getInstance();
114 }
115
126 public function getTrackingCategories() {
127 $categories = array_merge(
128 self::CORE_TRACKING_CATEGORIES,
129 $this->extensionRegistry->getAttribute( MainConfigNames::TrackingCategories ),
130 $this->options->get( MainConfigNames::TrackingCategories ) // deprecated
131 );
132
133 // Only show magic link tracking categories if they are enabled
134 $enableMagicLinks = $this->options->get( MainConfigNames::EnableMagicLinks );
135 if ( $enableMagicLinks['ISBN'] ) {
136 $categories[] = 'magiclink-tracking-isbn';
137 }
138 if ( $enableMagicLinks['RFC'] ) {
139 $categories[] = 'magiclink-tracking-rfc';
140 }
141 if ( $enableMagicLinks['PMID'] ) {
142 $categories[] = 'magiclink-tracking-pmid';
143 }
144
145 $trackingCategories = [];
146 foreach ( $categories as $catMsg ) {
147 /*
148 * Check if the tracking category varies by namespace
149 * Otherwise only pages in the current namespace will be displayed
150 * If it does vary, show pages considering all namespaces
151 *
152 * TODO replace uses of wfMessage with an injected service once that is available
153 */
154 $msgObj = wfMessage( $catMsg )->inContentLanguage();
155 $allCats = [];
156 $catMsgTitle = $this->titleParser->makeTitleValueSafe( NS_MEDIAWIKI, $catMsg );
157 if ( !$catMsgTitle ) {
158 continue;
159 }
160
161 // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
162 // False positives are ok, this is just an efficiency shortcut
163 if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
164 $ns = $this->namespaceInfo->getValidNamespaces();
165 foreach ( $ns as $namesp ) {
166 $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
167 if ( !$tempTitle ) {
168 continue;
169 }
170 // XXX: should be a better way to convert a TitleValue
171 // to a PageReference!
172 $tempTitle = Title::newFromLinkTarget( $tempTitle );
173 $catName = $msgObj->page( $tempTitle )->text();
174 # Allow tracking categories to be disabled by setting them to "-"
175 if ( $catName !== '-' ) {
176 $catTitle = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $catName );
177 if ( $catTitle ) {
178 $allCats[] = $catTitle;
179 }
180 }
181 }
182 } else {
183 $catName = $msgObj->text();
184 # Allow tracking categories to be disabled by setting them to "-"
185 if ( $catName !== '-' ) {
186 $catTitle = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $catName );
187 if ( $catTitle ) {
188 $allCats[] = $catTitle;
189 }
190 }
191 }
192 $trackingCategories[$catMsg] = [
193 'cats' => $allCats,
194 'msg' => $catMsgTitle,
195 ];
196 }
197
198 return $trackingCategories;
199 }
200
209 public function resolveTrackingCategory( string $msg, ?PageReference $contextPage ): ?LinkTarget {
210 if ( !$contextPage ) {
211 $this->logger->debug( "Not adding tracking category $msg to missing page!" );
212 return null;
213 }
214
215 if ( $contextPage->getNamespace() === NS_SPECIAL ) {
216 $this->logger->debug( "Not adding tracking category $msg to special page!" );
217 return null;
218 }
219
220 // Important to parse with correct title (T33469)
221 // TODO replace uses of wfMessage with an injected service once that is available
222 $cat = wfMessage( $msg )
223 ->page( $contextPage )
224 ->inContentLanguage()
225 ->text();
226
227 # Allow tracking categories to be disabled by setting them to "-"
228 if ( $cat === '-' ) {
229 return null;
230 }
231
232 $containerCategory = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $cat );
233 if ( $containerCategory === null ) {
234 $this->logger->debug( "[[MediaWiki:$msg]] is not a valid title!" );
235 return null;
236 }
237 return $containerCategory;
238 }
239
257 public function addTrackingCategory( ParserOutput $parserOutput, string $msg, ?PageReference $contextPage ): bool {
258 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
259 if ( $categoryPage === null ) {
260 return false;
261 }
262 $parserOutput->addCategory( $categoryPage->getDBkey() );
263 return true;
264 }
265}
266
268class_alias( TrackingCategories::class, 'TrackingCategories' );
const NS_MEDIAWIKI
Definition Defines.php:72
const NS_SPECIAL
Definition Defines.php:53
const NS_CATEGORY
Definition Defines.php:78
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Load JSON files, and uses a Processor to extract information.
This class performs some operations related to tracking categories, such as adding a tracking categor...
resolveTrackingCategory(string $msg, ?PageReference $contextPage)
Resolve a tracking category.
__construct(ServiceOptions $options, NamespaceInfo $namespaceInfo, TitleParser $titleParser, LoggerInterface $logger)
addTrackingCategory(ParserOutput $parserOutput, string $msg, ?PageReference $contextPage)
Add a tracking category to a ParserOutput, getting the title from a system message.
getTrackingCategories()
Read the global and extract title objects from the corresponding messages.
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()
ParserOutput is a rendering of a Content object or a message.
addCategory( $c, $sort='')
Add a category.
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:156
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.
getNamespace()
Returns the page's namespace number.
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...