MediaWiki
master
Toggle main menu visibility
CloneDatabase.php
Go to the documentation of this file.
1
<?php
9
10
namespace
MediaWiki\DB;
11
12
use InvalidArgumentException;
13
use LogicException;
14
use MediaWiki\MediaWikiServices;
15
use RuntimeException;
16
use Wikimedia\Rdbms\IMaintainableDatabase;
17
18
class
CloneDatabase
{
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
(
44
IMaintainableDatabase
$db,
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 {
69
global
$wgSharedTables
,
$wgSharedDB
;
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
143
class_alias( CloneDatabase::class,
'CloneDatabase'
);
if
if(!defined('MW_SETUP_CALLBACK'))
Definition
WebStart.php:72
MediaWiki\DB\CloneDatabase
Definition
CloneDatabase.php:18
MediaWiki\DB\CloneDatabase\useTemporaryTables
useTemporaryTables(bool $u=true)
Set whether to use temporary tables or not.
Definition
CloneDatabase.php:64
MediaWiki\DB\CloneDatabase\cloneTableStructure
cloneTableStructure()
Definition
CloneDatabase.php:68
MediaWiki\DB\CloneDatabase\__construct
__construct(IMaintainableDatabase $db, array $tablesToClone, string $newTablePrefix, ?string $oldTablePrefix=null, bool $dropCurrentTables=true)
Definition
CloneDatabase.php:43
MediaWiki\DB\CloneDatabase\changePrefix
static changePrefix(string $prefix)
Change the table prefix on all open DB connections.
Definition
CloneDatabase.php:124
MediaWiki\DB\CloneDatabase\destroy
destroy(bool $dropTables=false)
Change the prefix back to the original.
Definition
CloneDatabase.php:108
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition
MediaWikiServices.php:361
$wgDBprefix
$wgDBprefix
Config variable stub for the DBprefix setting, for use by phpdoc and IDEs.
Definition
config-vars.php:1077
$wgSharedTables
$wgSharedTables
Config variable stub for the SharedTables setting, for use by phpdoc and IDEs.
Definition
config-vars.php:1185
$wgDBname
$wgDBname
Config variable stub for the DBname setting, for use by phpdoc and IDEs.
Definition
config-vars.php:1065
$wgSharedDB
$wgSharedDB
Config variable stub for the SharedDB setting, for use by phpdoc and IDEs.
Definition
config-vars.php:1173
Wikimedia\Rdbms\IMaintainableDatabase
Advanced database interface for IDatabase handles that include maintenance methods.
Definition
IMaintainableDatabase.php:20
includes
DB
CloneDatabase.php
Generated on
for MediaWiki by
1.18.0