Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RenameDbPrefix
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDbType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
72
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.
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @ingroup Maintenance
24 */
25
26use MediaWiki\MainConfigNames;
27
28require_once __DIR__ . '/Maintenance.php';
29
30/**
31 * Maintenance script that changes the prefix of database tables.
32 *
33 * @ingroup Maintenance
34 */
35class RenameDbPrefix extends Maintenance {
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() {
43        return Maintenance::DB_ADMIN;
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->getPrimaryDB();
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;