MediaWiki master
renameDbPrefix.php
Go to the documentation of this file.
1<?php
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
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;
90require_once RUN_MAINTENANCE_IF_MAIN;
91// @codeCoverageIgnoreEnd
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
getPrimaryDB(string|false $virtualDomain=false)
Maintenance script that changes the prefix of database tables.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
__construct()
Default constructor.
execute()
Do the actual work.
$maintClass