2006-07-13 17:38:01 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2012-06-25 19:54:41 +00:00
|
|
|
* Generic class to cleanup a database table.
|
2009-08-24 02:14:52 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-09-05 13:15:48 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2006-07-13 17:38:01 +00:00
|
|
|
|
2023-02-23 20:44:38 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-08-24 02:14:52 +00:00
|
|
|
|
2012-06-25 19:54:41 +00:00
|
|
|
/**
|
|
|
|
|
* Generic class to cleanup a database table. Already subclasses Maintenance.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-09-24 04:19:25 +00:00
|
|
|
class TableCleanup extends Maintenance {
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var array */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $defaultParams = [
|
2009-09-24 04:19:25 +00:00
|
|
|
'table' => 'page',
|
2016-02-17 09:09:32 +00:00
|
|
|
'conds' => [],
|
2009-09-24 04:19:25 +00:00
|
|
|
'index' => 'page_id',
|
|
|
|
|
'callback' => 'processRow',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2009-09-24 04:19:25 +00:00
|
|
|
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var bool */
|
2009-08-24 02:14:52 +00:00
|
|
|
protected $dryrun = false;
|
2024-09-12 19:59:28 +00:00
|
|
|
/** @var int */
|
2020-04-21 06:57:21 +00:00
|
|
|
protected $reportInterval = 100;
|
2009-08-24 02:14:52 +00:00
|
|
|
|
2024-04-21 14:43:04 +00:00
|
|
|
protected int $processed;
|
|
|
|
|
protected int $updated;
|
|
|
|
|
protected int $count;
|
|
|
|
|
protected float $startTime;
|
|
|
|
|
protected string $table;
|
2013-09-02 15:49:22 +00:00
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addOption( 'dry-run', 'Perform a dry run' );
|
2020-04-21 06:57:21 +00:00
|
|
|
$this->addOption( 'reporting-interval', 'How often to print status line' );
|
|
|
|
|
$this->setBatchSize( 100 );
|
2006-07-13 17:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
public function execute() {
|
2020-04-21 06:57:21 +00:00
|
|
|
$this->reportInterval = $this->getOption( 'reporting-interval', $this->reportInterval );
|
|
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->dryrun = $this->hasOption( 'dry-run' );
|
2020-10-09 19:43:28 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->dryrun ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Checking for bad titles...\n" );
|
2006-07-13 17:38:01 +00:00
|
|
|
} else {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Checking and fixing bad titles...\n" );
|
2006-07-13 17:38:01 +00:00
|
|
|
}
|
2020-10-09 19:43:28 +00:00
|
|
|
|
2009-09-24 04:19:25 +00:00
|
|
|
$this->runTable( $this->defaultParams );
|
2006-07-13 17:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
protected function init( $count, $table ) {
|
2006-07-13 17:38:01 +00:00
|
|
|
$this->processed = 0;
|
|
|
|
|
$this->updated = 0;
|
|
|
|
|
$this->count = $count;
|
2012-09-04 19:05:40 +00:00
|
|
|
$this->startTime = microtime( true );
|
2006-07-13 17:38:01 +00:00
|
|
|
$this->table = $table;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $updated
|
|
|
|
|
*/
|
2009-08-24 02:14:52 +00:00
|
|
|
protected function progress( $updated ) {
|
2006-07-13 17:38:01 +00:00
|
|
|
$this->updated += $updated;
|
|
|
|
|
$this->processed++;
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->processed % $this->reportInterval != 0 ) {
|
2006-07-13 17:38:01 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$portion = $this->processed / $this->count;
|
|
|
|
|
$updateRate = $this->updated / $this->processed;
|
|
|
|
|
|
2012-09-04 19:05:40 +00:00
|
|
|
$now = microtime( true );
|
2006-07-13 17:38:01 +00:00
|
|
|
$delta = $now - $this->startTime;
|
|
|
|
|
$estimatedTotalTime = $delta / $portion;
|
|
|
|
|
$eta = $this->startTime + $estimatedTotalTime;
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
$this->output(
|
2009-08-24 02:14:52 +00:00
|
|
|
sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
|
2019-09-24 22:42:00 +00:00
|
|
|
WikiMap::getCurrentWikiDbDomain()->getId(),
|
2009-08-24 02:14:52 +00:00
|
|
|
wfTimestamp( TS_DB, intval( $now ) ),
|
|
|
|
|
$portion * 100.0,
|
|
|
|
|
$this->table,
|
|
|
|
|
wfTimestamp( TS_DB, intval( $eta ) ),
|
|
|
|
|
$this->processed,
|
|
|
|
|
$this->count,
|
|
|
|
|
$this->processed / $delta,
|
2010-05-22 16:50:39 +00:00
|
|
|
$updateRate * 100.0
|
2009-08-24 02:14:52 +00:00
|
|
|
)
|
|
|
|
|
);
|
2006-07-13 17:38:01 +00:00
|
|
|
flush();
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
*/
|
2009-09-24 04:19:25 +00:00
|
|
|
public function runTable( $params ) {
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbr = $this->getReplicaDB();
|
2009-09-24 04:19:25 +00:00
|
|
|
|
|
|
|
|
if ( array_diff( array_keys( $params ),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'table', 'conds', 'index', 'callback' ] )
|
2013-10-08 17:11:35 +00:00
|
|
|
) {
|
2023-01-29 13:41:55 +00:00
|
|
|
$this->fatalError( __METHOD__ . ': Missing parameter ' . implode( ', ', $params ) );
|
2009-09-24 04:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$table = $params['table'];
|
2011-07-08 22:08:19 +00:00
|
|
|
// count(*) would melt the DB for huge tables, we can estimate here
|
2024-04-26 23:08:48 +00:00
|
|
|
$count = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->table( $table )
|
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
|
->estimateRowCount();
|
2006-07-13 17:38:01 +00:00
|
|
|
$this->init( $count, $table );
|
2009-09-24 04:19:25 +00:00
|
|
|
$this->output( "Processing $table...\n" );
|
|
|
|
|
|
|
|
|
|
$index = (array)$params['index'];
|
2016-02-17 09:09:32 +00:00
|
|
|
$indexConds = [];
|
|
|
|
|
$callback = [ $this, $params['callback'] ];
|
2006-07-13 17:38:01 +00:00
|
|
|
|
2009-09-24 04:19:25 +00:00
|
|
|
while ( true ) {
|
|
|
|
|
$conds = array_merge( $params['conds'], $indexConds );
|
2023-07-18 22:56:37 +00:00
|
|
|
$res = $dbr->newSelectQueryBuilder()
|
|
|
|
|
->select( '*' )
|
|
|
|
|
->from( $table )
|
|
|
|
|
->where( $conds )
|
|
|
|
|
->orderBy( implode( ',', $index ) )
|
|
|
|
|
->limit( $this->getBatchSize() )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2009-09-24 04:19:25 +00:00
|
|
|
if ( !$res->numRows() ) {
|
|
|
|
|
// Done
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-07-13 17:38:01 +00:00
|
|
|
|
2009-09-24 04:19:25 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
call_user_func( $callback, $row );
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-21 06:57:21 +00:00
|
|
|
if ( $res->numRows() < $this->getBatchSize() ) {
|
2009-09-24 04:19:25 +00:00
|
|
|
// Done
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the conditions to select the next batch.
|
2022-09-03 22:48:19 +00:00
|
|
|
$conds = [];
|
|
|
|
|
foreach ( $index as $field ) {
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable $res has at at least one item
|
2022-09-03 22:48:19 +00:00
|
|
|
$conds[ $field ] = $row->$field;
|
2009-09-24 04:19:25 +00:00
|
|
|
}
|
2022-09-03 22:48:19 +00:00
|
|
|
$indexConds = [ $dbr->buildComparison( '>', $conds ) ];
|
2006-07-13 17:38:01 +00:00
|
|
|
}
|
2009-09-24 04:19:25 +00:00
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
|
2006-07-13 17:38:01 +00:00
|
|
|
}
|
2006-07-13 21:54:13 +00:00
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
2019-02-28 11:33:47 +00:00
|
|
|
* @param string[] $matches
|
2013-09-02 15:49:22 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-08-24 02:14:52 +00:00
|
|
|
protected function hexChar( $matches ) {
|
2006-07-13 21:54:13 +00:00
|
|
|
return sprintf( "\\x%02x", ord( $matches[1] ) );
|
|
|
|
|
}
|
2006-07-13 17:38:01 +00:00
|
|
|
}
|