Merge "(bug 32297) Use symbolic names, not offsets for a default timezone."
[lhc/web/wiklou.git] / maintenance / cleanupRemovedModules.php
1 <?php
2 /**
3 * Maintenance script to remove cache entries for removed ResourceLoader modules
4 * from the database
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 * @author Roan Kattouw
24 */
25
26 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
27
28 class CleanupRemovedModules extends Maintenance {
29
30 public function __construct() {
31 parent::__construct();
32 $this->mDescription = 'Remove cache entries for removed ResourceLoader modules from the database';
33 $this->addOption( 'batchsize', 'Delete rows in batches of this size. Default: 500', false, true );
34 $this->addOption( 'max-slave-lag', 'If the slave lag exceeds this many seconds, wait until it drops below this value. Default: 5', false, true );
35 }
36
37 public function execute() {
38 $dbw = wfGetDB( DB_MASTER );
39 $rl = new ResourceLoader();
40 $moduleNames = $rl->getModuleNames();
41 $moduleList = implode( ', ', array_map( array( $dbw, 'addQuotes' ), $moduleNames ) );
42 $limit = max( 1, intval( $this->getOption( 'batchsize', 500 ) ) );
43 $maxlag = intval( $this->getOption( 'max-slave-lag', 5 ) );
44
45 $this->output( "Cleaning up module_deps table...\n" );
46 $i = 1;
47 $modDeps = $dbw->tableName( 'module_deps' );
48 do {
49 // $dbw->delete() doesn't support LIMIT :(
50 $where = $moduleList ? "md_module NOT IN ($moduleList)" : '1=1';
51 $dbw->query( "DELETE FROM $modDeps WHERE $where LIMIT $limit", __METHOD__ );
52 $numRows = $dbw->affectedRows();
53 $this->output( "Batch $i: $numRows rows\n" );
54 $i++;
55 wfWaitForSlaves( $maxlag );
56 } while( $numRows > 0 );
57 $this->output( "done\n" );
58
59 $this->output( "Cleaning up msg_resource table...\n" );
60 $i = 1;
61
62 $mrRes = $dbw->tableName( 'msg_resource' );
63 do {
64 $where = $moduleList ? "mr_resource NOT IN ($moduleList)" : '1=1';
65 $dbw->query( "DELETE FROM $mrRes WHERE $where LIMIT $limit", __METHOD__ );
66 $numRows = $dbw->affectedRows();
67 $this->output( "Batch $i: $numRows rows\n" );
68 $i++;
69 wfWaitForSlaves( $maxlag );
70 } while( $numRows > 0 );
71 $this->output( "done\n" );
72
73 $this->output( "Cleaning up msg_resource_links table...\n" );
74 $i = 1;
75 $msgResLinks = $dbw->tableName( 'msg_resource_links' );
76 do {
77 $where = $moduleList ? "mrl_resource NOT IN ($moduleList)" : '1=1';
78 $dbw->query( "DELETE FROM $msgResLinks WHERE $where LIMIT $limit", __METHOD__ );
79 $numRows = $dbw->affectedRows();
80 $this->output( "Batch $i: $numRows rows\n" );
81 $i++;
82 wfWaitForSlaves( $maxlag );
83 } while( $numRows > 0 );
84 $this->output( "done\n" );
85 }
86 }
87
88 $maintClass = "CleanupRemovedModules";
89 require_once( RUN_MAINTENANCE_IF_MAIN );