Moodle release 2.8.10
[moodle.git] / backup / moodle2 / restore_subplugin.class.php
blobbf21779f4fe7bc28068eb20ab70ce06ba0326cde
1 <?php
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.
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/>.
18 /**
19 * Defines restore_subplugin class
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
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * Class implementing the subplugins support for moodle2 restore
33 * TODO: Finish phpdocs
34 * TODO: Make this subclass of restore_plugin
35 * TODO: Add support for declaring decode_contents (not decode_rules)
37 abstract class restore_subplugin {
39 protected $subplugintype;
40 protected $subpluginname;
41 protected $connectionpoint;
42 protected $step;
43 protected $task;
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 = '';
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);
58 $paths = array();
59 $this->connectionpoint = $connectionpoint;
60 $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_subplugin_structure';
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;
70 return $paths;
73 /**
74 * after_execute dispatcher for any restore_subplugin class
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
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();
90 // Protected API starts here
92 // restore_step/structure_step/task wrappers
94 protected function get_restoreid() {
95 if (is_null($this->task)) {
96 throw new restore_step_exception('not_specified_restore_task');
98 return $this->task->get_restoreid();
102 * To send ids pairs to backup_ids_table and to store them into paths
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
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);
115 * Returns the latest (parent) old id mapped by one pathelement
117 protected function get_old_parentid($itemname) {
118 return $this->step->get_old_parentid($itemname);
122 * Returns the latest (parent) new id mapped by one pathelement
124 protected function get_new_parentid($itemname) {
125 return $this->step->get_new_parentid($itemname);
129 * Return the new id of a mapping for the given itemname
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
135 protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
136 return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
140 * Return the complete mapping from the given itemname, itemid
142 protected function get_mapping($itemname, $oldid) {
143 return $this->step->get_mapping($itemname, $oldid);
147 * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
149 protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
150 $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
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
158 protected function apply_date_offset($value) {
159 return $this->step->apply_date_offset($value);
163 * Returns the value of one (task/plan) setting
165 protected function get_setting_value($name) {
166 if (is_null($this->task)) {
167 throw new restore_step_exception('not_specified_restore_task');
169 return $this->task->get_setting_value($name);
172 // end of restore_step/structure_step/task wrappers
175 * Simple helper function that returns the name for the restore_path_element
176 * It's not mandatory to use it but recommended ;-)
178 protected function get_namefor($name = '') {
179 $name = $name !== '' ? '_' . $name : '';
180 return $this->subplugintype . '_' . $this->subpluginname . $name;
184 * Simple helper function that returns the base (prefix) of the path for the restore_path_element
185 * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
187 protected function get_pathfor($path = '') {
188 $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
189 return $this->connectionpoint->get_path() . '/' .
190 'subplugin_' . $this->subplugintype . '_' .
191 $this->subpluginname . '_' . basename($this->connectionpoint->get_path()) . $path;