Re-add maintenance script from r75555, not totally broken this time
[lhc/web/wiklou.git] / maintenance / cleanupRemovedModules.php
1 <?php
2 /**
3 * Maintenance script to create an account and grant it administrator rights
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 * @author Rob Church <robchur@gmail.com>
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class CleanupRemovedModules extends Maintenance {
28
29 public function __construct() {
30 parent::__construct();
31 $this->mDescription = 'Remove cache entries for removed ResourceLoader modules from the database';
32 $this->addOption( 'batchsize', 'Delete rows in batches of this size. Default: 500', false, true );
33 $this->addOption( 'max-slave-lag', 'If the slave lag exceeds this many seconds, wait until it drops below this value. Default: 5', false, true );
34 }
35
36 public function execute() {
37 $dbw = wfGetDB( DB_MASTER );
38 $rl = new ResourceLoader();
39 $moduleNames = array_keys( $rl->getModules() );
40 $moduleList = implode( ', ', array_map( array( $dbw, 'addQuotes' ), $moduleNames ) );
41 $limit = max( 1, intval( $this->getOption( 'batchsize', 500 ) ) );
42 $maxlag = intval( $this->getOption( 'max-slave-lag', 5 ) );
43
44 $this->output( "Cleaning up module_deps table...\n" );
45 $i = 1;
46 do {
47 // $dbw->delete() doesn't support LIMIT :(
48 $where = $moduleList ? "md_module NOT IN ($moduleList)" : '1=1';
49 $dbw->query( "DELETE FROM module_deps WHERE $where LIMIT $limit", __METHOD__ );
50 $numRows = $dbw->affectedRows();
51 $this->output( "Batch $i: $numRows rows\n" );
52 $i++;
53 wfWaitForSlaves( $maxlag );
54 } while( $numRows > 0 );
55 $this->output( "done\n" );
56
57 $this->output( "Cleaning up msg_resource table...\n" );
58 $i = 1;
59 do {
60 $where = $moduleList ? "mr_resource NOT IN ($moduleList)" : '1=1';
61 $dbw->query( "DELETE FROM msg_resource WHERE $where LIMIT $limit", __METHOD__ );
62 $numRows = $dbw->affectedRows();
63 $this->output( "Batch $i: $numRows rows\n" );
64 $i++;
65 wfWaitForSlaves( $maxlag );
66 } while( $numRows > 0 );
67 $this->output( "done\n" );
68
69 $this->output( "Cleaning up msg_resource_links table...\n" );
70 $i = 1;
71 do {
72 $where = $moduleList ? "mrl_resource NOT IN ($moduleList)" : '1=1';
73 $dbw->query( "DELETE FROM msg_resource_links WHERE $where LIMIT $limit", __METHOD__ );
74 $numRows = $dbw->affectedRows();
75 $this->output( "Batch $i: $numRows rows\n" );
76 $i++;
77 wfWaitForSlaves( $maxlag );
78 } while( $numRows > 0 );
79 $this->output( "done\n" );
80 }
81 }
82
83 $maintClass = "CleanupRemovedModules";
84 require_once( DO_MAINTENANCE );