MediaWiki
REL1_19
|
00001 <?php 00026 class CloneDatabase { 00027 00032 private $newTablePrefix = ''; 00033 00038 private $oldTablePrefix = ''; 00039 00044 private $tablesToClone = array(); 00045 00050 private $dropCurrentTables = true; 00051 00056 private $useTemporaryTables = true; 00057 00067 public function __construct( DatabaseBase $db, array $tablesToClone, 00068 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true ) 00069 { 00070 $this->db = $db; 00071 $this->tablesToClone = $tablesToClone; 00072 $this->newTablePrefix = $newTablePrefix; 00073 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix(); 00074 $this->dropCurrentTables = $dropCurrentTables; 00075 } 00076 00081 public function useTemporaryTables( $u = true ) { 00082 $this->useTemporaryTables = $u; 00083 } 00084 00088 public function cloneTableStructure() { 00089 00090 foreach( $this->tablesToClone as $tbl ) { 00091 # Clean up from previous aborted run. So that table escaping 00092 # works correctly across DB engines, we need to change the pre- 00093 # fix back and forth so tableName() works right. 00094 00095 self::changePrefix( $this->oldTablePrefix ); 00096 $oldTableName = $this->db->tableName( $tbl, 'raw' ); 00097 00098 self::changePrefix( $this->newTablePrefix ); 00099 $newTableName = $this->db->tableName( $tbl, 'raw' ); 00100 00101 if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) { 00102 $this->db->dropTable( $tbl, __METHOD__ ); 00103 wfDebug( __METHOD__." dropping {$newTableName}\n", true); 00104 //Dropping the oldTable because the prefix was changed 00105 } 00106 00107 # Create new table 00108 wfDebug( __METHOD__." duplicating $oldTableName to $newTableName\n", true ); 00109 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables ); 00110 00111 } 00112 00113 } 00114 00119 public function destroy( $dropTables = false ) { 00120 if( $dropTables ) { 00121 self::changePrefix( $this->newTablePrefix ); 00122 foreach( $this->tablesToClone as $tbl ) { 00123 $this->db->dropTable( $tbl ); 00124 } 00125 } 00126 self::changePrefix( $this->oldTablePrefix ); 00127 } 00128 00135 public static function changePrefix( $prefix ) { 00136 global $wgDBprefix; 00137 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) ); 00138 $wgDBprefix = $prefix; 00139 } 00140 00146 public static function changeLBPrefix( $lb, $prefix ) { 00147 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) ); 00148 } 00149 00155 public static function changeDBPrefix( $db, $prefix ) { 00156 $db->tablePrefix( $prefix ); 00157 } 00158 }