PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/backup/moodle2/restore_plugin.class.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 261 lines | 114 code | 27 blank | 120 comment | 13 complexity | 131b091c6a8d2d396fcfc65e2dfba223 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-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. protected $plugintype;
  33. protected $pluginname;
  34. protected $connectionpoint;
  35. protected $step;
  36. protected $task;
  37. public function __construct($plugintype, $pluginname, $step) {
  38. $this->plugintype = $plugintype;
  39. $this->pluginname = $pluginname;
  40. $this->step = $step;
  41. $this->task = $step->get_task();
  42. $this->connectionpoint = '';
  43. }
  44. public function define_plugin_structure($connectionpoint) {
  45. if (!$connectionpoint instanceof restore_path_element) {
  46. throw new restore_step_exception('restore_path_element_required', $connectionpoint);
  47. }
  48. $paths = array();
  49. $this->connectionpoint = $connectionpoint;
  50. $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_plugin_structure';
  51. if (method_exists($this, $methodname)) {
  52. if ($bluginpaths = $this->$methodname()) {
  53. foreach ($bluginpaths as $path) {
  54. $path->set_processing_object($this);
  55. $paths[] = $path;
  56. }
  57. }
  58. }
  59. return $paths;
  60. }
  61. /**
  62. * after_execute dispatcher for any restore_plugin class
  63. *
  64. * This method will dispatch execution to the corresponding
  65. * after_execute_xxx() method when available, with xxx
  66. * being the connection point of the instance, so plugin
  67. * classes with multiple connection points will support
  68. * multiple after_execute methods, one for each connection point
  69. */
  70. public function launch_after_execute_methods() {
  71. // Check if the after_execute method exists and launch it
  72. $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path());
  73. if (method_exists($this, $afterexecute)) {
  74. $this->$afterexecute();
  75. }
  76. }
  77. /**
  78. * after_restore dispatcher for any restore_plugin class
  79. *
  80. * This method will dispatch execution to the corresponding
  81. * after_restore_xxx() method when available, with xxx
  82. * being the connection point of the instance, so plugin
  83. * classes with multiple connection points will support
  84. * multiple after_restore methods, one for each connection point
  85. */
  86. public function launch_after_restore_methods() {
  87. // Check if the after_restore method exists and launch it
  88. $afterrestore = 'after_restore_' . basename($this->connectionpoint->get_path());
  89. if (method_exists($this, $afterrestore)) {
  90. $this->$afterrestore();
  91. }
  92. }
  93. /**
  94. * Returns one array with all the decode contents
  95. * to be processed by the links decoder
  96. *
  97. * This method, given one plugin type, returns one
  98. * array of {@link restore_decode_content} objects
  99. * that will be added to the restore decoder in order
  100. * to perform modifications under the plugin contents.
  101. *
  102. * The objects are retrieved by calling to the {@link define_decode_contents}
  103. * method (when available), first in the main restore_xxxx_plugin class
  104. * and later on each of the available subclasses
  105. */
  106. static public function get_restore_decode_contents($plugintype) {
  107. $decodecontents = array();
  108. // Check the requested plugintype is a valid one
  109. if (!array_key_exists($plugintype, core_component::get_plugin_types($plugintype))) {
  110. throw new backup_step_exception('incorrect_plugin_type', $plugintype);
  111. }
  112. // Check the base plugin class exists
  113. $classname = 'restore_' . $plugintype . '_plugin';
  114. if (!class_exists($classname)) {
  115. throw new backup_step_exception('plugin_class_not_found', $classname);
  116. }
  117. // First, call to the define_plugin_decode_contents in the base plugin class
  118. // (must exist by design in all the plugin base classes)
  119. if (method_exists($classname, 'define_plugin_decode_contents')) {
  120. $decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_plugin_decode_contents')));
  121. }
  122. // Now, iterate over all the possible plugins available
  123. // (only the needed ones have been loaded, so they will
  124. // be the ones being asked here). Fetch their restore contents
  125. // by calling (if exists) to their define_decode_contents() method
  126. $plugins = core_component::get_plugin_list($plugintype);
  127. foreach ($plugins as $plugin => $plugindir) {
  128. $classname = 'restore_' . $plugintype . '_' . $plugin . '_plugin';
  129. if (class_exists($classname)) {
  130. if (method_exists($classname, 'define_decode_contents')) {
  131. $decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_decode_contents')));
  132. }
  133. }
  134. }
  135. return $decodecontents;
  136. }
  137. /**
  138. * Define the contents in the plugin that must be
  139. * processed by the link decoder
  140. */
  141. static public function define_plugin_decode_contents() {
  142. throw new coding_exception('define_plugin_decode_contents() method needs to be overridden in each subclass of restore_plugin');
  143. }
  144. // Protected API starts here
  145. // restore_step/structure_step/task wrappers
  146. protected function get_restoreid() {
  147. if (is_null($this->task)) {
  148. throw new restore_step_exception('not_specified_restore_task');
  149. }
  150. return $this->task->get_restoreid();
  151. }
  152. /**
  153. * To send ids pairs to backup_ids_table and to store them into paths
  154. *
  155. * This method will send the given itemname and old/new ids to the
  156. * backup_ids_temp table, and, at the same time, will save the new id
  157. * into the corresponding restore_path_element for easier access
  158. * by children. Also will inject the known old context id for the task
  159. * in case it's going to be used for restoring files later
  160. */
  161. protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
  162. $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid);
  163. }
  164. /**
  165. * Returns the latest (parent) old id mapped by one pathelement
  166. */
  167. protected function get_old_parentid($itemname) {
  168. return $this->step->get_old_parentid($itemname);
  169. }
  170. /**
  171. * Returns the latest (parent) new id mapped by one pathelement
  172. */
  173. protected function get_new_parentid($itemname) {
  174. return $this->step->get_new_parentid($itemname);
  175. }
  176. /**
  177. * Return the new id of a mapping for the given itemname
  178. *
  179. * @param string $itemname the type of item
  180. * @param int $oldid the item ID from the backup
  181. * @param mixed $ifnotfound what to return if $oldid wasnt found. Defaults to false
  182. */
  183. protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
  184. return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
  185. }
  186. /**
  187. * Return the complete mapping from the given itemname, itemid
  188. */
  189. protected function get_mapping($itemname, $oldid) {
  190. return $this->step->get_mapping($itemname, $oldid);
  191. }
  192. /**
  193. * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
  194. */
  195. protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
  196. $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
  197. }
  198. /**
  199. * Apply course startdate offset based in original course startdate and course_offset_startdate setting
  200. * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
  201. * executions in the same request
  202. */
  203. protected function apply_date_offset($value) {
  204. return $this->step->apply_date_offset($value);
  205. }
  206. /**
  207. * Returns the value of one (task/plan) setting
  208. */
  209. protected function get_setting_value($name) {
  210. if (is_null($this->task)) {
  211. throw new restore_step_exception('not_specified_restore_task');
  212. }
  213. return $this->task->get_setting_value($name);
  214. }
  215. // end of restore_step/structure_step/task wrappers
  216. /**
  217. * Simple helper function that returns the name for the restore_path_element
  218. * It's not mandatory to use it but recommended ;-)
  219. */
  220. protected function get_namefor($name = '') {
  221. $name = $name !== '' ? '_' . $name : '';
  222. return $this->plugintype . '_' . $this->pluginname . $name;
  223. }
  224. /**
  225. * Simple helper function that returns the base (prefix) of the path for the restore_path_element
  226. * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
  227. */
  228. protected function get_pathfor($path = '') {
  229. $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
  230. return $this->connectionpoint->get_path() . '/' .
  231. 'plugin_' . $this->plugintype . '_' .
  232. $this->pluginname . '_' . basename($this->connectionpoint->get_path()) . $path;
  233. }
  234. }