MDL-21432 backup - allow restore of only one itemid (instead of the whole itemname)
[moodle.git] / backup / moodle2 / restore_subplugin.class.php
CommitLineData
91d11057
EL
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-moodle2
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 * Class implementing the subplugins support for moodle2 restore
27 *
28 * TODO: Finish phpdocs
f2745cbe 29 * TODO: Add support for declaring decode_contents (not decode_rules)
91d11057
EL
30 */
31abstract class restore_subplugin {
32
33 protected $subplugintype;
34 protected $subpluginname;
35 protected $connectionpoint;
36 protected $step;
37 protected $task;
38
39 public function __construct($subplugintype, $subpluginname, $step) {
40 $this->subplugintype = $subplugintype;
41 $this->subpluginname = $subpluginname;
42 $this->step = $step;
43 $this->task = $step->get_task();
44 $this->connectionpoint = '';
45 }
46
47 public function define_subplugin_structure($connectionpoint) {
48 if (!$connectionpoint instanceof restore_path_element) {
49 throw new restore_step_exception('restore_path_element_required', $connectionpoint);
50 }
51
52 $paths = array();
53 $this->connectionpoint = $connectionpoint;
54 $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_subplugin_structure';
55
56 if (method_exists($this, $methodname)) {
57 if ($subbluginpaths = $this->$methodname()) {
58 foreach ($subbluginpaths as $path) {
59 $path->set_processing_object($this);
60 $paths[] = $path;
61 }
62 }
63 }
64 return $paths;
65 }
66
67// Protected API starts here
68
69// restore_step/structure_step/task wrappers
70
71 protected function get_restoreid() {
72 if (is_null($this->task)) {
73 throw new restore_step_exception('not_specified_restore_task');
74 }
75 return $this->task->get_restoreid();
76 }
77
78 /**
79 * To send ids pairs to backup_ids_table and to store them into paths
80 *
81 * This method will send the given itemname and old/new ids to the
82 * backup_ids_temp table, and, at the same time, will save the new id
83 * into the corresponding restore_path_element for easier access
84 * by children. Also will inject the known old context id for the task
85 * in case it's going to be used for restoring files later
86 */
87 protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null) {
88 $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid);
89 }
90
91 /**
92 * Returns the latest (parent) old id mapped by one pathelement
93 */
94 protected function get_old_parentid($itemname) {
95 return $this->step->get_old_parentid($itemname);
96 }
97
98 /**
99 * Returns the latest (parent) new id mapped by one pathelement
100 */
101 protected function get_new_parentid($itemname) {
102 return $this->step->get_new_parentid($itemname);
103 }
104
105 /**
106 * Return the new id of a mapping for the given itemname
107 *
108 */
109 protected function get_mappingid($itemname, $oldid) {
110 return $this->step->get_mappingid($itemname, $oldid);
111 }
112
113 /**
114 * Return the complete mapping from the given itemname, itemid
115 */
116 protected function get_mapping($itemname, $oldid) {
117 return $this->step->get_mapping($itemname, $oldid);
118 }
119
120 /**
121 * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
122 */
f2745cbe
EL
123 protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
124 $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
91d11057
EL
125 }
126
127 /**
128 * Apply course startdate offset based in original course startdate and course_offset_startdate setting
129 * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
130 * executions in the same request
131 */
132 protected function apply_date_offset($value) {
133 return $this->step->apply_date_offset($value);
134 }
135
136 /**
137 * Simple helper function that returns the name for the restore_path_element
138 * It's not mandatory to use it but recommended ;-)
139 */
140 protected function get_namefor($name = '') {
141 $name = $name !== '' ? '_' . $name : '';
142 return $this->subplugintype . '_' . $this->subpluginname . $name;
143 }
144
145 /**
146 * Simple helper function that returns the base (prefix) of the path for the restore_path_element
147 * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
148 */
149 protected function get_pathfor($path = '') {
150 $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
151 return $this->connectionpoint->get_path() . '/' .
152 'subplugin_' . $this->subplugintype . '_' .
153 $this->subpluginname . '_' . basename($this->connectionpoint->get_path()) . $path;
154 }
155}