MediaWiki
master
Toggle main menu visibility
renameDbPrefix.php
Go to the documentation of this file.
1
<?php
11
12
use
MediaWiki\MainConfigNames
;
13
use
MediaWiki\Maintenance\Maintenance
;
14
15
// @codeCoverageIgnoreStart
16
require_once __DIR__ .
'/Maintenance.php'
;
17
// @codeCoverageIgnoreEnd
18
24
class
RenameDbPrefix
extends
Maintenance
{
25
public
function
__construct
() {
26
parent::__construct();
27
$this->addOption(
"old"
,
"Old db prefix [0 for none]"
,
true
,
true
);
28
$this->addOption(
"new"
,
"New db prefix [0 for none]"
,
true
,
true
);
29
}
30
32
public
function
getDbType
() {
33
return
Maintenance::DB_ADMIN;
34
}
35
36
public
function
execute
() {
37
$dbName = $this->getConfig()->get(
MainConfigNames::DBname
);
38
39
// Allow for no old prefix
40
if
( $this->getOption(
'old'
,
'0'
) ===
'0'
) {
41
$old =
''
;
42
}
else
{
43
// Use nice safe, sensible, prefixes
44
preg_match(
'/^[a-zA-Z]+_$/'
, $this->getOption(
'old'
), $m );
45
$old = $m[0] ??
false
;
46
}
47
// Allow for no new prefix
48
if
( $this->getOption(
'new'
,
'0'
) ===
'0'
) {
49
$new =
''
;
50
}
else
{
51
// Use nice safe, sensible, prefixes
52
preg_match(
'/^[a-zA-Z]+_$/'
, $this->getOption(
'new'
), $m );
53
$new = $m[0] ??
false
;
54
}
55
56
if
( $old ===
false
|| $new ===
false
) {
57
$this->fatalError(
"Invalid prefix!"
);
58
}
59
if
( $old === $new ) {
60
$this->output(
"Same prefix. Nothing to rename!\n"
);
61
return
;
62
}
63
64
$this->output(
"Renaming DB prefix for tables of $dbName from '$old' to '$new'\n"
);
65
$count = 0;
66
67
$dbw = $this->getPrimaryDB();
68
$res = $dbw->query(
"SHOW TABLES "
. $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ );
69
foreach
( $res as $row ) {
70
// XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
71
// sort of message. Best not to try $row->x stuff...
72
$fields = get_object_vars( $row );
73
// Silly for loop over one field...
74
foreach
( $fields as $table ) {
75
// $old should be regexp safe ([a-zA-Z_])
76
$newTable = preg_replace(
'/^'
. $old .
'/'
, $new, $table );
77
$this->output(
"Renaming table $table to $newTable\n"
);
78
$oldTableEnc = $dbw->addIdentifierQuotes( $table );
79
$newTableEnc = $dbw->addIdentifierQuotes( $newTable );
80
$dbw->query(
"RENAME TABLE $oldTableEnc TO $newTableEnc"
, __METHOD__ );
81
}
82
$count++;
83
}
84
$this->output(
"Done! [$count tables]\n"
);
85
}
86
}
87
88
// @codeCoverageIgnoreStart
89
$maintClass
= RenameDbPrefix::class;
90
require_once RUN_MAINTENANCE_IF_MAIN;
91
// @codeCoverageIgnoreEnd
$maintClass
$maintClass
Definition
addChangeTag.php:56
MediaWiki\MainConfigNames
A class containing constants representing the names of configuration variables.
Definition
MainConfigNames.php:22
MediaWiki\MainConfigNames\DBname
const DBname
Name constant for the DBname setting, for use with Config\get().
Definition
MainConfigNames.php:1080
RenameDbPrefix
Maintenance script that changes the prefix of database tables.
Definition
renameDbPrefix.php:24
RenameDbPrefix\getDbType
getDbType()
Definition
renameDbPrefix.php:32
RenameDbPrefix\__construct
__construct()
Definition
renameDbPrefix.php:25
RenameDbPrefix\execute
execute()
Definition
renameDbPrefix.php:36
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
renameDbPrefix.php
Generated on
for MediaWiki by
1.18.0