MediaWiki  REL1_24
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(
00040             'max-slave-lag',
00041             'If the slave lag exceeds this many seconds, wait until it drops below this value. '
00042                 . 'Default: 5',
00043             false,
00044             true
00045         );
00046     }
00047 
00048     public function execute() {
00049         $dbw = wfGetDB( DB_MASTER );
00050         $rl = new ResourceLoader( ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
00051         $moduleNames = $rl->getModuleNames();
00052         $moduleList = implode( ', ', array_map( array( $dbw, 'addQuotes' ), $moduleNames ) );
00053         $limit = max( 1, intval( $this->getOption( 'batchsize', 500 ) ) );
00054         $maxlag = intval( $this->getOption( 'max-slave-lag', 5 ) );
00055 
00056         $this->output( "Cleaning up module_deps table...\n" );
00057         $i = 1;
00058         $modDeps = $dbw->tableName( 'module_deps' );
00059         do {
00060             // $dbw->delete() doesn't support LIMIT :(
00061             $where = $moduleList ? "md_module NOT IN ($moduleList)" : '1=1';
00062             $dbw->query( "DELETE FROM $modDeps WHERE $where LIMIT $limit", __METHOD__ );
00063             $numRows = $dbw->affectedRows();
00064             $this->output( "Batch $i: $numRows rows\n" );
00065             $i++;
00066             wfWaitForSlaves( $maxlag );
00067         } while ( $numRows > 0 );
00068         $this->output( "done\n" );
00069 
00070         $this->output( "Cleaning up msg_resource table...\n" );
00071         $i = 1;
00072 
00073         $mrRes = $dbw->tableName( 'msg_resource' );
00074         do {
00075             $where = $moduleList ? "mr_resource NOT IN ($moduleList)" : '1=1';
00076             $dbw->query( "DELETE FROM $mrRes WHERE $where LIMIT $limit", __METHOD__ );
00077             $numRows = $dbw->affectedRows();
00078             $this->output( "Batch $i: $numRows rows\n" );
00079             $i++;
00080             wfWaitForSlaves( $maxlag );
00081         } while ( $numRows > 0 );
00082         $this->output( "done\n" );
00083 
00084         $this->output( "Cleaning up msg_resource_links table...\n" );
00085         $i = 1;
00086         $msgResLinks = $dbw->tableName( 'msg_resource_links' );
00087         do {
00088             $where = $moduleList ? "mrl_resource NOT IN ($moduleList)" : '1=1';
00089             $dbw->query( "DELETE FROM $msgResLinks WHERE $where LIMIT $limit", __METHOD__ );
00090             $numRows = $dbw->affectedRows();
00091             $this->output( "Batch $i: $numRows rows\n" );
00092             $i++;
00093             wfWaitForSlaves( $maxlag );
00094         } while ( $numRows > 0 );
00095         $this->output( "done\n" );
00096     }
00097 }
00098 
00099 $maintClass = "CleanupRemovedModules";
00100 require_once RUN_MAINTENANCE_IF_MAIN;