patchSql.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Manually run an SQL patch outside of the general updaters.
  4. * This ensures that the DB options (charset, prefix, engine) are correctly set.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Maintenance
  23. */
  24. require_once __DIR__ . '/Maintenance.php';
  25. /**
  26. * Maintenance script that manually runs an SQL patch outside of the general updaters.
  27. *
  28. * @ingroup Maintenance
  29. */
  30. class PatchSql extends Maintenance {
  31. public function __construct() {
  32. parent::__construct();
  33. $this->addDescription( 'Run an SQL file into the DB, replacing prefix and charset vars' );
  34. $this->addArg(
  35. 'patch-name',
  36. 'Name of the patch file, either full path or in maintenance/archives'
  37. );
  38. }
  39. public function getDbType() {
  40. return Maintenance::DB_ADMIN;
  41. }
  42. public function execute() {
  43. $dbw = $this->getDB( DB_MASTER );
  44. $updater = DatabaseUpdater::newForDB( $dbw, true, $this );
  45. foreach ( $this->mArgs as $arg ) {
  46. $files = [
  47. $arg,
  48. $updater->patchPath( $dbw, $arg ),
  49. $updater->patchPath( $dbw, "patch-$arg.sql" ),
  50. ];
  51. foreach ( $files as $file ) {
  52. if ( file_exists( $file ) ) {
  53. $this->output( "$file ...\n" );
  54. $dbw->sourceFile( $file );
  55. continue 2;
  56. }
  57. }
  58. $this->error( "Could not find $arg\n" );
  59. }
  60. $this->output( "done.\n" );
  61. }
  62. }
  63. $maintClass = "PatchSql";
  64. require_once RUN_MAINTENANCE_IF_MAIN;