Merge "PrefixSearch (ApiOpenSearch) now supports searching in multiple namespaces"
[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 global $wgSharedTables, $wgSharedDB;
75 foreach ( $this->tablesToClone as $tbl ) {
76 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
77 // Shared tables don't work properly when cloning due to
78 // how prefixes are handled (bug 65654)
79 throw new MWException( "Cannot clone shared table $tbl." );
80 }
81 # Clean up from previous aborted run. So that table escaping
82 # works correctly across DB engines, we need to change the pre-
83 # fix back and forth so tableName() works right.
84
85 self::changePrefix( $this->oldTablePrefix );
86 $oldTableName = $this->db->tableName( $tbl, 'raw' );
87
88 self::changePrefix( $this->newTablePrefix );
89 $newTableName = $this->db->tableName( $tbl, 'raw' );
90
91 if ( $this->dropCurrentTables
92 && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) )
93 ) {
94 $this->db->dropTable( $tbl, __METHOD__ );
95 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
96 //Dropping the oldTable because the prefix was changed
97 }
98
99 # Create new table
100 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
101 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
102 }
103 }
104
105 /**
106 * Change the prefix back to the original.
107 * @param bool $dropTables Optionally drop the tables we created
108 */
109 public function destroy( $dropTables = false ) {
110 if ( $dropTables ) {
111 self::changePrefix( $this->newTablePrefix );
112 foreach ( $this->tablesToClone as $tbl ) {
113 $this->db->dropTable( $tbl );
114 }
115 }
116 self::changePrefix( $this->oldTablePrefix );
117 }
118
119 /**
120 * Change the table prefix on all open DB connections/
121 *
122 * @param string $prefix
123 * @return void
124 */
125 public static function changePrefix( $prefix ) {
126 global $wgDBprefix;
127 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
128 $wgDBprefix = $prefix;
129 }
130
131 /**
132 * @param LoadBalancer $lb
133 * @param string $prefix
134 * @return void
135 */
136 public static function changeLBPrefix( $lb, $prefix ) {
137 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
138 }
139
140 /**
141 * @param DatabaseBase $db
142 * @param string $prefix
143 * @return void
144 */
145 public static function changeDBPrefix( $db, $prefix ) {
146 $db->tablePrefix( $prefix );
147 }
148 }