Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/helper/restore_decode_processor.class.php
Go to the documentation of this file.
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 
00025 defined('MOODLE_INTERNAL') || die();
00026 
00039 class restore_decode_processor {
00040 
00041     protected $contents;  // Array of restore_decode_content providers
00042     protected $rules;     // Array of restore_decode_rule workers
00043     protected $restoreid;   // The unique restoreid we are executing
00044     protected $sourcewwwroot; // The original wwwroot of the backup file
00045     protected $targetwwwroot; // The target wwwroot of the restore operation
00046 
00047     public function __construct($restoreid, $sourcewwwroot, $targetwwwroot) {
00048         $this->restoreid = $restoreid;
00049         $this->sourcewwwroot = $sourcewwwroot;
00050         $this->targetwwwroot = $targetwwwroot;
00051 
00052         $this->contents = array();
00053         $this->rules    = array();
00054     }
00055 
00056     public function add_content($content) {
00057         if (!$content instanceof restore_decode_content) {
00058             throw new restore_decode_processor_exception('incorrect_restore_decode_content', get_class($content));
00059         }
00060         $content->set_restoreid($this->restoreid);
00061         $this->contents[] = $content;
00062     }
00063 
00064     public function add_rule($rule) {
00065         if (!$rule instanceof restore_decode_rule) {
00066             throw new restore_decode_processor_exception('incorrect_restore_decode_rule', get_class($rule));
00067         }
00068         $rule->set_restoreid($this->restoreid);
00069         $rule->set_wwwroots($this->sourcewwwroot, $this->targetwwwroot);
00070         $this->rules[] = $rule;
00071     }
00072 
00078     public function execute() {
00079         // Iterate over all contents, visiting them
00080         foreach ($this->contents as $content) {
00081             $content->process($this);
00082         }
00083     }
00084 
00089     public function decode_content($content) {
00090         if (!$content = $this->precheck_content($content)) { // Perform some prechecks
00091             return false;
00092         }
00093         // Iterate over all rules, chaining results
00094         foreach ($this->rules as $rule) {
00095             $content = $rule->decode($content);
00096         }
00097         return $content;
00098     }
00099 
00103     public static function register_link_decoders($processor) {
00104         $tasks = array(); // To get the list of tasks having decoders
00105 
00106         // Add the course task
00107         $tasks[] = 'restore_course_task';
00108 
00109         // Add the section task
00110         $tasks[] = 'restore_section_task';
00111 
00112         // Add the module tasks
00113         $mods = get_plugin_list('mod');
00114         foreach ($mods as $mod => $moddir) {
00115             if (class_exists('restore_' . $mod . '_activity_task')) {
00116                 $tasks[] = 'restore_' . $mod . '_activity_task';
00117             }
00118         }
00119 
00120         // Add the default block task
00121         $tasks[] = 'restore_default_block_task';
00122 
00123         // Add the custom block tasks
00124         $blocks = get_plugin_list('block');
00125         foreach ($blocks as $block => $blockdir) {
00126             if (class_exists('restore_' . $block . '_block_task')) {
00127                 $tasks[] = 'restore_' . $block . '_block_task';
00128             }
00129         }
00130 
00131         // We have all the tasks registered, let's iterate over them, getting
00132         // contents and rules and adding them to the processor
00133         foreach ($tasks as $classname) {
00134             // Get restore_decode_content array and add to processor
00135             $contents = call_user_func(array($classname, 'define_decode_contents'));
00136             if (!is_array($contents)) {
00137                 throw new restore_decode_processor_exception('define_decode_contents_not_array', $classname);
00138             }
00139             foreach ($contents as $content) {
00140                 $processor->add_content($content);
00141             }
00142             // Get restore_decode_rule array and add to processor
00143             $rules = call_user_func(array($classname, 'define_decode_rules'));
00144             if (!is_array($rules)) {
00145                 throw new restore_decode_processor_exception('define_decode_rules_not_array', $classname);
00146             }
00147             foreach ($rules as $rule) {
00148                 $processor->add_rule($rule);
00149             }
00150         }
00151 
00152         // Now process all the plugins contents (note plugins don't have support for rules)
00153         // TODO: Add other plugin types (course formats, local...) here if we add them to backup/restore
00154         $plugins = array('qtype');
00155         foreach ($plugins as $plugin) {
00156             $contents = restore_plugin::get_restore_decode_contents($plugin);
00157             if (!is_array($contents)) {
00158                 throw new restore_decode_processor_exception('get_restore_decode_contents_not_array', $plugin);
00159             }
00160             foreach ($contents as $content) {
00161                 $processor->add_content($content);
00162             }
00163         }
00164     }
00165 
00166 // Protected API starts here
00167 
00171     protected function precheck_content($content) {
00172         // Look for $@ in content (all interlinks contain that)
00173         return (strpos($content, '$@') === false) ? false : $content;
00174     }
00175 }
00176 
00177 /*
00178  * Exception class used by all the @restore_decode_content stuff
00179  */
00180 class restore_decode_processor_exception extends backup_exception {
00181 
00182     public function __construct($errorcode, $a=NULL, $debuginfo=null) {
00183         return parent::__construct($errorcode, $a, $debuginfo);
00184     }
00185 }
 All Data Structures Namespaces Files Functions Variables Enumerations