Make Special:TrackingCategories fully detect namespace switching
[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 // Global array containing names of tracking categories
41 global $wgTrackingCategories;
42
43 $this->setHeaders();
44 $this->outputHeader();
45 $this->getOutput()->allowClickjacking();
46 $this->getOutput()->addHTML(
47 Html::openElement( 'table', array( 'class' => 'mw-datatable TablePager',
48 'id' => 'mw-trackingcategories-table' ) ) . "\n" .
49 "<thead><tr>
50 <th>" .
51 $this->msg( 'trackingcategories-msg' )->escaped() . "
52 </th>
53 <th>" .
54 $this->msg( 'trackingcategories-name' )->escaped() .
55 "</th>
56 <th>" .
57 $this->msg( 'trackingcategories-desc' )->escaped() . "
58 </th>
59 </tr></thead>"
60 );
61
62 foreach( $wgTrackingCategories as $catMsg ) {
63 /*
64 * Check if the tracking category varies by namespace
65 * Otherwise only pages in the current namespace will be displayed
66 * If it does vary, show pages considering all namespaces
67 */
68 $msgObj = $this->msg( $catMsg )->inContentLanguage();
69 $allMsgs = array();
70 $catDesc = $catMsg . '-desc';
71 $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
72 $catMsgTitleText = Linker::link(
73 $catMsgTitle,
74 htmlspecialchars( $catMsg )
75 );
76
77 // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
78 // False positives are ok, this is just an efficiency shortcut
79 if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
80 $ns = MWNamespace::getValidNamespaces();
81 foreach ( $ns as $namesp ) {
82 $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
83 $catName = $msgObj->title( $tempTitle )->text();
84 if ( !$msgObj->isDisabled() ) {
85 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
86 $catTitleText = Linker::link(
87 $catTitle,
88 htmlspecialchars( $catName )
89 );
90 $allMsgs[] = $catTitleText;
91 }
92 }
93 } else {
94 $catName = $msgObj->text();
95 if ( !$msgObj->isDisabled() ) {
96 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
97 $catTitleText = Linker::link(
98 $catTitle,
99 htmlspecialchars( $catName )
100 );
101 } else {
102 $catTitleText = $this->msg( 'trackingcategories-disabled' )->parse();
103 }
104 $allMsgs[] = $catTitleText;
105 }
106
107 /*
108 * Show category description if it exists as a system message
109 * as category-name-desc
110 */
111 $descMsg = $this->msg( $catDesc );
112 if ( $descMsg->isBlank() ) {
113 $descMsg = $this->msg( 'trackingcategories-nodesc' );
114 }
115
116 $this->getOutput()->addHTML(
117 Html::openElement( 'tr' ) .
118 Html::openElement( 'td', array( 'class' => 'mw-trackingcategories-name' ) ) .
119 $this->getLanguage()->commaList( array_unique( $allMsgs ) ) .
120 Html::closeElement( 'td' ) .
121 Html::openElement( 'td', array( 'class' => 'mw-trackingcategories-msg' ) ) .
122 $catMsgTitleText .
123 Html::closeElement( 'td' ) .
124 Html::openElement( 'td', array( 'class' => 'mw-trackingcategories-desc' ) ) .
125 $descMsg->parse() .
126 Html::closeElement( 'td' ) .
127 Html::closeElement( 'tr' )
128 );
129 }
130 $this->getOutput()->addHTML( Html::closeElement( 'table' ) );
131 }
132
133 protected function getGroupName() {
134 return 'pages';
135 }
136 }