renameDbPrefix.php tweaks: corrected script name in help message (this is not updateS...
[lhc/web/wiklou.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 * @file
7 * @ingroup Maintenance
8 */
9 $optionsWithArgs = array( 'old', 'new', 'help' );
10
11 require_once( 'commandLine.inc' );
12
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";
20 wfDie();
21 }
22
23 // Allow for no old prefix
24 if( $options['old'] === '0' ) {
25 $old = '';
26 } else {
27 // Use nice safe, sane, prefixes
28 preg_match( '/^[a-zA-Z]+_$/', $options['old'], $m );
29 $old = isset( $m[0] ) ? $m[0] : false;
30 }
31 // Allow for no new prefix
32 if( $options['new'] === '0' ) {
33 $new = '';
34 } else {
35 // Use nice safe, sane, prefixes
36 preg_match( '/^[a-zA-Z]+_$/', $options['new'], $m );
37 $new = isset( $m[0] ) ? $m[0] : false;
38 }
39
40 if( $old === false || $new === false ) {
41 print "Invalid prefix!\n";
42 wfDie();
43 }
44 if( $old === $new ) {
45 print "Same prefix. Nothing to rename!\n";
46 wfDie();
47 }
48
49 print "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n";
50 $count = 0;
51
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" );
64 }
65 $count++;
66 }
67 print "Done! [$count tables]\n";