3 * Helper class for making a copy of the database, mostly for unit testing.
5 * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
28 /** @var string Table prefix for cloning */
29 private $newTablePrefix = '';
31 /** @var string Current table prefix */
32 private $oldTablePrefix = '';
34 /** @var array List of tables to be cloned */
35 private $tablesToClone = array();
37 /** @var bool Should we DROP tables containing the new names? */
38 private $dropCurrentTables = true;
40 /** @var bool Whether to use temporary tables or not */
41 private $useTemporaryTables = true;
46 * @param DatabaseBase $db A database subclass
47 * @param array $tablesToClone An array of tables to clone, unprefixed
48 * @param string $newTablePrefix Prefix to assign to the tables
49 * @param string $oldTablePrefix Prefix on current tables, if not $wgDBprefix
50 * @param bool $dropCurrentTables
52 public function __construct( DatabaseBase
$db, array $tablesToClone,
53 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true
56 $this->tablesToClone
= $tablesToClone;
57 $this->newTablePrefix
= $newTablePrefix;
58 $this->oldTablePrefix
= $oldTablePrefix ?
$oldTablePrefix : $this->db
->tablePrefix();
59 $this->dropCurrentTables
= $dropCurrentTables;
63 * Set whether to use temporary tables or not
64 * @param bool $u Use temporary tables when cloning the structure
66 public function useTemporaryTables( $u = true ) {
67 $this->useTemporaryTables
= $u;
71 * Clone the table structure
73 public function cloneTableStructure() {
74 foreach ( $this->tablesToClone
as $tbl ) {
75 # Clean up from previous aborted run. So that table escaping
76 # works correctly across DB engines, we need to change the pre-
77 # fix back and forth so tableName() works right.
79 self
::changePrefix( $this->oldTablePrefix
);
80 $oldTableName = $this->db
->tableName( $tbl, 'raw' );
82 self
::changePrefix( $this->newTablePrefix
);
83 $newTableName = $this->db
->tableName( $tbl, 'raw' );
85 if ( $this->dropCurrentTables
86 && !in_array( $this->db
->getType(), array( 'postgres', 'oracle' ) )
88 $this->db
->dropTable( $tbl, __METHOD__
);
89 wfDebug( __METHOD__
. " dropping {$newTableName}\n" );
90 //Dropping the oldTable because the prefix was changed
94 wfDebug( __METHOD__
. " duplicating $oldTableName to $newTableName\n" );
95 $this->db
->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables
);
100 * Change the prefix back to the original.
101 * @param bool $dropTables Optionally drop the tables we created
103 public function destroy( $dropTables = false ) {
105 self
::changePrefix( $this->newTablePrefix
);
106 foreach ( $this->tablesToClone
as $tbl ) {
107 $this->db
->dropTable( $tbl );
110 self
::changePrefix( $this->oldTablePrefix
);
114 * Change the table prefix on all open DB connections/
116 * @param string $prefix
119 public static function changePrefix( $prefix ) {
121 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
122 $wgDBprefix = $prefix;
126 * @param LoadBalancer $lb
127 * @param string $prefix
130 public static function changeLBPrefix( $lb, $prefix ) {
131 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
135 * @param DatabaseBase $db
136 * @param string $prefix
139 public static function changeDBPrefix( $db, $prefix ) {
140 $db->tablePrefix( $prefix );