Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * General database mover class
  19   *
  20   * @package    core_dtl
  21   * @copyright  2008 Andrei Bautu
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  class database_mover extends database_exporter {
  28      /** @var database_importer Importer object used to transfer data. */
  29      protected $importer;
  30      /** @var progress_trace Progress tracing object */
  31      protected $feedback;
  32  
  33      /**
  34       * Object constructor.
  35       *
  36       * @param moodle_database $mdb_source Connection to the source database (a
  37       * @see moodle_database object).
  38       * @param moodle_database $mdb_target Connection to the target database (a
  39       * @see moodle_database object).
  40       * @param boolean $check_schema - whether or not to check that XML database
  41       * schema matches the RDBMS database schema before exporting (used by
  42       * @param progress_trace $feedback Progress tracing object
  43       * @see export_database).
  44       */
  45      public function __construct(moodle_database $mdb_source, moodle_database $mdb_target,
  46              $check_schema = true, progress_trace $feedback = null) {
  47          if (empty($feedback)) {
  48              $this->feedback = new null_progress_trace();
  49          } else {
  50              $this->feedback = $feedback;
  51          }
  52          if ($check_schema) {
  53              $this->feedback->output(get_string('checkingsourcetables', 'core_dbtransfer'));
  54          }
  55          parent::__construct($mdb_source, $check_schema);
  56          $this->feedback->output(get_string('creatingtargettables', 'core_dbtransfer'));
  57          $this->importer = new database_importer($mdb_target, $check_schema);
  58      }
  59  
  60      /**
  61       * How to use transactions during the transfer.
  62       * @param string $mode 'pertable', 'allinone' or 'none'.
  63       */
  64      public function set_transaction_mode($mode) {
  65          $this->importer->set_transaction_mode($mode);
  66      }
  67  
  68      /**
  69       * Callback function. Calls importer's begin_database_import callback method.
  70       *
  71       * @param float $version the version of the system which generating the data
  72       * @param string $release moodle release info
  73       * @param string $timestamp the timestamp of the data (in ISO 8601) format.
  74       * @param string $description a user description of the data.
  75       * @return void
  76       */
  77      public function begin_database_export($version, $release, $timestamp, $description) {
  78          $this->feedback->output(get_string('copyingtables', 'core_dbtransfer'));
  79          $this->importer->begin_database_import($version, $timestamp, $description);
  80      }
  81  
  82      /**
  83       * Callback function. Calls importer's begin_table_import callback method.
  84       *
  85       * @param xmldb_table $table - XMLDB object for the exported table
  86       * @return void
  87       */
  88      public function begin_table_export(xmldb_table $table) {
  89          $this->feedback->output(get_string('copyingtable', 'core_dbtransfer', $table->getName()), 1);
  90          $this->importer->begin_table_import($table->getName(), $table->getHash());
  91      }
  92  
  93      /**
  94       * Callback function. Calls importer's import_table_data callback method.
  95       *
  96       * @param xmldb_table $table - XMLDB object of the table from which data
  97       * was retrieved
  98       * @param object $data - data object (fields and values from record)
  99       * @return void
 100       */
 101      public function export_table_data(xmldb_table $table, $data) {
 102          $this->importer->import_table_data($table->getName(), $data);
 103      }
 104  
 105      /**
 106       * Callback function. Calls importer's finish_table_import callback method.
 107       * @param xmldb_table $table - XMLDB object for the exported table
 108       * @return void
 109       */
 110      public function finish_table_export(xmldb_table $table) {
 111          $this->feedback->output(get_string('done', 'core_dbtransfer', $table->getName()), 2);
 112          $this->importer->finish_table_import($table->getName());
 113      }
 114  
 115      /**
 116       * Callback function. Calls importer's finish_database_import callback method.
 117       * @return void
 118       */
 119      public function finish_database_export() {
 120          $this->importer->finish_database_import();
 121      }
 122  }