SessionManager: Change behavior of getSessionById()
[mediawiki.git] / includes / db / CloneDatabase.php
blobbc703f3658b995acdf16426ddd1f937a46681638
1 <?php
2 /**
3 * Helper class for making a copy of the database, mostly for unit testing.
5 * Copyright © 2010 Chad Horohoe <chad@anyonecanedit.org>
6 * https://www.mediawiki.org/
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.
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.
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
23 * @file
24 * @ingroup Database
27 class CloneDatabase {
28 /** @var string Table prefix for cloning */
29 private $newTablePrefix = '';
31 /** @var string Current table prefix */
32 private $oldTablePrefix = '';
34 /** @var array List of tables to be cloned */
35 private $tablesToClone = array();
37 /** @var bool Should we DROP tables containing the new names? */
38 private $dropCurrentTables = true;
40 /** @var bool Whether to use temporary tables or not */
41 private $useTemporaryTables = true;
43 /**
44 * Constructor
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
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;
62 /**
63 * Set whether to use temporary tables or not
64 * @param bool $u Use temporary tables when cloning the structure
66 public function useTemporaryTables( $u = true ) {
67 $this->useTemporaryTables = $u;
70 /**
71 * Clone the table structure
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." );
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.
85 self::changePrefix( $this->oldTablePrefix );
86 $oldTableName = $this->db->tableName( $tbl, 'raw' );
88 self::changePrefix( $this->newTablePrefix );
89 $newTableName = $this->db->tableName( $tbl, 'raw' );
91 if ( $this->dropCurrentTables
92 && !in_array( $this->db->getType(), array( 'postgres', 'oracle' ) )
93 ) {
94 if ( $oldTableName === $newTableName ) {
95 // Last ditch check to avoid data loss
96 throw new MWException( "Not dropping new table, as '$newTableName'"
97 . " is name of both the old and the new table." );
99 $this->db->dropTable( $tbl, __METHOD__ );
100 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
101 // Dropping the oldTable because the prefix was changed
104 # Create new table
105 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
106 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
111 * Change the prefix back to the original.
112 * @param bool $dropTables Optionally drop the tables we created
114 public function destroy( $dropTables = false ) {
115 if ( $dropTables ) {
116 self::changePrefix( $this->newTablePrefix );
117 foreach ( $this->tablesToClone as $tbl ) {
118 $this->db->dropTable( $tbl );
121 self::changePrefix( $this->oldTablePrefix );
125 * Change the table prefix on all open DB connections/
127 * @param string $prefix
128 * @return void
130 public static function changePrefix( $prefix ) {
131 global $wgDBprefix;
132 wfGetLBFactory()->forEachLB( array( 'CloneDatabase', 'changeLBPrefix' ), array( $prefix ) );
133 $wgDBprefix = $prefix;
137 * @param LoadBalancer $lb
138 * @param string $prefix
139 * @return void
141 public static function changeLBPrefix( $lb, $prefix ) {
142 $lb->forEachOpenConnection( array( 'CloneDatabase', 'changeDBPrefix' ), array( $prefix ) );
146 * @param DatabaseBase $db
147 * @param string $prefix
148 * @return void
150 public static function changeDBPrefix( $db, $prefix ) {
151 $db->tablePrefix( $prefix );