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