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