MediaWiki  master
CloneDatabase.php
Go to the documentation of this file.
1 <?php
26 
29  private $newTablePrefix;
30 
32  private $oldTablePrefix;
33 
35  private $tablesToClone;
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 
74  public function cloneTableStructure() {
76  foreach ( $this->tablesToClone as $tbl ) {
77  if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
78  // Shared tables don't work properly when cloning due to
79  // how prefixes are handled (T67654)
80  throw new RuntimeException( "Cannot clone shared table $tbl." );
81  }
82  # Clean up from previous aborted run. So that table escaping
83  # works correctly across DB engines, we need to change the pre-
84  # fix back and forth so tableName() works right.
85 
86  $this->db->tablePrefix( $this->oldTablePrefix );
87  $oldTableName = $this->db->tableName( $tbl, 'raw' );
88 
89  $this->db->tablePrefix( $this->newTablePrefix );
90  $newTableName = $this->db->tableName( $tbl, 'raw' );
91 
92  // Postgres: Temp tables are automatically deleted upon end of session
93  // Same Temp table name hides existing table for current session
94  if ( $this->dropCurrentTables ) {
95  if ( $oldTableName === $newTableName ) {
96  // Last ditch check to avoid data loss
97  throw new LogicException( "Not dropping new table, as '$newTableName'"
98  . " is name of both the old and the new table." );
99  }
100  $this->db->dropTable( $tbl, __METHOD__ );
101  wfDebug( __METHOD__ . " dropping {$newTableName}" );
102  // Dropping the oldTable because the prefix was changed
103  }
104 
105  # Create new table
106  wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName" );
107  $this->db->duplicateTableStructure(
108  $oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ );
109  }
110  }
111 
116  public function destroy( $dropTables = false ) {
117  if ( $dropTables ) {
118  $this->db->tablePrefix( $this->newTablePrefix );
119  foreach ( $this->tablesToClone as $tbl ) {
120  $this->db->dropTable( $tbl, __METHOD__ );
121  }
122  }
123  $this->db->tablePrefix( $this->oldTablePrefix );
124  }
125 
132  public static function changePrefix( $prefix ) {
133  global $wgDBprefix, $wgDBname;
134 
135  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
136  $lbFactory->setLocalDomainPrefix( $prefix );
137 
138  $aliases = [
139  $wgDBname => $lbFactory->getLocalDomainID()
140  ];
141  $lbFactory->setDomainAliases( $aliases );
142  $lbFactory->forEachLB( function ( ILoadBalancer $lb ) use ( $aliases ) {
143  $lb->setDomainAliases( $aliases );
144  } );
145 
146  $wgDBprefix = $prefix;
147  }
148 }
CloneDatabase\cloneTableStructure
cloneTableStructure()
Definition: CloneDatabase.php:74
$wgDBname
$wgDBname
Current wiki database name.
Definition: DefaultSettings.php:1996
$wgSharedTables
$wgSharedTables
Definition: DefaultSettings.php:2161
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:165
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:2150
CloneDatabase\$useTemporaryTables
bool $useTemporaryTables
Whether to use temporary tables or not.
Definition: CloneDatabase.php:41
$wgDBprefix
$wgDBprefix
Current wiki database table name prefix.
Definition: DefaultSettings.php:2016
CloneDatabase\__construct
__construct(IMaintainableDatabase $db, array $tablesToClone, $newTablePrefix, $oldTablePrefix=null, $dropCurrentTables=true)
Definition: CloneDatabase.php:53
CloneDatabase\$tablesToClone
array $tablesToClone
List of tables to be cloned.
Definition: CloneDatabase.php:35
CloneDatabase\$oldTablePrefix
string $oldTablePrefix
Current table prefix.
Definition: CloneDatabase.php:32
Wikimedia\Rdbms\ILoadBalancer\setDomainAliases
setDomainAliases(array $aliases)
Convert certain database domains to alternative ones.
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:910
CloneDatabase\destroy
destroy( $dropTables=false)
Change the prefix back to the original.
Definition: CloneDatabase.php:116
CloneDatabase\useTemporaryTables
useTemporaryTables( $u=true)
Set whether to use temporary tables or not.
Definition: CloneDatabase.php:70
CloneDatabase
Definition: CloneDatabase.php:27
CloneDatabase\changePrefix
static changePrefix( $prefix)
Change the table prefix on all open DB connections.
Definition: CloneDatabase.php:132
CloneDatabase\$db
IMaintainableDatabase $db
Definition: CloneDatabase.php:44
CloneDatabase\$dropCurrentTables
bool $dropCurrentTables
Should we DROP tables containing the new names?
Definition: CloneDatabase.php:38
Wikimedia\Rdbms\IMaintainableDatabase
Advanced database interface for IDatabase handles that include maintenance methods.
Definition: IMaintainableDatabase.php:38
CloneDatabase\$newTablePrefix
string $newTablePrefix
Table prefix for cloning.
Definition: CloneDatabase.php:29
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81