MediaWiki REL1_40
renameDbPrefix.php
Go to the documentation of this file.
1<?php
27
28require_once __DIR__ . '/Maintenance.php';
29
36 public function __construct() {
37 parent::__construct();
38 $this->addOption( "old", "Old db prefix [0 for none]", true, true );
39 $this->addOption( "new", "New db prefix [0 for none]", true, true );
40 }
41
42 public function getDbType() {
44 }
45
46 public function execute() {
47 $dbName = $this->getConfig()->get( MainConfigNames::DBname );
48
49 // Allow for no old prefix
50 if ( $this->getOption( 'old', 0 ) === '0' ) {
51 $old = '';
52 } else {
53 // Use nice safe, sensible, prefixes
54 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
55 $old = $m[0] ?? false;
56 }
57 // Allow for no new prefix
58 if ( $this->getOption( 'new', 0 ) === '0' ) {
59 $new = '';
60 } else {
61 // Use nice safe, sensible, prefixes
62 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
63 $new = $m[0] ?? false;
64 }
65
66 if ( $old === false || $new === false ) {
67 $this->fatalError( "Invalid prefix!" );
68 }
69 if ( $old === $new ) {
70 $this->output( "Same prefix. Nothing to rename!\n", true );
71 }
72
73 $this->output( "Renaming DB prefix for tables of $dbName from '$old' to '$new'\n" );
74 $count = 0;
75
76 $dbw = $this->getDB( DB_PRIMARY );
77 $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ );
78 foreach ( $res as $row ) {
79 // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
80 // sort of message. Best not to try $row->x stuff...
81 $fields = get_object_vars( $row );
82 // Silly for loop over one field...
83 foreach ( $fields as $table ) {
84 // $old should be regexp safe ([a-zA-Z_])
85 $newTable = preg_replace( '/^' . $old . '/', $new, $table );
86 $this->output( "Renaming table $table to $newTable\n" );
87 $oldTableEnc = $dbw->addIdentifierQuotes( $table );
88 $newTableEnc = $dbw->addIdentifierQuotes( $newTable );
89 $dbw->query( "RENAME TABLE $oldTableEnc TO $newTableEnc", __METHOD__ );
90 }
91 $count++;
92 }
93 $this->output( "Done! [$count tables]\n" );
94 }
95}
96
97$maintClass = RenameDbPrefix::class;
98require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
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.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
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.
const DB_PRIMARY
Definition defines.php:28
$maintClass