Merge "Http::getProxy() method to get proxy configuration"
[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 ];
56
57 function execute( $par ) {
58 $this->setHeaders();
59 $this->outputHeader();
60 $this->getOutput()->allowClickjacking();
61 $this->getOutput()->addHTML(
62 Html::openElement( 'table', [ 'class' => 'mw-datatable',
63 'id' => 'mw-trackingcategories-table' ] ) . "\n" .
64 "<thead><tr>
65 <th>" .
66 $this->msg( 'trackingcategories-msg' )->escaped() . "
67 </th>
68 <th>" .
69 $this->msg( 'trackingcategories-name' )->escaped() .
70 "</th>
71 <th>" .
72 $this->msg( 'trackingcategories-desc' )->escaped() . "
73 </th>
74 </tr></thead>"
75 );
76
77 $trackingCategories = $this->prepareTrackingCategoriesData();
78
79 $batch = new LinkBatch();
80 foreach ( $trackingCategories as $catMsg => $data ) {
81 $batch->addObj( $data['msg'] );
82 foreach ( $data['cats'] as $catTitle ) {
83 $batch->addObj( $catTitle );
84 }
85 }
86 $batch->execute();
87
88 foreach ( $trackingCategories as $catMsg => $data ) {
89 $allMsgs = [];
90 $catDesc = $catMsg . '-desc';
91
92 $catMsgTitleText = Linker::link(
93 $data['msg'],
94 htmlspecialchars( $catMsg )
95 );
96
97 foreach ( $data['cats'] as $catTitle ) {
98 $catTitleText = Linker::link(
99 $catTitle,
100 htmlspecialchars( $catTitle->getText() )
101 );
102 $allMsgs[] = $catTitleText;
103 }
104
105 # Extra message, when no category was found
106 if ( !count( $allMsgs ) ) {
107 $allMsgs[] = $this->msg( 'trackingcategories-disabled' )->parse();
108 }
109
110 /*
111 * Show category description if it exists as a system message
112 * as category-name-desc
113 */
114 $descMsg = $this->msg( $catDesc );
115 if ( $descMsg->isBlank() ) {
116 $descMsg = $this->msg( 'trackingcategories-nodesc' );
117 }
118
119 $this->getOutput()->addHTML(
120 Html::openElement( 'tr' ) .
121 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-name' ] ) .
122 $this->getLanguage()->commaList( array_unique( $allMsgs ) ) .
123 Html::closeElement( 'td' ) .
124 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-msg' ] ) .
125 $catMsgTitleText .
126 Html::closeElement( 'td' ) .
127 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-desc' ] ) .
128 $descMsg->parse() .
129 Html::closeElement( 'td' ) .
130 Html::closeElement( 'tr' )
131 );
132 }
133 $this->getOutput()->addHTML( Html::closeElement( 'table' ) );
134 }
135
136 /**
137 * Read the global and extract title objects from the corresponding messages
138 * @return array Array( 'msg' => Title, 'cats' => Title[] )
139 */
140 private function prepareTrackingCategoriesData() {
141 $categories = array_merge(
142 self::$coreTrackingCategories,
143 ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
144 $this->getConfig()->get( 'TrackingCategories' ) // deprecated
145 );
146 $trackingCategories = [];
147 foreach ( $categories as $catMsg ) {
148 /*
149 * Check if the tracking category varies by namespace
150 * Otherwise only pages in the current namespace will be displayed
151 * If it does vary, show pages considering all namespaces
152 */
153 $msgObj = $this->msg( $catMsg )->inContentLanguage();
154 $allCats = [];
155 $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
156 if ( !$catMsgTitle ) {
157 continue;
158 }
159
160 // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
161 // False positives are ok, this is just an efficiency shortcut
162 if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
163 $ns = MWNamespace::getValidNamespaces();
164 foreach ( $ns as $namesp ) {
165 $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
166 if ( !$tempTitle ) {
167 continue;
168 }
169 $catName = $msgObj->title( $tempTitle )->text();
170 # Allow tracking categories to be disabled by setting them to "-"
171 if ( $catName !== '-' ) {
172 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
173 if ( $catTitle ) {
174 $allCats[] = $catTitle;
175 }
176 }
177 }
178 } else {
179 $catName = $msgObj->text();
180 # Allow tracking categories to be disabled by setting them to "-"
181 if ( $catName !== '-' ) {
182 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
183 if ( $catTitle ) {
184 $allCats[] = $catTitle;
185 }
186 }
187 }
188 $trackingCategories[$catMsg] = [
189 'cats' => $allCats,
190 'msg' => $catMsgTitle,
191 ];
192 }
193
194 return $trackingCategories;
195 }
196
197 protected function getGroupName() {
198 return 'pages';
199 }
200 }