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',
94 public function __construct(
98 LoggerInterface $logger
101 $this->options = $options;
102 $this->namespaceInfo = $namespaceInfo;
103 $this->titleParser = $titleParser;
104 $this->logger = $logger;
107 $this->extensionRegistry = ExtensionRegistry::getInstance();
120 public function getTrackingCategories() {
121 $categories = array_merge(
122 self::CORE_TRACKING_CATEGORIES,
129 if ( $enableMagicLinks[
'ISBN'] ) {
130 $categories[] =
'magiclink-tracking-isbn';
132 if ( $enableMagicLinks[
'RFC'] ) {
133 $categories[] =
'magiclink-tracking-rfc';
135 if ( $enableMagicLinks[
'PMID'] ) {
136 $categories[] =
'magiclink-tracking-pmid';
139 $trackingCategories = [];
140 foreach ( $categories as $catMsg ) {
148 $msgObj =
wfMessage( $catMsg )->inContentLanguage();
150 $catMsgTitle = $this->titleParser->makeTitleValueSafe(
NS_MEDIAWIKI, $catMsg );
151 if ( !$catMsgTitle ) {
157 if ( str_contains( $msgObj->plain(),
'{{' ) ) {
158 $ns = $this->namespaceInfo->getValidNamespaces();
159 foreach ( $ns as $namesp ) {
160 $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
166 $tempTitle = Title::newFromLinkTarget( $tempTitle );
167 $allCats[] = $msgObj->page( $tempTitle )->text();
170 $allCats[] = $msgObj->text();
173 foreach ( $allCats as $catName ) {
175 if ( $catName !==
'-' ) {
176 $catTitle = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $catName );
178 $titles[] = $catTitle;
182 $trackingCategories[$catMsg] = [
184 'msg' => $catMsgTitle,
188 return $trackingCategories;
199 public function resolveTrackingCategory(
string $msg, ?PageReference $contextPage ): ?LinkTarget {
200 if ( !$contextPage ) {
201 $this->logger->debug(
"Not adding tracking category $msg to missing page!" );
205 if ( $contextPage->getNamespace() ===
NS_SPECIAL ) {
206 $this->logger->debug(
"Not adding tracking category $msg to special page!" );
213 ->page( $contextPage )
214 ->inContentLanguage()
217 # Allow tracking categories to be disabled by setting them to "-"
218 if ( $cat ===
'-' ) {
222 $containerCategory = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $cat );
223 if ( $containerCategory ===
null ) {
224 $this->logger->debug(
"[[MediaWiki:$msg]] is not a valid title!" );
227 return $containerCategory;
247 public function addTrackingCategory(
248 ContentMetadataCollector $parserOutput,
250 ?PageReference $contextPage
252 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
253 if ( $categoryPage ===
null ) {
256 $parserOutput->addCategory( $categoryPage );