mirrored from git://git.moodle.org/moodle.git
/
restore_decode_processor.class.php
185 lines (162 loc) · 6.7 KB
/
restore_decode_processor.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package moodlecore
* @subpackage backup-helper
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Helper class that will perform all the necessary decoding tasks on restore
*
* This class will register all the restore_decode_content and
* restore_decode_rule instances defined by the restore tasks
* in order to perform the complete decoding of links in the
* final task of the restore_plan execution.
*
* By visiting each content provider will apply all the defined rules
*
* TODO: Complete phpdocs
*/
class restore_decode_processor {
protected $contents; // Array of restore_decode_content providers
protected $rules; // Array of restore_decode_rule workers
protected $restoreid; // The unique restoreid we are executing
protected $sourcewwwroot; // The original wwwroot of the backup file
protected $targetwwwroot; // The target wwwroot of the restore operation
public function __construct($restoreid, $sourcewwwroot, $targetwwwroot) {
$this->restoreid = $restoreid;
$this->sourcewwwroot = $sourcewwwroot;
$this->targetwwwroot = $targetwwwroot;
$this->contents = array();
$this->rules = array();
}
public function add_content($content) {
if (!$content instanceof restore_decode_content) {
throw new restore_decode_processor_exception('incorrect_restore_decode_content', get_class($content));
}
$content->set_restoreid($this->restoreid);
$this->contents[] = $content;
}
public function add_rule($rule) {
if (!$rule instanceof restore_decode_rule) {
throw new restore_decode_processor_exception('incorrect_restore_decode_rule', get_class($rule));
}
$rule->set_restoreid($this->restoreid);
$rule->set_wwwroots($this->sourcewwwroot, $this->targetwwwroot);
$this->rules[] = $rule;
}
/**
* Visit all the restore_decode_content providers
* that will cause decode_content() to be called
* for each content
*/
public function execute() {
// Iterate over all contents, visiting them
foreach ($this->contents as $content) {
$content->process($this);
}
}
/**
* Receive content from restore_decode_content objects
* and apply all the restore_decode_rules to them
*/
public function decode_content($content) {
if (!$content = $this->precheck_content($content)) { // Perform some prechecks
return false;
}
// Iterate over all rules, chaining results
foreach ($this->rules as $rule) {
$content = $rule->decode($content);
}
return $content;
}
/**
* Adds all the course/section/activity/block contents and rules
*/
public static function register_link_decoders($processor) {
$tasks = array(); // To get the list of tasks having decoders
// Add the course task
$tasks[] = 'restore_course_task';
// Add the section task
$tasks[] = 'restore_section_task';
// Add the module tasks
$mods = get_plugin_list('mod');
foreach ($mods as $mod => $moddir) {
if (class_exists('restore_' . $mod . '_activity_task')) {
$tasks[] = 'restore_' . $mod . '_activity_task';
}
}
// Add the default block task
$tasks[] = 'restore_default_block_task';
// Add the custom block tasks
$blocks = get_plugin_list('block');
foreach ($blocks as $block => $blockdir) {
if (class_exists('restore_' . $block . '_block_task')) {
$tasks[] = 'restore_' . $block . '_block_task';
}
}
// We have all the tasks registered, let's iterate over them, getting
// contents and rules and adding them to the processor
foreach ($tasks as $classname) {
// Get restore_decode_content array and add to processor
$contents = call_user_func(array($classname, 'define_decode_contents'));
if (!is_array($contents)) {
throw new restore_decode_processor_exception('define_decode_contents_not_array', $classname);
}
foreach ($contents as $content) {
$processor->add_content($content);
}
// Get restore_decode_rule array and add to processor
$rules = call_user_func(array($classname, 'define_decode_rules'));
if (!is_array($rules)) {
throw new restore_decode_processor_exception('define_decode_rules_not_array', $classname);
}
foreach ($rules as $rule) {
$processor->add_rule($rule);
}
}
// Now process all the plugins contents (note plugins don't have support for rules)
// TODO: Add other plugin types (course formats, local...) here if we add them to backup/restore
$plugins = array('qtype');
foreach ($plugins as $plugin) {
$contents = restore_plugin::get_restore_decode_contents($plugin);
if (!is_array($contents)) {
throw new restore_decode_processor_exception('get_restore_decode_contents_not_array', $plugin);
}
foreach ($contents as $content) {
$processor->add_content($content);
}
}
}
// Protected API starts here
/**
* Perform some general checks in content. Returning false rules processing is skipped
*/
protected function precheck_content($content) {
// Look for $@ in content (all interlinks contain that)
return (strpos($content, '$@') === false) ? false : $content;
}
}
/*
* Exception class used by all the @restore_decode_content stuff
*/
class restore_decode_processor_exception extends backup_exception {
public function __construct($errorcode, $a=NULL, $debuginfo=null) {
return parent::__construct($errorcode, $a, $debuginfo);
}
}