Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
7.14% covered (danger)
7.14%
5 / 70
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TrackingCategories
7.25% covered (danger)
7.25%
5 / 69
25.00% covered (danger)
25.00%
1 / 4
372.91
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getTrackingCategories
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
182
 resolveTrackingCategory
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
30
 addTrackingCategory
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Categories
20 */
21
22namespace MediaWiki\Category;
23
24use ExtensionRegistry;
25use MediaWiki\Config\ServiceOptions;
26use MediaWiki\Linker\LinkTarget;
27use MediaWiki\MainConfigNames;
28use MediaWiki\Page\PageReference;
29use MediaWiki\Parser\Parser;
30use MediaWiki\Parser\ParserOutput;
31use MediaWiki\Title\NamespaceInfo;
32use MediaWiki\Title\Title;
33use MediaWiki\Title\TitleParser;
34use Psr\Log\LoggerInterface;
35
36/**
37 * This class performs some operations related to tracking categories, such as
38 * adding a tracking category to a ParserOutput, and creating a list of all
39 * such categories.
40 * @since 1.29
41 */
42class TrackingCategories {
43
44    /**
45     * @internal For use by ServiceWiring
46     */
47    public const CONSTRUCTOR_OPTIONS = [
48        MainConfigNames::TrackingCategories,
49        MainConfigNames::EnableMagicLinks,
50    ];
51
52    /** @var ServiceOptions */
53    private $options;
54
55    /** @var NamespaceInfo */
56    private $namespaceInfo;
57
58    /** @var TitleParser */
59    private $titleParser;
60
61    /** @var ExtensionRegistry */
62    private $extensionRegistry;
63
64    /** @var LoggerInterface */
65    private $logger;
66
67    /**
68     * Tracking categories that exist in core
69     *
70     * @var array
71     */
72    private const CORE_TRACKING_CATEGORIES = [
73        'broken-file-category',
74        'duplicate-args-category',
75        'expansion-depth-exceeded-category',
76        'expensive-parserfunction-category',
77        'hidden-category-category',
78        'index-category',
79        'node-count-exceeded-category',
80        'noindex-category',
81        'nonnumeric-formatnum',
82        'post-expand-template-argument-category',
83        'post-expand-template-inclusion-category',
84        'restricted-displaytitle-ignored',
85        # template-equals-category is unused in MW>=1.39, but the category
86        # can be left around for a major release or so for an easier
87        # transition for anyone who didn't do the cleanup. T91154
88        'template-equals-category',
89        'template-loop-category',
90        'unstrip-depth-category',
91        'unstrip-size-category',
92    ];
93
94    /**
95     * @param ServiceOptions $options
96     * @param NamespaceInfo $namespaceInfo
97     * @param TitleParser $titleParser
98     * @param LoggerInterface $logger
99     */
100    public function __construct(
101        ServiceOptions $options,
102        NamespaceInfo $namespaceInfo,
103        TitleParser $titleParser,
104        LoggerInterface $logger
105    ) {
106        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
107        $this->options = $options;
108        $this->namespaceInfo = $namespaceInfo;
109        $this->titleParser = $titleParser;
110        $this->logger = $logger;
111
112        // TODO convert ExtensionRegistry to a service and inject it
113        $this->extensionRegistry = ExtensionRegistry::getInstance();
114    }
115
116    /**
117     * Read the global and extract title objects from the corresponding messages
118     *
119     * TODO consider renaming this method, since this class is retrieved from
120     * MediaWikiServices, resulting in calls like:
121     * MediaWikiServices::getInstance()->getTrackingCategories()->getTrackingCategories()
122     *
123     * @return array[] [ 'msg' => LinkTarget, 'cats' => LinkTarget[] ]
124     * @phan-return array<string,array{msg:LinkTarget,cats:LinkTarget[]}>
125     */
126    public function getTrackingCategories() {
127        $categories = array_merge(
128            self::CORE_TRACKING_CATEGORIES,
129            $this->extensionRegistry->getAttribute( MainConfigNames::TrackingCategories ),
130            $this->options->get( MainConfigNames::TrackingCategories ) // deprecated
131        );
132
133        // Only show magic link tracking categories if they are enabled
134        $enableMagicLinks = $this->options->get( MainConfigNames::EnableMagicLinks );
135        if ( $enableMagicLinks['ISBN'] ) {
136            $categories[] = 'magiclink-tracking-isbn';
137        }
138        if ( $enableMagicLinks['RFC'] ) {
139            $categories[] = 'magiclink-tracking-rfc';
140        }
141        if ( $enableMagicLinks['PMID'] ) {
142            $categories[] = 'magiclink-tracking-pmid';
143        }
144
145        $trackingCategories = [];
146        foreach ( $categories as $catMsg ) {
147            /*
148             * Check if the tracking category varies by namespace
149             * Otherwise only pages in the current namespace will be displayed
150             * If it does vary, show pages considering all namespaces
151             *
152             * TODO replace uses of wfMessage with an injected service once that is available
153             */
154            $msgObj = wfMessage( $catMsg )->inContentLanguage();
155            $allCats = [];
156            $catMsgTitle = $this->titleParser->makeTitleValueSafe( NS_MEDIAWIKI, $catMsg );
157            if ( !$catMsgTitle ) {
158                continue;
159            }
160
161            // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
162            // False positives are ok, this is just an efficiency shortcut
163            if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
164                $ns = $this->namespaceInfo->getValidNamespaces();
165                foreach ( $ns as $namesp ) {
166                    $tempTitle = $this->titleParser->makeTitleValueSafe( $namesp, $catMsg );
167                    if ( !$tempTitle ) {
168                        continue;
169                    }
170                    // XXX: should be a better way to convert a TitleValue
171                    // to a PageReference!
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 );
177                        if ( $catTitle ) {
178                            $allCats[] = $catTitle;
179                        }
180                    }
181                }
182            } else {
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 );
187                    if ( $catTitle ) {
188                        $allCats[] = $catTitle;
189                    }
190                }
191            }
192            $trackingCategories[$catMsg] = [
193                'cats' => $allCats,
194                'msg' => $catMsgTitle,
195            ];
196        }
197
198        return $trackingCategories;
199    }
200
201    /**
202     * Resolve a tracking category.
203     * @param string $msg Message key
204     * @param ?PageReference $contextPage Context page title
205     * @return ?LinkTarget the proper category page, or null if
206     *   the tracking category is disabled or unsafe
207     * @since 1.38
208     */
209    public function resolveTrackingCategory( string $msg, ?PageReference $contextPage ): ?LinkTarget {
210        if ( !$contextPage ) {
211            $this->logger->debug( "Not adding tracking category $msg to missing page!" );
212            return null;
213        }
214
215        if ( $contextPage->getNamespace() === NS_SPECIAL ) {
216            $this->logger->debug( "Not adding tracking category $msg to special page!" );
217            return null;
218        }
219
220        // Important to parse with correct title (T33469)
221        // TODO replace uses of wfMessage with an injected service once that is available
222        $cat = wfMessage( $msg )
223            ->page( $contextPage )
224            ->inContentLanguage()
225            ->text();
226
227        # Allow tracking categories to be disabled by setting them to "-"
228        if ( $cat === '-' ) {
229            return null;
230        }
231
232        $containerCategory = $this->titleParser->makeTitleValueSafe( NS_CATEGORY, $cat );
233        if ( $containerCategory === null ) {
234            $this->logger->debug( "[[MediaWiki:$msg]] is not a valid title!" );
235            return null;
236        }
237        return $containerCategory;
238    }
239
240    /**
241     * Add a tracking category to a ParserOutput, getting the title from a
242     * system message.
243     *
244     * Any message used with this function should be registered so it will
245     * show up on [[Special:TrackingCategories]].  Core messages should be
246     * added to TrackingCategories::CORE_TRACKING_CATEGORIES, and extensions
247     * should add to "TrackingCategories" in their extension.json.
248     *
249     * @param ParserOutput $parserOutput The target ParserOutput which will
250     *  store the new category
251     * @param string $msg Message key
252     * @param ?PageReference $contextPage Context page title
253     * @return bool Whether the addition was successful
254     * @since 1.38
255     * @see Parser::addTrackingCategory
256     */
257    public function addTrackingCategory( ParserOutput $parserOutput, string $msg, ?PageReference $contextPage ): bool {
258        $categoryPage = $this->resolveTrackingCategory( $msg, $contextPage );
259        if ( $categoryPage === null ) {
260            return false;
261        }
262        $parserOutput->addCategory( $categoryPage->getDBkey() );
263        return true;
264    }
265}
266
267/** @deprecated class alias since 1.40 */
268class_alias( TrackingCategories::class, 'TrackingCategories' );