Updated the HEAD build version to 20100427
[moodle.git] / backup / util / helper / backup_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 * Base abstract class for all the helper classes providing various operations
27 *
28 * TODO: Finish phpdocs
29 */
30abstract class backup_helper {
31
32 /**
33 * Given one backupid, create all the needed dirs to have one backup temp dir available
34 */
35 static public function check_and_create_backup_dir($backupid) {
36 global $CFG;
37 if (!check_dir_exists($CFG->dataroot . '/temp/backup/' . $backupid, true, true)) {
38 throw new backup_helper_exception('cannot_create_backup_temp_dir');
39 }
40 }
41
42 /**
43 * Given one backupid, ensure its temp dir is completelly empty
44 */
45 static public function clear_backup_dir($backupid) {
46 global $CFG;
47 if (!self::delete_dir_contents($CFG->dataroot . '/temp/backup/' . $backupid)) {
48 throw new backup_helper_exception('cannot_empty_backup_temp_dir');
49 }
50 }
51
52 /**
53 * Given one fullpath to directory, delete its contents recursively
54 * Copied originally from somewhere in the net.
55 * TODO: Modernise this
56 */
57 static public function delete_dir_contents($dir, $excludeddir='') {
58 if (!is_dir($dir)) {
59 // if we've been given a directory that doesn't exist yet, return true.
60 // this happens when we're trying to clear out a course that has only just
61 // been created.
62 return true;
63 }
64 $slash = "/";
65
66 // Create arrays to store files and directories
67 $dir_files = array();
68 $dir_subdirs = array();
69
70 // Make sure we can delete it
71 chmod($dir, 0777);
72
73 if ((($handle = opendir($dir))) == false) {
74 // The directory could not be opened
75 return false;
76 }
77
78 // Loop through all directory entries, and construct two temporary arrays containing files and sub directories
79 while (false !== ($entry = readdir($handle))) {
80 if (is_dir($dir. $slash .$entry) && $entry != ".." && $entry != "." && $entry != $excludeddir) {
81 $dir_subdirs[] = $dir. $slash .$entry;
82
83 } else if ($entry != ".." && $entry != "." && $entry != $excludeddir) {
84 $dir_files[] = $dir. $slash .$entry;
85 }
86 }
87
88 // Delete all files in the curent directory return false and halt if a file cannot be removed
89 for ($i=0; $i<count($dir_files); $i++) {
90 chmod($dir_files[$i], 0777);
91 if (((unlink($dir_files[$i]))) == false) {
92 return false;
93 }
94 }
95
96 // Empty sub directories and then remove the directory
97 for ($i=0; $i<count($dir_subdirs); $i++) {
98 chmod($dir_subdirs[$i], 0777);
99 if (self::delete_dir_contents($dir_subdirs[$i]) == false) {
100 return false;
101 } else {
102 if (remove_dir($dir_subdirs[$i]) == false) {
103 return false;
104 }
105 }
106 }
107
108 // Close directory
109 closedir($handle);
110
111 // Success, every thing is gone return true
112 return true;
113 }
114
115 /**
116 * Delete all the temp dirs older than the time specified
117 */
118 static public function delete_old_backup_dirs($deletefrom) {
119 global $CFG;
120
121 $status = true;
122 // Get files and directories in the temp backup dir witout descend
123 $list = get_directory_list($CFG->dataroot . '/temp/backup', '', false, true, true);
124 foreach ($list as $file) {
125 $file_path = $CFG->dataroot . '/temp/backup/' . $file;
126 $moddate = filemtime($file_path);
127 if ($status && $moddate < $deletefrom) {
128 //If directory, recurse
129 if (is_dir($file_path)) {
130 $status = self::delete_dir_contents($file_path);
131 //There is nothing, delete the directory itself
132 if ($status) {
133 $status = rmdir($file_path);
134 }
135 //If file
136 } else {
137 unlink($file_path);
138 }
139 }
140 }
141 if (!$status) {
142 throw new backup_helper_exception('problem_deleting_old_backup_temp_dirs');
143 }
144 }
145
146 /**
147 * This function will be invoked by any log() method in backup/restore, acting
148 * as a simple forwarder to the standard loggers but also, if the $display
149 * parameter is true, supporting translation via get_string() and sending to
150 * standard output.
151 */
152 static public function log($message, $level, $a, $depth, $display, $logger) {
153 // Send to standard loggers
154 $logmessage = $message;
155 $options = empty($depth) ? array() : array('depth' => $depth);
156 if (!empty($a)) {
157 $logmessage = $logmessage . ' ' . implode(', ', (array)$a);
158 }
159 $logger->process($logmessage, $level, $options);
160
161 // If $display specified, send translated string to output_controller
162 if ($display) {
163 output_controller::get_instance()->output($message, 'backup', $a, $depth);
164 }
165 }
166}
167
168/*
169 * Exception class used by all the @helper stuff
170 */
171class backup_helper_exception extends backup_exception {
172
173 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
174 parent::__construct($errorcode, $a, $debuginfo);
175 }
176}