userOptions: make --old a multi option
[mediawiki.git] / maintenance / renameDbPrefix.php
bloba7d5247774a0ced5ec08ca9be32acbe0fcbf2faa
1 <?php
2 /**
3 * Change the prefix of database tables.
4 * Run this script to after changing $wgDBprefix on a wiki.
5 * The wiki will have to get downtime to do this correctly.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @file
23 * @ingroup Maintenance
26 use MediaWiki\MainConfigNames;
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
32 /**
33 * Maintenance script that changes the prefix of database tables.
35 * @ingroup Maintenance
37 class RenameDbPrefix extends Maintenance {
38 public function __construct() {
39 parent::__construct();
40 $this->addOption( "old", "Old db prefix [0 for none]", true, true );
41 $this->addOption( "new", "New db prefix [0 for none]", true, true );
44 public function getDbType() {
45 return Maintenance::DB_ADMIN;
48 public function execute() {
49 $dbName = $this->getConfig()->get( MainConfigNames::DBname );
51 // Allow for no old prefix
52 if ( $this->getOption( 'old', 0 ) === '0' ) {
53 $old = '';
54 } else {
55 // Use nice safe, sensible, prefixes
56 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
57 $old = $m[0] ?? false;
59 // Allow for no new prefix
60 if ( $this->getOption( 'new', 0 ) === '0' ) {
61 $new = '';
62 } else {
63 // Use nice safe, sensible, prefixes
64 preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
65 $new = $m[0] ?? false;
68 if ( $old === false || $new === false ) {
69 $this->fatalError( "Invalid prefix!" );
71 if ( $old === $new ) {
72 $this->output( "Same prefix. Nothing to rename!\n", true );
75 $this->output( "Renaming DB prefix for tables of $dbName from '$old' to '$new'\n" );
76 $count = 0;
78 $dbw = $this->getPrimaryDB();
79 $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ );
80 foreach ( $res as $row ) {
81 // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
82 // sort of message. Best not to try $row->x stuff...
83 $fields = get_object_vars( $row );
84 // Silly for loop over one field...
85 foreach ( $fields as $table ) {
86 // $old should be regexp safe ([a-zA-Z_])
87 $newTable = preg_replace( '/^' . $old . '/', $new, $table );
88 $this->output( "Renaming table $table to $newTable\n" );
89 $oldTableEnc = $dbw->addIdentifierQuotes( $table );
90 $newTableEnc = $dbw->addIdentifierQuotes( $newTable );
91 $dbw->query( "RENAME TABLE $oldTableEnc TO $newTableEnc", __METHOD__ );
93 $count++;
95 $this->output( "Done! [$count tables]\n" );
99 // @codeCoverageIgnoreStart
100 $maintClass = RenameDbPrefix::class;
101 require_once RUN_MAINTENANCE_IF_MAIN;
102 // @codeCoverageIgnoreEnd