MediaWiki  master
SpecialWantedCategories.php
Go to the documentation of this file.
1 <?php
29 
37 
40 
46  public function __construct(
49  LanguageConverterFactory $languageConverterFactory
50  ) {
51  parent::__construct( 'Wantedcategories' );
52  $this->setDBLoadBalancer( $loadBalancer );
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 IS 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->select(
91  'category',
92  [ 'cat_title', 'cat_pages' ],
93  [ 'cat_title' => $allCategories ],
94  __METHOD__
95  );
96  foreach ( $categoryRes as $row ) {
97  $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
98  }
99 
100  // Back to start for display
101  $res->seek( 0 );
102  }
103 
109  public function formatResult( $skin, $result ) {
110  $nt = Title::makeTitle( $result->namespace, $result->title );
111 
112  $text = new HtmlArmor( $this->languageConverter->convertHtml( $nt->getText() ) );
113 
114  if ( !$this->isCached() ) {
115  // We can assume the freshest data
116  $plink = $this->getLinkRenderer()->makeBrokenLink(
117  $nt,
118  $text
119  );
120  $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
121  } else {
122  $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
123 
124  $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
125  $cachedValue = intval( $result->value ); // T76910
126 
127  // If the category has been created or emptied since the list was refreshed, strike it
128  if ( $nt->isKnown() || $currentValue === 0 ) {
129  $plink = "<del>$plink</del>";
130  }
131 
132  // Show the current number of category entries if it changed
133  if ( $currentValue !== $cachedValue ) {
134  $nlinks = $this->msg( 'nmemberschanged' )
135  ->numParams( $cachedValue, $currentValue )->escaped();
136  } else {
137  $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
138  }
139  }
140 
141  return $this->getLanguage()->specialList( $plink, $nlinks );
142  }
143 
144  protected function getGroupName() {
145  return 'maintenance';
146  }
147 }
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
HtmlArmor
Marks HTML that shouldn't be escaped.
Definition: HtmlArmor.php:30
SpecialWantedCategories\$languageConverter
ILanguageConverter $languageConverter
Definition: SpecialWantedCategories.php:39
SpecialWantedCategories\$currentCategoryCounts
$currentCategoryCounts
Definition: SpecialWantedCategories.php:36
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:830
$res
$res
Definition: testCompression.php:57
MediaWiki\Languages\LanguageConverterFactory
An interface for creating language converters.
Definition: LanguageConverterFactory.php:46
SpecialWantedCategories
A querypage to list the most wanted categories - implements Special:Wantedcategories.
Definition: SpecialWantedCategories.php:35
QueryPage\setLinkBatchFactory
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:135
MediaWiki\Languages\LanguageConverterFactory\getLanguageConverter
getLanguageConverter( $language=null)
Provide a LanguageConverter for given language.
Definition: LanguageConverterFactory.php:125
QueryPage\isCached
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
Definition: QueryPage.php:297
SpecialWantedCategories\__construct
__construct(ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory, LanguageConverterFactory $languageConverterFactory)
Definition: SpecialWantedCategories.php:46
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
SpecialWantedCategories\formatResult
formatResult( $skin, $result)
Definition: SpecialWantedCategories.php:109
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:663
SpecialWantedCategories\getQueryInfo
getQueryInfo()
Subclasses return an SQL query here, formatted as an array with the following keys: tables => Table(s...
Definition: SpecialWantedCategories.php:57
ILanguageConverter
The shared interface for all language converters.
Definition: ILanguageConverter.php:29
SpecialWantedCategories\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialWantedCategories.php:144
QueryPage\setDBLoadBalancer
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:892
QueryPage\$linkBatchFactory
LinkBatchFactory null $linkBatchFactory
Definition: QueryPage.php:74
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1028
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:78
QueryPage\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:71
SpecialPage\getContentLanguage
getContentLanguage()
Shortcut to get content language.
Definition: SpecialPage.php:840
WantedQueryPage
Class definition for a wanted query page like WantedPages, WantedTemplates, etc.
Definition: WantedQueryPage.php:33
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
SpecialWantedCategories\preprocessResults
preprocessResults( $db, $res)
Cache page existence for performance.
Definition: SpecialWantedCategories.php:73