Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package moodlecore
  20   * @subpackage backup-plan
  21   * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Implementable class defining the needed stuf for one restore plan
  27   *
  28   * TODO: Finish phpdocs
  29   */
  30  class restore_plan extends base_plan implements loggable {
  31  
  32      /**
  33       *
  34       * @var restore_controller
  35       */
  36      protected $controller; // The restore controller building/executing this plan
  37      protected $basepath;   // Fullpath to dir where backup is available
  38      protected $preloaded;  // When executing the plan, do we have preloaded (from checks) info
  39      protected $decoder;    // restore_decode_processor in charge of decoding all the interlinks
  40      protected $missingmodules; // to flag if restore has detected some missing module
  41      protected $excludingdactivities; // to flag if restore settings are excluding any activity
  42  
  43      /**
  44       * Constructor - instantiates one object of this class
  45       */
  46      public function __construct($controller) {
  47          global $CFG;
  48  
  49          if (! $controller instanceof restore_controller) {
  50              throw new restore_plan_exception('wrong_restore_controller_specified');
  51          }
  52          $backuptempdir    = make_backup_temp_directory('');
  53          $this->controller = $controller;
  54          $this->basepath   = $backuptempdir . '/' . $controller->get_tempdir();
  55          $this->preloaded  = false;
  56          $this->decoder    = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot);
  57          $this->missingmodules = false;
  58          $this->excludingdactivities = false;
  59  
  60          parent::__construct('restore_plan');
  61      }
  62  
  63      /**
  64       * Destroy all circular references. It helps PHP 5.2 a lot!
  65       */
  66      public function destroy() {
  67          // No need to destroy anything recursively here, direct reset
  68          $this->controller = null;
  69          // Delegate to base plan the rest
  70          parent::destroy();
  71      }
  72  
  73      public function build() {
  74          restore_plan_builder::build_plan($this->controller); // We are moodle2 always, go straight to builder
  75          $this->built = true;
  76      }
  77  
  78      public function get_restoreid() {
  79          return $this->controller->get_restoreid();
  80      }
  81  
  82      public function get_courseid() {
  83          return $this->controller->get_courseid();
  84      }
  85  
  86      public function get_mode() {
  87          return $this->controller->get_mode();
  88      }
  89  
  90      public function get_basepath() {
  91          return $this->basepath;
  92      }
  93  
  94      public function get_logger() {
  95          return $this->controller->get_logger();
  96      }
  97  
  98      /**
  99       * Gets the progress reporter, which can be used to report progress within
 100       * the backup or restore process.
 101       *
 102       * @return \core\progress\base Progress reporting object
 103       */
 104      public function get_progress() {
 105          return $this->controller->get_progress();
 106      }
 107  
 108      public function get_info() {
 109          return $this->controller->get_info();
 110      }
 111  
 112      public function get_target() {
 113          return $this->controller->get_target();
 114      }
 115  
 116      public function get_userid() {
 117          return $this->controller->get_userid();
 118      }
 119  
 120      public function get_decoder() {
 121          return $this->decoder;
 122      }
 123  
 124      public function is_samesite() {
 125          return $this->controller->is_samesite();
 126      }
 127  
 128      public function is_missing_modules() {
 129          return $this->missingmodules;
 130      }
 131  
 132      public function is_excluding_activities() {
 133          return $this->excludingdactivities;
 134      }
 135  
 136      public function set_preloaded_information() {
 137          $this->preloaded = true;
 138      }
 139  
 140      public function get_preloaded_information() {
 141          return $this->preloaded;
 142      }
 143  
 144      public function get_tempdir() {
 145          return $this->controller->get_tempdir();
 146      }
 147  
 148      public function set_missing_modules() {
 149          $this->missingmodules = true;
 150      }
 151  
 152      public function set_excluding_activities() {
 153          $this->excludingdactivities = true;
 154      }
 155  
 156      public function log($message, $level, $a = null, $depth = null, $display = false) {
 157          backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
 158      }
 159  
 160      /**
 161       * Function responsible for executing the tasks of any plan
 162       */
 163      public function execute() {
 164          if ($this->controller->get_status() != backup::STATUS_AWAITING) {
 165              throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
 166          }
 167          $this->controller->set_status(backup::STATUS_EXECUTING);
 168          parent::execute();
 169          $this->controller->set_status(backup::STATUS_FINISHED_OK);
 170  
 171          // Check if we are restoring a course.
 172          if ($this->controller->get_type() === backup::TYPE_1COURSE) {
 173  
 174              // Check to see if we are on the same site to pass original course info.
 175              $issamesite = $this->controller->is_samesite();
 176  
 177              $otherarray = array('type' => $this->controller->get_type(),
 178                                  'target' => $this->controller->get_target(),
 179                                  'mode' => $this->controller->get_mode(),
 180                                  'operation' => $this->controller->get_operation(),
 181                                  'samesite' => $issamesite
 182              );
 183  
 184              if ($this->controller->is_samesite()) {
 185                  $otherarray['originalcourseid'] = $this->controller->get_info()->original_course_id;
 186              }
 187  
 188              // Trigger a course restored event.
 189              $event = \core\event\course_restored::create(array(
 190                  'objectid' => $this->get_courseid(),
 191                  'userid' => $this->get_userid(),
 192                  'context' => context_course::instance($this->get_courseid()),
 193                  'other' => $otherarray
 194              ));
 195              $event->trigger();
 196          }
 197      }
 198  
 199      /**
 200       * Execute the after_restore methods of all the executed tasks in the plan
 201       */
 202      public function execute_after_restore() {
 203          // Simply iterate over each task in the plan and delegate to them the execution
 204          $progress = $this->get_progress();
 205          $progress->start_progress($this->get_name() .
 206                  ': executing execute_after_restore for all tasks', count($this->tasks));
 207  
 208          /** @var base_task $task */
 209          foreach ($this->tasks as $task) {
 210              $task->execute_after_restore();
 211              $progress->increment_progress();
 212          }
 213          $progress->end_progress();
 214      }
 215  
 216      /**
 217       * Compares the provided moodle version with the one the backup was taken from.
 218       *
 219       * @param int $version Moodle version number (YYYYMMDD or YYYYMMDDXX)
 220       * @param string $operator Operator to compare the provided version to the backup version. {@see version_compare()}
 221       * @return bool True if the comparison passes.
 222       */
 223      public function backup_version_compare(int $version, string $operator): bool {
 224          preg_match('/(\d{' . strlen($version) . '})/', $this->get_info()->moodle_version, $matches);
 225          $backupbuild = (int)$matches[1];
 226          return version_compare($backupbuild, $version, $operator);
 227      }
 228  
 229      /**
 230       * Compares the provided moodle release with the one the backup was taken from.
 231       *
 232       * @param string $release Moodle release (X.Y or X.Y.Z)
 233       * @param string $operator Operator to compare the provided release to the backup release. {@see version_compare()}
 234       * @return bool True if the comparison passes.
 235       */
 236      public function backup_release_compare(string $release, string $operator): bool {
 237          return version_compare($this->get_info()->backup_release, $release, $operator);
 238      }
 239  }
 240  
 241  /*
 242   * Exception class used by all the @restore_plan stuff
 243   */
 244  class restore_plan_exception extends base_plan_exception {
 245  
 246      public function __construct($errorcode, $a=NULL, $debuginfo=null) {
 247          parent::__construct($errorcode, $a, $debuginfo);
 248      }
 249  }