[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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-controller 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 * Class implementing the controller of any backup process 27 * 28 * This final class is in charge of controlling all the backup architecture, for any 29 * type of backup. Based in type, format, interactivity and target, it stores the 30 * whole execution plan and settings that will be used later by the @backup_worker, 31 * applies all the defaults, performs all the security contraints and is in charge 32 * of handling the ui if necessary. Also logging strategy is defined here. 33 * 34 * Note the class is 100% neutral and usable for *any* backup. It just stores/requests 35 * all the needed information from other backup classes in order to have everything well 36 * structured in order to allow the @backup_worker classes to do their job. 37 * 38 * In other words, a mammoth class, but don't worry, practically everything is delegated/ 39 * aggregated!) 40 * 41 * TODO: Finish phpdocs 42 */ 43 class backup_controller extends base_controller { 44 45 protected $backupid; // Unique identificator for this backup 46 47 protected $type; // Type of backup (activity, section, course) 48 protected $id; // Course/section/course_module id to backup 49 protected $courseid; // courseid where the id belongs to 50 protected $format; // Format of backup (moodle, imscc) 51 protected $interactive; // yes/no 52 protected $mode; // Purpose of the backup (default settings) 53 protected $userid; // user id executing the backup 54 protected $operation; // Type of operation (backup/restore) 55 56 protected $status; // Current status of the controller (created, planned, configured...) 57 58 protected $plan; // Backup execution plan 59 protected $includefiles; // Whether this backup includes files or not. 60 61 protected $execution; // inmediate/delayed 62 protected $executiontime; // epoch time when we want the backup to be executed (requires cron to run) 63 64 protected $destination; // Destination chain object (fs_moodle, fs_os, db, email...) 65 66 protected $checksum; // Cache @checksumable results for lighter @is_checksum_correct() uses 67 68 /** 69 * Constructor for the backup controller class. 70 * 71 * @param int $type Type of the backup; One of backup::TYPE_1COURSE, TYPE_1SECTION, TYPE_1ACTIVITY 72 * @param int $id The ID of the item to backup; e.g the course id 73 * @param int $format The backup format to use; Most likely backup::FORMAT_MOODLE 74 * @param bool $interactive Whether this backup will require user interaction; backup::INTERACTIVE_YES or INTERACTIVE_NO 75 * @param int $mode One of backup::MODE_GENERAL, MODE_IMPORT, MODE_SAMESITE, MODE_HUB, MODE_AUTOMATED 76 * @param int $userid The id of the user making the backup 77 */ 78 public function __construct($type, $id, $format, $interactive, $mode, $userid){ 79 $this->type = $type; 80 $this->id = $id; 81 $this->courseid = backup_controller_dbops::get_courseid_from_type_id($this->type, $this->id); 82 $this->format = $format; 83 $this->interactive = $interactive; 84 $this->mode = $mode; 85 $this->userid = $userid; 86 87 // Apply some defaults 88 $this->execution = backup::EXECUTION_INMEDIATE; 89 $this->operation = backup::OPERATION_BACKUP; 90 $this->executiontime = 0; 91 $this->checksum = ''; 92 93 // Apply current backup version and release if necessary 94 backup_controller_dbops::apply_version_and_release(); 95 96 // Check format and type are correct 97 backup_check::check_format_and_type($this->format, $this->type); 98 99 // Check id is correct 100 backup_check::check_id($this->type, $this->id); 101 102 // Check user is correct 103 backup_check::check_user($this->userid); 104 105 // Calculate unique $backupid 106 $this->calculate_backupid(); 107 108 // Default logger chain (based on interactive/execution) 109 $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->backupid); 110 111 // By default there is no progress reporter. Interfaces that wish to 112 // display progress must set it. 113 $this->progress = new \core\progress\null(); 114 115 // Instantiate the output_controller singleton and active it if interactive and inmediate 116 $oc = output_controller::get_instance(); 117 if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) { 118 $oc->set_active(true); 119 } 120 121 $this->log('instantiating backup controller', backup::LOG_INFO, $this->backupid); 122 123 // Default destination chain (based on type/mode/execution) 124 $this->destination = backup_factory::get_destination_chain($this->type, $this->id, $this->mode, $this->execution); 125 126 // Set initial status 127 $this->set_status(backup::STATUS_CREATED); 128 129 // Load plan (based on type/format) 130 $this->load_plan(); 131 132 // Apply all default settings (based on type/format/mode) 133 $this->apply_defaults(); 134 135 // Perform all initial security checks and apply (2nd param) them to settings automatically 136 backup_check::check_security($this, true); 137 138 // Set status based on interactivity 139 if ($this->interactive == backup::INTERACTIVE_YES) { 140 $this->set_status(backup::STATUS_SETTING_UI); 141 } else { 142 $this->set_status(backup::STATUS_AWAITING); 143 } 144 } 145 146 /** 147 * Clean structures used by the backup_controller 148 * 149 * This method clean various structures used by the backup_controller, 150 * destroying them in an ordered way, so their memory will be gc properly 151 * by PHP (mainly circular references). 152 * 153 * Note that, while it's not mandatory to execute this method, it's highly 154 * recommended to do so, specially in scripts performing multiple operations 155 * (like the automated backups) or the system will run out of memory after 156 * a few dozens of backups) 157 */ 158 public function destroy() { 159 // Only need to destroy circulars under the plan. Delegate to it. 160 $this->plan->destroy(); 161 } 162 163 public function finish_ui() { 164 if ($this->status != backup::STATUS_SETTING_UI) { 165 throw new backup_controller_exception('cannot_finish_ui_if_not_setting_ui'); 166 } 167 $this->set_status(backup::STATUS_AWAITING); 168 } 169 170 public function process_ui_event() { 171 172 // Perform security checks throwing exceptions (2nd param) if something is wrong 173 backup_check::check_security($this, false); 174 } 175 176 public function set_status($status) { 177 // Note: never save_controller() with the object info after STATUS_EXECUTING or the whole controller, 178 // containing all the steps will be sent to DB. 100% (monster) useless. 179 $this->log('setting controller status to', backup::LOG_DEBUG, $status); 180 // TODO: Check it's a correct status. 181 $this->status = $status; 182 // Ensure that, once set to backup::STATUS_AWAITING, controller is stored in DB. 183 if ($status == backup::STATUS_AWAITING) { 184 $this->save_controller(); 185 $tbc = self::load_controller($this->backupid); 186 $this->logger = $tbc->logger; // wakeup loggers 187 $tbc->destroy(); // Clean temp controller structures 188 189 } else if ($status == backup::STATUS_FINISHED_OK) { 190 // If the operation has ended without error (backup::STATUS_FINISHED_OK) 191 // proceed by cleaning the object from database. MDL-29262. 192 $this->save_controller(false, true); 193 } 194 } 195 196 public function set_execution($execution, $executiontime = 0) { 197 $this->log('setting controller execution', backup::LOG_DEBUG); 198 // TODO: Check valid execution mode 199 // TODO: Check time in future 200 // TODO: Check time = 0 if inmediate 201 $this->execution = $execution; 202 $this->executiontime = $executiontime; 203 204 // Default destination chain (based on type/mode/execution) 205 $this->destination = backup_factory::get_destination_chain($this->type, $this->id, $this->mode, $this->execution); 206 207 // Default logger chain (based on interactive/execution) 208 $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->backupid); 209 } 210 211 // checksumable interface methods 212 213 public function calculate_checksum() { 214 // Reset current checksum to take it out from calculations! 215 $this->checksum = ''; 216 // Init checksum 217 $tempchecksum = md5('backupid-' . $this->backupid . 218 'type-' . $this->type . 219 'id-' . $this->id . 220 'format-' . $this->format . 221 'interactive-'. $this->interactive . 222 'mode-' . $this->mode . 223 'userid-' . $this->userid . 224 'operation-' . $this->operation . 225 'status-' . $this->status . 226 'execution-' . $this->execution . 227 'plan-' . backup_general_helper::array_checksum_recursive(array($this->plan)) . 228 'destination-'. backup_general_helper::array_checksum_recursive(array($this->destination)) . 229 'logger-' . backup_general_helper::array_checksum_recursive(array($this->logger))); 230 $this->log('calculating controller checksum', backup::LOG_DEBUG, $tempchecksum); 231 return $tempchecksum; 232 } 233 234 public function is_checksum_correct($checksum) { 235 return $this->checksum === $checksum; 236 } 237 238 public function get_backupid() { 239 return $this->backupid; 240 } 241 242 public function get_type() { 243 return $this->type; 244 } 245 246 /** 247 * Returns the current value of the include_files setting. 248 * This setting is intended to ensure that files are not included in 249 * generated backups. 250 * 251 * @return int Indicates whether files should be included in backups. 252 */ 253 public function get_include_files() { 254 return $this->includefiles; 255 } 256 257 public function get_operation() { 258 return $this->operation; 259 } 260 261 public function get_id() { 262 return $this->id; 263 } 264 265 public function get_courseid() { 266 return $this->courseid; 267 } 268 269 public function get_format() { 270 return $this->format; 271 } 272 273 public function get_interactive() { 274 return $this->interactive; 275 } 276 277 public function get_mode() { 278 return $this->mode; 279 } 280 281 public function get_userid() { 282 return $this->userid; 283 } 284 285 public function get_status() { 286 return $this->status; 287 } 288 289 public function get_execution() { 290 return $this->execution; 291 } 292 293 public function get_executiontime() { 294 return $this->executiontime; 295 } 296 297 /** 298 * @return backup_plan 299 */ 300 public function get_plan() { 301 return $this->plan; 302 } 303 304 /** 305 * Executes the backup 306 * @return void Throws and exception of completes 307 */ 308 public function execute_plan() { 309 // Basic/initial prevention against time/memory limits 310 core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted 311 raise_memory_limit(MEMORY_EXTRA); 312 // If this is not a course backup, inform the plan we are not 313 // including all the activities for sure. This will affect any 314 // task/step executed conditionally to stop including information 315 // for section and activity backup. MDL-28180. 316 if ($this->get_type() !== backup::TYPE_1COURSE) { 317 $this->log('notifying plan about excluded activities by type', backup::LOG_DEBUG); 318 $this->plan->set_excluding_activities(); 319 } 320 return $this->plan->execute(); 321 } 322 323 public function get_results() { 324 return $this->plan->get_results(); 325 } 326 327 /** 328 * Save controller information 329 * 330 * @param bool $includeobj to decide if the object itself must be updated (true) or no (false) 331 * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false) 332 */ 333 public function save_controller($includeobj = true, $cleanobj = false) { 334 // Going to save controller to persistent storage, calculate checksum for later checks and save it 335 // TODO: flag the controller as NA. Any operation on it should be forbidden util loaded back 336 $this->log('saving controller to db', backup::LOG_DEBUG); 337 if ($includeobj ) { // Only calculate checksum if we are going to include the object. 338 $this->checksum = $this->calculate_checksum(); 339 } 340 backup_controller_dbops::save_controller($this, $this->checksum, $includeobj, $cleanobj); 341 } 342 343 public static function load_controller($backupid) { 344 // Load controller from persistent storage 345 // TODO: flag the controller as available. Operations on it can continue 346 $controller = backup_controller_dbops::load_controller($backupid); 347 $controller->log('loading controller from db', backup::LOG_DEBUG); 348 return $controller; 349 } 350 351 // Protected API starts here 352 353 protected function calculate_backupid() { 354 // Current epoch time + type + id + format + interactive + mode + userid + operation 355 // should be unique enough. Add one random part at the end 356 $this->backupid = md5(time() . '-' . $this->type . '-' . $this->id . '-' . $this->format . '-' . 357 $this->interactive . '-' . $this->mode . '-' . $this->userid . '-' . 358 $this->operation . '-' . random_string(20)); 359 } 360 361 protected function load_plan() { 362 $this->log('loading controller plan', backup::LOG_DEBUG); 363 $this->plan = new backup_plan($this); 364 $this->plan->build(); // Build plan for this controller 365 $this->set_status(backup::STATUS_PLANNED); 366 } 367 368 protected function apply_defaults() { 369 $this->log('applying plan defaults', backup::LOG_DEBUG); 370 backup_controller_dbops::apply_config_defaults($this); 371 $this->set_status(backup::STATUS_CONFIGURED); 372 $this->set_include_files(); 373 } 374 375 /** 376 * Set the initial value for the include_files setting. 377 * 378 * @see backup_controller::get_include_files for further information on the purpose of this setting. 379 * @return int Indicates whether files should be included in backups. 380 */ 381 protected function set_include_files() { 382 // We normally include files. 383 $includefiles = true; 384 385 // In an import, we don't need to include files. 386 if ($this->get_mode() === backup::MODE_IMPORT) { 387 $includefiles = false; 388 } 389 390 // When a backup is intended for the same site, we don't need to include the files. 391 // Note, this setting is only used for duplication of an entire course. 392 if ($this->get_mode() === backup::MODE_SAMESITE) { 393 $includefiles = false; 394 } 395 396 $this->includefiles = (int) $includefiles; 397 $this->log("setting file inclusion to {$this->includefiles}", backup::LOG_DEBUG); 398 return $this->includefiles; 399 } 400 } 401 402 /* 403 * Exception class used by all the @backup_controller stuff 404 */ 405 class backup_controller_exception extends backup_exception { 406 407 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 408 parent::__construct($errorcode, $a, $debuginfo); 409 } 410 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |