MediaWiki
REL1_22
|
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 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 00117 public function destroy( $dropTables = false ) { 00118 if ( $dropTables ) { 00119 self::changePrefix( $this->newTablePrefix ); 00120 foreach ( $this->tablesToClone as $tbl ) { 00121 $this->db->dropTable( $tbl ); 00122 } 00123 } 00124 self::changePrefix( $this->oldTablePrefix ); 00125 } 00126 00133 public static function changePrefix( $prefix ) { 00134 global $wgDBprefix; 00135 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) ); 00136 $wgDBprefix = $prefix; 00137 } 00138 00144 public static function changeLBPrefix( $lb, $prefix ) { 00145 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) ); 00146 } 00147 00153 public static function changeDBPrefix( $db, $prefix ) { 00154 $db->tablePrefix( $prefix ); 00155 } 00156 }