56 private $namespaceInfo;
62 private $extensionRegistry;
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',
77 'node-count-exceeded-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',
100 public function __construct(
104 LoggerInterface $logger
107 $this->options = $options;
108 $this->namespaceInfo = $namespaceInfo;
109 $this->titleParser = $titleParser;
110 $this->logger = $logger;
113 $this->extensionRegistry = ExtensionRegistry::getInstance();
126 public function getTrackingCategories() {
127 $categories = array_merge(
128 self::CORE_TRACKING_CATEGORIES,
135 if ( $enableMagicLinks[
'ISBN'] ) {
136 $categories[] =
'magiclink-tracking-isbn';
138 if ( $enableMagicLinks[
'RFC'] ) {
139 $categories[] =
'magiclink-tracking-rfc';
141 if ( $enableMagicLinks[
'PMID'] ) {
142 $categories[] =
'magiclink-tracking-pmid';
145 $trackingCategories = [];
146 foreach ( $categories as $catMsg ) {
154 $msgObj =
wfMessage( $catMsg )->inContentLanguage();
156 $catMsgTitle = $this->titleParser->makeTitleValueSafe(
NS_MEDIAWIKI, $catMsg );
157 if ( !$catMsgTitle ) {
163 if ( strpos( $msgObj->plain(),
'{{' ) !==
false ) {
164 $ns = $this->namespaceInfo->getValidNamespaces();
165 foreach ( $ns as $namesp ) {
166 $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
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 );
178 $allCats[] = $catTitle;
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 );
188 $allCats[] = $catTitle;
192 $trackingCategories[$catMsg] = [
194 'msg' => $catMsgTitle,
198 return $trackingCategories;
209 public function resolveTrackingCategory(
string $msg, ?PageReference $contextPage ): ?LinkTarget {
210 if ( !$contextPage ) {
211 $this->logger->debug(
"Not adding tracking category $msg to missing page!" );
215 if ( $contextPage->getNamespace() ===
NS_SPECIAL ) {
216 $this->logger->debug(
"Not adding tracking category $msg to special page!" );
223 ->page( $contextPage )
224 ->inContentLanguage()
227 # Allow tracking categories to be disabled by setting them to "-"
228 if ( $cat ===
'-' ) {
232 $containerCategory = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $cat );
233 if ( $containerCategory ===
null ) {
234 $this->logger->debug(
"[[MediaWiki:$msg]] is not a valid title!" );
237 return $containerCategory;
257 public function addTrackingCategory(
258 ContentMetadataCollector $parserOutput,
260 ?PageReference $contextPage
262 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
263 if ( $categoryPage ===
null ) {
266 $parserOutput->addCategory( $categoryPage );