MDL-35117 librairies: File Packer can only extract a specific list a files
[moodle.git] / backup / util / helper / backup_general_helper.class.php
CommitLineData
69dd0c8c
EL
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 * @package moodlecore
20 * @subpackage backup-helper
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Non instantiable helper class providing general helper methods for backup/restore
27 *
28 * This class contains various general helper static methods available for backup/restore
29 *
30 * TODO: Finish phpdocs
31 */
32abstract class backup_general_helper extends backup_helper {
33
34 /**
35 * Calculate one checksum for any array/object. Works recursively
36 */
37 public static function array_checksum_recursive($arr) {
38
39 $checksum = ''; // Init checksum
40
41 // Check we are going to process one array always, objects must be cast before
42 if (!is_array($arr)) {
43 throw new backup_helper_exception('array_expected');
44 }
45 foreach ($arr as $key => $value) {
46 if ($value instanceof checksumable) {
47 $checksum = md5($checksum . '-' . $key . '-' . $value->calculate_checksum());
48 } else if (is_object($value)) {
49 $checksum = md5($checksum . '-' . $key . '-' . self::array_checksum_recursive((array)$value));
50 } else if (is_array($value)) {
51 $checksum = md5($checksum . '-' . $key . '-' . self::array_checksum_recursive($value));
52 } else {
53 $checksum = md5($checksum . '-' . $key . '-' . $value);
54 }
55 }
56 return $checksum;
57 }
f60f4666 58
90338ad1
EL
59 /**
60 * Load all the blocks information needed for a given path within moodle2 backup
61 *
62 * This function, given one full path (course, activities/xxxx) will look for all the
63 * blocks existing in the backup file, returning one array used to build the
64 * proper restore plan by the @restore_plan_builder
65 */
66 public static function get_blocks_from_path($path) {
67 global $DB;
68
69 $blocks = array(); // To return results
70
71 static $availableblocks = array(); // Get and cache available blocks
72 if (empty($availableblocks)) {
73 $availableblocks = array_keys(get_plugin_list('block'));
74 }
75
76 $path = $path . '/blocks'; // Always look under blocks subdir
77
78 if (!is_dir($path)) {
79 return array();
80 }
81
80c27aab
CF
82 if (!$dir = opendir($path)) {
83 return array();
84 }
90338ad1
EL
85 while (false !== ($file = readdir($dir))) {
86 if ($file == '.' || $file == '..') { // Skip dots
87 continue;
88 }
89 if (is_dir($path .'/' . $file)) { // Dir found, check it's a valid block
90 if (!file_exists($path .'/' . $file . '/block.xml')) { // Skip if xml file not found
91 continue;
92 }
93 // Extract block name
94 $blockname = preg_replace('/(.*)_\d+/', '\\1', $file);
95 // Check block exists and is installed
96 if (in_array($blockname, $availableblocks) && $DB->record_exists('block', array('name' => $blockname))) {
97 $blocks[$path .'/' . $file] = $blockname;
98 }
99 }
100 }
101 closedir($dir);
102
103 return $blocks;
104 }
105
dc5a2f8a
EL
106 /**
107 * Load and format all the needed information from moodle_backup.xml
108 *
109 * This function loads and process all the moodle_backup.xml
110 * information, composing a big information structure that will
111 * be the used by the plan builder in order to generate the
112 * appropiate tasks / steps / settings
113 */
114 public static function get_backup_information($tempdir) {
115 global $CFG;
116
117 $info = new stdclass(); // Final information goes here
118
7aa06e6d 119 $moodlefile = $CFG->tempdir . '/backup/' . $tempdir . '/moodle_backup.xml';
dc5a2f8a 120 if (!file_exists($moodlefile)) { // Shouldn't happen ever, but...
482aac65 121 throw new backup_helper_exception('missing_moodle_backup_xml_file', $moodlefile);
dc5a2f8a
EL
122 }
123 // Load the entire file to in-memory array
124 $xmlparser = new progressive_parser();
125 $xmlparser->set_file($moodlefile);
126 $xmlprocessor = new restore_moodlexml_parser_processor();
127 $xmlparser->set_processor($xmlprocessor);
128 $xmlparser->process();
129 $infoarr = $xmlprocessor->get_all_chunks();
130 if (count($infoarr) !== 1) { // Shouldn't happen ever, but...
131 throw new backup_helper_exception('problem_parsing_moodle_backup_xml_file');
132 }
133 $infoarr = $infoarr[0]['tags']; // for commodity
134
135 // Let's build info
136 $info->moodle_version = $infoarr['moodle_version'];
137 $info->moodle_release = $infoarr['moodle_release'];
138 $info->backup_version = $infoarr['backup_version'];
139 $info->backup_release = $infoarr['backup_release'];
140 $info->backup_date = $infoarr['backup_date'];
c3ea499d 141 $info->mnet_remoteusers = $infoarr['mnet_remoteusers'];
dc5a2f8a 142 $info->original_wwwroot = $infoarr['original_wwwroot'];
482aac65 143 $info->original_site_identifier_hash = $infoarr['original_site_identifier_hash'];
dc5a2f8a 144 $info->original_course_id = $infoarr['original_course_id'];
560811a9
EL
145 $info->original_course_fullname = $infoarr['original_course_fullname'];
146 $info->original_course_shortname= $infoarr['original_course_shortname'];
147 $info->original_course_startdate= $infoarr['original_course_startdate'];
2df0f295 148 $info->original_course_contextid= $infoarr['original_course_contextid'];
3a1cccc6 149 $info->original_system_contextid= $infoarr['original_system_contextid'];
ac0628e9
DC
150 // Moodle backup file don't have this option before 2.3
151 if (!empty($infoarr['include_file_references_to_external_content'])) {
152 $info->include_file_references_to_external_content = 1;
153 } else {
154 $info->include_file_references_to_external_content = 0;
155 }
dc5a2f8a
EL
156 $info->type = $infoarr['details']['detail'][0]['type'];
157 $info->format = $infoarr['details']['detail'][0]['format'];
158 $info->mode = $infoarr['details']['detail'][0]['mode'];
8d4e41f4
EL
159 // Build the role mappings custom object
160 $rolemappings = new stdclass();
161 $rolemappings->modified = false;
162 $rolemappings->mappings = array();
163 $info->role_mappings = $rolemappings;
438606fc
EL
164 // Some initially empty containers
165 $info->sections = array();
166 $info->activities = array();
dc5a2f8a
EL
167
168 // Now the contents
169 $contentsarr = $infoarr['contents'];
170 if (isset($contentsarr['course']) && isset($contentsarr['course'][0])) {
171 $info->course = new stdclass();
172 $info->course = (object)$contentsarr['course'][0];
173 $info->course->settings = array();
174 }
175 if (isset($contentsarr['sections']) && isset($contentsarr['sections']['section'])) {
176 $sectionarr = $contentsarr['sections']['section'];
dc5a2f8a
EL
177 foreach ($sectionarr as $section) {
178 $section = (object)$section;
179 $section->settings = array();
180 $sections[basename($section->directory)] = $section;
181 }
182 $info->sections = $sections;
183 }
184 if (isset($contentsarr['activities']) && isset($contentsarr['activities']['activity'])) {
185 $activityarr = $contentsarr['activities']['activity'];
dc5a2f8a
EL
186 foreach ($activityarr as $activity) {
187 $activity = (object)$activity;
188 $activity->settings = array();
189 $activities[basename($activity->directory)] = $activity;
190 }
191 $info->activities = $activities;
192 }
193 $info->root_settings = array(); // For root settings
194
195 // Now the settings, putting each one under its owner
196 $settingsarr = $infoarr['settings']['setting'];
197 foreach($settingsarr as $setting) {
198 switch ($setting['level']) {
199 case 'root':
200 $info->root_settings[$setting['name']] = $setting['value'];
201 break;
202 case 'course':
203 $info->course->settings[$setting['name']] = $setting['value'];
204 break;
205 case 'section':
206 $info->sections[$setting['section']]->settings[$setting['name']] = $setting['value'];
207 break;
208 case 'activity':
209 $info->activities[$setting['activity']]->settings[$setting['name']] = $setting['value'];
210 break;
211 default: // Shouldn't happen
212 throw new backup_helper_exception('wrong_setting_level_moodle_backup_xml_file', $setting['level']);
213 }
214 }
215
216 return $info;
217 }
218
482aac65
EL
219 /**
220 * Given the information fetched from moodle_backup.xml file
221 * decide if we are restoring in the same site the backup was
222 * generated or no. Behavior of various parts of restore are
223 * dependent of this.
224 *
99a82518
DM
225 * Backups created natively in 2.0 and later declare the hashed
226 * site identifier. Backups created by conversion from a 1.9
227 * backup do not declare such identifier, so there is a fallback
228 * to wwwroot comparison. See MDL-16614.
482aac65
EL
229 */
230 public static function backup_is_samesite($info) {
231 global $CFG;
232 $hashedsiteid = md5(get_site_identifier());
233 if (isset($info->original_site_identifier_hash) && !empty($info->original_site_identifier_hash)) {
234 return $info->original_site_identifier_hash == $hashedsiteid;
235 } else {
236 return $info->original_wwwroot == $CFG->wwwroot;
237 }
238 }
239
f60f4666 240 /**
0164592b 241 * Detects the format of the given unpacked backup directory
f60f4666 242 *
0164592b
DM
243 * @param string $tempdir the name of the backup directory
244 * @return string one of backup::FORMAT_xxx constants
f60f4666
EL
245 */
246 public static function detect_backup_format($tempdir) {
247 global $CFG;
1e2c7351 248 require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php');
90338ad1 249
0164592b
DM
250 if (convert_helper::detect_moodle2_format($tempdir)) {
251 return backup::FORMAT_MOODLE;
90338ad1 252 }
f60f4666 253
0164592b 254 // see if a converter can identify the format
383f6f63 255 $converters = convert_helper::available_converters();
0164592b
DM
256 foreach ($converters as $name) {
257 $classname = "{$name}_converter";
258 if (!class_exists($classname)) {
259 throw new coding_exception("available_converters() is supposed to load
260 converter classes but class $classname not found");
f60f4666 261 }
f60f4666 262
0164592b
DM
263 $detected = call_user_func($classname .'::detect_format', $tempdir);
264 if (!empty($detected)) {
265 return $detected;
f60f4666
EL
266 }
267 }
268
f60f4666
EL
269 return backup::FORMAT_UNKNOWN;
270 }
69dd0c8c 271}