Commit | Line | Data |
---|---|---|
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 | */ | |
32 | abstract 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 | |
dc5a2f8a EL |
59 | /** |
60 | * Load and format all the needed information from moodle_backup.xml | |
61 | * | |
62 | * This function loads and process all the moodle_backup.xml | |
63 | * information, composing a big information structure that will | |
64 | * be the used by the plan builder in order to generate the | |
65 | * appropiate tasks / steps / settings | |
66 | */ | |
67 | public static function get_backup_information($tempdir) { | |
68 | global $CFG; | |
69 | ||
70 | $info = new stdclass(); // Final information goes here | |
71 | ||
72 | $moodlefile = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle_backup.xml'; | |
73 | if (!file_exists($moodlefile)) { // Shouldn't happen ever, but... | |
74 | throw new backup_helper_exception('missing_moodle_backup_xml_file'); | |
75 | } | |
76 | // Load the entire file to in-memory array | |
77 | $xmlparser = new progressive_parser(); | |
78 | $xmlparser->set_file($moodlefile); | |
79 | $xmlprocessor = new restore_moodlexml_parser_processor(); | |
80 | $xmlparser->set_processor($xmlprocessor); | |
81 | $xmlparser->process(); | |
82 | $infoarr = $xmlprocessor->get_all_chunks(); | |
83 | if (count($infoarr) !== 1) { // Shouldn't happen ever, but... | |
84 | throw new backup_helper_exception('problem_parsing_moodle_backup_xml_file'); | |
85 | } | |
86 | $infoarr = $infoarr[0]['tags']; // for commodity | |
87 | ||
88 | // Let's build info | |
89 | $info->moodle_version = $infoarr['moodle_version']; | |
90 | $info->moodle_release = $infoarr['moodle_release']; | |
91 | $info->backup_version = $infoarr['backup_version']; | |
92 | $info->backup_release = $infoarr['backup_release']; | |
93 | $info->backup_date = $infoarr['backup_date']; | |
94 | $info->original_wwwroot = $infoarr['original_wwwroot']; | |
95 | $info->original_site_identifier = $infoarr['original_site_identifier']; | |
96 | $info->original_course_id = $infoarr['original_course_id']; | |
97 | $info->type = $infoarr['details']['detail'][0]['type']; | |
98 | $info->format = $infoarr['details']['detail'][0]['format']; | |
99 | $info->mode = $infoarr['details']['detail'][0]['mode']; | |
100 | ||
101 | // Now the contents | |
102 | $contentsarr = $infoarr['contents']; | |
103 | if (isset($contentsarr['course']) && isset($contentsarr['course'][0])) { | |
104 | $info->course = new stdclass(); | |
105 | $info->course = (object)$contentsarr['course'][0]; | |
106 | $info->course->settings = array(); | |
107 | } | |
108 | if (isset($contentsarr['sections']) && isset($contentsarr['sections']['section'])) { | |
109 | $sectionarr = $contentsarr['sections']['section']; | |
110 | $sections = array(); | |
111 | foreach ($sectionarr as $section) { | |
112 | $section = (object)$section; | |
113 | $section->settings = array(); | |
114 | $sections[basename($section->directory)] = $section; | |
115 | } | |
116 | $info->sections = $sections; | |
117 | } | |
118 | if (isset($contentsarr['activities']) && isset($contentsarr['activities']['activity'])) { | |
119 | $activityarr = $contentsarr['activities']['activity']; | |
120 | $activities = array(); | |
121 | foreach ($activityarr as $activity) { | |
122 | $activity = (object)$activity; | |
123 | $activity->settings = array(); | |
124 | $activities[basename($activity->directory)] = $activity; | |
125 | } | |
126 | $info->activities = $activities; | |
127 | } | |
128 | $info->root_settings = array(); // For root settings | |
129 | ||
130 | // Now the settings, putting each one under its owner | |
131 | $settingsarr = $infoarr['settings']['setting']; | |
132 | foreach($settingsarr as $setting) { | |
133 | switch ($setting['level']) { | |
134 | case 'root': | |
135 | $info->root_settings[$setting['name']] = $setting['value']; | |
136 | break; | |
137 | case 'course': | |
138 | $info->course->settings[$setting['name']] = $setting['value']; | |
139 | break; | |
140 | case 'section': | |
141 | $info->sections[$setting['section']]->settings[$setting['name']] = $setting['value']; | |
142 | break; | |
143 | case 'activity': | |
144 | $info->activities[$setting['activity']]->settings[$setting['name']] = $setting['value']; | |
145 | break; | |
146 | default: // Shouldn't happen | |
147 | throw new backup_helper_exception('wrong_setting_level_moodle_backup_xml_file', $setting['level']); | |
148 | } | |
149 | } | |
150 | ||
151 | return $info; | |
152 | } | |
153 | ||
f60f4666 EL |
154 | /** |
155 | * Given one temp/backup/xxx dir, detect its format | |
156 | * | |
157 | * TODO: Move harcoded detection here to delegated classes under backup/format (moodle1, imscc..) | |
158 | * conversion code will be there too. | |
159 | */ | |
160 | public static function detect_backup_format($tempdir) { | |
161 | global $CFG; | |
162 | ||
163 | // First look for MOODLE (moodle2) format | |
164 | $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle_backup.xml'; | |
165 | if (file_exists($filepath)) { // Looks promising, lets load some information | |
166 | $handle = fopen ($filepath, "r"); | |
167 | $first_chars = fread($handle,200); | |
168 | $status = fclose ($handle); | |
169 | // Check if it has the required strings | |
170 | if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false && | |
171 | strpos($first_chars,'<moodle_backup>') !== false && | |
172 | strpos($first_chars,'<information>') !== false) { | |
173 | return backup::FORMAT_MOODLE; | |
174 | } | |
175 | } | |
176 | ||
177 | // Then look for MOODLE1 (moodle1) format | |
178 | $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle.xml'; | |
179 | if (file_exists($filepath)) { // Looks promising, lets load some information | |
180 | $handle = fopen ($filepath, "r"); | |
181 | $first_chars = fread($handle,200); | |
182 | $status = fclose ($handle); | |
183 | // Check if it has the required strings | |
184 | if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false && | |
185 | strpos($first_chars,'<MOODLE_BACKUP>') !== false && | |
186 | strpos($first_chars,'<INFO>') !== false) { | |
187 | return backup::FORMAT_MOODLE1; | |
188 | } | |
189 | } | |
190 | ||
191 | // Other formats | |
192 | ||
193 | // Arrived here, unknown format | |
194 | return backup::FORMAT_UNKNOWN; | |
195 | } | |
69dd0c8c | 196 | } |