3 * Generic table cleanup class. Already subclasses maintenance
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once( dirname(__FILE__) . '/Maintenance.php' );
26 abstract class TableCleanup extends Maintenance {
27 protected $targetTable = 'page';
28 protected $dryrun = false;
29 protected $maxLag = 10; # if slaves are lagged more than 10 secs, wait
31 public function __construct() {
32 parent::__construct();
33 $this->addOption( 'dry-run', 'Perform a dry run' );
36 public function execute() {
38 $wgUser->setName( 'Conversion script' );
39 $this->dryrun = $this->hasOption( 'dry-run' );
41 $this->output( "Checking for bad titles...\n" );
43 $this->output( "Checking and fixing bad titles...\n" );
45 $this->runTable( $this->targetTable,
46 '', //'WHERE page_namespace=0',
47 array( $this, 'processPage' ) );
50 protected function init( $count, $table ) {
53 $this->count = $count;
54 $this->startTime = wfTime();
55 $this->table = $table;
58 protected function progress( $updated ) {
59 $this->updated += $updated;
61 if( $this->processed % 100 != 0 ) {
64 $portion = $this->processed / $this->count;
65 $updateRate = $this->updated / $this->processed;
68 $delta = $now - $this->startTime;
69 $estimatedTotalTime = $delta / $portion;
70 $eta = $this->startTime + $estimatedTotalTime;
73 sprintf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
75 wfTimestamp( TS_DB, intval( $now ) ),
78 wfTimestamp( TS_DB, intval( $eta ) ),
81 $this->processed / $delta,
88 protected function runTable( $table, $where, $callback ) {
89 $dbw = wfGetDB( DB_MASTER );
90 $count = $dbw->selectField( $table, 'count(*)', '', __METHOD__ );
91 $this->init( $count, $table );
92 $this->output( "Processing $table..." );
94 $tableName = $dbw->tableName( $table );
95 $sql = "SELECT * FROM $tableName $where";
96 $result = $dbw->query( $sql, __METHOD__ );
98 foreach( $result as $row ) {
99 call_user_func( $callback, $row );
101 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
105 protected function hexChar( $matches ) {
106 return sprintf( "\\x%02x", ord( $matches[1] ) );
109 abstract protected function processPage( $row );