MediaWiki REL1_35
CloneDatabase.php
Go to the documentation of this file.
1<?php
26
30
33
36
39
41 private $useTemporaryTables = true;
42
44 private $db;
45
55 ) {
56 if ( !$tablesToClone ) {
57 throw new InvalidArgumentException( 'Empty list of tables to clone' );
58 }
59 $this->db = $db;
60 $this->tablesToClone = $tablesToClone;
61 $this->newTablePrefix = $newTablePrefix;
62 $this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix();
63 $this->dropCurrentTables = $dropCurrentTables;
64 }
65
70 public function useTemporaryTables( $u = true ) {
71 $this->useTemporaryTables = $u;
72 }
73
77 public function cloneTableStructure() {
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 (T67654)
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 $this->db->tablePrefix( $this->oldTablePrefix );
90 $oldTableName = $this->db->tableName( $tbl, 'raw' );
91
92 $this->db->tablePrefix( $this->newTablePrefix );
93 $newTableName = $this->db->tableName( $tbl, 'raw' );
94
95 // Postgres: Temp tables are automatically deleted upon end of session
96 // Same Temp table name hides existing table for current session
97 if ( $this->dropCurrentTables ) {
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}" );
105 // Dropping the oldTable because the prefix was changed
106 }
107
108 # Create new table
109 wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName" );
110 $this->db->duplicateTableStructure(
111 $oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ );
112 }
113 }
114
119 public function destroy( $dropTables = false ) {
120 if ( $dropTables ) {
121 $this->db->tablePrefix( $this->newTablePrefix );
122 foreach ( $this->tablesToClone as $tbl ) {
123 $this->db->dropTable( $tbl, __METHOD__ );
124 }
125 }
126 $this->db->tablePrefix( $this->oldTablePrefix );
127 }
128
135 public static function changePrefix( $prefix ) {
136 global $wgDBprefix, $wgDBname;
137
138 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
139 $lbFactory->setLocalDomainPrefix( $prefix );
140
141 $aliases = [
142 $wgDBname => $lbFactory->getLocalDomainID()
143 ];
144 $lbFactory->setDomainAliases( $aliases );
145 $lbFactory->forEachLB( function ( ILoadBalancer $lb ) use ( $aliases ) {
146 $lb->setDomainAliases( $aliases );
147 } );
148
149 $wgDBprefix = $prefix;
150 }
151}
$wgDBprefix
Current wiki database table name prefix.
$wgSharedTables
$wgDBname
Current wiki database name.
$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.
tablePrefix( $prefix=null)
Get/set the table prefix.
Database cluster connection, tracking, load balancing, and transaction manager interface.
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
Advanced database interface for IDatabase handles that include maintenance methods.