4d22452cb4dc0e5e4b97a8dc3d281c1678c190be
[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 */
66 public function __construct( DatabaseBase $db, array $tablesToClone,
67 $newTablePrefix = 'parsertest', $oldTablePrefix = '', $dropCurrentTables = true )
68 {
69 $this->db = $db;
70 $this->tablesToClone = $tablesToClone;
71 $this->newTablePrefix = $newTablePrefix;
72 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
73 $this->dropCurrentTables = $dropCurrentTables;
74 }
75
76 /**
77 * Set whether to use temporary tables or not
78 * @param $u Bool Use temporary tables when cloning the structure
79 */
80 public function useTemporaryTables( $u = true ) {
81 $this->useTemporaryTables = $u;
82 }
83
84 /**
85 * Clone the table structure
86 */
87 public function cloneTableStructure() {
88
89 foreach( $this->tablesToClone as $tbl ) {
90 # Clean up from previous aborted run. So that table escaping
91 # works correctly across DB engines, we need to change the pre-
92 # fix back and forth so tableName() works right.
93
94 $this->changePrefix( $this->oldTablePrefix );
95 $oldTableName = $this->db->tableName( $tbl );
96
97 $this->changePrefix( $this->newTablePrefix );
98 $newTableName = $this->db->tableName( $tbl );
99
100 if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres') ) ) {
101 $this->db->dropTable( $oldTableName, __METHOD__ );
102 //Dropping the oldTable because the prefix was changed
103 }
104
105 # Create new table
106 wfDebug( "Duplicating $oldTableName to $newTableName\n", __METHOD__ );
107 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
108
109 }
110
111 }
112
113 /**
114 * Change the prefix back to the original.
115 * @param $dropTables bool Optionally drop the tables we created
116 */
117 public function destroy( $dropTables = false ) {
118 if( $dropTables ) {
119 $this->changePrefix( $this->newTablePrefix );
120 foreach( $this->tablesToClone as $tbl ) {
121 $this->db->dropTable( $tbl );
122 }
123 }
124 $this->changePrefix( $this->oldTablePrefix );
125 }
126
127 /**
128 * Change the table prefix on all open DB connections/
129 *
130 * @param $prefix
131 * @return void
132 */
133 protected function changePrefix( $prefix ) {
134 global $wgDBprefix;
135 wfGetLBFactory()->forEachLB( array( $this, 'changeLBPrefix' ), array( $prefix ) );
136 $wgDBprefix = $prefix;
137 }
138
139 /**
140 * @param $lb LoadBalancer
141 * @param $prefix
142 * @return void
143 */
144 public function changeLBPrefix( $lb, $prefix ) {
145 $lb->forEachOpenConnection( array( $this, 'changeDBPrefix' ), array( $prefix ) );
146 }
147
148 /**
149 * @param $db DatabaseBase
150 * @param $prefix
151 * @return void
152 */
153 public function changeDBPrefix( $db, $prefix ) {
154 $db->tablePrefix( $prefix );
155 }
156 }