Merge branch 'MDL-77933-402' of https://github.com/snake/moodle into MOODLE_402_STABLE
[moodle.git] / backup / moodle2 / restore_subplugin.class.php
blob226cefb766213ffd5bd5fe219ab13bd0be84f017
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 /** @var string */
40 protected $subplugintype;
41 /** @var string */
42 protected $subpluginname;
43 /** @var restore_path_element */
44 protected $connectionpoint;
45 /** @var restore_step */
46 protected $step;
47 /** @var restore_task */
48 protected $task;
50 public function __construct($subplugintype, $subpluginname, $step) {
51 $this->subplugintype = $subplugintype;
52 $this->subpluginname = $subpluginname;
53 $this->step = $step;
54 $this->task = $step->get_task();
55 $this->connectionpoint = '';
58 public function define_subplugin_structure($connectionpoint) {
59 if (!$connectionpoint instanceof restore_path_element) {
60 throw new restore_step_exception('restore_path_element_required', $connectionpoint);
63 $paths = array();
64 $this->connectionpoint = $connectionpoint;
65 $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_subplugin_structure';
67 if (method_exists($this, $methodname)) {
68 if ($subbluginpaths = $this->$methodname()) {
69 foreach ($subbluginpaths as $path) {
70 $path->set_processing_object($this);
71 $paths[] = $path;
75 return $paths;
78 /**
79 * after_execute dispatcher for any restore_subplugin class
81 * This method will dispatch execution to the corresponding
82 * after_execute_xxx() method when available, with xxx
83 * being the connection point of the instance, so subplugin
84 * classes with multiple connection points will support
85 * multiple after_execute methods, one for each connection point
87 public function launch_after_execute_methods() {
88 // Check if the after_execute method exists and launch it
89 $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path());
90 if (method_exists($this, $afterexecute)) {
91 $this->$afterexecute();
95 /**
96 * The after_restore dispatcher for any restore_subplugin class.
98 * This method will dispatch execution to the corresponding
99 * after_restore_xxx() method when available, with xxx
100 * being the connection point of the instance, so subplugin
101 * classes with multiple connection points will support
102 * multiple after_restore methods, one for each connection point.
104 public function launch_after_restore_methods() {
105 // Check if the after_restore method exists and launch it.
106 $afterestore = 'after_restore_' . basename($this->connectionpoint->get_path());
107 if (method_exists($this, $afterestore)) {
108 $this->$afterestore();
112 // Protected API starts here
114 // restore_step/structure_step/task wrappers
116 protected function get_restoreid() {
117 if (is_null($this->task)) {
118 throw new restore_step_exception('not_specified_restore_task');
120 return $this->task->get_restoreid();
124 * To send ids pairs to backup_ids_table and to store them into paths
126 * This method will send the given itemname and old/new ids to the
127 * backup_ids_temp table, and, at the same time, will save the new id
128 * into the corresponding restore_path_element for easier access
129 * by children. Also will inject the known old context id for the task
130 * in case it's going to be used for restoring files later
132 protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
133 $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid);
137 * Returns the latest (parent) old id mapped by one pathelement
139 protected function get_old_parentid($itemname) {
140 return $this->step->get_old_parentid($itemname);
144 * Returns the latest (parent) new id mapped by one pathelement
146 protected function get_new_parentid($itemname) {
147 return $this->step->get_new_parentid($itemname);
151 * Return the new id of a mapping for the given itemname
153 * @param string $itemname the type of item
154 * @param int $oldid the item ID from the backup
155 * @param mixed $ifnotfound what to return if $oldid wasnt found. Defaults to false
157 protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
158 return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
162 * Return the complete mapping from the given itemname, itemid
164 protected function get_mapping($itemname, $oldid) {
165 return $this->step->get_mapping($itemname, $oldid);
169 * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
171 protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
172 $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
176 * Apply course startdate offset based in original course startdate and course_offset_startdate setting
177 * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
178 * executions in the same request
180 protected function apply_date_offset($value) {
181 return $this->step->apply_date_offset($value);
185 * Call the log function from the step.
187 public function log($message, $level, $a = null, $depth = null, $display = false) {
188 return $this->step->log($message, $level, $a, $depth, $display);
192 * Returns the value of one (task/plan) setting
194 protected function get_setting_value($name) {
195 if (is_null($this->task)) {
196 throw new restore_step_exception('not_specified_restore_task');
198 return $this->task->get_setting_value($name);
201 // end of restore_step/structure_step/task wrappers
204 * Simple helper function that returns the name for the restore_path_element
205 * It's not mandatory to use it but recommended ;-)
207 protected function get_namefor($name = '') {
208 $name = $name !== '' ? '_' . $name : '';
209 return $this->subplugintype . '_' . $this->subpluginname . $name;
213 * Simple helper function that returns the base (prefix) of the path for the restore_path_element
214 * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
216 protected function get_pathfor($path = '') {
217 $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
218 return $this->connectionpoint->get_path() . '/' .
219 'subplugin_' . $this->subplugintype . '_' .
220 $this->subpluginname . '_' . basename($this->connectionpoint->get_path()) . $path;