MediaWiki  master
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 
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  $this->db->tablePrefix( $this->oldTablePrefix );
86  $oldTableName = $this->db->tableName( $tbl, 'raw' );
87 
88  $this->db->tablePrefix( $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  if ( $oldTableName === $newTableName ) {
95  // Last ditch check to avoid data loss
96  throw new LogicException( "Not dropping new table, as '$newTableName'"
97  . " is name of both the old and the new table." );
98  }
99  $this->db->dropTable( $tbl, __METHOD__ );
100  wfDebug( __METHOD__ . " dropping {$newTableName}" );
101  // Dropping the oldTable because the prefix was changed
102  }
103 
104  # Create new table
105  wfDebug( __METHOD__ . " duplicating $oldTableName to $newTableName" );
106  $this->db->duplicateTableStructure(
107  $oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ );
108  }
109  }
110 
115  public function destroy( $dropTables = false ) {
116  if ( $dropTables ) {
117  $this->db->tablePrefix( $this->newTablePrefix );
118  foreach ( $this->tablesToClone as $tbl ) {
119  $this->db->dropTable( $tbl, __METHOD__ );
120  }
121  }
122  $this->db->tablePrefix( $this->oldTablePrefix );
123  }
124 
131  public static function changePrefix( $prefix ) {
132  global $wgDBprefix, $wgDBname;
133 
134  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
135  $lbFactory->setLocalDomainPrefix( $prefix );
136 
137  $aliases = [
138  $wgDBname => $lbFactory->getLocalDomainID()
139  ];
140  $lbFactory->setDomainAliases( $aliases );
141  foreach ( $lbFactory->getAllLBs() as $lb ) {
142  $lb->setDomainAliases( $aliases );
143  }
144 
145  $wgDBprefix = $prefix;
146  }
147 }
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?
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.
$wgDBprefix
Config variable stub for the DBprefix setting, for use by phpdoc and IDEs.
$wgSharedTables
Config variable stub for the SharedTables setting, for use by phpdoc and IDEs.
$wgDBname
Config variable stub for the DBname setting, for use by phpdoc and IDEs.
$wgSharedDB
Config variable stub for the SharedDB setting, for use by phpdoc and IDEs.
Advanced database interface for IDatabase handles that include maintenance methods.