PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/backup/moodle2/restore_plugin.class.php

https://bitbucket.org/moodle/moodle
PHP | 284 lines | 119 code | 28 blank | 137 comment | 15 complexity | 44c36c4d19cb663fb6805c1be4baa30b MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Defines restore_plugin class
  18. *
  19. * @package core_backup
  20. * @subpackage moodle2
  21. * @category backup
  22. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * Class implementing the plugins support for moodle2 restore
  28. *
  29. * TODO: Finish phpdocs
  30. */
  31. abstract class restore_plugin {
  32. /** @var string */
  33. protected $plugintype;
  34. /** @var string */
  35. protected $pluginname;
  36. /** @var string */
  37. protected $connectionpoint;
  38. /** @var restore_structure_step */
  39. protected $step;
  40. /** @var restore_course_task|restore_activity_task */
  41. protected $task;
  42. /**
  43. * restore_plugin constructor.
  44. *
  45. * @param string $plugintype
  46. * @param string $pluginname
  47. * @param restore_structure_step $step
  48. */
  49. public function __construct($plugintype, $pluginname, $step) {
  50. $this->plugintype = $plugintype;
  51. $this->pluginname = $pluginname;
  52. $this->step = $step;
  53. $this->task = $step->get_task();
  54. $this->connectionpoint = '';
  55. }
  56. public function define_plugin_structure($connectionpoint) {
  57. if (!$connectionpoint instanceof restore_path_element) {
  58. throw new restore_step_exception('restore_path_element_required', $connectionpoint);
  59. }
  60. $paths = array();
  61. $this->connectionpoint = $connectionpoint;
  62. $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_plugin_structure';
  63. if (method_exists($this, $methodname)) {
  64. if ($pluginpaths = $this->$methodname()) {
  65. foreach ($pluginpaths as $path) {
  66. if ($path->get_processing_object() === null && !$this->step->grouped_parent_exists($path, $paths)) {
  67. $path->set_processing_object($this);
  68. }
  69. $paths[] = $path;
  70. }
  71. }
  72. }
  73. return $paths;
  74. }
  75. /**
  76. * after_execute dispatcher for any restore_plugin class
  77. *
  78. * This method will dispatch execution to the corresponding
  79. * after_execute_xxx() method when available, with xxx
  80. * being the connection point of the instance, so plugin
  81. * classes with multiple connection points will support
  82. * multiple after_execute methods, one for each connection point
  83. */
  84. public function launch_after_execute_methods() {
  85. // Check if the after_execute method exists and launch it
  86. $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path());
  87. if (method_exists($this, $afterexecute)) {
  88. $this->$afterexecute();
  89. }
  90. }
  91. /**
  92. * after_restore dispatcher for any restore_plugin class
  93. *
  94. * This method will dispatch execution to the corresponding
  95. * after_restore_xxx() method when available, with xxx
  96. * being the connection point of the instance, so plugin
  97. * classes with multiple connection points will support
  98. * multiple after_restore methods, one for each connection point
  99. */
  100. public function launch_after_restore_methods() {
  101. // Check if the after_restore method exists and launch it
  102. $afterrestore = 'after_restore_' . basename($this->connectionpoint->get_path());
  103. if (method_exists($this, $afterrestore)) {
  104. $this->$afterrestore();
  105. }
  106. }
  107. /**
  108. * Returns one array with all the decode contents
  109. * to be processed by the links decoder
  110. *
  111. * This method, given one plugin type, returns one
  112. * array of {@link restore_decode_content} objects
  113. * that will be added to the restore decoder in order
  114. * to perform modifications under the plugin contents.
  115. *
  116. * The objects are retrieved by calling to the {@link define_decode_contents}
  117. * method (when available), first in the main restore_xxxx_plugin class
  118. * and later on each of the available subclasses
  119. */
  120. static public function get_restore_decode_contents($plugintype) {
  121. $decodecontents = array();
  122. // Check the requested plugintype is a valid one
  123. if (!array_key_exists($plugintype, core_component::get_plugin_types($plugintype))) {
  124. throw new backup_step_exception('incorrect_plugin_type', $plugintype);
  125. }
  126. // Check the base plugin class exists
  127. $classname = 'restore_' . $plugintype . '_plugin';
  128. if (!class_exists($classname)) {
  129. throw new backup_step_exception('plugin_class_not_found', $classname);
  130. }
  131. // First, call to the define_plugin_decode_contents in the base plugin class
  132. // (must exist by design in all the plugin base classes)
  133. if (method_exists($classname, 'define_plugin_decode_contents')) {
  134. $decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_plugin_decode_contents')));
  135. }
  136. // Now, iterate over all the possible plugins available
  137. // (only the needed ones have been loaded, so they will
  138. // be the ones being asked here). Fetch their restore contents
  139. // by calling (if exists) to their define_decode_contents() method
  140. $plugins = core_component::get_plugin_list($plugintype);
  141. foreach ($plugins as $plugin => $plugindir) {
  142. $classname = 'restore_' . $plugintype . '_' . $plugin . '_plugin';
  143. if (class_exists($classname)) {
  144. if (method_exists($classname, 'define_decode_contents')) {
  145. $decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_decode_contents')));
  146. }
  147. }
  148. }
  149. return $decodecontents;
  150. }
  151. /**
  152. * Define the contents in the plugin that must be
  153. * processed by the link decoder
  154. */
  155. static public function define_plugin_decode_contents() {
  156. throw new coding_exception('define_plugin_decode_contents() method needs to be overridden in each subclass of restore_plugin');
  157. }
  158. // Protected API starts here
  159. // restore_step/structure_step/task wrappers
  160. protected function get_restoreid() {
  161. if (is_null($this->task)) {
  162. throw new restore_step_exception('not_specified_restore_task');
  163. }
  164. return $this->task->get_restoreid();
  165. }
  166. /**
  167. * To send ids pairs to backup_ids_table and to store them into paths
  168. *
  169. * This method will send the given itemname and old/new ids to the
  170. * backup_ids_temp table, and, at the same time, will save the new id
  171. * into the corresponding restore_path_element for easier access
  172. * by children. Also will inject the known old context id for the task
  173. * in case it's going to be used for restoring files later
  174. */
  175. protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
  176. $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid);
  177. }
  178. /**
  179. * Returns the latest (parent) old id mapped by one pathelement
  180. */
  181. protected function get_old_parentid($itemname) {
  182. return $this->step->get_old_parentid($itemname);
  183. }
  184. /**
  185. * Returns the latest (parent) new id mapped by one pathelement
  186. */
  187. protected function get_new_parentid($itemname) {
  188. return $this->step->get_new_parentid($itemname);
  189. }
  190. /**
  191. * Return the new id of a mapping for the given itemname
  192. *
  193. * @param string $itemname the type of item
  194. * @param int $oldid the item ID from the backup
  195. * @param mixed $ifnotfound what to return if $oldid wasnt found. Defaults to false
  196. */
  197. protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
  198. return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
  199. }
  200. /**
  201. * Return the complete mapping from the given itemname, itemid
  202. */
  203. protected function get_mapping($itemname, $oldid) {
  204. return $this->step->get_mapping($itemname, $oldid);
  205. }
  206. /**
  207. * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
  208. */
  209. protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
  210. $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
  211. }
  212. /**
  213. * Apply course startdate offset based in original course startdate and course_offset_startdate setting
  214. * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
  215. * executions in the same request
  216. */
  217. protected function apply_date_offset($value) {
  218. return $this->step->apply_date_offset($value);
  219. }
  220. /**
  221. * Returns the value of one (task/plan) setting
  222. */
  223. protected function get_setting_value($name) {
  224. if (is_null($this->task)) {
  225. throw new restore_step_exception('not_specified_restore_task');
  226. }
  227. return $this->task->get_setting_value($name);
  228. }
  229. // end of restore_step/structure_step/task wrappers
  230. /**
  231. * Simple helper function that returns the name for the restore_path_element
  232. * It's not mandatory to use it but recommended ;-)
  233. */
  234. protected function get_namefor($name = '') {
  235. $name = $name !== '' ? '_' . $name : '';
  236. return $this->plugintype . '_' . $this->pluginname . $name;
  237. }
  238. /**
  239. * Simple helper function that returns the base (prefix) of the path for the restore_path_element
  240. * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
  241. */
  242. protected function get_pathfor($path = '') {
  243. $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
  244. return $this->connectionpoint->get_path() . '/' .
  245. 'plugin_' . $this->plugintype . '_' .
  246. $this->pluginname . '_' . basename($this->connectionpoint->get_path()) . $path;
  247. }
  248. /**
  249. * Get the task we are part of.
  250. *
  251. * @return restore_activity_task|restore_course_task the task.
  252. */
  253. protected function get_task() {
  254. return $this->task;
  255. }
  256. }