Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
TrackingCategories.php
Go to the documentation of this file.
1<?php
8namespaceMediaWiki\Category;
9
10useMediaWiki\Config\ServiceOptions;
11useMediaWiki\Linker\LinkTarget;
12useMediaWiki\MainConfigNames;
13useMediaWiki\Page\PageReference;
14useMediaWiki\Parser\Parser;
15useMediaWiki\Registration\ExtensionRegistry;
16useMediaWiki\Title\NamespaceInfo;
17useMediaWiki\Title\Title;
18useMediaWiki\Title\TitleParser;
19use Psr\Log\LoggerInterface;
20use Wikimedia\Parsoid\Core\ContentMetadataCollector;
21
28classTrackingCategories {
29
33publicconstCONSTRUCTOR_OPTIONS = [
34MainConfigNames::TrackingCategories,
35MainConfigNames::EnableMagicLinks,
36 ];
37
39private $options;
40
42private $namespaceInfo;
43
45private $titleParser;
46
48private $extensionRegistry;
49
51private $logger;
52
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',
62'index-category',
63'node-count-exceeded-category',
64'noindex-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',
78'double-px-category',
79 ];
80
81publicfunction __construct(
82ServiceOptions $options,
83NamespaceInfo $namespaceInfo,
84TitleParser $titleParser,
85 LoggerInterface $logger
86 ) {
87 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
88 $this->options = $options;
89 $this->namespaceInfo = $namespaceInfo;
90 $this->titleParser = $titleParser;
91 $this->logger = $logger;
92
93// TODO convert ExtensionRegistry to a service and inject it
94 $this->extensionRegistry = ExtensionRegistry::getInstance();
95 }
96
107publicfunction getTrackingCategories() {
108 $categories = array_merge(
109 self::CORE_TRACKING_CATEGORIES,
110 $this->extensionRegistry->getAttribute(MainConfigNames::TrackingCategories ),
111 $this->options->get(MainConfigNames::TrackingCategories )// deprecated
112 );
113
114// Only show magic link tracking categories if they are enabled
115 $enableMagicLinks = $this->options->get(MainConfigNames::EnableMagicLinks );
116if ( $enableMagicLinks['ISBN'] ) {
117 $categories[] ='magiclink-tracking-isbn';
118 }
119if ( $enableMagicLinks['RFC'] ) {
120 $categories[] ='magiclink-tracking-rfc';
121 }
122if ( $enableMagicLinks['PMID'] ) {
123 $categories[] ='magiclink-tracking-pmid';
124 }
125
126 $trackingCategories = [];
127foreach ( $categories as $catMsg ) {
128/*
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
132 *
133 * TODO replace uses of wfMessage with an injected service once that is available
134 */
135 $msgObj =wfMessage( $catMsg )->inContentLanguage();
136 $allCats = [];
137 $catMsgTitle = $this->titleParser->makeTitleValueSafe(NS_MEDIAWIKI, $catMsg );
138if ( !$catMsgTitle ) {
139continue;
140 }
141
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 );
148if ( !$tempTitle ) {
149continue;
150 }
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();
155 }
156 }else {
157 $allCats[] = $msgObj->text();
158 }
159 $titles = [];
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 );
164if ( $catTitle ) {
165 $titles[] = $catTitle;
166 }
167 }
168 }
169 $trackingCategories[$catMsg] = [
170'cats' => $titles,
171'msg' => $catMsgTitle,
172 ];
173 }
174
175return $trackingCategories;
176 }
177
186publicfunction resolveTrackingCategory(string $msg, ?PageReference $contextPage ): ?LinkTarget {
187if ( !$contextPage ) {
188 $this->logger->debug("Not adding tracking category $msg to missing page!" );
189returnnull;
190 }
191
192if ( $contextPage->getNamespace() ===NS_SPECIAL ) {
193 $this->logger->debug("Not adding tracking category $msg to special page!" );
194returnnull;
195 }
196
197// Important to parse with correct title (T33469)
198// TODO replace uses of wfMessage with an injected service once that is available
199 $cat =wfMessage( $msg )
200 ->page( $contextPage )
201 ->inContentLanguage()
202 ->text();
203
204 # Allow tracking categories to be disabled by setting them to "-"
205if ( $cat ==='-' ) {
206returnnull;
207 }
208
209 $containerCategory = $this->titleParser->makeTitleValueSafe(NS_CATEGORY, $cat );
210if ( $containerCategory ===null ) {
211 $this->logger->debug("[[MediaWiki:$msg]] is not a valid title!" );
212returnnull;
213 }
214return $containerCategory;
215 }
216
234publicfunction addTrackingCategory(
235 ContentMetadataCollector $parserOutput,
236string $msg,
237 ?PageReference $contextPage
238 ): bool {
239 $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
240if ( $categoryPage ===null ) {
241returnfalse;
242 }
243 $parserOutput->addCategory( $categoryPage );
244returntrue;
245 }
246}
NS_MEDIAWIKI
const NS_MEDIAWIKI
DefinitionDefines.php:59
NS_SPECIAL
const NS_SPECIAL
DefinitionDefines.php:40
NS_CATEGORY
const NS_CATEGORY
DefinitionDefines.php:65
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
DefinitionGlobalFunctions.php:820
if
if(!defined('MW_SETUP_CALLBACK'))
DefinitionWebStart.php:68
MediaWiki\Category\TrackingCategories
This class performs some operations related to tracking categories, such as adding a tracking categor...
DefinitionTrackingCategories.php:28
MediaWiki\Category\TrackingCategories\CONSTRUCTOR_OPTIONS
const CONSTRUCTOR_OPTIONS
DefinitionTrackingCategories.php:33
MediaWiki\Config\ServiceOptions
A class for passing options to services.
DefinitionServiceOptions.php:26
MediaWiki\Config\ServiceOptions\assertRequiredOptions
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
DefinitionServiceOptions.php:70
MediaWiki\MainConfigNames
A class containing constants representing the names of configuration variables.
DefinitionMainConfigNames.php:22
MediaWiki\MainConfigNames\EnableMagicLinks
const EnableMagicLinks
Name constant for the EnableMagicLinks setting, for use with Config::get()
DefinitionMainConfigNames.php:2469
MediaWiki\MainConfigNames\TrackingCategories
const TrackingCategories
Name constant for the TrackingCategories setting, for use with Config::get()
DefinitionMainConfigNames.php:4074
MediaWiki\Parser\Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
DefinitionParser.php:135
MediaWiki\Registration\ExtensionRegistry
Load JSON files, and uses a Processor to extract information.
DefinitionExtensionRegistry.php:35
MediaWiki\Title\NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
DefinitionNamespaceInfo.php:25
MediaWiki\Title\TitleParser
A title parser service for MediaWiki.
DefinitionTitleParser.php:29
MediaWiki\Title\Title
Represents a title within MediaWiki.
DefinitionTitle.php:69
MediaWiki\Linker\LinkTarget
Represents the target of a wiki link.
DefinitionLinkTarget.php:19
MediaWiki\Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
DefinitionPageReference.php:38
MediaWiki\Category
DefinitionCategoriesRdf.php:6

[8]ページ先頭

©2009-2025 Movatter.jp