Reverted r113177 per CR
[lhc/web/wiklou.git] / includes / db / CloneDatabase.php
1 <?php
2 /**
3 * Helper class for making a copy of the database, mostly for unit testing.
4 *
5 * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
6 * http://www.mediawiki.org/
7 *
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.
12 *
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.
17 *
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
22 *
23 * @ingroup Database
24 */
25
26 class CloneDatabase {
27
28 /**
29 * Table prefix for cloning
30 * @var String
31 */
32 private $newTablePrefix = '';
33
34 /**
35 * Current table prefix
36 * @var String
37 */
38 private $oldTablePrefix = '';
39
40 /**
41 * List of tables to be cloned
42 * @var Array
43 */
44 private $tablesToClone = array();
45
46 /**
47 * Should we DROP tables containing the new names?
48 * @var Bool
49 */
50 private $dropCurrentTables = true;
51
52 /**
53 * Whether to use temporary tables or not
54 * @var Bool
55 */
56 private $useTemporaryTables = true;
57
58 /**
59 * Constructor
60 *
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
65 * @param $dropCurrentTables bool
66 */
67 public function __construct( DatabaseBase $db, array $tablesToClone,
68 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true )
69 {
70 $this->db = $db;
71 $this->tablesToClone = $tablesToClone;
72 $this->newTablePrefix = $newTablePrefix;
73 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
74 $this->dropCurrentTables = $dropCurrentTables;
75 }
76
77 /**
78 * Set whether to use temporary tables or not
79 * @param $u Bool Use temporary tables when cloning the structure
80 */
81 public function useTemporaryTables( $u = true ) {
82 $this->useTemporaryTables = $u;
83 }
84
85 /**
86 * Clone the table structure
87 */
88 public function cloneTableStructure() {
89
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.
94
95 self::changePrefix( $this->oldTablePrefix );
96 $oldTableName = $this->db->tableName( $tbl, 'raw' );
97
98 self::changePrefix( $this->newTablePrefix );
99 $newTableName = $this->db->tableName( $tbl, 'raw' );
100
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
105 }
106
107 # Create new table
108 wfDebug( __METHOD__." duplicating $oldTableName to $newTableName\n", true );
109 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
110
111 }
112
113 }
114
115 /**
116 * Change the prefix back to the original.
117 * @param $dropTables bool Optionally drop the tables we created
118 */
119 public function destroy( $dropTables = false ) {
120 if( $dropTables ) {
121 self::changePrefix( $this->newTablePrefix );
122 foreach( $this->tablesToClone as $tbl ) {
123 $this->db->dropTable( $tbl );
124 }
125 }
126 self::changePrefix( $this->oldTablePrefix );
127 }
128
129 /**
130 * Change the table prefix on all open DB connections/
131 *
132 * @param $prefix
133 * @return void
134 */
135 public static function changePrefix( $prefix ) {
136 global $wgDBprefix;
137 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
138 $wgDBprefix = $prefix;
139 }
140
141 /**
142 * @param $lb LoadBalancer
143 * @param $prefix
144 * @return void
145 */
146 public static function changeLBPrefix( $lb, $prefix ) {
147 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
148 }
149
150 /**
151 * @param $db DatabaseBase
152 * @param $prefix
153 * @return void
154 */
155 public static function changeDBPrefix( $db, $prefix ) {
156 $db->tablePrefix( $prefix );
157 }
158 }