]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/renameDbPrefix.php
MediaWiki 1.17.1-scripts
[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                 global $wgDBname;
39
40                 // Allow for no old prefix
41                 if ( $this->getOption( 'old', 0 ) === '0' ) {
42                         $old = '';
43                 } else {
44                         // Use nice safe, sane, prefixes
45                         preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
46                         $old = isset( $m[0] ) ? $m[0] : false;
47                 }
48                 // Allow for no new prefix
49                 if ( $this->getOption( 'new', 0 ) === '0' ) {
50                         $new = '';
51                 } else {
52                         // Use nice safe, sane, prefixes
53                         preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
54                         $new = isset( $m[0] ) ? $m[0] : false;
55                 }
56
57                 if ( $old === false || $new === false ) {
58                         $this->error( "Invalid prefix!", true );
59                 }
60                 if ( $old === $new ) {
61                         $this->output( "Same prefix. Nothing to rename!\n", true );
62                 }
63
64                 $this->output( "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n" );
65                 $count = 0;
66
67                 $dbw = wfGetDB( DB_MASTER );
68                 $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ) );
69                 foreach ( $res as $row ) {
70                         // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
71                         // sort of message. Best not to try $row->x stuff...
72                         $fields = get_object_vars( $row );
73                         // Silly for loop over one field...
74                         foreach ( $fields as $table ) {
75                                 // $old should be regexp safe ([a-zA-Z_])
76                                 $newTable = preg_replace( '/^' . $old . '/', $new, $table );
77                                 $this->output( "Renaming table $table to $newTable\n" );
78                                 $dbw->query( "RENAME TABLE $table TO $newTable" );
79                         }
80                         $count++;
81                 }
82                 $this->output( "Done! [$count tables]\n" );
83         }
84 }
85
86 $maintClass = "RenameDbPrefix";
87 require_once( RUN_MAINTENANCE_IF_MAIN );