SpecialWantedCategories.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Implements Special:Wantedcategories
  4. *
  5. * Copyright © 2005 Ævar Arnfjörð Bjarmason
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup SpecialPage
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * A querypage to list the most wanted categories - implements Special:Wantedcategories
  28. *
  29. * @ingroup SpecialPage
  30. */
  31. class SpecialWantedCategories extends WantedQueryPage {
  32. private $currentCategoryCounts;
  33. function __construct( $name = 'Wantedcategories' ) {
  34. parent::__construct( $name );
  35. }
  36. function getQueryInfo() {
  37. return [
  38. 'tables' => [ 'categorylinks', 'page' ],
  39. 'fields' => [
  40. 'namespace' => NS_CATEGORY,
  41. 'title' => 'cl_to',
  42. 'value' => 'COUNT(*)'
  43. ],
  44. 'conds' => [ 'page_title IS NULL' ],
  45. 'options' => [ 'GROUP BY' => 'cl_to' ],
  46. 'join_conds' => [ 'page' => [ 'LEFT JOIN',
  47. [ 'page_title = cl_to',
  48. 'page_namespace' => NS_CATEGORY ] ] ]
  49. ];
  50. }
  51. function preprocessResults( $db, $res ) {
  52. parent::preprocessResults( $db, $res );
  53. $this->currentCategoryCounts = [];
  54. if ( !$res->numRows() || !$this->isCached() ) {
  55. return;
  56. }
  57. // Fetch (hopefully) up-to-date numbers of pages in each category.
  58. // This should be fast enough as we limit the list to a reasonable length.
  59. $allCategories = [];
  60. foreach ( $res as $row ) {
  61. $allCategories[] = $row->title;
  62. }
  63. $categoryRes = $db->select(
  64. 'category',
  65. [ 'cat_title', 'cat_pages' ],
  66. [ 'cat_title' => $allCategories ],
  67. __METHOD__
  68. );
  69. foreach ( $categoryRes as $row ) {
  70. $this->currentCategoryCounts[$row->cat_title] = intval( $row->cat_pages );
  71. }
  72. // Back to start for display
  73. $res->seek( 0 );
  74. }
  75. /**
  76. * @param Skin $skin
  77. * @param object $result Result row
  78. * @return string
  79. */
  80. function formatResult( $skin, $result ) {
  81. $nt = Title::makeTitle( $result->namespace, $result->title );
  82. $text = new HtmlArmor( MediaWikiServices::getInstance()->getContentLanguage()
  83. ->convert( htmlspecialchars( $nt->getText() ) ) );
  84. if ( !$this->isCached() ) {
  85. // We can assume the freshest data
  86. $plink = $this->getLinkRenderer()->makeBrokenLink(
  87. $nt,
  88. $text
  89. );
  90. $nlinks = $this->msg( 'nmembers' )->numParams( $result->value )->escaped();
  91. } else {
  92. $plink = $this->getLinkRenderer()->makeLink( $nt, $text );
  93. $currentValue = $this->currentCategoryCounts[$result->title] ?? 0;
  94. $cachedValue = intval( $result->value ); // T76910
  95. // If the category has been created or emptied since the list was refreshed, strike it
  96. if ( $nt->isKnown() || $currentValue === 0 ) {
  97. $plink = "<del>$plink</del>";
  98. }
  99. // Show the current number of category entries if it changed
  100. if ( $currentValue !== $cachedValue ) {
  101. $nlinks = $this->msg( 'nmemberschanged' )
  102. ->numParams( $cachedValue, $currentValue )->escaped();
  103. } else {
  104. $nlinks = $this->msg( 'nmembers' )->numParams( $cachedValue )->escaped();
  105. }
  106. }
  107. return $this->getLanguage()->specialList( $plink, $nlinks );
  108. }
  109. protected function getGroupName() {
  110. return 'maintenance';
  111. }
  112. }