39 parent::__construct();
40 $this->addDescription( <<<
'TEXT'
41This script refreshes the category membership counts stored in the category
42table. As time passes, these counts often drift from the actual number of
43category members. The script identifies rows where the value in the category
44table does not match the number of categorylinks rows
for that category, and
45updates the category table accordingly.
47To fully refresh the data in the category table, you need to
run this script
48for all three modes. Alternatively, just one mode can be
run if required.
53 '(REQUIRED) Which category count column to recompute: "pages", "subcats", "files" or "all".',
59 'Only recount categories with cat_id greater than the given value',
65 'Wait this many milliseconds after each batch. Default: 0',
72 'Skip running cleanupEmptyCategories if the "page" mode is selected',
77 $this->setBatchSize( 500 );
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".' );
86 if ( $originalMode ===
'all' ) {
87 $modes = [
'pages',
'subcats',
'files' ];
89 $modes = [ $originalMode ];
92 foreach ( $modes as $mode ) {
93 $this->output(
"Starting to recount {$mode} counts.\n" );
94 $this->minimumId = intval( $this->getOption(
'begin', 0 ) );
99 while ( ( $result = $this->
doWork( $mode ) ) !==
false ) {
100 $affectedRows += $result;
101 usleep( $this->getOption(
'throttle', 0 ) * 1000 );
104 $this->output(
"Updated the {$mode} counts of $affectedRows categories.\n" );
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" );
113 if ( in_array(
'pages', $modes ) ) {
114 if ( $this->hasOption(
'skip-cleanup' ) ) {
116 "Also run 'php cleanupEmptyCategories.php --mode remove' to remove empty,\n" .
117 "nonexistent categories from the category table.\n\n" );
119 $this->output(
"Running cleanupEmptyCategories.php\n" );
120 $cleanup = $this->runChild( CleanupEmptyCategories::class );
121 '@phan-var CleanupEmptyCategories $cleanup';
124 $cleanup->loadParamsAndArgs( $this->mSelf, [], [] );
126 $cleanup->setForce(
true );
133 $this->output(
"Finding up to {$this->getBatchSize()} drifted rows " .
134 "greater than cat_id {$this->minimumId}...\n" );
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' ] );
147 $countingSubquery = $queryBuilder->caller( __METHOD__ )->getSQL();
153 $idsToUpdate = $dbr->newSelectQueryBuilder()
156 ->where( [ $dbr->expr(
'cat_id',
'>', (
int)$this->minimumId ),
"cat_{$mode} != ($countingSubquery)" ] )
157 ->limit( $this->getBatchSize() )
158 ->caller( __METHOD__ )->fetchFieldValues();
159 if ( !$idsToUpdate ) {
162 $this->output(
"Updating cat_{$mode} field on " .
163 count( $idsToUpdate ) .
" rows...\n" );
169 $this->minimumId = end( $idsToUpdate );
172 $dbw = $this->getPrimaryDB();
173 $res = $dbw->newSelectQueryBuilder()
174 ->select( [
'cat_id',
'count' =>
"($countingSubquery)" ] )
176 ->where( [
'cat_id' => $idsToUpdate ] )
177 ->caller( __METHOD__ )->fetchResultSet();
185 foreach ( $res as $row ) {
186 $dbw->newUpdateQueryBuilder()
187 ->update(
'category' )
188 ->set( [
"cat_{$mode}" => $row->count ] )
190 'cat_id' => $row->cat_id,
191 $dbw->expr(
"cat_{$mode}",
'!=', (
int)$row->count ),
193 ->caller( __METHOD__ )
195 $affectedRows += $dbw->affectedRows();
198 $this->waitForReplication();
200 return $affectedRows;