[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/moodle2/ -> restore_subplugin.class.php (source)

   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   * Defines restore_subplugin class
  20   *
  21   * @package     core_backup
  22   * @subpackage  moodle2
  23   * @category    backup
  24   * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  25   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class implementing the subplugins support for moodle2 restore
  32   *
  33   * TODO: Finish phpdocs
  34   * TODO: Make this subclass of restore_plugin
  35   * TODO: Add support for declaring decode_contents (not decode_rules)
  36   */
  37  abstract class restore_subplugin {
  38  
  39      protected $subplugintype;
  40      protected $subpluginname;
  41      protected $connectionpoint;
  42      protected $step;
  43      protected $task;
  44  
  45      public function __construct($subplugintype, $subpluginname, $step) {
  46          $this->subplugintype = $subplugintype;
  47          $this->subpluginname = $subpluginname;
  48          $this->step          = $step;
  49          $this->task          = $step->get_task();
  50          $this->connectionpoint = '';
  51      }
  52  
  53      public function define_subplugin_structure($connectionpoint) {
  54          if (!$connectionpoint instanceof restore_path_element) {
  55              throw new restore_step_exception('restore_path_element_required', $connectionpoint);
  56          }
  57  
  58          $paths = array();
  59          $this->connectionpoint = $connectionpoint;
  60          $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_subplugin_structure';
  61  
  62          if (method_exists($this, $methodname)) {
  63              if ($subbluginpaths = $this->$methodname()) {
  64                  foreach ($subbluginpaths as $path) {
  65                      $path->set_processing_object($this);
  66                      $paths[] = $path;
  67                  }
  68              }
  69          }
  70          return $paths;
  71      }
  72  
  73      /**
  74       * after_execute dispatcher for any restore_subplugin class
  75       *
  76       * This method will dispatch execution to the corresponding
  77       * after_execute_xxx() method when available, with xxx
  78       * being the connection point of the instance, so subplugin
  79       * classes with multiple connection points will support
  80       * multiple after_execute methods, one for each connection point
  81       */
  82      public function launch_after_execute_methods() {
  83          // Check if the after_execute method exists and launch it
  84          $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path());
  85          if (method_exists($this, $afterexecute)) {
  86              $this->$afterexecute();
  87          }
  88      }
  89  
  90  // Protected API starts here
  91  
  92  // restore_step/structure_step/task wrappers
  93  
  94      protected function get_restoreid() {
  95          if (is_null($this->task)) {
  96              throw new restore_step_exception('not_specified_restore_task');
  97          }
  98          return $this->task->get_restoreid();
  99      }
 100  
 101      /**
 102       * To send ids pairs to backup_ids_table and to store them into paths
 103       *
 104       * This method will send the given itemname and old/new ids to the
 105       * backup_ids_temp table, and, at the same time, will save the new id
 106       * into the corresponding restore_path_element for easier access
 107       * by children. Also will inject the known old context id for the task
 108       * in case it's going to be used for restoring files later
 109       */
 110      protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
 111          $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid);
 112      }
 113  
 114      /**
 115       * Returns the latest (parent) old id mapped by one pathelement
 116       */
 117      protected function get_old_parentid($itemname) {
 118          return $this->step->get_old_parentid($itemname);
 119      }
 120  
 121      /**
 122       * Returns the latest (parent) new id mapped by one pathelement
 123       */
 124      protected function get_new_parentid($itemname) {
 125          return $this->step->get_new_parentid($itemname);
 126      }
 127  
 128      /**
 129       * Return the new id of a mapping for the given itemname
 130       *
 131       * @param string $itemname the type of item
 132       * @param int $oldid the item ID from the backup
 133       * @param mixed $ifnotfound what to return if $oldid wasnt found. Defaults to false
 134       */
 135      protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
 136          return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
 137      }
 138  
 139      /**
 140       * Return the complete mapping from the given itemname, itemid
 141       */
 142      protected function get_mapping($itemname, $oldid) {
 143          return $this->step->get_mapping($itemname, $oldid);
 144      }
 145  
 146      /**
 147       * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
 148       */
 149      protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
 150          $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
 151      }
 152  
 153      /**
 154       * Apply course startdate offset based in original course startdate and course_offset_startdate setting
 155       * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
 156       * executions in the same request
 157       */
 158      protected function apply_date_offset($value) {
 159          return $this->step->apply_date_offset($value);
 160      }
 161  
 162      /**
 163       * Call the log function from the step.
 164       */
 165      public function log($message, $level, $a = null, $depth = null, $display = false) {
 166          return $this->step->log($message, $level, $a, $depth, $display);
 167      }
 168  
 169      /**
 170       * Returns the value of one (task/plan) setting
 171       */
 172      protected function get_setting_value($name) {
 173          if (is_null($this->task)) {
 174              throw new restore_step_exception('not_specified_restore_task');
 175          }
 176          return $this->task->get_setting_value($name);
 177      }
 178  
 179  // end of restore_step/structure_step/task wrappers
 180  
 181      /**
 182       * Simple helper function that returns the name for the restore_path_element
 183       * It's not mandatory to use it but recommended ;-)
 184       */
 185      protected function get_namefor($name = '') {
 186          $name = $name !== '' ? '_' . $name : '';
 187          return $this->subplugintype . '_' . $this->subpluginname . $name;
 188      }
 189  
 190      /**
 191       * Simple helper function that returns the base (prefix) of the path for the restore_path_element
 192       * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
 193       */
 194      protected function get_pathfor($path = '') {
 195          $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
 196          return $this->connectionpoint->get_path() . '/' .
 197                 'subplugin_' . $this->subplugintype . '_' .
 198                 $this->subpluginname . '_' . basename($this->connectionpoint->get_path()) . $path;
 199      }
 200  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1