MediaWiki master
SpecialWantedCategories.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Specials;
10
19use stdClass;
22
30 private $currentCategoryCounts;
31
32 private readonly ILanguageConverter $languageConverter;
33
34 public function __construct(
35 IConnectionProvider $dbProvider,
36 LinkBatchFactory $linkBatchFactory,
37 LanguageConverterFactory $languageConverterFactory,
38 private readonly LinksMigration $linksMigration
39 ) {
40 parent::__construct( 'Wantedcategories' );
41 $this->setDatabaseProvider( $dbProvider );
42 $this->setLinkBatchFactory( $linkBatchFactory );
43 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
44 }
45
47 public function getQueryInfo() {
48 $queryInfo = $this->linksMigration->getQueryInfo( 'categorylinks' );
49 $titleField = $this->linksMigration->getTitleFields( 'categorylinks' )[1];
50
51 return [
52 'tables' => array_merge( $queryInfo['tables'], [ 'page' ] ),
53 'fields' => [
54 'namespace' => NS_CATEGORY,
55 'title' => $titleField,
56 'value' => 'COUNT(*)'
57 ],
58 'conds' => [ 'page_title' => null ],
59 'options' => [ 'GROUP BY' => $titleField ],
60 'join_conds' => array_merge( $queryInfo['joins'],
61 [ 'page' => [ 'LEFT JOIN',
62 [ 'page_title = ' . $titleField,
63 'page_namespace' => NS_CATEGORY ] ] ] )
64 ];
65 }
66
68 protected function getRecacheDB() {
69 return $this->getDatabaseProvider()->getReplicaDatabase(
70 CategoryLinksTable::VIRTUAL_DOMAIN,
71 'vslow'
72 );
73 }
74
76 public function preprocessResults( $db, $res ) {
77 parent::preprocessResults( $db, $res );
78
79 $this->currentCategoryCounts = [];
80
81 if ( !$res->numRows() || !$this->isCached() ) {
82 return;
83 }
84
85 // Fetch (hopefully) up-to-date numbers of pages in each category.
86 // This should be fast enough as we limit the list to a reasonable length.
87
88 $allCategories = [];
89 foreach ( $res as $row ) {
90 $allCategories[] = $row->title;
91 }
92
93 $categoryRes = $db->newSelectQueryBuilder()
94 ->select( [ 'cat_title', 'cat_pages' ] )
95 ->from( 'category' )
96 ->where( [ 'cat_title' => $allCategories ] )
97 ->caller( __METHOD__ )->fetchResultSet();
98 foreach ( $categoryRes as $row ) {
99 $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
100 }
101
102 // Back to start for display
103 $res->seek( 0 );
104 }
105
111 public function formatResult( $skin, $result ) {
112 $nt = Title::makeTitle( $result->namespace, $result->title );
113
114 $text = new HtmlArmor( $this->languageConverter->convertHtml( $nt->getText() ) );
115
116 if ( !$this->isCached() ) {
117 // We can assume the freshest data
118 $plink = $this->getLinkRenderer()->makeBrokenLink(
119 $nt,
120 $text
121 );
122 $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
123 } else {
124 $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
125
126 $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
127 $cachedValue = intval( $result->value ); // T76910
128
129 // If the category has been created or emptied since the list was refreshed, strike it
130 if ( $nt->isKnown() || $currentValue === 0 ) {
131 $plink = "<del>$plink</del>";
132 }
133
134 // Show the current number of category entries if it changed
135 if ( $currentValue !== $cachedValue ) {
136 $nlinks = $this->msg( 'nmemberschanged' )
137 ->numParams( $cachedValue, $currentValue )->escaped();
138 } else {
139 $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
140 }
141 }
142
143 return $this->getLanguage()->specialList( $plink, $nlinks );
144 }
145
147 protected function getGroupName() {
148 return 'maintenance';
149 }
150}
151
156class_alias( SpecialWantedCategories::class, 'SpecialWantedCategories' );
const NS_CATEGORY
Definition Defines.php:65
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
An interface for creating language converters.
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Service for compat reading of links tables.
Factory for LinkBatch objects to batch query page metadata.
The base class for all skins.
Definition Skin.php:54
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.to override
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
__construct(IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory, LanguageConverterFactory $languageConverterFactory, private readonly LinksMigration $linksMigration)
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
getRecacheDB()
Get a DB connection to be used for slow recache queries.to override IReadableDatabase
Represents a title within MediaWiki.
Definition Title.php:69
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:18
The shared interface for all language converters.
Provide primary and replica IDatabase connections.