| 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 |
|
| 25 |
require_once __DIR__ . '/Maintenance.php'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class RecountCategories extends Maintenance { |
| 35 |
|
| 36 |
private $minimumId; |
| 37 |
|
| 38 |
public function __construct() { |
| 39 |
parent::__construct(); |
| 40 |
$this->addDescription( <<<'TEXT' |
| 41 |
This script refreshes the category membership counts stored in the category |
| 42 |
table. As time passes, these counts often drift from the actual number of |
| 43 |
category members. The script identifies rows where the value in the category |
| 44 |
table does not match the number of categorylinks rows for that category, and |
| 45 |
updates the category table accordingly. |
| 46 |
|
| 47 |
To fully refresh the data in the category table, you need to run this script |
| 48 |
for all three modes. Alternatively, just one mode can be run if required. |
| 49 |
TEXT |
| 50 |
); |
| 51 |
$this->addOption( |
| 52 |
'mode', |
| 53 |
'(REQUIRED) Which category count column to recompute: "pages", "subcats", "files" or "all".', |
| 54 |
true, |
| 55 |
true |
| 56 |
); |
| 57 |
$this->addOption( |
| 58 |
'begin', |
| 59 |
'Only recount categories with cat_id greater than the given value', |
| 60 |
false, |
| 61 |
true |
| 62 |
); |
| 63 |
$this->addOption( |
| 64 |
'throttle', |
| 65 |
'Wait this many milliseconds after each batch. Default: 0', |
| 66 |
false, |
| 67 |
true |
| 68 |
); |
| 69 |
|
| 70 |
$this->addOption( |
| 71 |
'skip-cleanup', |
| 72 |
'Skip running cleanupEmptyCategories if the "page" mode is selected', |
| 73 |
false, |
| 74 |
false |
| 75 |
); |
| 76 |
|
| 77 |
$this->setBatchSize( 500 ); |
| 78 |
} |
| 79 |
|
| 80 |
public function execute() { |
| 81 |
$originalMode = $this->getOption( 'mode' ); |
| 82 |
if ( !in_array( $originalMode, [ 'pages', 'subcats', 'files', 'all' ] ) ) { |
| 83 |
$this->fatalError( 'Please specify a valid mode: one of "pages", "subcats", "files" or "all".' ); |
| 84 |
} |
| 85 |
|
| 86 |
if ( $originalMode === 'all' ) { |
| 87 |
$modes = [ 'pages', 'subcats', 'files' ]; |
| 88 |
} else { |
| 89 |
$modes = [ $originalMode ]; |
| 90 |
} |
| 91 |
|
| 92 |
foreach ( $modes as $mode ) { |
| 93 |
$this->output( "Starting to recount {$mode} counts.\n" ); |
| 94 |
$this->minimumId = intval( $this->getOption( 'begin', 0 ) ); |
| 95 |
|
| 96 |
|
| 97 |
$affectedRows = 0; |
| 98 |
|
| 99 |
while ( ( $result = $this->doWork( $mode ) ) !== false ) { |
| 100 |
$affectedRows += $result; |
| 101 |
usleep( $this->getOption( 'throttle', 0 ) * 1000 ); |
| 102 |
} |
| 103 |
|
| 104 |
$this->output( "Updated the {$mode} counts of $affectedRows categories.\n" ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
$this->output( "Done!\n" ); |
| 109 |
if ( $originalMode !== 'all' ) { |
| 110 |
$this->output( "Now run the script using the other --mode options if you haven't already.\n" ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( in_array( 'pages', $modes ) ) { |
| 114 |
if ( $this->hasOption( 'skip-cleanup' ) ) { |
| 115 |
$this->output( |
| 116 |
"Also run 'php cleanupEmptyCategories.php --mode remove' to remove empty,\n" . |
| 117 |
"nonexistent categories from the category table.\n\n" ); |
| 118 |
} else { |
| 119 |
$this->output( "Running cleanupEmptyCategories.php\n" ); |
| 120 |
$cleanup = $this->runChild( CleanupEmptyCategories::class ); |
| 121 |
'@phan-var CleanupEmptyCategories $cleanup'; |
| 122 |
|
| 123 |
|
| 124 |
$cleanup->loadParamsAndArgs( $this->mSelf, [], [] ); |
| 125 |
|
| 126 |
$cleanup->setForce( true ); |
| 127 |
$cleanup->execute(); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
protected function doWork( $mode ) { |
| 133 |
$this->output( "Finding up to {$this->getBatchSize()} drifted rows " . |
| 134 |
"greater than cat_id {$this->minimumId}...\n" ); |
| 135 |
|
| 136 |
$dbr = $this->getDB( DB_REPLICA, 'vslow' ); |
| 137 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 138 |
->select( 'COUNT(*)' ) |
| 139 |
->from( 'categorylinks' ) |
| 140 |
->where( 'cl_to = cat_title' ); |
| 141 |
if ( $mode === 'subcats' ) { |
| 142 |
$queryBuilder->andWhere( [ 'cl_type' => 'subcat' ] ); |
| 143 |
} elseif ( $mode === 'files' ) { |
| 144 |
$queryBuilder->andWhere( [ 'cl_type' => 'file' ] ); |
| 145 |
} |
| 146 |
|
| 147 |
$countingSubquery = $queryBuilder->caller( __METHOD__ )->getSQL(); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
$idsToUpdate = $dbr->newSelectQueryBuilder() |
| 154 |
->select( 'cat_id' ) |
| 155 |
->from( 'category' ) |
| 156 |
->where( [ $dbr->expr( 'cat_id', '>', (int)$this->minimumId ), "cat_{$mode} != ($countingSubquery)" ] ) |
| 157 |
->limit( $this->getBatchSize() ) |
| 158 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 159 |
if ( !$idsToUpdate ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
$this->output( "Updating cat_{$mode} field on " . |
| 163 |
count( $idsToUpdate ) . " rows...\n" ); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
$this->minimumId = end( $idsToUpdate ); |
| 170 |
|
| 171 |
|
| 172 |
$dbw = $this->getPrimaryDB(); |
| 173 |
$res = $dbw->newSelectQueryBuilder() |
| 174 |
->select( [ 'cat_id', 'count' => "($countingSubquery)" ] ) |
| 175 |
->from( 'category' ) |
| 176 |
->where( [ 'cat_id' => $idsToUpdate ] ) |
| 177 |
->caller( __METHOD__ )->fetchResultSet(); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
$affectedRows = 0; |
| 185 |
foreach ( $res as $row ) { |
| 186 |
$dbw->newUpdateQueryBuilder() |
| 187 |
->update( 'category' ) |
| 188 |
->set( [ "cat_{$mode}" => $row->count ] ) |
| 189 |
->where( [ |
| 190 |
'cat_id' => $row->cat_id, |
| 191 |
$dbw->expr( "cat_{$mode}", '!=', (int)$row->count ), |
| 192 |
] ) |
| 193 |
->caller( __METHOD__ ) |
| 194 |
->execute(); |
| 195 |
$affectedRows += $dbw->affectedRows(); |
| 196 |
} |
| 197 |
|
| 198 |
$this->waitForReplication(); |
| 199 |
|
| 200 |
return $affectedRows; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
$maintClass = RecountCategories::class; |
| 206 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 207 |
|