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