42 private $namespaceInfo;
48 private $extensionRegistry;
56 private const CORE_TRACKING_CATEGORIES = [
57 'broken-file-category',
58 'duplicate-args-category',
59 'expansion-depth-exceeded-category',
60 'expensive-parserfunction-category',
61 'hidden-category-category',
63 'node-count-exceeded-category',
65 'nonnumeric-formatnum',
66 'post-expand-template-argument-category',
67 'post-expand-template-inclusion-category',
68 'restricted-displaytitle-ignored',
69 # template-equals-category is unused in MW>=1.39, but the category
70 # can be left around for a major release or so for an easier
71 # transition for anyone who didn't do the cleanup. T91154
72 'template-equals-category',
73 'template-loop-category',
74 'unstrip-depth-category',
75 'unstrip-size-category',
76 'bad-language-code-category',
77 'bad-double-underscore-category',
81 public function __construct(
85 LoggerInterface $logger
88 $this->options = $options;
89 $this->namespaceInfo = $namespaceInfo;
90 $this->titleParser = $titleParser;
91 $this->logger = $logger;
94 $this->extensionRegistry = ExtensionRegistry::getInstance();
107 public function getTrackingCategories() {
108 $categories = array_merge(
109 self::CORE_TRACKING_CATEGORIES,
116 if ( $enableMagicLinks[
'ISBN'] ) {
117 $categories[] =
'magiclink-tracking-isbn';
119 if ( $enableMagicLinks[
'RFC'] ) {
120 $categories[] =
'magiclink-tracking-rfc';
122 if ( $enableMagicLinks[
'PMID'] ) {
123 $categories[] =
'magiclink-tracking-pmid';
126 $trackingCategories = [];
127 foreach ( $categories as $catMsg ) {
135 $msgObj =
wfMessage( $catMsg )->inContentLanguage();
137 $catMsgTitle = $this->titleParser->makeTitleValueSafe(
NS_MEDIAWIKI, $catMsg );
138 if ( !$catMsgTitle ) {
144 if ( str_contains( $msgObj->plain(),
'{{' ) ) {
145 $ns = $this->namespaceInfo->getValidNamespaces();
146 foreach ( $ns as $namesp ) {
147 $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
153 $tempTitle = Title::newFromLinkTarget( $tempTitle );
154 $allCats[] = $msgObj->page( $tempTitle )->text();
157 $allCats[] = $msgObj->text();
160 foreach ( $allCats as $catName ) {
162 if ( $catName !==
'-' ) {
163 $catTitle = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $catName );
165 $titles[] = $catTitle;
169 $trackingCategories[$catMsg] = [
171 'msg' => $catMsgTitle,
175 return $trackingCategories;
186 public function resolveTrackingCategory(
string $msg, ?PageReference $contextPage ): ?LinkTarget {
187 if ( !$contextPage ) {
188 $this->logger->debug(
"Not adding tracking category $msg to missing page!" );
192 if ( $contextPage->getNamespace() ===
NS_SPECIAL ) {
193 $this->logger->debug(
"Not adding tracking category $msg to special page!" );
200 ->page( $contextPage )
201 ->inContentLanguage()
204 # Allow tracking categories to be disabled by setting them to "-"
205 if ( $cat ===
'-' ) {
209 $containerCategory = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $cat );
210 if ( $containerCategory ===
null ) {
211 $this->logger->debug(
"[[MediaWiki:$msg]] is not a valid title!" );
214 return $containerCategory;
234 public function addTrackingCategory(
235 ContentMetadataCollector $parserOutput,
237 ?PageReference $contextPage
239 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
240 if ( $categoryPage ===
null ) {
243 $parserOutput->addCategory( $categoryPage );