MediaWiki  1.34.0
CloneDatabase.php
Go to the documentation of this file.
1 <?php
25 
28  private $newTablePrefix;
29 
31  private $oldTablePrefix;
32 
34  private $tablesToClone;
35 
38 
40  private $useTemporaryTables = true;
41 
43  private $db;
44 
54  ) {
55  if ( !$tablesToClone ) {
56  throw new InvalidArgumentException( 'Empty list of tables to clone' );
57  }
58  $this->db = $db;
59  $this->tablesToClone = $tablesToClone;
60  $this->newTablePrefix = $newTablePrefix;
61  $this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix();
62  $this->dropCurrentTables = $dropCurrentTables;
63  }
64 
69  public function useTemporaryTables( $u = true ) {
70  $this->useTemporaryTables = $u;
71  }
72 
76  public function cloneTableStructure() {
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  $this->db->tablePrefix( $this->oldTablePrefix );
89  $oldTableName = $this->db->tableName( $tbl, 'raw' );
90 
91  $this->db->tablePrefix( $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  if ( $oldTableName === $newTableName ) {
98  // Last ditch check to avoid data loss
99  throw new LogicException( "Not dropping new table, as '$newTableName'"
100  . " is name of both the old and the new table." );
101  }
102  $this->db->dropTable( $tbl, __METHOD__ );
103  wfDebug( __METHOD__ . " dropping {$newTableName}\n" );
104  // Dropping the oldTable because the prefix was changed
105  }
106 
107  # Create new table
108  wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName\n" );
109  $this->db->duplicateTableStructure(
110  $oldTableName, $newTableName, $this->useTemporaryTables );
111  }
112  }
113 
118  public function destroy( $dropTables = false ) {
119  if ( $dropTables ) {
120  $this->db->tablePrefix( $this->newTablePrefix );
121  foreach ( $this->tablesToClone as $tbl ) {
122  $this->db->dropTable( $tbl );
123  }
124  }
125  $this->db->tablePrefix( $this->oldTablePrefix );
126  }
127 
134  public static function changePrefix( $prefix ) {
135  global $wgDBprefix;
136 
137  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
138  $lbFactory->setLocalDomainPrefix( $prefix );
139  $wgDBprefix = $prefix;
140  }
141 }
CloneDatabase\cloneTableStructure
cloneTableStructure()
Clone the table structure.
Definition: CloneDatabase.php:76
$wgSharedTables
$wgSharedTables
Definition: DefaultSettings.php:2047
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$wgSharedDB
$wgSharedDB
Shared database for multiple wikis.
Definition: DefaultSettings.php:2037
CloneDatabase\$useTemporaryTables
bool $useTemporaryTables
Whether to use temporary tables or not.
Definition: CloneDatabase.php:40
$wgDBprefix
$wgDBprefix
Current wiki database table name prefix.
Definition: DefaultSettings.php:1913
CloneDatabase\__construct
__construct(IMaintainableDatabase $db, array $tablesToClone, $newTablePrefix, $oldTablePrefix=null, $dropCurrentTables=true)
Definition: CloneDatabase.php:52
CloneDatabase\$tablesToClone
array $tablesToClone
List of tables to be cloned.
Definition: CloneDatabase.php:34
CloneDatabase\$oldTablePrefix
string $oldTablePrefix
Current table prefix.
Definition: CloneDatabase.php:31
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:913
CloneDatabase\destroy
destroy( $dropTables=false)
Change the prefix back to the original.
Definition: CloneDatabase.php:118
CloneDatabase\useTemporaryTables
useTemporaryTables( $u=true)
Set whether to use temporary tables or not.
Definition: CloneDatabase.php:69
CloneDatabase
Definition: CloneDatabase.php:26
CloneDatabase\changePrefix
static changePrefix( $prefix)
Change the table prefix on all open DB connections.
Definition: CloneDatabase.php:134
CloneDatabase\$db
IMaintainableDatabase $db
Definition: CloneDatabase.php:43
CloneDatabase\$dropCurrentTables
bool $dropCurrentTables
Should we DROP tables containing the new names?
Definition: CloneDatabase.php:37
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:28