[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/backup/util/plan/ -> backup_structure_step.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   * @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   * Abstract class defining the needed stuff to backup one @backup_structure
  27   *
  28   * TODO: Finish phpdocs
  29   */
  30  abstract class backup_structure_step extends backup_step {
  31  
  32      protected $filename; // Name of the file to be generated
  33      protected $contenttransformer; // xml content transformer being used
  34                                     // (need it here, apart from xml_writer,
  35                                     // thanks to serialized data to process -
  36                                     // say thanks to blocks!)
  37  
  38      /**
  39       * Constructor - instantiates one object of this class
  40       */
  41      public function __construct($name, $filename, $task = null) {
  42          if (!is_null($task) && !($task instanceof backup_task)) {
  43              throw new backup_step_exception('wrong_backup_task_specified');
  44          }
  45          $this->filename = $filename;
  46          $this->contenttransformer = null;
  47          parent::__construct($name, $task);
  48      }
  49  
  50      public function execute() {
  51  
  52          if (!$this->execute_condition()) { // Check any condition to execute this
  53              return;
  54          }
  55  
  56          $fullpath = $this->task->get_taskbasepath();
  57  
  58          // We MUST have one fullpath here, else, error
  59          if (empty($fullpath)) {
  60              throw new backup_step_exception('backup_structure_step_undefined_fullpath');
  61          }
  62  
  63          // Append the filename to the fullpath
  64          $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
  65  
  66          // Create output, transformer, writer, processor
  67          $xo = new file_xml_output($fullpath);
  68          $xt = null;
  69          if (class_exists('backup_xml_transformer')) {
  70              $xt = new backup_xml_transformer($this->get_courseid());
  71              $this->contenttransformer = $xt; // Save the reference to the transformer
  72                                               // as far as we are going to need it out
  73                                               // from xml_writer (blame serialized data!)
  74          }
  75          $xw = new xml_writer($xo, $xt);
  76          $progress = $this->task->get_progress();
  77          $progress->start_progress($this->get_name());
  78          $pr = new backup_structure_processor($xw, $progress);
  79  
  80          // Set processor variables from settings
  81          foreach ($this->get_settings() as $setting) {
  82              $pr->set_var($setting->get_name(), $setting->get_value());
  83          }
  84          // Add backupid as one more var for processor
  85          $pr->set_var(backup::VAR_BACKUPID, $this->get_backupid());
  86  
  87          // Get structure definition
  88          $structure = $this->define_structure();
  89          if (! $structure instanceof backup_nested_element) {
  90              throw new backup_step_exception('backup_structure_step_wrong_structure');
  91          }
  92  
  93          // Start writer
  94          $xw->start();
  95  
  96          // Process structure definition
  97          $structure->process($pr);
  98  
  99          // Get the results from the nested elements
 100          $results = $structure->get_results();
 101  
 102          // Get the log messages to append to the log
 103          $logs = $structure->get_logs();
 104          foreach ($logs as $log) {
 105              $this->log($log->message, $log->level, $log->a, $log->depth, $log->display);
 106          }
 107  
 108          // Close everything
 109          $xw->stop();
 110          $progress->end_progress();
 111  
 112          // Destroy the structure. It helps PHP 5.2 memory a lot!
 113          $structure->destroy();
 114  
 115          return $results;
 116      }
 117  
 118      /**
 119       * As far as backup structure steps are implementing backup_plugin stuff, they need to
 120       * have the parent task available for wrapping purposes (get course/context....)
 121       */
 122      public function get_task() {
 123          return $this->task;
 124      }
 125  
 126  // Protected API starts here
 127  
 128      /**
 129       * Add plugin structure to any element in the structure backup tree
 130       *
 131       * @param string $plugintype type of plugin as defined by core_component::get_plugin_types()
 132       * @param backup_nested_element $element element in the structure backup tree that
 133       *                                       we are going to add plugin information to
 134       * @param bool $multiple to define if multiple plugins can produce information
 135       *                       for each instance of $element (true) or no (false)
 136       */
 137      protected function add_plugin_structure($plugintype, $element, $multiple) {
 138  
 139          global $CFG;
 140  
 141          // Check the requested plugintype is a valid one
 142          if (!array_key_exists($plugintype, core_component::get_plugin_types($plugintype))) {
 143               throw new backup_step_exception('incorrect_plugin_type', $plugintype);
 144          }
 145  
 146          // Arrived here, plugin is correct, let's create the optigroup
 147          $optigroupname = $plugintype . '_' . $element->get_name() . '_plugin';
 148          $optigroup = new backup_optigroup($optigroupname, null, $multiple);
 149          $element->add_child($optigroup); // Add optigroup to stay connected since beginning
 150  
 151          // Get all the optigroup_elements, looking across all the plugin dirs
 152          $pluginsdirs = core_component::get_plugin_list($plugintype);
 153          foreach ($pluginsdirs as $name => $plugindir) {
 154              $classname = 'backup_' . $plugintype . '_' . $name . '_plugin';
 155              $backupfile = $plugindir . '/backup/moodle2/' . $classname . '.class.php';
 156              if (file_exists($backupfile)) {
 157                  require_once($backupfile);
 158                  $backupplugin = new $classname($plugintype, $name, $optigroup, $this);
 159                  // Add plugin returned structure to optigroup
 160                  $backupplugin->define_plugin_structure($element->get_name());
 161              }
 162          }
 163      }
 164  
 165      /**
 166       * To conditionally decide if one step will be executed or no
 167       *
 168       * For steps needing to be executed conditionally, based in dynamic
 169       * conditions (at execution time vs at declaration time) you must
 170       * override this function. It will return true if the step must be
 171       * executed and false if not
 172       */
 173      protected function execute_condition() {
 174          return true;
 175      }
 176  
 177      /**
 178       * Function that will return the structure to be processed by this backup_step.
 179       * Must return one backup_nested_element
 180       */
 181      abstract protected function define_structure();
 182  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1