MediaWiki master
SpecialWantedCategories.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Specials;
24
25use HtmlArmor;
31use Skin;
32use stdClass;
34
42 private $currentCategoryCounts;
43
44 private ILanguageConverter $languageConverter;
45
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
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
157class_alias( SpecialWantedCategories::class, 'SpecialWantedCategories' );
const NS_CATEGORY
Definition Defines.php:79
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
setDatabaseProvider(IConnectionProvider $databaseProvider)
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getContentLanguage()
Shortcut to get content language.
getLanguage()
Shortcut to get user's language.
Base class for a "wanted" query page like WantedPages, WantedTemplates, etc.
preprocessResults( $db, $res)
Cache page existence for performance.
__construct(IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory, LanguageConverterFactory $languageConverterFactory)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Represents a title within MediaWiki.
Definition Title.php:78
The base class for all skins.
Definition Skin.php:64
The shared interface for all language converters.
Provide primary and replica IDatabase connections.