Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.25% covered (warning)
56.25%
18 / 32
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RenameDbPrefix
56.25% covered (warning)
56.25%
18 / 32
66.67% covered (warning)
66.67%
2 / 3
18.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getDbType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
50.00% covered (danger)
50.00%
14 / 28
0.00% covered (danger)
0.00%
0 / 1
16.00
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 * @license GPL-2.0-or-later
8 * @file
9 * @ingroup Maintenance
10 */
11
12use MediaWiki\MainConfigNames;
13use MediaWiki\Maintenance\Maintenance;
14
15// @codeCoverageIgnoreStart
16require_once __DIR__ . '/Maintenance.php';
17// @codeCoverageIgnoreEnd
18
19/**
20 * Maintenance script that changes the prefix of database tables.
21 *
22 * @ingroup Maintenance
23 */
24class RenameDbPrefix extends Maintenance {
25    public function __construct() {
26        parent::__construct();
27        $this->addOption( "old", "Old db prefix [0 for none]", true, true );
28        $this->addOption( "new", "New db prefix [0 for none]", true, true );
29    }
30
31    /** @inheritDoc */
32    public function getDbType() {
33        return Maintenance::DB_ADMIN;
34    }
35
36    public function execute() {
37        $dbName = $this->getConfig()->get( MainConfigNames::DBname );
38
39        // Allow for no old prefix
40        if ( $this->getOption( 'old', '0' ) === '0' ) {
41            $old = '';
42        } else {
43            // Use nice safe, sensible, prefixes
44            preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
45            $old = $m[0] ?? false;
46        }
47        // Allow for no new prefix
48        if ( $this->getOption( 'new', '0' ) === '0' ) {
49            $new = '';
50        } else {
51            // Use nice safe, sensible, prefixes
52            preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
53            $new = $m[0] ?? false;
54        }
55
56        if ( $old === false || $new === false ) {
57            $this->fatalError( "Invalid prefix!" );
58        }
59        if ( $old === $new ) {
60            $this->output( "Same prefix. Nothing to rename!\n" );
61            return;
62        }
63
64        $this->output( "Renaming DB prefix for tables of $dbName from '$old' to '$new'\n" );
65        $count = 0;
66
67        $dbw = $this->getPrimaryDB();
68        $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ );
69        foreach ( $res as $row ) {
70            // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
71            // sort of message. Best not to try $row->x stuff...
72            $fields = get_object_vars( $row );
73            // Silly for loop over one field...
74            foreach ( $fields as $table ) {
75                // $old should be regexp safe ([a-zA-Z_])
76                $newTable = preg_replace( '/^' . $old . '/', $new, $table );
77                $this->output( "Renaming table $table to $newTable\n" );
78                $oldTableEnc = $dbw->addIdentifierQuotes( $table );
79                $newTableEnc = $dbw->addIdentifierQuotes( $newTable );
80                $dbw->query( "RENAME TABLE $oldTableEnc TO $newTableEnc", __METHOD__ );
81            }
82            $count++;
83        }
84        $this->output( "Done! [$count tables]\n" );
85    }
86}
87
88// @codeCoverageIgnoreStart
89$maintClass = RenameDbPrefix::class;
90require_once RUN_MAINTENANCE_IF_MAIN;
91// @codeCoverageIgnoreEnd