Debian Codegraph @

includes/specials/SpecialWantedCategories.php

raw symbols SCIP
1 <?php
2 /**
3 * Copyright © 2005 Ævar Arnfjörð Bjarmason
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 */
22
23 namespace MediaWiki\Specials;
24
25 use HtmlArmor;
26 use MediaWiki\Cache\LinkBatchFactory;
27 use MediaWiki\Language\ILanguageConverter;
28 use MediaWiki\Languages\LanguageConverterFactory;
29 use MediaWiki\SpecialPage\WantedQueryPage;
30 use MediaWiki\Title\Title;
31 use Skin;
32 use stdClass;
33 use Wikimedia\Rdbms\IConnectionProvider;
34
35 /**
36 * List of the most wanted categories
37 *
38 * @ingroup SpecialPage
39 */
40 class SpecialWantedCategories extends WantedQueryPage {
41 /** @var int[] */
42 private $currentCategoryCounts;
43
44 private ILanguageConverter $languageConverter;
45
46 /**
47 * @param IConnectionProvider $dbProvider
48 * @param LinkBatchFactory $linkBatchFactory
49 * @param LanguageConverterFactory $languageConverterFactory
50 */
51 public function __construct(
52 IConnectionProvider $dbProvider,
53 LinkBatchFactory $linkBatchFactory,
54 LanguageConverterFactory $languageConverterFactory
55 ) {
56 parent::__construct( 'Wantedcategories' );
57 $this->setDatabaseProvider( $dbProvider );
58 $this->setLinkBatchFactory( $linkBatchFactory );
59 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
60 }
61
62 public function getQueryInfo() {
63 return [
64 'tables' => [ 'categorylinks', 'page' ],
65 'fields' => [
66 'namespace' => NS_CATEGORY,
67 'title' => 'cl_to',
68 'value' => 'COUNT(*)'
69 ],
70 'conds' => [ 'page_title' => null ],
71 'options' => [ 'GROUP BY' => 'cl_to' ],
72 'join_conds' => [ 'page' => [ 'LEFT JOIN',
73 [ 'page_title = cl_to',
74 'page_namespace' => NS_CATEGORY ] ] ]
75 ];
76 }
77
78 public function preprocessResults( $db, $res ) {
79 parent::preprocessResults( $db, $res );
80
81 $this->currentCategoryCounts = [];
82
83 if ( !$res->numRows() || !$this->isCached() ) {
84 return;
85 }
86
87 // Fetch (hopefully) up-to-date numbers of pages in each category.
88 // This should be fast enough as we limit the list to a reasonable length.
89
90 $allCategories = [];
91 foreach ( $res as $row ) {
92 $allCategories[] = $row->title;
93 }
94
95 $categoryRes = $db->newSelectQueryBuilder()
96 ->select( [ 'cat_title', 'cat_pages' ] )
97 ->from( 'category' )
98 ->where( [ 'cat_title' => $allCategories ] )
99 ->caller( __METHOD__ )->fetchResultSet();
100 foreach ( $categoryRes as $row ) {
101 $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
102 }
103
104 // Back to start for display
105 $res->seek( 0 );
106 }
107
108 /**
109 * @param Skin $skin
110 * @param stdClass $result Result row
111 * @return string
112 */
113 public function formatResult( $skin, $result ) {
114 $nt = Title::makeTitle( $result->namespace, $result->title );
115
116 $text = new HtmlArmor( $this->languageConverter->convertHtml( $nt->getText() ) );
117
118 if ( !$this->isCached() ) {
119 // We can assume the freshest data
120 $plink = $this->getLinkRenderer()->makeBrokenLink(
121 $nt,
122 $text
123 );
124 $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
125 } else {
126 $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
127
128 $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
129 $cachedValue = intval( $result->value ); // T76910
130
131 // If the category has been created or emptied since the list was refreshed, strike it
132 if ( $nt->isKnown() || $currentValue === 0 ) {
133 $plink = "<del>$plink</del>";
134 }
135
136 // Show the current number of category entries if it changed
137 if ( $currentValue !== $cachedValue ) {
138 $nlinks = $this->msg( 'nmemberschanged' )
139 ->numParams( $cachedValue, $currentValue )->escaped();
140 } else {
141 $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
142 }
143 }
144
145 return $this->getLanguage()->specialList( $plink, $nlinks );
146 }
147
148 protected function getGroupName() {
149 return 'maintenance';
150 }
151 }
152
153 /**
154 * Retain the old class name for backwards compatibility.
155 * @deprecated since 1.41
156 */
157 class_alias( SpecialWantedCategories::class, 'SpecialWantedCategories' );