3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
26 * Base abstract class for all the helper classes providing various operations
28 * TODO: Finish phpdocs
30 abstract class backup_helper {
33 * Given one backupid, create all the needed dirs to have one backup temp dir available
35 static public function check_and_create_backup_dir($backupid) {
37 if (!check_dir_exists($CFG->dataroot . '/temp/backup/' . $backupid, true, true)) {
38 throw new backup_helper_exception('cannot_create_backup_temp_dir');
43 * Given one backupid, ensure its temp dir is completely empty
45 static public function clear_backup_dir($backupid) {
47 if (!self::delete_dir_contents($CFG->dataroot . '/temp/backup/' . $backupid)) {
48 throw new backup_helper_exception('cannot_empty_backup_temp_dir');
54 * Given one backupid, delete completely its temp dir
56 static public function delete_backup_dir($backupid) {
58 self::clear_backup_dir($backupid);
59 return rmdir($CFG->dataroot . '/temp/backup/' . $backupid);
63 * Given one fullpath to directory, delete its contents recursively
64 * Copied originally from somewhere in the net.
65 * TODO: Modernise this
67 static public function delete_dir_contents($dir, $excludeddir='') {
71 // if we've been given a directory that doesn't exist yet, return true.
72 // this happens when we're trying to clear out a course that has only just
78 // Create arrays to store files and directories
80 $dir_subdirs = array();
82 // Make sure we can delete it
83 chmod($dir, $CFG->directorypermissions);
85 if ((($handle = opendir($dir))) == false) {
86 // The directory could not be opened
90 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
91 while (false !== ($entry = readdir($handle))) {
92 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
93 $dir_subdirs[] = $dir. $slash .$entry;
95 } else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
96 $dir_files[] = $dir. $slash .$entry;
100 // Delete all files in the curent directory return false and halt if a file cannot be removed
101 for ($i=0; $i<count($dir_files); $i++) {
102 chmod($dir_files[$i], $CFG->directorypermissions);
103 if (((unlink($dir_files[$i]))) == false) {
108 // Empty sub directories and then remove the directory
109 for ($i=0; $i<count($dir_subdirs); $i++) {
110 chmod($dir_subdirs[$i], $CFG->directorypermissions);
111 if (self::delete_dir_contents($dir_subdirs[$i]) == false) {
114 if (remove_dir($dir_subdirs[$i]) == false) {
123 // Success, every thing is gone return true
128 * Delete all the temp dirs older than the time specified
130 static public function delete_old_backup_dirs($deletefrom) {
134 // Get files and directories in the temp backup dir witout descend
135 $list = get_directory_list($CFG->dataroot . '/temp/backup', '', false, true, true);
136 foreach ($list as $file) {
137 $file_path = $CFG->dataroot . '/temp/backup/' . $file;
138 $moddate = filemtime($file_path);
139 if ($status && $moddate < $deletefrom) {
140 //If directory, recurse
141 if (is_dir($file_path)) {
142 // $file is really the backupid
143 $status = self::delete_backup_dir($file);
151 throw new backup_helper_exception('problem_deleting_old_backup_temp_dirs');
156 * This function will be invoked by any log() method in backup/restore, acting
157 * as a simple forwarder to the standard loggers but also, if the $display
158 * parameter is true, supporting translation via get_string() and sending to
161 static public function log($message, $level, $a, $depth, $display, $logger) {
162 // Send to standard loggers
163 $logmessage = $message;
164 $options = empty($depth) ? array() : array('depth' => $depth);
166 $logmessage = $logmessage . ' ' . implode(', ', (array)$a);
168 $logger->process($logmessage, $level, $options);
170 // If $display specified, send translated string to output_controller
172 output_controller::get_instance()->output($message, 'backup', $a, $depth);
177 * Given one backupid and the (FS) final generated file, perform its final storage
178 * into Moodle file storage. For stored files it returns the complete file_info object
180 static public function store_backup_file($backupid, $filepath) {
182 // First of all, get some information from the backup_controller to help us decide
183 list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($backupid);
185 // Extract useful information to decide
186 $hasusers = (bool)$sinfo['users']->value; // Backup has users
187 $isannon = (bool)$sinfo['anonymize']->value; // Backup is anonymised
188 $filename = $sinfo['filename']->value; // Backup filename
189 $backupmode= $dinfo[0]->mode; // Backup mode backup::MODE_GENERAL/IMPORT/HUB
190 $backuptype= $dinfo[0]->type; // Backup type backup::TYPE_1ACTIVITY/SECTION/COURSE
191 $userid = $dinfo[0]->userid; // User->id executing the backup
192 $id = $dinfo[0]->id; // Id of activity/section/course (depends of type)
193 $courseid = $dinfo[0]->courseid; // Id of the course
195 // Quick hack. If for any reason, filename is blank, fix it here.
196 // TODO: This hack will be out once MDL-22142 - P26 gets fixed
197 if (empty($filename)) {
198 $filename = backup_plan_dbops::get_default_backup_filename('moodle2', $backuptype, $id, $hasusers, $isannon);
201 // Backups of type IMPORT aren't stored ever
202 if ($backupmode == backup::MODE_IMPORT) {
206 // Calculate file storage options of id being backup
211 switch ($backuptype) {
212 case backup::TYPE_1ACTIVITY:
213 $ctxid = get_context_instance(CONTEXT_MODULE, $id)->id;
214 $component = 'backup';
215 $filearea = 'activity';
218 case backup::TYPE_1SECTION:
219 $ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
220 $component = 'backup';
221 $filearea = 'section';
224 case backup::TYPE_1COURSE:
225 $ctxid = get_context_instance(CONTEXT_COURSE, $courseid)->id;
226 $component = 'backup';
227 $filearea = 'course';
232 // Backups of type HUB (by definition never have user info)
233 // are sent to user's "user_tohub" file area. The upload process
234 // will be responsible for cleaning that filearea once finished
235 if ($backupmode == backup::MODE_HUB) {
236 $ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
242 // Backups without user info or with the anonymise functionality
243 // enabled are sent to user's "user_backup"
244 // file area. Maintenance of such area is responsibility of
245 // the user via corresponding file manager frontend
246 if ($backupmode == backup::MODE_GENERAL && (!$hasusers || $isannon)) {
247 $ctxid = get_context_instance(CONTEXT_USER, $userid)->id;
249 $filearea = 'backup';
253 // Let's send the file to file storage, everything already defined
254 $fs = get_file_storage();
256 'contextid' => $ctxid,
257 'component' => $component,
258 'filearea' => $filearea,
261 'filename' => $filename,
263 'timecreated' => time(),
264 'timemodified'=> time());
265 // If file already exists, delete if before
266 // creating it again. This is BC behaviour - copy()
267 // overwrites by default
268 if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
269 $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
270 $sf = $fs->get_file_by_hash($pathnamehash);
273 return $fs->create_file_from_pathname($fr, $filepath);
277 * This function simply marks one param to be considered as straight sql
278 * param, so it won't be searched in the structure tree nor converted at
279 * all. Useful for better integration of definition of sources in structure
282 public static function is_sqlparam($value) {
283 return array('sqlparam' => $value);
287 * This function returns one array of itemnames that are being handled by
288 * inforef.xml files. Used both by backup and restore
290 public static function get_inforef_itemnames() {
291 return array('user', 'grouping', 'group', 'role', 'file', 'scale', 'outcome', 'grade_item', 'question_category');
296 * Exception class used by all the @helper stuff
298 class backup_helper_exception extends backup_exception {
300 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
301 parent::__construct($errorcode, $a, $debuginfo);