MediaWiki master
SpecialWantedCategories.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Specials;
24
25use HtmlArmor;
31use Skin;
32use stdClass;
34
41 private $currentCategoryCounts;
42
43 private ILanguageConverter $languageConverter;
44
50 public function __construct(
51 IConnectionProvider $dbProvider,
52 LinkBatchFactory $linkBatchFactory,
53 LanguageConverterFactory $languageConverterFactory
54 ) {
55 parent::__construct( 'Wantedcategories' );
56 $this->setDatabaseProvider( $dbProvider );
57 $this->setLinkBatchFactory( $linkBatchFactory );
58 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
59 }
60
61 public function getQueryInfo() {
62 return [
63 'tables' => [ 'categorylinks', 'page' ],
64 'fields' => [
65 'namespace' => NS_CATEGORY,
66 'title' => 'cl_to',
67 'value' => 'COUNT(*)'
68 ],
69 'conds' => [ 'page_title' => null ],
70 'options' => [ 'GROUP BY' => 'cl_to' ],
71 'join_conds' => [ 'page' => [ 'LEFT JOIN',
72 [ 'page_title = cl_to',
73 'page_namespace' => NS_CATEGORY ] ] ]
74 ];
75 }
76
77 public function preprocessResults( $db, $res ) {
78 parent::preprocessResults( $db, $res );
79
80 $this->currentCategoryCounts = [];
81
82 if ( !$res->numRows() || !$this->isCached() ) {
83 return;
84 }
85
86 // Fetch (hopefully) up-to-date numbers of pages in each category.
87 // This should be fast enough as we limit the list to a reasonable length.
88
89 $allCategories = [];
90 foreach ( $res as $row ) {
91 $allCategories[] = $row->title;
92 }
93
94 $categoryRes = $db->newSelectQueryBuilder()
95 ->select( [ 'cat_title', 'cat_pages' ] )
96 ->from( 'category' )
97 ->where( [ 'cat_title' => $allCategories ] )
98 ->caller( __METHOD__ )->fetchResultSet();
99 foreach ( $categoryRes as $row ) {
100 $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
101 }
102
103 // Back to start for display
104 $res->seek( 0 );
105 }
106
112 public function formatResult( $skin, $result ) {
113 $nt = Title::makeTitle( $result->namespace, $result->title );
114
115 $text = new HtmlArmor( $this->languageConverter->convertHtml( $nt->getText() ) );
116
117 if ( !$this->isCached() ) {
118 // We can assume the freshest data
119 $plink = $this->getLinkRenderer()->makeBrokenLink(
120 $nt,
121 $text
122 );
123 $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
124 } else {
125 $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
126
127 $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
128 $cachedValue = intval( $result->value ); // T76910
129
130 // If the category has been created or emptied since the list was refreshed, strike it
131 if ( $nt->isKnown() || $currentValue === 0 ) {
132 $plink = "<del>$plink</del>";
133 }
134
135 // Show the current number of category entries if it changed
136 if ( $currentValue !== $cachedValue ) {
137 $nlinks = $this->msg( 'nmemberschanged' )
138 ->numParams( $cachedValue, $currentValue )->escaped();
139 } else {
140 $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
141 }
142 }
143
144 return $this->getLanguage()->specialList( $plink, $nlinks );
145 }
146
147 protected function getGroupName() {
148 return 'maintenance';
149 }
150}
151
156class_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:79
The base class for all skins.
Definition Skin.php:59
The shared interface for all language converters.
Provide primary and replica IDatabase connections.