Can't use return value of void functions
[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 * @file
24 * @ingroup Database
25 */
26
27 class CloneDatabase {
28
29 /**
30 * Table prefix for cloning
31 * @var String
32 */
33 private $newTablePrefix = '';
34
35 /**
36 * Current table prefix
37 * @var String
38 */
39 private $oldTablePrefix = '';
40
41 /**
42 * List of tables to be cloned
43 * @var Array
44 */
45 private $tablesToClone = array();
46
47 /**
48 * Should we DROP tables containing the new names?
49 * @var Bool
50 */
51 private $dropCurrentTables = true;
52
53 /**
54 * Whether to use temporary tables or not
55 * @var Bool
56 */
57 private $useTemporaryTables = true;
58
59 /**
60 * Constructor
61 *
62 * @param $db DatabaseBase A database subclass
63 * @param $tablesToClone Array An array of tables to clone, unprefixed
64 * @param $newTablePrefix String Prefix to assign to the tables
65 * @param $oldTablePrefix String Prefix on current tables, if not $wgDBprefix
66 * @param $dropCurrentTables bool
67 */
68 public function __construct( DatabaseBase $db, array $tablesToClone,
69 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true )
70 {
71 $this->db = $db;
72 $this->tablesToClone = $tablesToClone;
73 $this->newTablePrefix = $newTablePrefix;
74 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
75 $this->dropCurrentTables = $dropCurrentTables;
76 }
77
78 /**
79 * Set whether to use temporary tables or not
80 * @param $u Bool Use temporary tables when cloning the structure
81 */
82 public function useTemporaryTables( $u = true ) {
83 $this->useTemporaryTables = $u;
84 }
85
86 /**
87 * Clone the table structure
88 */
89 public function cloneTableStructure() {
90
91 foreach( $this->tablesToClone as $tbl ) {
92 # Clean up from previous aborted run. So that table escaping
93 # works correctly across DB engines, we need to change the pre-
94 # fix back and forth so tableName() works right.
95
96 self::changePrefix( $this->oldTablePrefix );
97 $oldTableName = $this->db->tableName( $tbl, 'raw' );
98
99 self::changePrefix( $this->newTablePrefix );
100 $newTableName = $this->db->tableName( $tbl, 'raw' );
101
102 if( $this->dropCurrentTables && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) ) ) {
103 $this->db->dropTable( $tbl, __METHOD__ );
104 wfDebug( __METHOD__." dropping {$newTableName}\n", true);
105 //Dropping the oldTable because the prefix was changed
106 }
107
108 # Create new table
109 wfDebug( __METHOD__." duplicating $oldTableName to $newTableName\n", true );
110 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
111
112 }
113
114 }
115
116 /**
117 * Change the prefix back to the original.
118 * @param $dropTables bool Optionally drop the tables we created
119 */
120 public function destroy( $dropTables = false ) {
121 if( $dropTables ) {
122 self::changePrefix( $this->newTablePrefix );
123 foreach( $this->tablesToClone as $tbl ) {
124 $this->db->dropTable( $tbl );
125 }
126 }
127 self::changePrefix( $this->oldTablePrefix );
128 }
129
130 /**
131 * Change the table prefix on all open DB connections/
132 *
133 * @param $prefix
134 * @return void
135 */
136 public static function changePrefix( $prefix ) {
137 global $wgDBprefix;
138 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
139 $wgDBprefix = $prefix;
140 }
141
142 /**
143 * @param $lb LoadBalancer
144 * @param $prefix
145 * @return void
146 */
147 public static function changeLBPrefix( $lb, $prefix ) {
148 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
149 }
150
151 /**
152 * @param $db DatabaseBase
153 * @param $prefix
154 * @return void
155 */
156 public static function changeDBPrefix( $db, $prefix ) {
157 $db->tablePrefix( $prefix );
158 }
159 }