42private $namespaceInfo;
48private $extensionRegistry;
56privateconst 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',
81publicfunction __construct(
85 LoggerInterface $logger
88 $this->options = $options;
89 $this->namespaceInfo = $namespaceInfo;
90 $this->titleParser = $titleParser;
91 $this->logger = $logger;
93// TODO convert ExtensionRegistry to a service and inject it 94 $this->extensionRegistry = ExtensionRegistry::getInstance();
107publicfunction getTrackingCategories() {
108 $categories = array_merge(
109 self::CORE_TRACKING_CATEGORIES,
114// Only show magic link tracking categories if they are enabled 116if ( $enableMagicLinks[
'ISBN'] ) {
117 $categories[] =
'magiclink-tracking-isbn';
119if ( $enableMagicLinks[
'RFC'] ) {
120 $categories[] =
'magiclink-tracking-rfc';
122if ( $enableMagicLinks[
'PMID'] ) {
123 $categories[] =
'magiclink-tracking-pmid';
126 $trackingCategories = [];
127foreach ( $categories as $catMsg ) {
129 * Check if the tracking category varies by namespace 130 * Otherwise only pages in the current namespace will be displayed 131 * If it does vary, show pages considering all namespaces 133 * TODO replace uses of wfMessage with an injected service once that is available 135 $msgObj =
wfMessage( $catMsg )->inContentLanguage();
137 $catMsgTitle = $this->titleParser->makeTitleValueSafe(
NS_MEDIAWIKI, $catMsg );
138if ( !$catMsgTitle ) {
142// Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}. 143// False positives are ok, this is just an efficiency shortcut 144if ( str_contains( $msgObj->plain(),
'{{' ) ) {
145 $ns = $this->namespaceInfo->getValidNamespaces();
146foreach ( $ns as $namesp ) {
147 $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
151// XXX: should be a better way to convert a TitleValue 152// to a PageReference! 153 $tempTitle = Title::newFromLinkTarget( $tempTitle );
154 $allCats[] = $msgObj->page( $tempTitle )->text();
157 $allCats[] = $msgObj->text();
160foreach ( $allCats as $catName ) {
161// Extra check in case a message does fancy stuff with {{#if:… and such 162if ( $catName !==
'-' ) {
163 $catTitle = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $catName );
165 $titles[] = $catTitle;
169 $trackingCategories[$catMsg] = [
171'msg' => $catMsgTitle,
175return $trackingCategories;
186publicfunction resolveTrackingCategory(
string $msg, ?PageReference $contextPage ): ?LinkTarget {
187if ( !$contextPage ) {
188 $this->logger->debug(
"Not adding tracking category $msg to missing page!" );
192if ( $contextPage->getNamespace() ===
NS_SPECIAL ) {
193 $this->logger->debug(
"Not adding tracking category $msg to special page!" );
197// Important to parse with correct title (T33469) 198// TODO replace uses of wfMessage with an injected service once that is available 200 ->page( $contextPage )
201 ->inContentLanguage()
204 # Allow tracking categories to be disabled by setting them to "-" 209 $containerCategory = $this->titleParser->makeTitleValueSafe(
NS_CATEGORY, $cat );
210if ( $containerCategory ===
null ) {
211 $this->logger->debug(
"[[MediaWiki:$msg]] is not a valid title!" );
214return $containerCategory;
234publicfunction addTrackingCategory(
235 ContentMetadataCollector $parserOutput,
237 ?PageReference $contextPage
239 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
240if ( $categoryPage ===
null ) {
243 $parserOutput->addCategory( $categoryPage );