]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/renameDbPrefix.php
MediaWiki 1.16.4
[autoinstalls/mediawiki.git] / maintenance / renameDbPrefix.php
1 <?php
2 /**
3  * Run this script to after changing $wgDBprefix on a wiki.
4  * The wiki will have to get downtime to do this correctly.
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  * @ingroup Maintenance
22  */
23  
24 require_once( dirname(__FILE__) . '/Maintenance.php' );
25
26 class RenameDbPrefix extends Maintenance {
27         public function __construct() {
28                 parent::__construct();
29                 $this->addOption( "old", "Old db prefix [0 for none]", true, true );
30                 $this->addOption( "new", "New db prefix [0 for none]", true, true );
31         }
32
33         public function getDbType() {
34                 return Maintenance::DB_ADMIN;
35         }
36
37         public function execute() {
38                 // Allow for no old prefix
39                 if( $this->getOption( 'old', 0 ) === '0' ) {
40                         $old = '';
41                 } else {
42                         // Use nice safe, sane, prefixes
43                         preg_match( '/^[a-zA-Z]+_$/', $this->getOption('old'), $m );
44                         $old = isset( $m[0] ) ? $m[0] : false;
45                 }
46                 // Allow for no new prefix
47                 if( $this->getOption( 'new', 0 ) === '0' ) {
48                         $new = '';
49                 } else {
50                         // Use nice safe, sane, prefixes
51                         preg_match( '/^[a-zA-Z]+_$/', $this->getOption('new'), $m );
52                         $new = isset( $m[0] ) ? $m[0] : false;
53                 }
54         
55                 if( $old === false || $new === false ) {
56                         $this->error( "Invalid prefix!", true );
57                 }
58                 if( $old === $new ) {
59                         $this->output( "Same prefix. Nothing to rename!\n", true );
60                 }
61         
62                 $this->output( "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n" );
63                 $count = 0;
64         
65                 $dbw = wfGetDB( DB_MASTER );
66                 $res = $dbw->query( "SHOW TABLES LIKE '".$dbw->escapeLike( $old )."%'" );
67                 foreach( $res as $row ) {
68                         // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
69                         // sort of message. Best not to try $row->x stuff...
70                         $fields = get_object_vars( $row );
71                         // Silly for loop over one field...
72                         foreach( $fields as $resName => $table ) {
73                                 // $old should be regexp safe ([a-zA-Z_])
74                                 $newTable = preg_replace( '/^'.$old.'/', $new, $table );
75                                 $this->output( "Renaming table $table to $newTable\n" );
76                                 $dbw->query( "RENAME TABLE $table TO $newTable" );
77                         }
78                         $count++;
79                 }
80                 $this->output( "Done! [$count tables]\n" );
81         }
82 }
83
84 $maintClass = "RenameDbPrefix";
85 require_once( DO_MAINTENANCE );