renameDbPrefix.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. require_once __DIR__ . '/Maintenance.php';
  26. /**
  27. * Maintenance script that changes the prefix of database tables.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class RenameDbPrefix extends Maintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addOption( "old", "Old db prefix [0 for none]", true, true );
  35. $this->addOption( "new", "New db prefix [0 for none]", true, true );
  36. }
  37. public function getDbType() {
  38. return Maintenance::DB_ADMIN;
  39. }
  40. public function execute() {
  41. global $wgDBname;
  42. // Allow for no old prefix
  43. if ( $this->getOption( 'old', 0 ) === '0' ) {
  44. $old = '';
  45. } else {
  46. // Use nice safe, sane, prefixes
  47. preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'old' ), $m );
  48. $old = isset( $m[0] ) ? $m[0] : false;
  49. }
  50. // Allow for no new prefix
  51. if ( $this->getOption( 'new', 0 ) === '0' ) {
  52. $new = '';
  53. } else {
  54. // Use nice safe, sane, prefixes
  55. preg_match( '/^[a-zA-Z]+_$/', $this->getOption( 'new' ), $m );
  56. $new = isset( $m[0] ) ? $m[0] : false;
  57. }
  58. if ( $old === false || $new === false ) {
  59. $this->error( "Invalid prefix!", true );
  60. }
  61. if ( $old === $new ) {
  62. $this->output( "Same prefix. Nothing to rename!\n", true );
  63. }
  64. $this->output( "Renaming DB prefix for tables of $wgDBname from '$old' to '$new'\n" );
  65. $count = 0;
  66. $dbw = $this->getDB( DB_MASTER );
  67. $res = $dbw->query( "SHOW TABLES " . $dbw->buildLike( $old, $dbw->anyString() ) );
  68. foreach ( $res as $row ) {
  69. // XXX: odd syntax. MySQL outputs an oddly cased "Tables of X"
  70. // sort of message. Best not to try $row->x stuff...
  71. $fields = get_object_vars( $row );
  72. // Silly for loop over one field...
  73. foreach ( $fields as $table ) {
  74. // $old should be regexp safe ([a-zA-Z_])
  75. $newTable = preg_replace( '/^' . $old . '/', $new, $table );
  76. $this->output( "Renaming table $table to $newTable\n" );
  77. $dbw->query( "RENAME TABLE $table TO $newTable" );
  78. }
  79. $count++;
  80. }
  81. $this->output( "Done! [$count tables]\n" );
  82. }
  83. }
  84. $maintClass = "RenameDbPrefix";
  85. require_once RUN_MAINTENANCE_IF_MAIN;