MediaWiki master
CloneDatabase.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\DB;
11
12use InvalidArgumentException;
13use LogicException;
15use RuntimeException;
17
20 private string $newTablePrefix;
21
23 private string $oldTablePrefix;
24
26 private array $tablesToClone;
27
29 private bool $dropCurrentTables;
30
32 private bool $useTemporaryTables = true;
33
34 private IMaintainableDatabase $db;
35
43 public function __construct(
45 array $tablesToClone,
46 string $newTablePrefix,
47 ?string $oldTablePrefix = null,
48 bool $dropCurrentTables = true
49 ) {
50 if ( !$tablesToClone ) {
51 throw new InvalidArgumentException( 'Empty list of tables to clone' );
52 }
53 $this->db = $db;
54 $this->tablesToClone = $tablesToClone;
55 $this->newTablePrefix = $newTablePrefix;
56 $this->oldTablePrefix = $oldTablePrefix ?? $this->db->tablePrefix();
57 $this->dropCurrentTables = $dropCurrentTables;
58 }
59
64 public function useTemporaryTables( bool $u = true ): void {
65 $this->useTemporaryTables = $u;
66 }
67
68 public function cloneTableStructure(): void {
70 foreach ( $this->tablesToClone as $tbl ) {
71 if ( $wgSharedDB && in_array( $tbl, $wgSharedTables, true ) ) {
72 // Shared tables don't work properly when cloning due to
73 // how prefixes are handled (T67654)
74 throw new RuntimeException( "Cannot clone shared table $tbl." );
75 }
76 # Clean up from previous aborted run. So that table escaping
77 # works correctly across DB engines, we need to change the pre-
78 # fix back and forth so tableName() works right.
79
80 $this->db->tablePrefix( $this->oldTablePrefix );
81 $oldTableName = $this->db->tableName( $tbl, 'raw' );
82
83 $this->db->tablePrefix( $this->newTablePrefix );
84 $newTableName = $this->db->tableName( $tbl, 'raw' );
85
86 // Postgres: Temp tables are automatically deleted upon end of session
87 // Same Temp table name hides existing table for current session
88 if ( $this->dropCurrentTables ) {
89 if ( $oldTableName === $newTableName ) {
90 // Last ditch check to avoid data loss
91 throw new LogicException( "Not dropping new table, as '$newTableName'"
92 . " is name of both the old and the new table." );
93 }
94 $this->db->dropTable( $tbl, __METHOD__ );
95 // Dropping the oldTable because the prefix was changed
96 }
97
98 # Create new table
99 $this->db->duplicateTableStructure(
100 $oldTableName, $newTableName, $this->useTemporaryTables, __METHOD__ );
101 }
102 }
103
108 public function destroy( bool $dropTables = false ): void {
109 if ( $dropTables ) {
110 $this->db->tablePrefix( $this->newTablePrefix );
111 foreach ( $this->tablesToClone as $tbl ) {
112 $this->db->dropTable( $tbl, __METHOD__ );
113 }
114 }
115 $this->db->tablePrefix( $this->oldTablePrefix );
116 }
117
124 public static function changePrefix( string $prefix ): void {
125 global $wgDBprefix, $wgDBname;
126
127 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
128 $lbFactory->setLocalDomainPrefix( $prefix );
129
130 $aliases = [
131 $wgDBname => $lbFactory->getLocalDomainID()
132 ];
133 $lbFactory->setDomainAliases( $aliases );
134 foreach ( $lbFactory->getAllLBs() as $lb ) {
135 $lb->setDomainAliases( $aliases );
136 }
137
138 $wgDBprefix = $prefix;
139 }
140}
141
143class_alias( CloneDatabase::class, 'CloneDatabase' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
useTemporaryTables(bool $u=true)
Set whether to use temporary tables or not.
__construct(IMaintainableDatabase $db, array $tablesToClone, string $newTablePrefix, ?string $oldTablePrefix=null, bool $dropCurrentTables=true)
static changePrefix(string $prefix)
Change the table prefix on all open DB connections.
destroy(bool $dropTables=false)
Change the prefix back to the original.
Service locator for MediaWiki core services.
$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.
tablePrefix( $prefix=null)
Get/set the table prefix.