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