Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
5
// Moodle is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// Moodle is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
/**
19
 * Defines restore_subplugin class
20
 *
21
 * @package     core_backup
22
 * @subpackage  moodle2
23
 * @category    backup
24
 * @copyright   2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
/**
31
 * Class implementing the subplugins support for moodle2 restore
32
 *
33
 * TODO: Finish phpdocs
34
 * TODO: Make this subclass of restore_plugin
35
 * TODO: Add support for declaring decode_contents (not decode_rules)
36
 */
37
abstract class restore_subplugin {
38
 
39
    /** @var string */
40
    protected $subplugintype;
41
    /** @var string */
42
    protected $subpluginname;
43
    /** @var restore_path_element */
44
    protected $connectionpoint;
45
    /** @var restore_step */
46
    protected $step;
47
    /** @var restore_task */
48
    protected $task;
49
 
50
    public function __construct($subplugintype, $subpluginname, $step) {
51
        $this->subplugintype = $subplugintype;
52
        $this->subpluginname = $subpluginname;
53
        $this->step          = $step;
54
        $this->task          = $step->get_task();
55
        $this->connectionpoint = '';
56
    }
57
 
58
    public function define_subplugin_structure($connectionpoint) {
59
        if (!$connectionpoint instanceof restore_path_element) {
60
            throw new restore_step_exception('restore_path_element_required', $connectionpoint);
61
        }
62
 
63
        $paths = array();
64
        $this->connectionpoint = $connectionpoint;
65
        $methodname = 'define_' . basename($this->connectionpoint->get_path()) . '_subplugin_structure';
66
 
67
        if (method_exists($this, $methodname)) {
68
            if ($subbluginpaths = $this->$methodname()) {
69
                foreach ($subbluginpaths as $path) {
70
                    $path->set_processing_object($this);
71
                    $paths[] = $path;
72
                }
73
            }
74
        }
75
        return $paths;
76
    }
77
 
78
    /**
79
     * after_execute dispatcher for any restore_subplugin class
80
     *
81
     * This method will dispatch execution to the corresponding
82
     * after_execute_xxx() method when available, with xxx
83
     * being the connection point of the instance, so subplugin
84
     * classes with multiple connection points will support
85
     * multiple after_execute methods, one for each connection point
86
     */
87
    public function launch_after_execute_methods() {
88
        // Check if the after_execute method exists and launch it
89
        $afterexecute = 'after_execute_' . basename($this->connectionpoint->get_path());
90
        if (method_exists($this, $afterexecute)) {
91
            $this->$afterexecute();
92
        }
93
    }
94
 
95
    /**
96
     * The after_restore dispatcher for any restore_subplugin class.
97
     *
98
     * This method will dispatch execution to the corresponding
99
     * after_restore_xxx() method when available, with xxx
100
     * being the connection point of the instance, so subplugin
101
     * classes with multiple connection points will support
102
     * multiple after_restore methods, one for each connection point.
103
     */
104
    public function launch_after_restore_methods() {
105
        // Check if the after_restore method exists and launch it.
106
        $afterestore = 'after_restore_' . basename($this->connectionpoint->get_path());
107
        if (method_exists($this, $afterestore)) {
108
            $this->$afterestore();
109
        }
110
    }
111
 
112
// Protected API starts here
113
 
114
// restore_step/structure_step/task wrappers
115
 
116
    protected function get_restoreid() {
117
        if (is_null($this->task)) {
118
            throw new restore_step_exception('not_specified_restore_task');
119
        }
120
        return $this->task->get_restoreid();
121
    }
122
 
123
    /**
124
     * To send ids pairs to backup_ids_table and to store them into paths
125
     *
126
     * This method will send the given itemname and old/new ids to the
127
     * backup_ids_temp table, and, at the same time, will save the new id
128
     * into the corresponding restore_path_element for easier access
129
     * by children. Also will inject the known old context id for the task
130
     * in case it's going to be used for restoring files later
131
     */
132
    protected function set_mapping($itemname, $oldid, $newid, $restorefiles = false, $filesctxid = null, $parentid = null) {
133
        $this->step->set_mapping($itemname, $oldid, $newid, $restorefiles, $filesctxid, $parentid);
134
    }
135
 
136
    /**
137
     * Returns the latest (parent) old id mapped by one pathelement
138
     */
139
    protected function get_old_parentid($itemname) {
140
        return $this->step->get_old_parentid($itemname);
141
    }
142
 
143
    /**
144
     * Returns the latest (parent) new id mapped by one pathelement
145
     */
146
    protected function get_new_parentid($itemname) {
147
        return $this->step->get_new_parentid($itemname);
148
    }
149
 
150
    /**
151
     * Return the new id of a mapping for the given itemname
152
     *
153
     * @param string $itemname the type of item
154
     * @param int $oldid the item ID from the backup
155
     * @param mixed $ifnotfound what to return if $oldid wasnt found. Defaults to false
156
     */
157
    protected function get_mappingid($itemname, $oldid, $ifnotfound = false) {
158
        return $this->step->get_mappingid($itemname, $oldid, $ifnotfound);
159
    }
160
 
161
    /**
162
     * Return the complete mapping from the given itemname, itemid
163
     */
164
    protected function get_mapping($itemname, $oldid) {
165
        return $this->step->get_mapping($itemname, $oldid);
166
    }
167
 
168
    /**
169
     * Add all the existing file, given their component and filearea and one backup_ids itemname to match with
170
     */
171
    protected function add_related_files($component, $filearea, $mappingitemname, $filesctxid = null, $olditemid = null) {
172
        $this->step->add_related_files($component, $filearea, $mappingitemname, $filesctxid, $olditemid);
173
    }
174
 
175
    /**
176
     * Apply course startdate offset based in original course startdate and course_offset_startdate setting
177
     * Note we are using one static cache here, but *by restoreid*, so it's ok for concurrence/multiple
178
     * executions in the same request
179
     */
180
    protected function apply_date_offset($value) {
181
        return $this->step->apply_date_offset($value);
182
    }
183
 
184
    /**
185
     * Call the log function from the step.
186
     */
187
    public function log($message, $level, $a = null, $depth = null, $display = false) {
188
        return $this->step->log($message, $level, $a, $depth, $display);
189
    }
190
 
191
    /**
192
     * Returns the value of one (task/plan) setting
193
     */
194
    protected function get_setting_value($name) {
195
        if (is_null($this->task)) {
196
            throw new restore_step_exception('not_specified_restore_task');
197
        }
198
        return $this->task->get_setting_value($name);
199
    }
200
 
201
// end of restore_step/structure_step/task wrappers
202
 
203
    /**
204
     * Simple helper function that returns the name for the restore_path_element
205
     * It's not mandatory to use it but recommended ;-)
206
     */
207
    protected function get_namefor($name = '') {
208
        $name = $name !== '' ? '_' . $name : '';
209
        return $this->subplugintype . '_' . $this->subpluginname . $name;
210
    }
211
 
212
    /**
213
     * Simple helper function that returns the base (prefix) of the path for the restore_path_element
214
     * Useful if we used get_recommended_name() in backup. It's not mandatory to use it but recommended ;-)
215
     */
216
    protected function get_pathfor($path = '') {
217
        $path = trim($path, '/') !== '' ? '/' . trim($path, '/') : '';
218
        return $this->connectionpoint->get_path() . '/' .
219
               'subplugin_' . $this->subplugintype . '_' .
220
               $this->subpluginname . '_' . basename($this->connectionpoint->get_path()) . $path;
221
    }
222
}