2008-12-25 04:31:44 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-08-11 20:48:09 +00:00
|
|
|
* Change the prefix of database tables.
|
2009-03-29 12:27:59 +00:00
|
|
|
* Run this script to after changing $wgDBprefix on a wiki.
|
2008-12-25 04:31:44 +00:00
|
|
|
* The wiki will have to get downtime to do this correctly.
|
|
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-11 20:48:09 +00:00
|
|
|
* @file
|
2008-12-25 04:31:44 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2022-07-10 21:18:31 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2009-06-24 02:49:24 +00:00
|
|
|
|
2012-08-11 20:48:09 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that changes the prefix of database tables.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class RenameDbPrefix extends Maintenance {
|
|
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->addOption( "old", "Old db prefix [0 for none]", true, true );
|
|
|
|
|
$this->addOption( "new", "New db prefix [0 for none]", true, true );
|
|
|
|
|
}
|
2009-08-09 13:40:43 +00:00
|
|
|
|
2010-03-10 12:59:44 +00:00
|
|
|
public function getDbType() {
|
2009-08-09 13:40:43 +00:00
|
|
|
return Maintenance::DB_ADMIN;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2022-07-10 21:18:31 +00:00
|
|
|
$dbName = $this->getConfig()->get( MainConfigNames::DBname );
|
2010-07-25 18:39:41 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
// Allow for no old prefix
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->getOption( 'old', 0 ) === '0' ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$old = '';
|
|
|
|
|
} else {
|
2021-11-22 13:35:17 +00:00
|
|
|
// Use nice safe, sensible, prefixes
|
2010-05-22 16:50:39 +00:00
|
|
|
preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
|
2017-10-06 22:17:58 +00:00
|
|
|
$old = $m[0] ?? false;
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
// Allow for no new prefix
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->getOption( 'new', 0 ) === '0' ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$new = '';
|
|
|
|
|
} else {
|
2021-11-22 13:35:17 +00:00
|
|
|
// Use nice safe, sensible, prefixes
|
2010-05-22 16:50:39 +00:00
|
|
|
preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
|
2017-10-06 22:17:58 +00:00
|
|
|
$new = $m[0] ?? false;
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $old === false || $new === false ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid prefix!" );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $old === $new ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Same prefix. Nothing to rename!\n", true );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2022-07-10 21:18:31 +00:00
|
|
|
$this->output( "Renaming DB prefix for tables of $dbName from '$old' to '$new'\n" );
|
2009-08-02 19:35:17 +00:00
|
|
|
$count = 0;
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2024-01-17 18:53:40 +00:00
|
|
|
$dbw = $this->getPrimaryDB();
|
2020-06-07 16:23:08 +00:00
|
|
|
$res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ );
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $res as $row ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
// XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
|
|
|
|
|
// sort of message. Best not to try $row->x stuff...
|
|
|
|
|
$fields = get_object_vars( $row );
|
|
|
|
|
// Silly for loop over one field...
|
2010-10-14 20:53:04 +00:00
|
|
|
foreach ( $fields as $table ) {
|
2009-08-02 19:35:17 +00:00
|
|
|
// $old should be regexp safe ([a-zA-Z_])
|
2010-05-22 16:50:39 +00:00
|
|
|
$newTable = preg_replace( '/^' . $old . '/', $new, $table );
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Renaming table $table to $newTable\n" );
|
2019-02-17 11:47:41 +00:00
|
|
|
$oldTableEnc = $dbw->addIdentifierQuotes( $table );
|
|
|
|
|
$newTableEnc = $dbw->addIdentifierQuotes( $newTable );
|
2020-06-07 16:23:08 +00:00
|
|
|
$dbw->query( "RENAME TABLE $oldTableEnc TO $newTableEnc", __METHOD__ );
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
$this->output( "Done! [$count tables]\n" );
|
2008-12-25 04:31:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = RenameDbPrefix::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|
2024-08-27 12:00:25 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|