3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 * Abstract class defining the needed stuff to restore one xml file
28 * TODO: Finish phpdocs
30 abstract class restore_structure_step extends restore_step {
32 protected $filename; // Name of the file to be parsed
33 protected $contentprocessor; // xml parser processor being used
34 // (need it here, apart from parser
35 // thanks to serialized data to process -
36 // say thanks to blocks!)
37 protected $pathelements; // Array of pathelements to process
38 protected $elementsoldid; // Array to store last oldid used on each element
39 protected $elementsnewid; // Array to store last newid used on each element
42 * Constructor - instantiates one object of this class
44 public function __construct($name, $filename, $task = null) {
45 if (!is_null($task) && !($task instanceof restore_task)) {
46 throw new restore_step_exception('wrong_restore_task_specified');
48 $this->filename = $filename;
49 $this->contentprocessor = null;
50 $this->pathelements = array();
51 $this->elementsoldid = array();
52 $this->elementsnewid = array();
53 parent::__construct($name, $task);
56 public function execute() {
58 if (!$this->execute_condition()) { // Check any condition to execute this
62 $fullpath = $this->task->get_taskbasepath();
64 // We MUST have one fullpath here, else, error
65 if (empty($fullpath)) {
66 throw new restore_step_exception('restore_structure_step_undefined_fullpath');
69 // Append the filename to the fullpath
70 $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
73 if (!file_exists($fullpath)) { // Shouldn't happen ever, but...
74 throw new restore_step_exception('missing_moodle_backup_xml_file', $fullpath);
77 // Get restore_path elements array adapting and preparing it for processing
78 $structure = $this->define_structure();
79 if (!is_array($structure)) {
80 throw new restore_step_exception('restore_step_structure_not_array', $this->get_name());
82 $this->prepare_pathelements($structure);
84 // Create parser and processor
85 $xmlparser = new progressive_parser();
86 $xmlparser->set_file($fullpath);
87 $xmlprocessor = new restore_structure_parser_processor($this->task->get_courseid(), $this);
88 $this->contentprocessor = $xmlprocessor; // Save the reference to the contentprocessor
89 // as far as we are going to need it out
90 // from parser (blame serialized data!)
91 $xmlparser->set_processor($xmlprocessor);
93 // Add pathelements to processor
94 foreach ($this->pathelements as $element) {
95 $xmlprocessor->add_path($element->get_path(), $element->is_grouped());
98 // And process it, dispatch to target methods in step will start automatically
99 $xmlparser->process();
101 // Have finished, launch the after_execute method of all the processing objects
102 $this->launch_after_execute_methods();
106 * Receive one chunk of information form the xml parser processor and
107 * dispatch it, following the naming rules
109 public function process($data) {
110 if (!array_key_exists($data['path'], $this->pathelements)) { // Incorrect path, must not happen
111 throw new restore_step_exception('restore_structure_step_missing_path', $data['path']);
113 $element = $this->pathelements[$data['path']];
114 $object = $element->get_processing_object();
115 $method = $element->get_processing_method();
116 if (empty($object)) { // No processing object defined
117 throw new restore_step_exception('restore_structure_step_missing_pobject', $object);
119 $rdata = $object->$method($data['tags']); // Dispatch to proper object/method
120 if ($rdata !== null) { // If the method has returned any info, set element data to it
121 $element->set_data($rdata);
122 } else { // Else, put the original parsed data
123 $element->set_data($data);
128 * To send ids pairs to backup_ids_table and to store them into paths
130 * This method will send the given itemname and old/new ids to the
131 * backup_ids_temp table, and, at the same time, will save the new id
132 * into the corresponding restore_path_element for easier access
133 * by children. Also will inject the known old context id for the task
134 * in case it's going to be used for restoring files later
136 public function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
137 if ($restorefiles && $parentid) {
138 throw new restore_step_exception('set_mapping_cannot_specify_both_restorefiles_and_parentitemid');
140 // If we haven't specified one context for the files, use the task one
141 if (is_null($filesctxid)) {
142 $parentitemid = $restorefiles ? $this->task->get_old_contextid() : null;
143 } else { // Use the specified one
144 $parentitemid = $restorefiles ? $filesctxid : null;
146 // We have passed one explicit parentid, apply it
147 $parentitemid = !is_null($parentid) ? $parentid : $parentitemid;
149 // Let's call the low level one
150 restore_dbops::set_backup_ids_record($this->get_restoreid(), $itemname, $oldid, $newid, $parentitemid);
151 // Now, if the itemname matches any pathelement->name, store the latest $newid
152 if (array_key_exists($itemname, $this->elementsoldid)) { // If present in $this->elementsoldid, is valid, put both ids
153 $this->elementsoldid[$itemname] = $oldid;
154 $this->elementsnewid[$itemname] = $newid;
159 * Returns the latest (parent) old id mapped by one pathelement
161 public function get_old_parentid($itemname) {
162 return array_key_exists($itemname, $this->elementsoldid) ? $this->elementsoldid[$itemname] : null;
166 * Returns the latest (parent) new id mapped by one pathelement
168 public function get_new_parentid($itemname) {
169 return array_key_exists($itemname, $this->elementsnewid) ? $this->elementsnewid[$itemname] : null;
173 * Return the new id of a mapping for the given itemname
176 public function get_mappingid($itemname, $oldid) {
177 $mapping = $this->get_mapping($itemname, $oldid);
178 return $mapping ? $mapping->newitemid : false;
182 * Return the complete mapping from the given itemname, itemid
184 public function get_mapping($itemname, $oldid) {
185 return restore_dbops::get_backup_ids_record($this->get_restoreid(), $itemname, $oldid);
189 * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
191 public function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
192 $filesctxid = is_null($filesctxid) ? $this->task->get_old_contextid() : $filesctxid;
193 restore_dbops::send_files_to_pool($this->get_basepath(), $this->get_restoreid(), $component,
194 $filearea, $filesctxid, $this->task->get_userid(), $mappingitemname, $olditemid);
198 * Apply course startdate offset based in original course startdate and course_offset_startdate setting
199 * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
200 * executions in the same request
202 public function apply_date_offset($value) {
204 // empties don't offset - zeros (int and string), false and nulls return original value
209 static $cache = array();
211 if (isset($cache[$this->get_restoreid()])) {
212 return $value + $cache[$this->get_restoreid()];
214 // No cache, let's calculate the offset
215 $original = $this->task->get_info()->original_course_startdate;
217 if ($this->setting_exists('course_startdate')) { // Seting may not exist (MDL-25019)
218 $setting = $this->get_setting_value('course_startdate');
221 // Original course has not startdate or setting doesn't exist, offset = 0
222 if (empty($original) || empty($setting)) {
223 $cache[$this->get_restoreid()] = 0;
225 // Less than 24h of difference, offset = 0 (this avoids some problems with timezones)
226 } else if (abs($setting - $original) < 24 * 60 * 60) {
227 $cache[$this->get_restoreid()] = 0;
229 // Re-enforce 'moodle/restore:rolldates' capability for the user in the course, just in case
230 } else if (!has_capability('moodle/restore:rolldates',
231 get_context_instance(CONTEXT_COURSE, $this->get_courseid()),
232 $this->task->get_userid())) {
233 $cache[$this->get_restoreid()] = 0;
235 // Arrived here, let's calculate the real offset
237 $cache[$this->get_restoreid()] = $setting - $original;
240 // Return the passed value with cached offset applied
241 return $value + $cache[$this->get_restoreid()];
245 * As far as restore structure steps are implementing restore_plugin stuff, they need to
246 * have the parent task available for wrapping purposes (get course/context....)
248 public function get_task() {
252 // Protected API starts here
255 * Add plugin structure to any element in the structure restore tree
257 * @param string $plugintype type of plugin as defined by get_plugin_types()
258 * @param restore_path_element $element element in the structure restore tree that
259 * we are going to add plugin information to
261 protected function add_plugin_structure($plugintype, $element) {
265 // Check the requested plugintype is a valid one
266 if (!array_key_exists($plugintype, get_plugin_types($plugintype))) {
267 throw new restore_step_exception('incorrect_plugin_type', $plugintype);
270 // Get all the restore path elements, looking across all the plugin dirs
271 $pluginsdirs = get_plugin_list($plugintype);
272 foreach ($pluginsdirs as $name => $pluginsdir) {
273 // We need to add also backup plugin classes on restore, they may contain
274 // some stuff used both in backup & restore
275 $backupclassname = 'backup_' . $plugintype . '_' . $name . '_plugin';
276 $backupfile = $pluginsdir . '/backup/moodle2/' . $backupclassname . '.class.php';
277 if (file_exists($backupfile)) {
278 require_once($backupfile);
280 // Now add restore plugin classes and prepare stuff
281 $restoreclassname = 'restore_' . $plugintype . '_' . $name . '_plugin';
282 $restorefile = $pluginsdir . '/backup/moodle2/' . $restoreclassname . '.class.php';
283 if (file_exists($restorefile)) {
284 require_once($restorefile);
285 $restoreplugin = new $restoreclassname($plugintype, $name, $this);
286 // Add plugin paths to the step
287 $this->prepare_pathelements($restoreplugin->define_plugin_structure($element));
293 * Launch all the after_execute methods present in all the processing objects
295 * This method will launch all the after_execute methods that can be defined
296 * both in restore_plugin and restore_structure_step classes
298 * For restore_plugin classes the name of the method to be executed will be
299 * "after_execute_" + connection point (as far as can be multiple connection
300 * points in the same class)
302 * For restore_structure_step classes is will be, simply, "after_execute". Note
303 * that this is executed *after* the plugin ones
305 protected function launch_after_execute_methods() {
306 $alreadylaunched = array(); // To avoid multiple executions
307 foreach ($this->pathelements as $key => $pathelement) {
308 // Get the processing object
309 $pobject = $pathelement->get_processing_object();
310 // Skip null processors (child of grouped ones for sure)
311 if (is_null($pobject)) {
314 // Skip restore structure step processors (this)
315 if ($pobject instanceof restore_structure_step) {
318 // Skip already launched processing objects
319 if (in_array($pobject, $alreadylaunched, true)) {
322 // Add processing object to array of launched ones
323 $alreadylaunched[] = $pobject;
324 // If the processing object has support for
325 // launching after_execute methods, use it
326 if (method_exists($pobject, 'launch_after_execute_methods')) {
327 $pobject->launch_after_execute_methods();
330 // Finally execute own (restore_structure_step) after_execute method
331 $this->after_execute();
336 * This method will be executed after the whole structure step have been processed
338 * After execution method for code needed to be executed after the whole structure
339 * has been processed. Useful for cleaning tasks, files process and others. Simply
340 * overwrite in in your steps if needed
342 protected function after_execute() {
343 // do nothing by default
347 * Prepare the pathelements for processing, looking for duplicates, applying
348 * processing objects and other adjustments
350 protected function prepare_pathelements($elementsarr) {
352 // First iteration, push them to new array, indexed by name
353 // detecting duplicates in names or paths
356 foreach($elementsarr as $element) {
357 if (!$element instanceof restore_path_element) {
358 throw new restore_step_exception('restore_path_element_wrong_class', get_class($element));
360 if (array_key_exists($element->get_name(), $names)) {
361 throw new restore_step_exception('restore_path_element_name_alreadyexists', $element->get_name());
363 if (array_key_exists($element->get_path(), $paths)) {
364 throw new restore_step_exception('restore_path_element_path_alreadyexists', $element->get_path());
366 $names[$element->get_name()] = true;
367 $paths[$element->get_path()] = $element;
369 // Now, for each element not having one processing object, if
370 // not child of grouped element, assign $this (the step itself) as processing element
371 // Note method must exist or we'll get one @restore_path_element_exception
372 foreach($paths as $key => $pelement) {
373 if ($pelement->get_processing_object() === null && !$this->grouped_parent_exists($pelement, $paths)) {
374 $paths[$key]->set_processing_object($this);
376 // Populate $elementsoldid and $elementsoldid based on available pathelements
377 $this->elementsoldid[$pelement->get_name()] = null;
378 $this->elementsnewid[$pelement->get_name()] = null;
380 // Done, add them to pathelements (dupes by key - path - are discarded)
381 $this->pathelements = array_merge($this->pathelements, $paths);
385 * Given one pathelement, return true if grouped parent was found
387 protected function grouped_parent_exists($pelement, $elements) {
388 foreach ($elements as $element) {
389 if ($pelement->get_path() == $element->get_path()) {
390 continue; // Don't compare against itself
392 // If element is grouped and parent of pelement, return true
393 if ($element->is_grouped() and strpos($pelement->get_path() . '/', $element->get_path()) === 0) {
397 return false; // no grouped parent found
401 * To conditionally decide if one step will be executed or no
403 * For steps needing to be executed conditionally, based in dynamic
404 * conditions (at execution time vs at declaration time) you must
405 * override this function. It will return true if the step must be
406 * executed and false if not
408 protected function execute_condition() {
413 * Function that will return the structure to be processed by this restore_step.
414 * Must return one array of @restore_path_element elements
416 abstract protected function define_structure();