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