Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Transfer tool
  19   *
  20   * @package    tool_dbtransfer
  21   * @copyright  2008 Petr Skoda {@link http://skodak.org/}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('NO_OUTPUT_BUFFERING', true);
  26  
  27  require('../../../config.php');
  28  require_once ('locallib.php');
  29  require_once ('database_transfer_form.php');
  30  
  31  admin_externalpage_setup('tooldbtransfer');
  32  
  33  // Create the form.
  34  $form = new database_transfer_form();
  35  $problem = '';
  36  
  37  // If we have valid input.
  38  if ($data = $form->get_data()) {
  39      // Connect to the other database.
  40      list($dbtype, $dblibrary) = explode('/', $data->driver);
  41      $targetdb = moodle_database::get_driver_instance($dbtype, $dblibrary);
  42      $dboptions = array();
  43      if ($data->dbport) {
  44          $dboptions['dbport'] = $data->dbport;
  45      }
  46      if ($data->dbsocket) {
  47          $dboptions['dbsocket'] = $data->dbsocket;
  48      }
  49      try {
  50          $targetdb->connect($data->dbhost, $data->dbuser, $data->dbpass, $data->dbname, $data->prefix, $dboptions);
  51          if ($targetdb->get_tables()) {
  52              $problem .= get_string('targetdatabasenotempty', 'tool_dbtransfer');
  53          }
  54      } catch (moodle_exception $e) {
  55          $problem .= get_string('notargetconectexception', 'tool_dbtransfer').'<br />'.$e->debuginfo;
  56      }
  57  
  58      if ($problem === '') {
  59          // Scroll down to the bottom when finished.
  60          $PAGE->requires->js_init_code("window.scrollTo(0, 5000000);");
  61  
  62          // Enable CLI maintenance mode if requested.
  63          if ($data->enablemaintenance) {
  64              $PAGE->set_pagelayout('maintenance');
  65              tool_dbtransfer_create_maintenance_file();
  66          }
  67  
  68          // Start output.
  69          echo $OUTPUT->header();
  70          $data->dbtype = $dbtype;
  71          $data->dbtypefrom = $CFG->dbtype;
  72          echo $OUTPUT->heading(get_string('transferringdbto', 'tool_dbtransfer', $data));
  73  
  74          // Do the transfer.
  75          $CFG->tool_dbransfer_migration_running = true;
  76          try {
  77              $feedback = new html_list_progress_trace();
  78              tool_dbtransfer_transfer_database($DB, $targetdb, $feedback);
  79              $feedback->finished();
  80          } catch (Exception $e) {
  81              if ($data->enablemaintenance) {
  82                  tool_dbtransfer_maintenance_callback();
  83              }
  84              unset($CFG->tool_dbransfer_migration_running);
  85              throw $e;
  86          }
  87          unset($CFG->tool_dbransfer_migration_running);
  88  
  89          // Finish up.
  90          echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
  91          echo $OUTPUT->continue_button("$CFG->wwwroot/$CFG->admin/");
  92          echo $OUTPUT->footer();
  93          die;
  94      }
  95  }
  96  
  97  // Otherwise display the settings form.
  98  echo $OUTPUT->header();
  99  echo $OUTPUT->heading(get_string('transferdbtoserver', 'tool_dbtransfer'));
 100  
 101  $info = format_text(get_string('transferdbintro', 'tool_dbtransfer'), FORMAT_MARKDOWN);
 102  echo $OUTPUT->box($info);
 103  
 104  $form->display();
 105  if ($problem !== '') {
 106      echo $OUTPUT->box($problem, 'generalbox error');
 107  }
 108  echo $OUTPUT->footer();