Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
RenameDbPrefix | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getDbType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 27 |
|
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 | |
26 | use MediaWiki\MainConfigNames; |
27 | use MediaWiki\Maintenance\Maintenance; |
28 | |
29 | // @codeCoverageIgnoreStart |
30 | require_once __DIR__ . '/Maintenance.php'; |
31 | // @codeCoverageIgnoreEnd |
32 | |
33 | /** |
34 | * Maintenance script that changes the prefix of database tables. |
35 | * |
36 | * @ingroup Maintenance |
37 | */ |
38 | class RenameDbPrefix extends Maintenance { |
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; |
102 | require_once RUN_MAINTENANCE_IF_MAIN; |
103 | // @codeCoverageIgnoreEnd |