PageRenderTime 1176ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/moodle2/restore_subplugin.class.php

http://github.com/moodle/moodle
PHP | 217 lines | 91 code | 26 blank | 100 comment | 8 complexity | 95b1277ded587edd2c40c971a205c001 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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_subplugin 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 subplugins support for moodle2 restore
  28. *
  29. * TODO: Finish phpdocs
  30. * TODO: Make this subclass of restore_plugin
  31. * TODO: Add support for declaring decode_contents (not decode_rules)
  32. */
  33. abstract class restore_subplugin {
  34. protected $subplugintype;
  35. protected $subpluginname;
  36. protected $connectionpoint;
  37. protected $step;
  38. protected $task;
  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. public function define_subplugin_structure($connectionpoint) {
  47. if (!$connectionpoint instanceof restore_path_element) {
  48. throw new restore_step_exception('restore_path_element_required', $connectionpoint);
  49. }
  50. $paths = array();
  51. $this->connectionpoint = $connectionpoint;
  52. $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_subplugin_structure';
  53. if (method_exists($this, $methodname)) {
  54. if ($subbluginpaths = $this->$methodname()) {
  55. foreach ($subbluginpaths as $path) {
  56. $path->set_processing_object($this);
  57. $paths[] = $path;
  58. }
  59. }
  60. }
  61. return $paths;
  62. }
  63. /**
  64. * after_execute dispatcher for any restore_subplugin class
  65. *
  66. * This method will dispatch execution to the corresponding
  67. * after_execute_xxx() method when available, with xxx
  68. * being the connection point of the instance, so subplugin
  69. * classes with multiple connection points will support
  70. * multiple after_execute methods, one for each connection point
  71. */
  72. public function launch_after_execute_methods() {
  73. // Check if the after_execute method exists and launch it
  74. $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path());
  75. if (method_exists($this, $afterexecute)) {
  76. $this->$afterexecute();
  77. }
  78. }
  79. /**
  80. * The after_restore dispatcher for any restore_subplugin class.
  81. *
  82. * This method will dispatch execution to the corresponding
  83. * after_restore_xxx() method when available, with xxx
  84. * being the connection point of the instance, so subplugin
  85. * classes with multiple connection points will support
  86. * multiple after_restore methods, one for each connection point.
  87. */
  88. public function launch_after_restore_methods() {
  89. // Check if the after_restore method exists and launch it.
  90. $afterestore = 'after_restore_' . basename($this->connectionpoint->get_path());
  91. if (method_exists($this, $afterestore)) {
  92. $this->$afterestore();
  93. }
  94. }
  95. // Protected API starts here
  96. // restore_step/structure_step/task wrappers
  97. protected function get_restoreid() {
  98. if (is_null($this->task)) {
  99. throw new restore_step_exception('not_specified_restore_task');
  100. }
  101. return $this->task->get_restoreid();
  102. }
  103. /**
  104. * To send ids pairs to backup_ids_table and to store them into paths
  105. *
  106. * This method will send the given itemname and old/new ids to the
  107. * backup_ids_temp table, and, at the same time, will save the new id
  108. * into the corresponding restore_path_element for easier access
  109. * by children. Also will inject the known old context id for the task
  110. * in case it's going to be used for restoring files later
  111. */
  112. protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
  113. $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid);
  114. }
  115. /**
  116. * Returns the latest (parent) old id mapped by one pathelement
  117. */
  118. protected function get_old_parentid($itemname) {
  119. return $this->step->get_old_parentid($itemname);
  120. }
  121. /**
  122. * Returns the latest (parent) new id mapped by one pathelement
  123. */
  124. protected function get_new_parentid($itemname) {
  125. return $this->step->get_new_parentid($itemname);
  126. }
  127. /**
  128. * Return the new id of a mapping for the given itemname
  129. *
  130. * @param string $itemname the type of item
  131. * @param int $oldid the item ID from the backup
  132. * @param mixed $ifnotfound what to return if $oldid wasnt found. Defaults to false
  133. */
  134. protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
  135. return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
  136. }
  137. /**
  138. * Return the complete mapping from the given itemname, itemid
  139. */
  140. protected function get_mapping($itemname, $oldid) {
  141. return $this->step->get_mapping($itemname, $oldid);
  142. }
  143. /**
  144. * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
  145. */
  146. protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
  147. $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
  148. }
  149. /**
  150. * Apply course startdate offset based in original course startdate and course_offset_startdate setting
  151. * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
  152. * executions in the same request
  153. */
  154. protected function apply_date_offset($value) {
  155. return $this->step->apply_date_offset($value);
  156. }
  157. /**
  158. * Call the log function from the step.
  159. */
  160. public function log($message, $level, $a = null, $depth = null, $display = false) {
  161. return $this->step->log($message, $level, $a, $depth, $display);
  162. }
  163. /**
  164. * Returns the value of one (task/plan) setting
  165. */
  166. protected function get_setting_value($name) {
  167. if (is_null($this->task)) {
  168. throw new restore_step_exception('not_specified_restore_task');
  169. }
  170. return $this->task->get_setting_value($name);
  171. }
  172. // end of restore_step/structure_step/task wrappers
  173. /**
  174. * Simple helper function that returns the name for the restore_path_element
  175. * It's not mandatory to use it but recommended ;-)
  176. */
  177. protected function get_namefor($name = '') {
  178. $name = $name !== '' ? '_' . $name : '';
  179. return $this->subplugintype . '_' . $this->subpluginname . $name;
  180. }
  181. /**
  182. * Simple helper function that returns the base (prefix) of the path for the restore_path_element
  183. * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
  184. */
  185. protected function get_pathfor($path = '') {
  186. $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
  187. return $this->connectionpoint->get_path() . '/' .
  188. 'subplugin_' . $this->subplugintype . '_' .
  189. $this->subpluginname . '_' . basename($this->connectionpoint->get_path()) . $path;
  190. }
  191. }