3 * Run this script to after changing $wgDBprefix on a wiki.
4 * The wiki will have to get downtime to do this correctly.
9 $optionsWithArgs = array( 'old', 'new', 'help' );
11 require_once( 'commandLine.inc' );
13 if( @$options['help'] ||
!isset( $options['old'] ) ||
!isset( $options['new'] ) ) {
14 print "usage: renameDbPrefix.php [--help] [--old x] [new y]\n";
15 print " --help : this help message\n";
16 print " --old x : old db prefix x\n";
17 print " --old 0 : EMPTY old db prefix x\n";
18 print " --new y : new db prefix y\n";
19 print " --new 0 : EMPTY new db prefix\n";
23 // Allow for no old prefix
24 if( $options['old'] === '0' ) {
27 // Use nice safe, sane, prefixes
28 preg_match( '/^[a-zA-Z]+_$/', $options['old'], $m );
29 $old = isset( $m[0] ) ?
$m[0] : false;
31 // Allow for no new prefix
32 if( $options['new'] === '0' ) {
35 // Use nice safe, sane, prefixes
36 preg_match( '/^[a-zA-Z]+_$/', $options['new'], $m );
37 $new = isset( $m[0] ) ?
$m[0] : false;
40 if( $old === false ||
$new === false ) {
41 print "Invalid prefix!\n";
45 print "Same prefix. Nothing to rename!\n";
49 print "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n";
52 $dbw = wfGetDB( DB_MASTER
);
53 $res = $dbw->query( "SHOW TABLES LIKE '".$dbw->escapeLike( $old )."%'" );
54 foreach( $res as $row ) {
55 // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
56 // sort of message. Best not to try $row->x stuff...
57 $fields = get_object_vars( $row );
58 // Silly for loop over one field...
59 foreach( $fields as $resName => $table ) {
60 // $old should be regexp safe ([a-zA-Z_])
61 $newTable = preg_replace( '/^'.$old.'/', $new, $table );
62 print "Renaming table $table to $newTable\n";
63 $dbw->query( "RENAME TABLE $table TO $newTable" );
67 print "Done! [$count tables]\n";