| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Category\Category; |
| 25 |
|
| 26 |
|
| 27 |
require_once __DIR__ . '/Maintenance.php'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class CleanupEmptyCategories extends LoggedUpdateMaintenance { |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addDescription( |
| 41 |
<<<TEXT |
| 42 |
This script will clean up the category table by removing entries for empty |
| 43 |
categories without a description page and adding entries for empty categories |
| 44 |
with a description page. It will print out progress indicators every batch. The |
| 45 |
script is perfectly safe to run on large, live wikis, and running it multiple |
| 46 |
times is harmless. You may want to use the throttling options if it's causing |
| 47 |
too much load; they will not affect correctness. |
| 48 |
|
| 49 |
If the script is stopped and later resumed, you can use the --mode and --begin |
| 50 |
options with the last printed progress indicator to pick up where you left off. |
| 51 |
|
| 52 |
When the script has finished, it will make a note of this in the database, and |
| 53 |
will not run again without the --force option. |
| 54 |
TEXT |
| 55 |
); |
| 56 |
|
| 57 |
$this->addOption( |
| 58 |
'mode', |
| 59 |
'"add" empty categories with description pages, "remove" empty categories ' |
| 60 |
. 'without description pages, or "both"', |
| 61 |
false, |
| 62 |
true |
| 63 |
); |
| 64 |
$this->addOption( |
| 65 |
'begin', |
| 66 |
'Only do categories whose names are alphabetically after the provided name', |
| 67 |
false, |
| 68 |
true |
| 69 |
); |
| 70 |
$this->addOption( |
| 71 |
'throttle', |
| 72 |
'Wait this many milliseconds after each batch. Default: 0', |
| 73 |
false, |
| 74 |
true |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
protected function getUpdateKey() { |
| 79 |
return 'cleanup empty categories'; |
| 80 |
} |
| 81 |
|
| 82 |
protected function doDBUpdates() { |
| 83 |
$mode = $this->getOption( 'mode', 'both' ); |
| 84 |
$begin = $this->getOption( 'begin', '' ); |
| 85 |
$throttle = $this->getOption( 'throttle', 0 ); |
| 86 |
|
| 87 |
if ( !in_array( $mode, [ 'add', 'remove', 'both' ] ) ) { |
| 88 |
$this->output( "--mode must be 'add', 'remove', or 'both'.\n" ); |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
$dbw = $this->getPrimaryDB(); |
| 93 |
|
| 94 |
$throttle = intval( $throttle ); |
| 95 |
|
| 96 |
if ( $mode === 'add' || $mode === 'both' ) { |
| 97 |
if ( $begin !== '' ) { |
| 98 |
$where = [ $dbw->expr( 'page_title', '>', $begin ) ]; |
| 99 |
} else { |
| 100 |
$where = []; |
| 101 |
} |
| 102 |
|
| 103 |
$this->output( "Adding empty categories with description pages...\n" ); |
| 104 |
while ( true ) { |
| 105 |
|
| 106 |
$rows = $dbw->newSelectQueryBuilder() |
| 107 |
->select( 'page_title' ) |
| 108 |
->from( 'page' ) |
| 109 |
->leftJoin( 'category', null, 'page_title = cat_title' ) |
| 110 |
->where( $where ) |
| 111 |
->andWhere( [ 'page_namespace' => NS_CATEGORY, 'cat_title' => null ] ) |
| 112 |
->orderBy( 'page_title' ) |
| 113 |
->limit( $this->getBatchSize() ) |
| 114 |
->caller( __METHOD__ )->fetchResultSet(); |
| 115 |
if ( !$rows || $rows->numRows() <= 0 ) { |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
foreach ( $rows as $row ) { |
| 120 |
$name = $row->page_title; |
| 121 |
$where = [ $dbw->expr( 'page_title', '>', $name ) ]; |
| 122 |
|
| 123 |
|
| 124 |
$cat = Category::newFromName( $name ); |
| 125 |
if ( !is_object( $cat ) ) { |
| 126 |
$this->output( "The category named $name is not valid?!\n" ); |
| 127 |
} else { |
| 128 |
$cat->refreshCounts(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$this->output( "--mode=$mode --begin=$name\n" ); |
| 133 |
|
| 134 |
$this->waitForReplication(); |
| 135 |
usleep( $throttle * 1000 ); |
| 136 |
} |
| 137 |
|
| 138 |
$begin = ''; |
| 139 |
} |
| 140 |
|
| 141 |
if ( $mode === 'remove' || $mode === 'both' ) { |
| 142 |
if ( $begin !== '' ) { |
| 143 |
$where = [ $dbw->expr( 'cat_title', '>', $begin ) ]; |
| 144 |
} else { |
| 145 |
$where = []; |
| 146 |
} |
| 147 |
|
| 148 |
$this->output( "Removing empty categories without description pages...\n" ); |
| 149 |
while ( true ) { |
| 150 |
|
| 151 |
$rows = $dbw->newSelectQueryBuilder() |
| 152 |
->select( 'cat_title' ) |
| 153 |
->from( 'category' ) |
| 154 |
->leftJoin( 'page', null, [ 'page_namespace' => NS_CATEGORY, 'page_title = cat_title' ] ) |
| 155 |
->where( $where ) |
| 156 |
->andWhere( [ 'page_title' => null, 'cat_pages' => 0 ] ) |
| 157 |
->orderBy( 'cat_title' ) |
| 158 |
->limit( $this->getBatchSize() ) |
| 159 |
->caller( __METHOD__ )->fetchResultSet(); |
| 160 |
if ( !$rows || $rows->numRows() <= 0 ) { |
| 161 |
break; |
| 162 |
} |
| 163 |
foreach ( $rows as $row ) { |
| 164 |
$name = $row->cat_title; |
| 165 |
$where = [ $dbw->expr( 'cat_title', '>', $name ) ]; |
| 166 |
|
| 167 |
|
| 168 |
$cat = Category::newFromName( $name ); |
| 169 |
if ( !is_object( $cat ) ) { |
| 170 |
$this->output( "The category named $name is not valid?!\n" ); |
| 171 |
} else { |
| 172 |
$cat->refreshCounts(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
$this->output( "--mode=remove --begin=$name\n" ); |
| 178 |
|
| 179 |
$this->waitForReplication(); |
| 180 |
usleep( $throttle * 1000 ); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$this->output( "Category cleanup complete.\n" ); |
| 185 |
|
| 186 |
return true; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
$maintClass = CleanupEmptyCategories::class; |
| 192 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 193 |
|