Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / db / CloneDatabase.php
blob2b394b6d7bf076e39b368ef9e55c5382d9b46d01
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
26 use MediaWiki\MediaWikiServices;
28 class CloneDatabase {
29 /** @var string Table prefix for cloning */
30 private $newTablePrefix = '';
32 /** @var string Current table prefix */
33 private $oldTablePrefix = '';
35 /** @var array List of tables to be cloned */
36 private $tablesToClone = [];
38 /** @var bool Should we DROP tables containing the new names? */
39 private $dropCurrentTables = true;
41 /** @var bool Whether to use temporary tables or not */
42 private $useTemporaryTables = true;
44 /** @var IMaintainableDatabase */
45 private $db;
47 /**
48 * Constructor
50 * @param IMaintainableDatabase $db A database subclass
51 * @param array $tablesToClone An array of tables to clone, unprefixed
52 * @param string $newTablePrefix Prefix to assign to the tables
53 * @param string $oldTablePrefix Prefix on current tables, if not $wgDBprefix
54 * @param bool $dropCurrentTables
56 public function __construct( IMaintainableDatabase $db, array $tablesToClone,
57 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true
58 ) {
59 $this->db = $db;
60 $this->tablesToClone = $tablesToClone;
61 $this->newTablePrefix = $newTablePrefix;
62 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
63 $this->dropCurrentTables = $dropCurrentTables;
66 /**
67 * Set whether to use temporary tables or not
68 * @param bool $u Use temporary tables when cloning the structure
70 public function useTemporaryTables( $u = true ) {
71 $this->useTemporaryTables = $u;
74 /**
75 * Clone the table structure
77 public function cloneTableStructure() {
78 global $wgSharedTables, $wgSharedDB;
79 foreach ( $this->tablesToClone as $tbl ) {
80 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
81 // Shared tables don't work properly when cloning due to
82 // how prefixes are handled (bug 65654)
83 throw new RuntimeException( "Cannot clone shared table $tbl." );
85 # Clean up from previous aborted run. So that table escaping
86 # works correctly across DB engines, we need to change the pre-
87 # fix back and forth so tableName() works right.
89 self::changePrefix( $this->oldTablePrefix );
90 $oldTableName = $this->db->tableName( $tbl, 'raw' );
92 self::changePrefix( $this->newTablePrefix );
93 $newTableName = $this->db->tableName( $tbl, 'raw' );
95 if ( $this->dropCurrentTables
96 && !in_array( $this->db->getType(), [ 'postgres', 'oracle' ] )
97 ) {
98 if ( $oldTableName === $newTableName ) {
99 // Last ditch check to avoid data loss
100 throw new LogicException( "Not dropping new table, as '$newTableName'"
101 . " is name of both the old and the new table." );
103 $this->db->dropTable( $tbl, __METHOD__ );
104 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
105 // Dropping the oldTable because the prefix was changed
108 # Create new table
109 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
110 $this->db->duplicateTableStructure(
111 $oldTableName, $newTableName, $this->useTemporaryTables );
116 * Change the prefix back to the original.
117 * @param bool $dropTables Optionally drop the tables we created
119 public function destroy( $dropTables = false ) {
120 if ( $dropTables ) {
121 self::changePrefix( $this->newTablePrefix );
122 foreach ( $this->tablesToClone as $tbl ) {
123 $this->db->dropTable( $tbl );
126 self::changePrefix( $this->oldTablePrefix );
130 * Change the table prefix on all open DB connections/
132 * @param string $prefix
133 * @return void
135 public static function changePrefix( $prefix ) {
136 global $wgDBprefix;
138 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
139 $lbFactory->setDomainPrefix( $prefix );
140 $wgDBprefix = $prefix;