MediaWiki master
SpecialWantedCategories.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Specials;
10
18use stdClass;
21
29 private $currentCategoryCounts;
30
31 private readonly ILanguageConverter $languageConverter;
32
33 public function __construct(
34 IConnectionProvider $dbProvider,
35 LinkBatchFactory $linkBatchFactory,
36 LanguageConverterFactory $languageConverterFactory,
37 ) {
38 parent::__construct( 'Wantedcategories' );
39 $this->setDatabaseProvider( $dbProvider );
40 $this->setLinkBatchFactory( $linkBatchFactory );
41 $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
42 }
43
45 public function getQueryInfo() {
46 return [
47 'tables' => [ 'categorylinks', 'linktarget', 'page' ],
48 'fields' => [
49 'namespace' => NS_CATEGORY,
50 'title' => 'lt_title',
51 'value' => 'COUNT(*)'
52 ],
53 'conds' => [ 'page_title' => null ],
54 'options' => [ 'GROUP BY' => 'lt_title' ],
55 'join_conds' => [
56 'linktarget' => [ 'JOIN', [ 'cl_target_id = lt_id' ] ],
57 'page' => [ 'LEFT JOIN', [ 'page_title = lt_title', 'page_namespace' => NS_CATEGORY ] ]
58 ]
59 ];
60 }
61
63 protected function getRecacheDB() {
64 return $this->getDatabaseProvider()->getReplicaDatabase(
65 CategoryLinksTable::VIRTUAL_DOMAIN,
66 'vslow'
67 );
68 }
69
71 public function preprocessResults( $db, $res ) {
72 parent::preprocessResults( $db, $res );
73
74 $this->currentCategoryCounts = [];
75
76 if ( !$res->numRows() || !$this->isCached() ) {
77 return;
78 }
79
80 // Fetch (hopefully) up-to-date numbers of pages in each category.
81 // This should be fast enough as we limit the list to a reasonable length.
82
83 $allCategories = [];
84 foreach ( $res as $row ) {
85 $allCategories[] = $row->title;
86 }
87
88 $categoryRes = $db->newSelectQueryBuilder()
89 ->select( [ 'cat_title', 'cat_pages' ] )
90 ->from( 'category' )
91 ->where( [ 'cat_title' => $allCategories ] )
92 ->caller( __METHOD__ )->fetchResultSet();
93 foreach ( $categoryRes as $row ) {
94 $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
95 }
96
97 // Back to start for display
98 $res->seek( 0 );
99 }
100
106 public function formatResult( $skin, $result ) {
107 $nt = Title::makeTitle( $result->namespace, $result->title );
108
109 $text = new HtmlArmor( $this->languageConverter->convertHtml( $nt->getText() ) );
110
111 if ( !$this->isCached() ) {
112 // We can assume the freshest data
113 $plink = $this->getLinkRenderer()->makeBrokenLink(
114 $nt,
115 $text
116 );
117 $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
118 } else {
119 $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
120
121 $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
122 $cachedValue = intval( $result->value ); // T76910
123
124 // If the category has been created or emptied since the list was refreshed, strike it
125 if ( $nt->isKnown() || $currentValue === 0 ) {
126 $plink = "<del>$plink</del>";
127 }
128
129 // Show the current number of category entries if it changed
130 if ( $currentValue !== $cachedValue ) {
131 $nlinks = $this->msg( 'nmemberschanged' )
132 ->numParams( $cachedValue, $currentValue )->escaped();
133 } else {
134 $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
135 }
136 }
137
138 return $this->getLanguage()->specialList( $plink, $nlinks );
139 }
140
142 protected function getGroupName() {
143 return 'maintenance';
144 }
145}
146
151class_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.
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
__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...
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.