MediaWiki master
renameDbPrefix.php
Go to the documentation of this file.
1<?php
28
29// @codeCoverageIgnoreStart
30require_once __DIR__ . '/Maintenance.php';
31// @codeCoverageIgnoreEnd
32
39 public function __construct() {
40 parent::__construct();
41 $this->addOption( "old", "Old db prefix [0 for none]", true, true );
42 $this->addOption( "new", "New db prefix [0 for none]", true, true );
43 }
44
45 public function getDbType() {
46 return Maintenance::DB_ADMIN;
47 }
48
49 public function execute() {
50 $dbName = $this->getConfig()->get( MainConfigNames::DBname );
51
52 // Allow for no old prefix
53 if ( $this->getOption( 'old', 0 ) === '0' ) {
54 $old = '';
55 } else {
56 // Use nice safe, sensible, prefixes
57 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
58 $old = $m[0] ?? false;
59 }
60 // Allow for no new prefix
61 if ( $this->getOption( 'new', 0 ) === '0' ) {
62 $new = '';
63 } else {
64 // Use nice safe, sensible, prefixes
65 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
66 $new = $m[0] ?? false;
67 }
68
69 if ( $old === false || $new === false ) {
70 $this->fatalError( "Invalid prefix!" );
71 }
72 if ( $old === $new ) {
73 $this->output( "Same prefix. Nothing to rename!\n", true );
74 }
75
76 $this->output( "Renaming DB prefix for tables of $dbName from '$old' to '$new'\n" );
77 $count = 0;
78
79 $dbw = $this->getPrimaryDB();
80 $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ );
81 foreach ( $res as $row ) {
82 // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
83 // sort of message. Best not to try $row->x stuff...
84 $fields = get_object_vars( $row );
85 // Silly for loop over one field...
86 foreach ( $fields as $table ) {
87 // $old should be regexp safe ([a-zA-Z_])
88 $newTable = preg_replace( '/^' . $old . '/', $new, $table );
89 $this->output( "Renaming table $table to $newTable\n" );
90 $oldTableEnc = $dbw->addIdentifierQuotes( $table );
91 $newTableEnc = $dbw->addIdentifierQuotes( $newTable );
92 $dbw->query( "RENAME TABLE $oldTableEnc TO $newTableEnc", __METHOD__ );
93 }
94 $count++;
95 }
96 $this->output( "Done! [$count tables]\n" );
97 }
98}
99
100// @codeCoverageIgnoreStart
101$maintClass = RenameDbPrefix::class;
102require_once RUN_MAINTENANCE_IF_MAIN;
103// @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.
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