27require_once __DIR__ .
'/Maintenance.php';
41 parent::__construct();
43This script refreshes the category membership counts stored in the category
44table. As time passes, these counts often drift from the actual number of
45category members. The script identifies rows where the value in the category
46table does not match the number of categorylinks rows
for that category, and
47updates the category table accordingly.
49To fully refresh the data in the category table, you need to
run this script
50for all three modes. Alternatively, just one mode can be
run if required.
55 '(REQUIRED) Which category count column to recompute: "pages", "subcats", "files" or "all".',
61 'Only recount categories with cat_id greater than the given value',
67 'Wait this many milliseconds after each batch. Default: 0',
74 'Skip running cleanupEmptyCategories if the "page" mode is selected',
83 $originalMode = $this->
getOption(
'mode' );
84 if ( !in_array( $originalMode, [
'pages',
'subcats',
'files',
'all' ] ) ) {
85 $this->
fatalError(
'Please specify a valid mode: one of "pages", "subcats", "files" or "all".' );
88 if ( $originalMode ===
'all' ) {
89 $modes = [
'pages',
'subcats',
'files' ];
91 $modes = [ $originalMode ];
94 foreach ( $modes as $mode ) {
95 $this->
output(
"Starting to recount {$mode} counts.\n" );
96 $this->minimumId = intval( $this->
getOption(
'begin', 0 ) );
101 while ( ( $result = $this->
doWork( $mode ) ) !==
false ) {
102 $affectedRows += $result;
103 usleep( $this->
getOption(
'throttle', 0 ) * 1000 );
106 $this->
output(
"Updated the {$mode} counts of $affectedRows categories.\n" );
110 $this->
output(
"Done!\n" );
111 if ( $originalMode !==
'all' ) {
112 $this->
output(
"Now run the script using the other --mode options if you haven't already.\n" );
115 if ( in_array(
'pages', $modes ) ) {
116 if ( $this->
hasOption(
'skip-cleanup' ) ) {
118 "Also run 'php cleanupEmptyCategories.php --mode remove' to remove empty,\n" .
119 "nonexistent categories from the category table.\n\n" );
121 $this->
output(
"Running cleanupEmptyCategories.php\n" );
122 $cleanup = $this->
runChild( CleanupEmptyCategories::class );
123 '@phan-var CleanupEmptyCategories $cleanup';
126 $cleanup->loadParamsAndArgs( $this->mSelf, [], [] );
128 $cleanup->setForce(
true );
135 $this->
output(
"Finding up to {$this->getBatchSize()} drifted rows " .
136 "greater than cat_id {$this->minimumId}...\n" );
139 $queryBuilder = $dbr->newSelectQueryBuilder()
140 ->select(
'COUNT(*)' )
141 ->from(
'categorylinks' )
142 ->where(
'cl_to = cat_title' );
143 if ( $mode ===
'subcats' ) {
144 $queryBuilder->andWhere( [
'cl_type' =>
'subcat' ] );
145 } elseif ( $mode ===
'files' ) {
146 $queryBuilder->andWhere( [
'cl_type' =>
'file' ] );
149 $countingSubquery = $queryBuilder->caller( __METHOD__ )->getSQL();
155 $idsToUpdate = $dbr->newSelectQueryBuilder()
158 ->where( [ $dbr->expr(
'cat_id',
'>', (
int)$this->minimumId ),
"cat_{$mode} != ($countingSubquery)" ] )
160 ->caller( __METHOD__ )->fetchFieldValues();
161 if ( !$idsToUpdate ) {
164 $this->
output(
"Updating cat_{$mode} field on " .
165 count( $idsToUpdate ) .
" rows...\n" );
171 $this->minimumId = end( $idsToUpdate );
175 $res = $dbw->newSelectQueryBuilder()
176 ->select( [
'cat_id',
'count' =>
"($countingSubquery)" ] )
178 ->where( [
'cat_id' => $idsToUpdate ] )
179 ->caller( __METHOD__ )->fetchResultSet();
187 foreach ( $res as $row ) {
188 $dbw->newUpdateQueryBuilder()
189 ->update(
'category' )
190 ->set( [
"cat_{$mode}" => $row->count ] )
192 'cat_id' => $row->cat_id,
193 $dbw->expr(
"cat_{$mode}",
'!=', (
int)$row->count ),
195 ->caller( __METHOD__ )
197 $affectedRows += $dbw->affectedRows();
202 return $affectedRows;
208require_once RUN_MAINTENANCE_IF_MAIN;
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
runChild( $maintClass, $classFile=null)
Returns an instance of the given maintenance script, with all of the current arguments passed to it.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
addDescription( $text)
Set the description text.
Maintenance script that refreshes category membership counts in the category table.
__construct()
Default constructor.
execute()
Do the actual work.