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
00039 if( $this->getOption( 'old', 0 ) === '0' ) {
00040 $old = '';
00041 } else {
00042
00043 preg_match( '/^[a-zA-Z]+_$/', $this->getOption('old'), $m );
00044 $old = isset( $m[0] ) ? $m[0] : false;
00045 }
00046
00047 if( $this->getOption( 'new', 0 ) === '0' ) {
00048 $new = '';
00049 } else {
00050
00051 preg_match( '/^[a-zA-Z]+_$/', $this->getOption('new'), $m );
00052 $new = isset( $m[0] ) ? $m[0] : false;
00053 }
00054
00055 if( $old === false || $new === false ) {
00056 $this->error( "Invalid prefix!", true );
00057 }
00058 if( $old === $new ) {
00059 $this->output( "Same prefix. Nothing to rename!\n", true );
00060 }
00061
00062 $this->output( "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n" );
00063 $count = 0;
00064
00065 $dbw = wfGetDB( DB_MASTER );
00066 $res = $dbw->query( "SHOW TABLES LIKE '".$dbw->escapeLike( $old )."%'" );
00067 foreach( $res as $row ) {
00068
00069
00070 $fields = get_object_vars( $row );
00071
00072 foreach( $fields as $resName => $table ) {
00073
00074 $newTable = preg_replace( '/^'.$old.'/', $new, $table );
00075 $this->output( "Renaming table $table to $newTable\n" );
00076 $dbw->query( "RENAME TABLE $table TO $newTable" );
00077 }
00078 $count++;
00079 }
00080 $this->output( "Done! [$count tables]\n" );
00081 }
00082 }
00083
00084 $maintClass = "RenameDbPrefix";
00085 require_once( DO_MAINTENANCE );