MediaWiki REL1_31
CloneDatabase.php
Go to the documentation of this file.
1<?php
25
28 private $newTablePrefix = '';
29
31 private $oldTablePrefix = '';
32
34 private $tablesToClone = [];
35
37 private $dropCurrentTables = true;
38
40 private $useTemporaryTables = true;
41
43 private $db;
44
54 ) {
55 $this->db = $db;
56 $this->tablesToClone = $tablesToClone;
57 $this->newTablePrefix = $newTablePrefix;
58 $this->oldTablePrefix = $oldTablePrefix !== null ? $oldTablePrefix : $this->db->tablePrefix();
59 $this->dropCurrentTables = $dropCurrentTables;
60 }
61
66 public function useTemporaryTables( $u = true ) {
67 $this->useTemporaryTables = $u;
68 }
69
73 public function cloneTableStructure() {
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 (T67654)
79 throw new RuntimeException( "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 // Postgres: Temp tables are automatically deleted upon end of session
92 // Same Temp table name hides existing table for current session
93 if ( $this->dropCurrentTables
94 && !in_array( $this->db->getType(), [ 'oracle' ] )
95 ) {
96 if ( $oldTableName === $newTableName ) {
97 // Last ditch check to avoid data loss
98 throw new LogicException( "Not dropping new table, as '$newTableName'"
99 . " is name of both the old and the new table." );
100 }
101 $this->db->dropTable( $tbl, __METHOD__ );
102 wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
103 // Dropping the oldTable because the prefix was changed
104 }
105
106 # Create new table
107 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
108 $this->db->duplicateTableStructure(
109 $oldTableName, $newTableName, $this->useTemporaryTables );
110 }
111 }
112
117 public function destroy( $dropTables = false ) {
118 if ( $dropTables ) {
119 self::changePrefix( $this->newTablePrefix );
120 foreach ( $this->tablesToClone as $tbl ) {
121 $this->db->dropTable( $tbl );
122 }
123 }
124 self::changePrefix( $this->oldTablePrefix );
125 }
126
133 public static function changePrefix( $prefix ) {
134 global $wgDBprefix;
135
136 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
137 $lbFactory->setDomainPrefix( $prefix );
138 $wgDBprefix = $prefix;
139 }
140}
$wgDBprefix
Table name prefix.
$wgSharedTables
$wgSharedDB
Shared database for multiple wikis.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
bool $useTemporaryTables
Whether to use temporary tables or not.
bool $dropCurrentTables
Should we DROP tables containing the new names?
cloneTableStructure()
Clone the table structure.
IMaintainableDatabase $db
static changePrefix( $prefix)
Change the table prefix on all open DB connections/.
string $newTablePrefix
Table prefix for cloning.
__construct(IMaintainableDatabase $db, array $tablesToClone, $newTablePrefix, $oldTablePrefix=null, $dropCurrentTables=true)
destroy( $dropTables=false)
Change the prefix back to the original.
array $tablesToClone
List of tables to be cloned.
useTemporaryTables( $u=true)
Set whether to use temporary tables or not.
string $oldTablePrefix
Current table prefix.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Advanced database interface for IDatabase handles that include maintenance methods.