| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
|
| 28 |
|
| 29 |
require_once __DIR__ . '/Maintenance.php'; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 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 ); |
| 42 |
} |
| 43 |
|
| 44 |
public function getDbType() { |
| 45 |
return Maintenance::DB_ADMIN; |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$dbName = $this->getConfig()->get( MainConfigNames::DBname ); |
| 50 |
|
| 51 |
|
| 52 |
if ( $this->getOption( 'old', 0 ) === '0' ) { |
| 53 |
$old = ''; |
| 54 |
} else { |
| 55 |
|
| 56 |
preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m ); |
| 57 |
$old = $m[0] ?? false; |
| 58 |
} |
| 59 |
|
| 60 |
if ( $this->getOption( 'new', 0 ) === '0' ) { |
| 61 |
$new = ''; |
| 62 |
} else { |
| 63 |
|
| 64 |
preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m ); |
| 65 |
$new = $m[0] ?? false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( $old === false || $new === false ) { |
| 69 |
$this->fatalError( "Invalid prefix!" ); |
| 70 |
} |
| 71 |
if ( $old === $new ) { |
| 72 |
$this->output( "Same prefix. Nothing to rename!\n", true ); |
| 73 |
} |
| 74 |
|
| 75 |
$this->output( "Renaming DB prefix for tables of $dbName from '$old' to '$new'\n" ); |
| 76 |
$count = 0; |
| 77 |
|
| 78 |
$dbw = $this->getPrimaryDB(); |
| 79 |
$res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ), __METHOD__ ); |
| 80 |
foreach ( $res as $row ) { |
| 81 |
|
| 82 |
|
| 83 |
$fields = get_object_vars( $row ); |
| 84 |
|
| 85 |
foreach ( $fields as $table ) { |
| 86 |
|
| 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__ ); |
| 92 |
} |
| 93 |
$count++; |
| 94 |
} |
| 95 |
$this->output( "Done! [$count tables]\n" ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
$maintClass = RenameDbPrefix::class; |
| 101 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 102 |
|