Remove Revision::getRevisionText from ApiQueryDeletedrevs
[lhc/web/wiklou.git] / includes / TrackingCategories.php
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
22 use MediaWiki\MediaWikiServices;
23
24 /**
25 * This class performs some operations related to tracking categories, such as creating
26 * a list of all such categories.
27 * @since 1.29
28 */
29 class TrackingCategories {
30 /** @var Config */
31 private $config;
32
33 /**
34 * Tracking categories that exist in core
35 *
36 * @var array
37 */
38 private static $coreTrackingCategories = [
39 'index-category',
40 'noindex-category',
41 'duplicate-args-category',
42 'expensive-parserfunction-category',
43 'post-expand-template-argument-category',
44 'post-expand-template-inclusion-category',
45 'hidden-category-category',
46 'broken-file-category',
47 'node-count-exceeded-category',
48 'expansion-depth-exceeded-category',
49 'restricted-displaytitle-ignored',
50 'deprecated-self-close-category',
51 'template-loop-category',
52 ];
53
54 /**
55 * @param Config $config
56 */
57 public function __construct( Config $config ) {
58 $this->config = $config;
59 }
60
61 /**
62 * Read the global and extract title objects from the corresponding messages
63 * @return array [ 'msg' => Title, 'cats' => Title[] ]
64 */
65 public function getTrackingCategories() {
66 $categories = array_merge(
67 self::$coreTrackingCategories,
68 ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
69 $this->config->get( 'TrackingCategories' ) // deprecated
70 );
71
72 // Only show magic link tracking categories if they are enabled
73 $enableMagicLinks = $this->config->get( 'EnableMagicLinks' );
74 if ( $enableMagicLinks['ISBN'] ) {
75 $categories[] = 'magiclink-tracking-isbn';
76 }
77 if ( $enableMagicLinks['RFC'] ) {
78 $categories[] = 'magiclink-tracking-rfc';
79 }
80 if ( $enableMagicLinks['PMID'] ) {
81 $categories[] = 'magiclink-tracking-pmid';
82 }
83
84 $trackingCategories = [];
85 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
86 foreach ( $categories as $catMsg ) {
87 /*
88 * Check if the tracking category varies by namespace
89 * Otherwise only pages in the current namespace will be displayed
90 * If it does vary, show pages considering all namespaces
91 */
92 $msgObj = wfMessage( $catMsg )->inContentLanguage();
93 $allCats = [];
94 $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
95 if ( !$catMsgTitle ) {
96 continue;
97 }
98
99 // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
100 // False positives are ok, this is just an efficiency shortcut
101 if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
102 $ns = $nsInfo->getValidNamespaces();
103 foreach ( $ns as $namesp ) {
104 $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
105 if ( !$tempTitle ) {
106 continue;
107 }
108 $catName = $msgObj->title( $tempTitle )->text();
109 # Allow tracking categories to be disabled by setting them to "-"
110 if ( $catName !== '-' ) {
111 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
112 if ( $catTitle ) {
113 $allCats[] = $catTitle;
114 }
115 }
116 }
117 } else {
118 $catName = $msgObj->text();
119 # Allow tracking categories to be disabled by setting them to "-"
120 if ( $catName !== '-' ) {
121 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
122 if ( $catTitle ) {
123 $allCats[] = $catTitle;
124 }
125 }
126 }
127 $trackingCategories[$catMsg] = [
128 'cats' => $allCats,
129 'msg' => $catMsgTitle,
130 ];
131 }
132
133 return $trackingCategories;
134 }
135 }