MediaWiki
REL1_19
|
00001 <?php 00024 require_once( dirname( __FILE__ ) . '/Maintenance.php' ); 00025 00026 class RenameDbPrefix extends Maintenance { 00027 public function __construct() { 00028 parent::__construct(); 00029 $this->addOption( "old", "Old db prefix [0 for none]", true, true ); 00030 $this->addOption( "new", "New db prefix [0 for none]", true, true ); 00031 } 00032 00033 public function getDbType() { 00034 return Maintenance::DB_ADMIN; 00035 } 00036 00037 public function execute() { 00038 global $wgDBname; 00039 00040 // Allow for no old prefix 00041 if ( $this->getOption( 'old', 0 ) === '0' ) { 00042 $old = ''; 00043 } else { 00044 // Use nice safe, sane, prefixes 00045 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m ); 00046 $old = isset( $m[0] ) ? $m[0] : false; 00047 } 00048 // Allow for no new prefix 00049 if ( $this->getOption( 'new', 0 ) === '0' ) { 00050 $new = ''; 00051 } else { 00052 // Use nice safe, sane, prefixes 00053 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m ); 00054 $new = isset( $m[0] ) ? $m[0] : false; 00055 } 00056 00057 if ( $old === false || $new === false ) { 00058 $this->error( "Invalid prefix!", true ); 00059 } 00060 if ( $old === $new ) { 00061 $this->output( "Same prefix. Nothing to rename!\n", true ); 00062 } 00063 00064 $this->output( "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n" ); 00065 $count = 0; 00066 00067 $dbw = wfGetDB( DB_MASTER ); 00068 $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ) ); 00069 foreach ( $res as $row ) { 00070 // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X" 00071 // sort of message. Best not to try $row->x stuff... 00072 $fields = get_object_vars( $row ); 00073 // Silly for loop over one field... 00074 foreach ( $fields as $table ) { 00075 // $old should be regexp safe ([a-zA-Z_]) 00076 $newTable = preg_replace( '/^' . $old . '/', $new, $table ); 00077 $this->output( "Renaming table $table to $newTable\n" ); 00078 $dbw->query( "RENAME TABLE $table TO $newTable" ); 00079 } 00080 $count++; 00081 } 00082 $this->output( "Done! [$count tables]\n" ); 00083 } 00084 } 00085 00086 $maintClass = "RenameDbPrefix"; 00087 require_once( RUN_MAINTENANCE_IF_MAIN );