MediaWiki  master
SpecialWantedCategories.php
Go to the documentation of this file.
1 <?php
26 namespace MediaWiki\Specials;
27 
28 use HtmlArmor;
34 use Skin;
35 use stdClass;
37 
44  private $currentCategoryCounts;
45 
46  private ILanguageConverter $languageConverter;
47 
53  public function __construct(
54  IConnectionProvider $dbProvider,
55  LinkBatchFactory $linkBatchFactory,
56  LanguageConverterFactory $languageConverterFactory
57  ) {
58  parent::__construct( 'Wantedcategories' );
59  $this->setDatabaseProvider( $dbProvider );
60  $this->setLinkBatchFactory( $linkBatchFactory );
61  $this->languageConverter = $languageConverterFactory->getLanguageConverter( $this->getContentLanguage() );
62  }
63 
64  public function getQueryInfo() {
65  return [
66  'tables' => [ 'categorylinks', 'page' ],
67  'fields' => [
68  'namespace' => NS_CATEGORY,
69  'title' => 'cl_to',
70  'value' => 'COUNT(*)'
71  ],
72  'conds' => [ 'page_title' => null ],
73  'options' => [ 'GROUP BY' => 'cl_to' ],
74  'join_conds' => [ 'page' => [ 'LEFT JOIN',
75  [ 'page_title = cl_to',
76  'page_namespace' => NS_CATEGORY ] ] ]
77  ];
78  }
79 
80  public function preprocessResults( $db, $res ) {
81  parent::preprocessResults( $db, $res );
82 
83  $this->currentCategoryCounts = [];
84 
85  if ( !$res->numRows() || !$this->isCached() ) {
86  return;
87  }
88 
89  // Fetch (hopefully) up-to-date numbers of pages in each category.
90  // This should be fast enough as we limit the list to a reasonable length.
91 
92  $allCategories = [];
93  foreach ( $res as $row ) {
94  $allCategories[] = $row->title;
95  }
96 
97  $categoryRes = $db->newSelectQueryBuilder()
98  ->select( [ 'cat_title', 'cat_pages' ] )
99  ->from( 'category' )
100  ->where( [ 'cat_title' => $allCategories ] )
101  ->caller( __METHOD__ )->fetchResultSet();
102  foreach ( $categoryRes as $row ) {
103  $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
104  }
105 
106  // Back to start for display
107  $res->seek( 0 );
108  }
109 
115  public function formatResult( $skin, $result ) {
116  $nt = Title::makeTitle( $result->namespace, $result->title );
117 
118  $text = new HtmlArmor( $this->languageConverter->convertHtml( $nt->getText() ) );
119 
120  if ( !$this->isCached() ) {
121  // We can assume the freshest data
122  $plink = $this->getLinkRenderer()->makeBrokenLink(
123  $nt,
124  $text
125  );
126  $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
127  } else {
128  $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
129 
130  $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
131  $cachedValue = intval( $result->value ); // T76910
132 
133  // If the category has been created or emptied since the list was refreshed, strike it
134  if ( $nt->isKnown() || $currentValue === 0 ) {
135  $plink = "<del>$plink</del>";
136  }
137 
138  // Show the current number of category entries if it changed
139  if ( $currentValue !== $cachedValue ) {
140  $nlinks = $this->msg( 'nmemberschanged' )
141  ->numParams( $cachedValue, $currentValue )->escaped();
142  } else {
143  $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
144  }
145  }
146 
147  return $this->getLanguage()->specialList( $plink, $nlinks );
148  }
149 
150  protected function getGroupName() {
151  return 'maintenance';
152  }
153 }
154 
159 class_alias( SpecialWantedCategories::class, 'SpecialWantedCategories' );
const NS_CATEGORY
Definition: Defines.php:78
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)
Definition: QueryPage.php:985
isCached()
Whether or not the output of the page in question is retrieved from the database cache.
Definition: QueryPage.php:360
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:185
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getContentLanguage()
Shortcut to get content language.
getLanguage()
Shortcut to get user's language.
Class definition for a wanted query page like WantedPages, WantedTemplates, etc.
A querypage to list the most wanted categories - implements Special:Wantedcategories.
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:76
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:624
The base class for all skins.
Definition: Skin.php:60
The shared interface for all language converters.
Provide primary and replica IDatabase connections.