Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / includes / specials / SpecialTrackingCategories.php
blob8ff052785eb0f136ce33b13e53e7844f62d6cd23
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 /**
40 * Tracking categories that exist in core
42 * @var array
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',
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>"
79 $trackingCategories = $this->prepareTrackingCategoriesData();
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 );
88 $batch->execute();
90 Hooks::run( 'SpecialTrackingCategories::preprocess', [ $this, $trackingCategories ] );
92 $linkRenderer = $this->getLinkRenderer();
94 foreach ( $trackingCategories as $catMsg => $data ) {
95 $allMsgs = [];
96 $catDesc = $catMsg . '-desc';
98 $catMsgTitleText = $linkRenderer->makeLink(
99 $data['msg'],
100 $catMsg
103 foreach ( $data['cats'] as $catTitle ) {
104 $html = $linkRenderer->makeLink(
105 $catTitle,
106 $catTitle->getText()
109 Hooks::run( 'SpecialTrackingCategories::generateCatLink',
110 [ $this, $catTitle, &$html ] );
112 $allMsgs[] = $html;
115 # Extra message, when no category was found
116 if ( !count( $allMsgs ) ) {
117 $allMsgs[] = $this->msg( 'trackingcategories-disabled' )->parse();
121 * Show category description if it exists as a system message
122 * as category-name-desc
124 $descMsg = $this->msg( $catDesc );
125 if ( $descMsg->isBlank() ) {
126 $descMsg = $this->msg( 'trackingcategories-nodesc' );
129 $this->getOutput()->addHTML(
130 Html::openElement( 'tr' ) .
131 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-name' ] ) .
132 $this->getLanguage()->commaList( array_unique( $allMsgs ) ) .
133 Html::closeElement( 'td' ) .
134 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-msg' ] ) .
135 $catMsgTitleText .
136 Html::closeElement( 'td' ) .
137 Html::openElement( 'td', [ 'class' => 'mw-trackingcategories-desc' ] ) .
138 $descMsg->parse() .
139 Html::closeElement( 'td' ) .
140 Html::closeElement( 'tr' )
143 $this->getOutput()->addHTML( Html::closeElement( 'table' ) );
147 * Read the global and extract title objects from the corresponding messages
148 * @return array Array( 'msg' => Title, 'cats' => Title[] )
150 private function prepareTrackingCategoriesData() {
151 $categories = array_merge(
152 self::$coreTrackingCategories,
153 ExtensionRegistry::getInstance()->getAttribute( 'TrackingCategories' ),
154 $this->getConfig()->get( 'TrackingCategories' ) // deprecated
157 // Only show magic link tracking categories if they are enabled
158 $enableMagicLinks = $this->getConfig()->get( 'EnableMagicLinks' );
159 if ( $enableMagicLinks['ISBN'] ) {
160 $categories[] = 'magiclink-tracking-isbn';
162 if ( $enableMagicLinks['RFC'] ) {
163 $categories[] = 'magiclink-tracking-rfc';
165 if ( $enableMagicLinks['PMID'] ) {
166 $categories[] = 'magiclink-tracking-pmid';
169 $trackingCategories = [];
170 foreach ( $categories as $catMsg ) {
172 * Check if the tracking category varies by namespace
173 * Otherwise only pages in the current namespace will be displayed
174 * If it does vary, show pages considering all namespaces
176 $msgObj = $this->msg( $catMsg )->inContentLanguage();
177 $allCats = [];
178 $catMsgTitle = Title::makeTitleSafe( NS_MEDIAWIKI, $catMsg );
179 if ( !$catMsgTitle ) {
180 continue;
183 // Match things like {{NAMESPACE}} and {{NAMESPACENUMBER}}.
184 // False positives are ok, this is just an efficiency shortcut
185 if ( strpos( $msgObj->plain(), '{{' ) !== false ) {
186 $ns = MWNamespace::getValidNamespaces();
187 foreach ( $ns as $namesp ) {
188 $tempTitle = Title::makeTitleSafe( $namesp, $catMsg );
189 if ( !$tempTitle ) {
190 continue;
192 $catName = $msgObj->title( $tempTitle )->text();
193 # Allow tracking categories to be disabled by setting them to "-"
194 if ( $catName !== '-' ) {
195 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
196 if ( $catTitle ) {
197 $allCats[] = $catTitle;
201 } else {
202 $catName = $msgObj->text();
203 # Allow tracking categories to be disabled by setting them to "-"
204 if ( $catName !== '-' ) {
205 $catTitle = Title::makeTitleSafe( NS_CATEGORY, $catName );
206 if ( $catTitle ) {
207 $allCats[] = $catTitle;
211 $trackingCategories[$catMsg] = [
212 'cats' => $allCats,
213 'msg' => $catMsgTitle,
217 return $trackingCategories;
220 protected function getGroupName() {
221 return 'pages';