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