|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 abstract class restore_plugin { 00031 00032 protected $plugintype; 00033 protected $pluginname; 00034 protected $connectionpoint; 00035 protected $step; 00036 protected $task; 00037 00038 public function __construct($plugintype, $pluginname, $step) { 00039 $this->plugintype = $plugintype; 00040 $this->pluginname = $pluginname; 00041 $this->step = $step; 00042 $this->task = $step->get_task(); 00043 $this->connectionpoint = ''; 00044 } 00045 00046 public function define_plugin_structure($connectionpoint) { 00047 if (!$connectionpoint instanceof restore_path_element) { 00048 throw new restore_step_exception('restore_path_element_required', $connectionpoint); 00049 } 00050 00051 $paths = array(); 00052 $this->connectionpoint = $connectionpoint; 00053 $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_plugin_structure'; 00054 00055 if (method_exists($this, $methodname)) { 00056 if ($bluginpaths = $this->$methodname()) { 00057 foreach ($bluginpaths as $path) { 00058 $path->set_processing_object($this); 00059 $paths[] = $path; 00060 } 00061 } 00062 } 00063 return $paths; 00064 } 00065 00075 public function launch_after_execute_methods() { 00076 // Check if the after_execute method exists and launch it 00077 $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path()); 00078 if (method_exists($this, $afterexecute)) { 00079 $this->$afterexecute(); 00080 } 00081 } 00082 00092 public function launch_after_restore_methods() { 00093 // Check if the after_restore method exists and launch it 00094 $afterrestore = 'after_restore_' . basename($this->connectionpoint->get_path()); 00095 if (method_exists($this, $afterrestore)) { 00096 $this->$afterrestore(); 00097 } 00098 } 00099 00113 static public function get_restore_decode_contents($plugintype) { 00114 $decodecontents = array(); 00115 // Check the requested plugintype is a valid one 00116 if (!array_key_exists($plugintype, get_plugin_types($plugintype))) { 00117 throw new backup_step_exception('incorrect_plugin_type', $plugintype); 00118 } 00119 // Check the base plugin class exists 00120 $classname = 'restore_' . $plugintype . '_plugin'; 00121 if (!class_exists($classname)) { 00122 throw new backup_step_exception('plugin_class_not_found', $classname); 00123 } 00124 // First, call to the define_plugin_decode_contents in the base plugin class 00125 // (must exist by design in all the plugin base classes) 00126 if (method_exists($classname, 'define_plugin_decode_contents')) { 00127 $decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_plugin_decode_contents'))); 00128 } 00129 // Now, iterate over all the possible plugins available 00130 // (only the needed ones have been loaded, so they will 00131 // be the ones being asked here). Fetch their restore contents 00132 // by calling (if exists) to their define_decode_contents() method 00133 $plugins = get_plugin_list($plugintype); 00134 foreach ($plugins as $plugin => $plugindir) { 00135 $classname = 'restore_' . $plugintype . '_' . $plugin . '_plugin'; 00136 if (class_exists($classname)) { 00137 if (method_exists($classname, 'define_decode_contents')) { 00138 $decodecontents = array_merge($decodecontents, call_user_func(array($classname, 'define_decode_contents'))); 00139 } 00140 } 00141 } 00142 return $decodecontents; 00143 } 00144 00149 static public function define_plugin_decode_contents() { 00150 throw new coding_exception('define_plugin_decode_contents() method needs to be overridden in each subclass of restore_plugin'); 00151 } 00152 00153 // Protected API starts here 00154 00155 // restore_step/structure_step/task wrappers 00156 00157 protected function get_restoreid() { 00158 if (is_null($this->task)) { 00159 throw new restore_step_exception('not_specified_restore_task'); 00160 } 00161 return $this->task->get_restoreid(); 00162 } 00163 00173 protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) { 00174 $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid); 00175 } 00176 00180 protected function get_old_parentid($itemname) { 00181 return $this->step->get_old_parentid($itemname); 00182 } 00183 00187 protected function get_new_parentid($itemname) { 00188 return $this->step->get_new_parentid($itemname); 00189 } 00190 00198 protected function get_mappingid($itemname, $oldid, $ifnotfound = false) { 00199 return $this->step->get_mappingid($itemname, $oldid, $ifnotfound); 00200 } 00201 00205 protected function get_mapping($itemname, $oldid) { 00206 return $this->step->get_mapping($itemname, $oldid); 00207 } 00208 00212 protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) { 00213 $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid); 00214 } 00215 00221 protected function apply_date_offset($value) { 00222 return $this->step->apply_date_offset($value); 00223 } 00224 00228 protected function get_setting_value($name) { 00229 if (is_null($this->task)) { 00230 throw new restore_step_exception('not_specified_restore_task'); 00231 } 00232 return $this->task->get_setting_value($name); 00233 } 00234 00235 // end of restore_step/structure_step/task wrappers 00236 00241 protected function get_namefor($name = '') { 00242 $name = $name !== '' ? '_' . $name : ''; 00243 return $this->plugintype . '_' . $this->pluginname . $name; 00244 } 00245 00250 protected function get_pathfor($path = '') { 00251 $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : ''; 00252 return $this->connectionpoint->get_path() . '/' . 00253 'plugin_' . $this->plugintype . '_' . 00254 $this->pluginname . '_' . basename($this->connectionpoint->get_path()) . $path; 00255 } 00256 }