Merge "Revert "Use display name in category page subheadings if provided""
[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 use MediaWiki\MediaWikiServices;
27
28 class CloneDatabase {
29 /** @var string Table prefix for cloning */
30 private $newTablePrefix = '';
31
32 /** @var string Current table prefix */
33 private $oldTablePrefix = '';
34
35 /** @var array List of tables to be cloned */
36 private $tablesToClone = [];
37
38 /** @var bool Should we DROP tables containing the new names? */
39 private $dropCurrentTables = true;
40
41 /** @var bool Whether to use temporary tables or not */
42 private $useTemporaryTables = true;
43
44 /** @var Database */
45 private $db;
46
47 /**
48 * Constructor
49 *
50 * @param Database $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
55 */
56 public function __construct( Database $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;
64 }
65
66 /**
67 * Set whether to use temporary tables or not
68 * @param bool $u Use temporary tables when cloning the structure
69 */
70 public function useTemporaryTables( $u = true ) {
71 $this->useTemporaryTables = $u;
72 }
73
74 /**
75 * Clone the table structure
76 */
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." );
84 }
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.
88
89 self::changePrefix( $this->oldTablePrefix );
90 $oldTableName = $this->db->tableName( $tbl, 'raw' );
91
92 self::changePrefix( $this->newTablePrefix );
93 $newTableName = $this->db->tableName( $tbl, 'raw' );
94
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." );
102 }
103 $this->db->dropTable( $tbl, __METHOD__ );
104 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
105 // Dropping the oldTable because the prefix was changed
106 }
107
108 # Create new table
109 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
110 $this->db->duplicateTableStructure( $oldTableName, $newTableName, $this->useTemporaryTables );
111 }
112 }
113
114 /**
115 * Change the prefix back to the original.
116 * @param bool $dropTables Optionally drop the tables we created
117 */
118 public function destroy( $dropTables = false ) {
119 if ( $dropTables ) {
120 self::changePrefix( $this->newTablePrefix );
121 foreach ( $this->tablesToClone as $tbl ) {
122 $this->db->dropTable( $tbl );
123 }
124 }
125 self::changePrefix( $this->oldTablePrefix );
126 }
127
128 /**
129 * Change the table prefix on all open DB connections/
130 *
131 * @param string $prefix
132 * @return void
133 */
134 public static function changePrefix( $prefix ) {
135 global $wgDBprefix;
136
137 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
138 $lbFactory->setDomainPrefix( $prefix );
139 $wgDBprefix = $prefix;
140 }
141 }