(bug 31420) Fix weird tablesorter bug where headers spanning multiple rows would...
[lhc/web/wiklou.git] / includes / db / CloneDatabase.php
index c0fcebc..f29e0b2 100644 (file)
@@ -62,9 +62,10 @@ class CloneDatabase {
         * @param $tablesToClone Array An array of tables to clone, unprefixed
         * @param $newTablePrefix String Prefix to assign to the tables
         * @param $oldTablePrefix String Prefix on current tables, if not $wgDBprefix
+        * @param $dropCurrentTables bool
         */
-       public function __construct( DatabaseBase $db, Array $tablesToClone, 
-               $newTablePrefix = 'parsertest', $oldTablePrefix = '', $dropCurrentTables = true )
+       public function __construct( DatabaseBase $db, array $tablesToClone,
+               $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true )
        {
                $this->db = $db;
                $this->tablesToClone = $tablesToClone;
@@ -78,54 +79,80 @@ class CloneDatabase {
         * @param $u Bool Use temporary tables when cloning the structure
         */
        public function useTemporaryTables( $u = true ) {
-               $this->useTemporaryTables = false;
+               $this->useTemporaryTables = $u;
        }
 
        /**
         * Clone the table structure
         */
        public function cloneTableStructure() {
+               
                foreach( $this->tablesToClone as $tbl ) {
                        # Clean up from previous aborted run.  So that table escaping
                        # works correctly across DB engines, we need to change the pre-
                        # fix back and forth so tableName() works right.
-                       $this->changePrefix( $this->oldTablePrefix );
-                       $oldTableName = $this->db->tableName( $tbl );
-                       $this->changePrefix( $this->newTablePrefix );
-                       $newTableName = $this->db->tableName( $tbl );
-
-                       if( $this->dropCurrentTables ) {
-                               if ( $this->db->getType() == 'mysql' && $this->db->tableExists( $tbl ) ) {
-                                       $this->db->query( "DROP TABLE IF EXISTS $newTableName" );
-                               } elseif ( in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) {
-                                       /* DROPs wouldn't work due to Foreign Key Constraints (bug 14990, r58669)
-                                        * Use "DROP TABLE IF EXISTS $newTableName CASCADE" for postgres? That
-                                        * syntax would also work for mysql.
-                                        */
-                               } elseif ( $this->db->tableExists( $tbl ) ) {
-                                       $this->db->query( "DROP TABLE $newTableName" );
-                               }
+                       
+                       self::changePrefix( $this->oldTablePrefix );
+                       $oldTableName = $this->db->tableName( $tbl, 'raw' );
+                       
+                       self::changePrefix( $this->newTablePrefix );
+                       $newTableName = $this->db->tableName( $tbl, 'raw' );
+                       
+                       if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres' ) ) ) {
+                               $this->db->dropTable( $tbl, __METHOD__ );
+                               wfDebug( __METHOD__." dropping {$newTableName}\n", true);
+                               //Dropping the oldTable because the prefix was changed
                        }
 
                        # Create new table
+                       wfDebug( __METHOD__." duplicating $oldTableName to $newTableName\n", true );
                        $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
+                       
+               }
+               
+       }
+
+       /**
+        * Change the prefix back to the original.
+        * @param $dropTables bool Optionally drop the tables we created
+        */
+       public function destroy( $dropTables = false ) {
+               if( $dropTables ) {
+                       self::changePrefix( $this->newTablePrefix );
+                       foreach( $this->tablesToClone as $tbl ) {
+                               $this->db->dropTable( $tbl );
+                       }
                }
+               self::changePrefix( $this->oldTablePrefix );
        }
 
        /**
         * Change the table prefix on all open DB connections/
+        *
+        * @param  $prefix
+        * @return void
         */
-       protected function changePrefix( $prefix ) {
+       public static function changePrefix( $prefix ) {
                global $wgDBprefix;
-               wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
+               wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
                $wgDBprefix = $prefix;
        }
 
-       public function changeLBPrefix( $lb, $prefix ) {
-               $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
+       /**
+        * @param  $lb LoadBalancer
+        * @param  $prefix
+        * @return void
+        */
+       public static function changeLBPrefix( $lb, $prefix ) {
+               $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
        }
 
-       public function changeDBPrefix( $db, $prefix ) {
+       /**
+        * @param  $db DatabaseBase
+        * @param  $prefix
+        * @return void
+        */
+       public static function changeDBPrefix( $db, $prefix ) {
                $db->tablePrefix( $prefix );
        }
 }