Merge "add LanguageTest::testEquals for Id7ed6a21c"
[lhc/web/wiklou.git] / includes / specials / SpecialTrackingCategories.php
1 <?php
2 /**
3 * Implements Special:TrackingCategories
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that displays list of tracking categories
26 * Tracking categories allow pages with certain characteristics to be tracked.
27 * It works by adding any such page to a category automatically.
28 * Category is specified by the tracking category's system message.
29 *
30 * @ingroup SpecialPage
31 * @since 1.23
32 */
33
34 class SpecialTrackingCategories extends SpecialPage {
35 function __construct() {
36 parent::__construct( 'TrackingCategories' );
37 }
38
39 /**
40 * Tracking categories that exist in core
41 *
42 * @var array
43 */
44 private static $coreTrackingCategories = [
45 'index-category',
46 'noindex-category',
47 'duplicate-args-category',
48 'expensive-parserfunction-category',
49 'post-expand-template-argument-category',
50 'post-expand-template-inclusion-category',
51 'hidden-category-category',
52 'broken-file-category',
53 'node-count-exceeded-category',
54 'expansion-depth-exceeded-category',
55 'restricted-displaytitle-ignored',
56 ];
57
58 function execute( $par ) {
59 $this->setHeaders();
60 $this->outputHeader();
61 $this->getOutput()->allowClickjacking();
62 $this->getOutput()->addHTML(
63 Html::openElement( 'table', [ 'class' => 'mw-datatable',
64 'id' => 'mw-trackingcategories-table' ] ) . "\n" .
65 "<thead><tr>
66 <th>" .
67 $this->msg( 'trackingcategories-msg' )->escaped() . "
68 </th>
69 <th>" .
70 $this->msg( 'trackingcategories-name' )->escaped() .
71 "</th>
72 <th>" .
73 $this->msg( 'trackingcategories-desc' )->escaped() . "
74 </th>
75 </tr></thead>"
76 );
77
78 $trackingCategories = $this->prepareTrackingCategoriesData();
79
80 $batch = new LinkBatch();
81 foreach ( $trackingCategories as $catMsg => $data ) {
82 $batch->addObj( $data['msg'] );
83 foreach ( $data['cats'] as $catTitle ) {
84 $batch->addObj( $catTitle );
85 }
86 }
87 $batch->execute();
88
89 foreach ( $trackingCategories as $catMsg => $data ) {
90 $allMsgs = [];
91 $catDesc = $catMsg . '-desc';
92
93 $catMsgTitleText = Linker::link(
94 $data['msg'],
95 htmlspecialchars( $catMsg )
96 );
97
98 foreach ( $data['cats'] as $catTitle ) {
99 $catTitleText = Linker::link(
100 $catTitle,
101 htmlspecialchars( $catTitle->getText() )
102 );
103 $allMsgs[] = $catTitleText;
104 }
105
106 # Extra message, when no category was found
107 if ( !count( $allMsgs ) ) {
108 $allMsgs[] = $this->msg( 'trackingcategories-disabled' )->parse();
109 }
110
111 /*
112 * Show category description if it exists as a system message
113 * as category-name-desc
114 */
115 $descMsg = $this->msg( $catDesc );
116 if ( $descMsg->isBlank() ) {
117 $descMsg = $this->msg( 'trackingcategories-nodesc' );
118 }
119
120 $this->getOutput()->addHTML(
121 Html::openElement( 'tr' ) .
122 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-name' ] ) .
123 $this->getLanguage()->commaList( array_unique( $allMsgs ) ) .
124 Html::closeElement( 'td' ) .
125 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-msg' ] ) .
126 $catMsgTitleText .
127 Html::closeElement( 'td' ) .
128 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-desc' ] ) .
129 $descMsg->parse() .
130 Html::closeElement( 'td' ) .
131 Html::closeElement( 'tr' )
132 );
133 }
134 $this->getOutput()->addHTML( Html::closeElement( 'table' ) );
135 }
136
137 /**
138 * Read the global and extract title objects from the corresponding messages
139 * @return array Array( 'msg' => Title, 'cats' => Title[] )
140 */
141 private function prepareTrackingCategoriesData() {
142 $categories = array_merge(
143 self::$coreTrackingCategories,
144 ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
145 $this->getConfig()->get( 'TrackingCategories' ) // deprecated
146 );
147 $trackingCategories = [];
148 foreach ( $categories as $catMsg ) {
149 /*
150 * Check if the tracking category varies by namespace
151 * Otherwise only pages in the current namespace will be displayed
152 * If it does vary, show pages considering all namespaces
153 */
154 $msgObj = $this->msg( $catMsg )->inContentLanguage();
155 $allCats = [];
156 $catMsgTitle = Title::makeTitleSafe( 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 = MWNamespace::getValidNamespaces();
165 foreach ( $ns as $namesp ) {
166 $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
167 if ( !$tempTitle ) {
168 continue;
169 }
170 $catName = $msgObj->title( $tempTitle )->text();
171 # Allow tracking categories to be disabled by setting them to "-"
172 if ( $catName !== '-' ) {
173 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
174 if ( $catTitle ) {
175 $allCats[] = $catTitle;
176 }
177 }
178 }
179 } else {
180 $catName = $msgObj->text();
181 # Allow tracking categories to be disabled by setting them to "-"
182 if ( $catName !== '-' ) {
183 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
184 if ( $catTitle ) {
185 $allCats[] = $catTitle;
186 }
187 }
188 }
189 $trackingCategories[$catMsg] = [
190 'cats' => $allCats,
191 'msg' => $catMsgTitle,
192 ];
193 }
194
195 return $trackingCategories;
196 }
197
198 protected function getGroupName() {
199 return 'pages';
200 }
201 }