Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / db / CloneDatabase.php
blob819925cb24afc1668e81cf9c902aa1f33d596291
1 <?php
2 /**
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
23 * @file
24 * @ingroup Database
27 class CloneDatabase {
29 /**
30 * Table prefix for cloning
31 * @var String
33 private $newTablePrefix = '';
35 /**
36 * Current table prefix
37 * @var String
39 private $oldTablePrefix = '';
41 /**
42 * List of tables to be cloned
43 * @var Array
45 private $tablesToClone = array();
47 /**
48 * Should we DROP tables containing the new names?
49 * @var Bool
51 private $dropCurrentTables = true;
53 /**
54 * Whether to use temporary tables or not
55 * @var Bool
57 private $useTemporaryTables = true;
59 /**
60 * Constructor
62 * @param $db DatabaseBase A database subclass
63 * @param array $tablesToClone An array of tables to clone, unprefixed
64 * @param string $newTablePrefix Prefix to assign to the tables
65 * @param string $oldTablePrefix Prefix on current tables, if not $wgDBprefix
66 * @param $dropCurrentTables bool
68 public function __construct( DatabaseBase $db, array $tablesToClone,
69 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true )
71 $this->db = $db;
72 $this->tablesToClone = $tablesToClone;
73 $this->newTablePrefix = $newTablePrefix;
74 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
75 $this->dropCurrentTables = $dropCurrentTables;
78 /**
79 * Set whether to use temporary tables or not
80 * @param bool $u Use temporary tables when cloning the structure
82 public function useTemporaryTables( $u = true ) {
83 $this->useTemporaryTables = $u;
86 /**
87 * Clone the table structure
89 public function cloneTableStructure() {
90 foreach ( $this->tablesToClone as $tbl ) {
91 # Clean up from previous aborted run. So that table escaping
92 # works correctly across DB engines, we need to change the pre-
93 # fix back and forth so tableName() works right.
95 self::changePrefix( $this->oldTablePrefix );
96 $oldTableName = $this->db->tableName( $tbl, 'raw' );
98 self::changePrefix( $this->newTablePrefix );
99 $newTableName = $this->db->tableName( $tbl, 'raw' );
101 if ( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) {
102 $this->db->dropTable( $tbl, __METHOD__ );
103 wfDebug( __METHOD__ . " dropping {$newTableName}\n", true );
104 //Dropping the oldTable because the prefix was changed
107 # Create new table
108 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n", true );
109 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
114 * Change the prefix back to the original.
115 * @param bool $dropTables Optionally drop the tables we created
117 public function destroy( $dropTables = false ) {
118 if ( $dropTables ) {
119 self::changePrefix( $this->newTablePrefix );
120 foreach ( $this->tablesToClone as $tbl ) {
121 $this->db->dropTable( $tbl );
124 self::changePrefix( $this->oldTablePrefix );
128 * Change the table prefix on all open DB connections/
130 * @param $prefix
131 * @return void
133 public static function changePrefix( $prefix ) {
134 global $wgDBprefix;
135 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
136 $wgDBprefix = $prefix;
140 * @param $lb LoadBalancer
141 * @param $prefix
142 * @return void
144 public static function changeLBPrefix( $lb, $prefix ) {
145 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
149 * @param $db DatabaseBase
150 * @param $prefix
151 * @return void
153 public static function changeDBPrefix( $db, $prefix ) {
154 $db->tablePrefix( $prefix );