Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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-structure
  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   * TODO: Finish phpdocs
  25   */
  26  
  27  /**
  28   * Class representing one path to be restored from XML file
  29   */
  30  class restore_path_element {
  31  
  32      /** @var string name of the element */
  33      private $name;
  34  
  35      /** @var string path within the XML file this element will handle */
  36      private $path;
  37  
  38      /** @var bool flag to define if this element will get child ones grouped or no */
  39      private $grouped;
  40  
  41      /** @var object object instance in charge of processing this element. */
  42      private $pobject;
  43  
  44      /** @var mixed last data read for this element or returned data by processing method */
  45      private $data;
  46  
  47      /**
  48       * Constructor - instantiates one restore_path_element, specifying its basic info.
  49       *
  50       * @param string $name name of the thing being restored. This determines the name of the process_... method called.
  51       * @param string $path path of the element.
  52       * @param bool $grouped to gather information in grouped mode or no.
  53       */
  54      public function __construct($name, $path, $grouped = false) {
  55  
  56          $this->validate_name($name); // Check name
  57  
  58          $this->name = $name;
  59          $this->path = $path;
  60          $this->grouped = $grouped;
  61          $this->pobject = null;
  62          $this->data = null;
  63      }
  64  
  65      protected function validate_name($name) {
  66          // Validate various name constraints, throwing exception if needed
  67          if (empty($name)) {
  68              throw new restore_path_element_exception('restore_path_element_emptyname', $name);
  69          }
  70          if (preg_replace('/\s/', '', $name) != $name) {
  71              throw new restore_path_element_exception('restore_path_element_whitespace', $name);
  72          }
  73          if (preg_replace('/[^\x30-\x39\x41-\x5a\x5f\x61-\x7a]/', '', $name) != $name) {
  74              throw new restore_path_element_exception('restore_path_element_notasciiname', $name);
  75          }
  76      }
  77  
  78      protected function validate_pobject($pobject) {
  79          if (!is_object($pobject)) {
  80              throw new restore_path_element_exception('restore_path_element_noobject', $pobject);
  81          }
  82          if (!method_exists($pobject, $this->get_processing_method())) {
  83              throw new restore_path_element_exception('restore_path_element_missingmethod', $this->get_processing_method());
  84          }
  85      }
  86  
  87  
  88  /// Public API starts here
  89  
  90      public function set_processing_object($pobject) {
  91          $this->validate_pobject($pobject);
  92          $this->pobject = $pobject;
  93      }
  94  
  95      public function set_data($data) {
  96          $this->data = $data;
  97      }
  98      public function get_name() {
  99          return $this->name;
 100      }
 101  
 102      public function get_path() {
 103          return $this->path;
 104      }
 105  
 106      public function is_grouped() {
 107          return $this->grouped;
 108      }
 109  
 110      public function get_processing_object() {
 111          return $this->pobject;
 112      }
 113  
 114      public function get_processing_method() {
 115          return 'process_' . $this->name;
 116      }
 117  
 118      public function get_data() {
 119          return $this->data;
 120      }
 121  }
 122  
 123  /**
 124   * restore_path_element exception class
 125   */
 126  class restore_path_element_exception extends moodle_exception {
 127  
 128      /**
 129       * Constructor - instantiates one restore_path_element_exception
 130       *
 131       * @param string $errorcode key for the corresponding error string
 132       * @param object $a extra words and phrases that might be required in the error string
 133       * @param string $debuginfo optional debugging information
 134       */
 135      public function __construct($errorcode, $a = null, $debuginfo = null) {
 136          parent::__construct($errorcode, '', '', $a, $debuginfo);
 137      }
 138  }