]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/db/CloneDatabase.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.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 use Wikimedia\Rdbms\IMaintainableDatabase;
28
29 class CloneDatabase {
30         /** @var string Table prefix for cloning */
31         private $newTablePrefix = '';
32
33         /** @var string Current table prefix */
34         private $oldTablePrefix = '';
35
36         /** @var array List of tables to be cloned */
37         private $tablesToClone = [];
38
39         /** @var bool Should we DROP tables containing the new names? */
40         private $dropCurrentTables = true;
41
42         /** @var bool Whether to use temporary tables or not */
43         private $useTemporaryTables = true;
44
45         /** @var IMaintainableDatabase */
46         private $db;
47
48         /**
49          * @param IMaintainableDatabase $db A database subclass
50          * @param array $tablesToClone An array of tables to clone, unprefixed
51          * @param string $newTablePrefix Prefix to assign to the tables
52          * @param string $oldTablePrefix Prefix on current tables, if not $wgDBprefix
53          * @param bool $dropCurrentTables
54          */
55         public function __construct( IMaintainableDatabase $db, array $tablesToClone,
56                 $newTablePrefix, $oldTablePrefix = '', $dropCurrentTables = true
57         ) {
58                 $this->db = $db;
59                 $this->tablesToClone = $tablesToClone;
60                 $this->newTablePrefix = $newTablePrefix;
61                 $this->oldTablePrefix = $oldTablePrefix ? $oldTablePrefix : $this->db->tablePrefix();
62                 $this->dropCurrentTables = $dropCurrentTables;
63         }
64
65         /**
66          * Set whether to use temporary tables or not
67          * @param bool $u Use temporary tables when cloning the structure
68          */
69         public function useTemporaryTables( $u = true ) {
70                 $this->useTemporaryTables = $u;
71         }
72
73         /**
74          * Clone the table structure
75          */
76         public function cloneTableStructure() {
77                 global $wgSharedTables, $wgSharedDB;
78                 foreach ( $this->tablesToClone as $tbl ) {
79                         if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
80                                 // Shared tables don't work properly when cloning due to
81                                 // how prefixes are handled (T67654)
82                                 throw new RuntimeException( "Cannot clone shared table $tbl." );
83                         }
84                         # Clean up from previous aborted run.  So that table escaping
85                         # works correctly across DB engines, we need to change the pre-
86                         # fix back and forth so tableName() works right.
87
88                         self::changePrefix( $this->oldTablePrefix );
89                         $oldTableName = $this->db->tableName( $tbl, 'raw' );
90
91                         self::changePrefix( $this->newTablePrefix );
92                         $newTableName = $this->db->tableName( $tbl, 'raw' );
93
94                         // Postgres: Temp tables are automatically deleted upon end of session
95                         //           Same Temp table name hides existing table for current session
96                         if ( $this->dropCurrentTables
97                                 && !in_array( $this->db->getType(), [ 'oracle' ] )
98                         ) {
99                                 if ( $oldTableName === $newTableName ) {
100                                         // Last ditch check to avoid data loss
101                                         throw new LogicException( "Not dropping new table, as '$newTableName'"
102                                                 . " is name of both the old and the new table." );
103                                 }
104                                 $this->db->dropTable( $tbl, __METHOD__ );
105                                 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
106                                 // Dropping the oldTable because the prefix was changed
107                         }
108
109                         # Create new table
110                         wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
111                         $this->db->duplicateTableStructure(
112                                 $oldTableName, $newTableName, $this->useTemporaryTables );
113                 }
114         }
115
116         /**
117          * Change the prefix back to the original.
118          * @param bool $dropTables Optionally drop the tables we created
119          */
120         public function destroy( $dropTables = false ) {
121                 if ( $dropTables ) {
122                         self::changePrefix( $this->newTablePrefix );
123                         foreach ( $this->tablesToClone as $tbl ) {
124                                 $this->db->dropTable( $tbl );
125                         }
126                 }
127                 self::changePrefix( $this->oldTablePrefix );
128         }
129
130         /**
131          * Change the table prefix on all open DB connections/
132          *
133          * @param string $prefix
134          * @return void
135          */
136         public static function changePrefix( $prefix ) {
137                 global $wgDBprefix;
138
139                 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
140                 $lbFactory->setDomainPrefix( $prefix );
141                 $wgDBprefix = $prefix;
142         }
143 }