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