MediaWiki  REL1_20
cleanupRemovedModules.php
Go to the documentation of this file.
00001 <?php
00025 require_once( __DIR__ . '/Maintenance.php' );
00026 
00033 class CleanupRemovedModules extends Maintenance {
00034 
00035         public function __construct() {
00036                 parent::__construct();
00037                 $this->mDescription = 'Remove cache entries for removed ResourceLoader modules from the database';
00038                 $this->addOption( 'batchsize', 'Delete rows in batches of this size. Default: 500', false, true );
00039                 $this->addOption( 'max-slave-lag', 'If the slave lag exceeds this many seconds, wait until it drops below this value. Default: 5', false, true );
00040         }
00041 
00042         public function execute() {
00043                 $dbw = wfGetDB( DB_MASTER );
00044                 $rl = new ResourceLoader();
00045                 $moduleNames = $rl->getModuleNames();
00046                 $moduleList = implode( ', ', array_map( array( $dbw, 'addQuotes' ), $moduleNames ) );
00047                 $limit = max( 1, intval( $this->getOption( 'batchsize', 500 ) ) );
00048                 $maxlag = intval( $this->getOption( 'max-slave-lag', 5 ) );
00049 
00050                 $this->output( "Cleaning up module_deps table...\n" );
00051                 $i = 1;
00052                 $modDeps = $dbw->tableName( 'module_deps' );
00053                 do {
00054                         // $dbw->delete() doesn't support LIMIT :(
00055                         $where = $moduleList ? "md_module NOT IN ($moduleList)" : '1=1';
00056                         $dbw->query( "DELETE FROM $modDeps WHERE $where LIMIT $limit", __METHOD__ );
00057                         $numRows = $dbw->affectedRows();
00058                         $this->output( "Batch $i: $numRows rows\n" );
00059                         $i++;
00060                         wfWaitForSlaves( $maxlag );
00061                 } while( $numRows > 0 );
00062                 $this->output( "done\n" );
00063 
00064                 $this->output( "Cleaning up msg_resource table...\n" );
00065                 $i = 1;
00066 
00067                 $mrRes = $dbw->tableName( 'msg_resource' );
00068                 do {
00069                         $where = $moduleList ? "mr_resource NOT IN ($moduleList)" : '1=1';
00070                         $dbw->query( "DELETE FROM $mrRes WHERE $where LIMIT $limit", __METHOD__ );
00071                         $numRows = $dbw->affectedRows();
00072                         $this->output( "Batch $i: $numRows rows\n" );
00073                         $i++;
00074                         wfWaitForSlaves( $maxlag );
00075                 } while( $numRows > 0 );
00076                 $this->output( "done\n" );
00077 
00078                 $this->output( "Cleaning up msg_resource_links table...\n" );
00079                 $i = 1;
00080                 $msgResLinks = $dbw->tableName( 'msg_resource_links' );
00081                 do {
00082                         $where = $moduleList ? "mrl_resource NOT IN ($moduleList)" : '1=1';
00083                         $dbw->query( "DELETE FROM $msgResLinks WHERE $where LIMIT $limit", __METHOD__ );
00084                         $numRows = $dbw->affectedRows();
00085                         $this->output( "Batch $i: $numRows rows\n" );
00086                         $i++;
00087                         wfWaitForSlaves( $maxlag );
00088                 } while( $numRows > 0 );
00089                 $this->output( "done\n" );
00090         }
00091 }
00092 
00093 $maintClass = "CleanupRemovedModules";
00094 require_once( RUN_MAINTENANCE_IF_MAIN );