MediaWiki  REL1_23
CloneDatabase.php
Go to the documentation of this file.
00001 <?php
00027 class CloneDatabase {
00029     private $newTablePrefix = '';
00030 
00032     private $oldTablePrefix = '';
00033 
00035     private $tablesToClone = array();
00036 
00038     private $dropCurrentTables = true;
00039 
00041     private $useTemporaryTables = true;
00042 
00052     public function __construct( DatabaseBase $db, array $tablesToClone,
00053         $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true
00054     ) {
00055         $this->db = $db;
00056         $this->tablesToClone = $tablesToClone;
00057         $this->newTablePrefix = $newTablePrefix;
00058         $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
00059         $this->dropCurrentTables = $dropCurrentTables;
00060     }
00061 
00066     public function useTemporaryTables( $u = true ) {
00067         $this->useTemporaryTables = $u;
00068     }
00069 
00073     public function cloneTableStructure() {
00074         foreach ( $this->tablesToClone as $tbl ) {
00075             # Clean up from previous aborted run.  So that table escaping
00076             # works correctly across DB engines, we need to change the pre-
00077             # fix back and forth so tableName() works right.
00078 
00079             self::changePrefix( $this->oldTablePrefix );
00080             $oldTableName = $this->db->tableName( $tbl, 'raw' );
00081 
00082             self::changePrefix( $this->newTablePrefix );
00083             $newTableName = $this->db->tableName( $tbl, 'raw' );
00084 
00085             if ( $this->dropCurrentTables
00086                 && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) )
00087             ) {
00088                 $this->db->dropTable( $tbl, __METHOD__ );
00089                 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
00090                 //Dropping the oldTable because the prefix was changed
00091             }
00092 
00093             # Create new table
00094             wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
00095             $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
00096         }
00097     }
00098 
00103     public function destroy( $dropTables = false ) {
00104         if ( $dropTables ) {
00105             self::changePrefix( $this->newTablePrefix );
00106             foreach ( $this->tablesToClone as $tbl ) {
00107                 $this->db->dropTable( $tbl );
00108             }
00109         }
00110         self::changePrefix( $this->oldTablePrefix );
00111     }
00112 
00119     public static function changePrefix( $prefix ) {
00120         global $wgDBprefix;
00121         wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
00122         $wgDBprefix = $prefix;
00123     }
00124 
00130     public static function changeLBPrefix( $lb, $prefix ) {
00131         $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
00132     }
00133 
00139     public static function changeDBPrefix( $db, $prefix ) {
00140         $db->tablePrefix( $prefix );
00141     }
00142 }