3 * Helper class for making a copy of the database, mostly for unit testing.
5 * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
6 * http://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
29 * Table prefix for cloning
32 private $newTablePrefix = '';
35 * Current table prefix
38 private $oldTablePrefix = '';
41 * List of tables to be cloned
44 private $tablesToClone = array();
47 * Should we DROP tables containing the new names?
50 private $dropCurrentTables = true;
53 * Whether to use temporary tables or not
56 private $useTemporaryTables = true;
61 * @param $db DatabaseBase A database subclass
62 * @param $tablesToClone Array An array of tables to clone, unprefixed
63 * @param $newTablePrefix String Prefix to assign to the tables
64 * @param $oldTablePrefix String Prefix on current tables, if not $wgDBprefix
66 public function __construct( DatabaseBase
$db, Array $tablesToClone,
67 $newTablePrefix = 'parsertest', $oldTablePrefix = '', $dropCurrentTables = true )
70 $this->tablesToClone
= $tablesToClone;
71 $this->newTablePrefix
= $newTablePrefix;
72 $this->oldTablePrefix
= $oldTablePrefix ?
$oldTablePrefix : $this->db
->tablePrefix();
73 $this->dropCurrentTables
= $dropCurrentTables;
77 * Set whether to use temporary tables or not
78 * @param $u Bool Use temporary tables when cloning the structure
80 public function useTemporaryTables( $u = true ) {
81 $this->useTemporaryTables
= false;
85 * Clone the table structure
87 public function cloneTableStructure() {
88 foreach( $this->tablesToClone
as $tbl ) {
89 # Clean up from previous aborted run. So that table escaping
90 # works correctly across DB engines, we need to change the pre-
91 # fix back and forth so tableName() works right.
92 $this->changePrefix( $this->oldTablePrefix
);
93 $oldTableName = $this->db
->tableName( $tbl );
94 $this->changePrefix( $this->newTablePrefix
);
95 $newTableName = $this->db
->tableName( $tbl );
97 if( $this->dropCurrentTables
) {
98 if ( $this->db
->getType() == 'mysql' && $this->db
->tableExists( $tbl ) ) {
99 $this->db
->query( "DROP TABLE IF EXISTS $newTableName" );
100 } elseif ( in_array( $this->db
->getType(), array( 'postgres', 'oracle' ) ) ) {
101 /* DROPs wouldn't work due to Foreign Key Constraints (bug 14990, r58669)
102 * Use "DROP TABLE IF EXISTS $newTableName CASCADE" for postgres? That
103 * syntax would also work for mysql.
105 } elseif ( $this->db
->tableExists( $tbl ) ) {
106 $this->db
->query( "DROP TABLE $newTableName" );
111 $this->db
->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables
);
116 * Change the table prefix on all open DB connections/
118 protected function changePrefix( $prefix ) {
120 wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
121 $wgDBprefix = $prefix;
124 public function changeLBPrefix( $lb, $prefix ) {
125 $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
128 public function changeDBPrefix( $db, $prefix ) {
129 $db->tablePrefix( $prefix );