cleanupEmptyCategories.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * Clean up empty categories in the category table.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Maintenance script to clean up empty categories in the category table.
  26. *
  27. * @ingroup Maintenance
  28. * @since 1.28
  29. */
  30. class CleanupEmptyCategories extends LoggedUpdateMaintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription(
  34. <<<TEXT
  35. This script will clean up the category table by removing entries for empty
  36. categories without a description page and adding entries for empty categories
  37. with a description page. It will print out progress indicators every batch. The
  38. script is perfectly safe to run on large, live wikis, and running it multiple
  39. times is harmless. You may want to use the throttling options if it's causing
  40. too much load; they will not affect correctness.
  41. If the script is stopped and later resumed, you can use the --mode and --begin
  42. options with the last printed progress indicator to pick up where you left off.
  43. When the script has finished, it will make a note of this in the database, and
  44. will not run again without the --force option.
  45. TEXT
  46. );
  47. $this->addOption(
  48. 'mode',
  49. '"add" empty categories with description pages, "remove" empty categories '
  50. . 'without description pages, or "both"',
  51. false,
  52. true
  53. );
  54. $this->addOption(
  55. 'begin',
  56. 'Only do categories whose names are alphabetically after the provided name',
  57. false,
  58. true
  59. );
  60. $this->addOption(
  61. 'throttle',
  62. 'Wait this many milliseconds after each batch. Default: 0',
  63. false,
  64. true
  65. );
  66. }
  67. protected function getUpdateKey() {
  68. return 'cleanup empty categories';
  69. }
  70. protected function doDBUpdates() {
  71. $mode = $this->getOption( 'mode', 'both' );
  72. $begin = $this->getOption( 'begin', '' );
  73. $throttle = $this->getOption( 'throttle', 0 );
  74. if ( !in_array( $mode, [ 'add', 'remove', 'both' ] ) ) {
  75. $this->output( "--mode must be 'add', 'remove', or 'both'.\n" );
  76. return false;
  77. }
  78. $dbw = $this->getDB( DB_MASTER );
  79. $throttle = intval( $throttle );
  80. if ( $mode === 'add' || $mode === 'both' ) {
  81. if ( $begin !== '' ) {
  82. $where = [ 'page_title > ' . $dbw->addQuotes( $begin ) ];
  83. } else {
  84. $where = [];
  85. }
  86. $this->output( "Adding empty categories with description pages...\n" );
  87. while ( true ) {
  88. # Find which category to update
  89. $rows = $dbw->select(
  90. [ 'page', 'category' ],
  91. 'page_title',
  92. array_merge( $where, [
  93. 'page_namespace' => NS_CATEGORY,
  94. 'cat_title' => null,
  95. ] ),
  96. __METHOD__,
  97. [
  98. 'ORDER BY' => 'page_title',
  99. 'LIMIT' => $this->mBatchSize,
  100. ],
  101. [
  102. 'category' => [ 'LEFT JOIN', 'page_title = cat_title' ],
  103. ]
  104. );
  105. if ( !$rows || $rows->numRows() <= 0 ) {
  106. # Done, hopefully.
  107. break;
  108. }
  109. foreach ( $rows as $row ) {
  110. $name = $row->page_title;
  111. $where = [ 'page_title > ' . $dbw->addQuotes( $name ) ];
  112. # Use the row to update the category count
  113. $cat = Category::newFromName( $name );
  114. if ( !is_object( $cat ) ) {
  115. $this->output( "The category named $name is not valid?!\n" );
  116. } else {
  117. $cat->refreshCounts();
  118. }
  119. }
  120. $this->output( "--mode=$mode --begin=$name\n" );
  121. wfWaitForSlaves();
  122. usleep( $throttle * 1000 );
  123. }
  124. $begin = '';
  125. }
  126. if ( $mode === 'remove' || $mode === 'both' ) {
  127. if ( $begin !== '' ) {
  128. $where = [ 'cat_title > ' . $dbw->addQuotes( $begin ) ];
  129. } else {
  130. $where = [];
  131. }
  132. $this->output( "Removing empty categories without description pages...\n" );
  133. while ( true ) {
  134. # Find which category to update
  135. $rows = $dbw->select(
  136. [ 'category', 'page' ],
  137. 'cat_title',
  138. array_merge( $where, [
  139. 'page_title' => null,
  140. 'cat_pages' => 0,
  141. ] ),
  142. __METHOD__,
  143. [
  144. 'ORDER BY' => 'cat_title',
  145. 'LIMIT' => $this->mBatchSize,
  146. ],
  147. [
  148. 'page' => [ 'LEFT JOIN', [
  149. 'page_namespace' => NS_CATEGORY, 'page_title = cat_title'
  150. ] ],
  151. ]
  152. );
  153. if ( !$rows || $rows->numRows() <= 0 ) {
  154. # Done, hopefully.
  155. break;
  156. }
  157. foreach ( $rows as $row ) {
  158. $name = $row->cat_title;
  159. $where = [ 'cat_title > ' . $dbw->addQuotes( $name ) ];
  160. # Use the row to update the category count
  161. $cat = Category::newFromName( $name );
  162. if ( !is_object( $cat ) ) {
  163. $this->output( "The category named $name is not valid?!\n" );
  164. } else {
  165. $cat->refreshCounts();
  166. }
  167. }
  168. $this->output( "--mode=remove --begin=$name\n" );
  169. wfWaitForSlaves();
  170. usleep( $throttle * 1000 );
  171. }
  172. }
  173. $this->output( "Category cleanup complete.\n" );
  174. return true;
  175. }
  176. }
  177. $maintClass = 'CleanupEmptyCategories';
  178. require_once RUN_MAINTENANCE_IF_MAIN;