Whitespace fixes for r75486
[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 = min( 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 $dbw->query( "DELETE FROM module_deps WHERE md_module NOT IN ($moduleList) LIMIT $limit", __METHOD__ );
49 $numRows = $dbw->affectedRows();
50 $this->output( "Batch $i: $numRows rows\n" );
51 $i++;
52 wfWaitForSlaves( $maxlag );
53 } while( $dbw->affectedRows() > 0 );
54 $this->output( "done\n" );
55
56 $this->output( "Cleaning up msg_resource table...\n" );
57 $i = 1;
58 do {
59 // $dbw->delete() doesn't support LIMIT :(
60 $dbw->query( "DELETE FROM msg_resource WHERE mr_resource NOT IN ($moduleList) LIMIT $limit", __METHOD__ );
61 $numRows = $dbw->affectedRows();
62 $this->output( "Batch $i: $numRows rows\n" );
63 $i++;
64 wfWaitForSlaves( $maxlag );
65 } while( $dbw->affectedRows() > 0 );
66 $this->output( "done\n" );
67
68 $this->output( "Cleaning up msg_resource_links table...\n" );
69 $i = 1;
70 do {
71 // $dbw->delete() doesn't support LIMIT :(
72 $dbw->query( "DELETE FROM msg_resource_links WHERE mrl_resource NOT IN ($moduleList) LIMIT $limit", __METHOD__ );
73 $numRows = $dbw->affectedRows();
74 $this->output( "Batch $i: $numRows rows\n" );
75 $i++;
76 wfWaitForSlaves( $maxlag );
77 } while( $dbw->affectedRows() > 0 );
78 $this->output( "done\n" );
79 }
80 }
81
82 $maintClass = "CleanupRemovedModules";
83 require_once( DO_MAINTENANCE );