Remove unused variable in ProfilerMwprof
[mediawiki.git] / includes / specials / SpecialTrackingCategories.php
blob7684c0511729d5fd7914cbf561afb7e917bdeba0
1 <?php
2 /**
3 * Implements Special:TrackingCategories
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
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.
30 * @ingroup SpecialPage
31 * @since 1.23
34 class SpecialTrackingCategories extends SpecialPage {
35 function __construct() {
36 parent::__construct( 'TrackingCategories' );
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>"
59 $trackingCategories = $this->prepareTrackingCategoriesData();
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 );
68 $batch->execute();
70 foreach ( $trackingCategories as $catMsg => $data ) {
71 $allMsgs = array();
72 $catDesc = $catMsg . '-desc';
74 $catMsgTitleText = Linker::link(
75 $data['msg'],
76 htmlspecialchars( $catMsg )
79 foreach ( $data['cats'] as $catTitle ) {
80 $catTitleText = Linker::link(
81 $catTitle,
82 htmlspecialchars( $catTitle->getText() )
84 $allMsgs[] = $catTitleText;
87 # Extra message, when no category was found
88 if ( !count( $allMsgs ) ) {
89 $allMsgs[] = $this->msg( 'trackingcategories-disabled' )->parse();
93 * Show category description if it exists as a system message
94 * as category-name-desc
96 $descMsg = $this->msg( $catDesc );
97 if ( $descMsg->isBlank() ) {
98 $descMsg = $this->msg( 'trackingcategories-nodesc' );
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' )
115 $this->getOutput()->addHTML( Html::closeElement( 'table' ) );
119 * Read the global and extract title objects from the corresponding messages
120 * @return array Array( 'msg' => Title, 'cats' => Title[] )
122 private function prepareTrackingCategoriesData() {
123 $trackingCategories = array();
124 foreach ( $this->getConfig()->get( 'TrackingCategories' ) as $catMsg ) {
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
130 $msgObj = $this->msg( $catMsg )->inContentLanguage();
131 $allCats = array();
132 $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
133 if ( !$catMsgTitle ) {
134 continue;
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;
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;
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;
165 $trackingCategories[$catMsg] = array(
166 'cats' => $allCats,
167 'msg' => $catMsgTitle,
171 return $trackingCategories;
174 protected function getGroupName() {
175 return 'pages';