Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]
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 restore process 27 * 28 * This final class is in charge of controlling all the restore architecture, for any 29 * type of backup. 30 * 31 * TODO: Finish phpdocs 32 */ 33 class restore_controller extends base_controller { 34 35 protected $tempdir; // Directory under $CFG->backuptempdir awaiting restore 36 protected $restoreid; // Unique identificator for this restore 37 38 protected $courseid; // courseid where restore is going to happen 39 40 protected $type; // Type of backup (activity, section, course) 41 protected $format; // Format of backup (moodle, imscc) 42 protected $interactive; // yes/no 43 protected $mode; // Purpose of the backup (default settings) 44 protected $userid; // user id executing the restore 45 protected $operation; // Type of operation (backup/restore) 46 protected $target; // Restoring to new/existing/current_adding/_deleting 47 protected $samesite; // Are we restoring to the same site where the backup was generated 48 49 protected $status; // Current status of the controller (created, planned, configured...) 50 protected $precheck; // Results of the execution of restore prechecks 51 52 protected $info; // Information retrieved from backup contents 53 /** @var restore_plan */ 54 protected $plan; // Restore execution plan 55 56 /** 57 * Immediate/delayed execution type. 58 * @var integer 59 */ 60 protected $execution; 61 protected $executiontime; // epoch time when we want the restore to be executed (requires cron to run) 62 63 protected $checksum; // Cache @checksumable results for lighter @is_checksum_correct() uses 64 65 /** @var int Number of restore_controllers that are currently executing */ 66 protected static $executing = 0; 67 68 /** 69 * Holds the relevant destination information for course copy operations. 70 * 71 * @var \stdClass. 72 */ 73 protected $copy; 74 75 /** 76 * Constructor. 77 * 78 * If you specify a progress monitor, this will be used to report progress 79 * while loading the plan, as well as for future use. (You can change it 80 * for a different one later using set_progress.) 81 * 82 * @param string $tempdir Directory under $CFG->backuptempdir awaiting restore 83 * @param int $courseid Course id where restore is going to happen 84 * @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false] 85 * @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ] 86 * @param int $userid 87 * @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ] 88 * @param \core\progress\base $progress Optional progress monitor 89 * @param \stdClass $copydata Course copy data, required when in MODE_COPY 90 * @param bool $releasesession Should release the session? backup::RELEASESESSION_YES or backup::RELEASESESSION_NO 91 */ 92 public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target, 93 \core\progress\base $progress = null, $releasesession = backup::RELEASESESSION_NO, ?\stdClass $copydata = null) { 94 95 if ($mode == backup::MODE_COPY && is_null($copydata)) { 96 throw new restore_controller_exception('cannot_instantiate_missing_copydata'); 97 } 98 99 $this->copy = $copydata; 100 $this->tempdir = $tempdir; 101 $this->courseid = $courseid; 102 $this->interactive = $interactive; 103 $this->mode = $mode; 104 $this->userid = $userid; 105 $this->target = $target; 106 $this->releasesession = $releasesession; 107 108 // Apply some defaults 109 $this->type = ''; 110 $this->format = backup::FORMAT_UNKNOWN; 111 $this->operation = backup::OPERATION_RESTORE; 112 $this->executiontime = 0; 113 $this->samesite = false; 114 $this->checksum = ''; 115 $this->precheck = null; 116 117 // Apply current backup version and release if necessary 118 backup_controller_dbops::apply_version_and_release(); 119 120 // Check courseid is correct 121 restore_check::check_courseid($this->courseid); 122 123 // Check user is correct 124 restore_check::check_user($this->userid); 125 126 // Calculate unique $restoreid 127 $this->calculate_restoreid(); 128 129 // Default logger chain (based on interactive/execution) 130 $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid); 131 132 // Set execution based on backup mode. 133 if ($mode == backup::MODE_ASYNC || $mode == backup::MODE_COPY) { 134 $this->execution = backup::EXECUTION_DELAYED; 135 } else { 136 $this->execution = backup::EXECUTION_INMEDIATE; 137 } 138 139 // By default there is no progress reporter unless you specify one so it 140 // can be used during loading of the plan. 141 if ($progress) { 142 $this->progress = $progress; 143 } else { 144 $this->progress = new \core\progress\none(); 145 } 146 $this->progress->start_progress('Constructing restore_controller'); 147 148 // Instantiate the output_controller singleton and active it if interactive and immediate. 149 $oc = output_controller::get_instance(); 150 if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) { 151 $oc->set_active(true); 152 } 153 154 $this->log('instantiating restore controller', backup::LOG_INFO, $this->restoreid); 155 156 // Set initial status 157 $this->set_status(backup::STATUS_CREATED); 158 159 // Calculate original restore format 160 $this->format = backup_general_helper::detect_backup_format($tempdir); 161 162 // If format is not moodle2, set to conversion needed 163 if ($this->format !== backup::FORMAT_MOODLE) { 164 $this->set_status(backup::STATUS_REQUIRE_CONV); 165 166 // Else, format is moodle2, load plan, apply security and set status based on interactivity 167 } else { 168 // Load plan 169 $this->load_plan(); 170 171 // Apply all default settings (based on type/format/mode). 172 $this->apply_defaults(); 173 174 // Perform all initial security checks and apply (2nd param) them to settings automatically 175 restore_check::check_security($this, true); 176 177 if ($this->interactive == backup::INTERACTIVE_YES) { 178 $this->set_status(backup::STATUS_SETTING_UI); 179 } else { 180 $this->set_status(backup::STATUS_NEED_PRECHECK); 181 } 182 } 183 184 // Tell progress monitor that we finished loading. 185 $this->progress->end_progress(); 186 } 187 188 /** 189 * Clean structures used by the restore_controller 190 * 191 * This method clean various structures used by the restore_controller, 192 * destroying them in an ordered way, so their memory will be gc properly 193 * by PHP (mainly circular references). 194 * 195 * Note that, while it's not mandatory to execute this method, it's highly 196 * recommended to do so, specially in scripts performing multiple operations 197 * (like the automated backups) or the system will run out of memory after 198 * a few dozens of backups) 199 */ 200 public function destroy() { 201 // Only need to destroy circulars under the plan. Delegate to it. 202 $this->plan->destroy(); 203 // Loggers may have also chained references, destroy them. Also closing resources when needed. 204 $this->logger->destroy(); 205 } 206 207 public function finish_ui() { 208 if ($this->status != backup::STATUS_SETTING_UI) { 209 throw new restore_controller_exception('cannot_finish_ui_if_not_setting_ui'); 210 } 211 $this->set_status(backup::STATUS_NEED_PRECHECK); 212 } 213 214 public function process_ui_event() { 215 216 // Perform security checks throwing exceptions (2nd param) if something is wrong 217 restore_check::check_security($this, false); 218 } 219 220 public function set_status($status) { 221 // Note: never save_controller() with the object info after STATUS_EXECUTING or the whole controller, 222 // containing all the steps will be sent to DB. 100% (monster) useless. 223 $this->log('setting controller status to', backup::LOG_DEBUG, $status); 224 // TODO: Check it's a correct status. 225 $this->status = $status; 226 // Ensure that, once set to backup::STATUS_AWAITING | STATUS_NEED_PRECHECK, controller is stored in DB. 227 // Also save if executing so we can better track progress. 228 if ($status == backup::STATUS_AWAITING || $status == backup::STATUS_NEED_PRECHECK || $status == backup::STATUS_EXECUTING) { 229 $this->save_controller(); 230 $tbc = self::load_controller($this->restoreid); 231 $this->logger = $tbc->logger; // wakeup loggers 232 $tbc->plan->destroy(); // Clean plan controller structures, keeping logger alive. 233 234 } else if ($status == backup::STATUS_FINISHED_OK) { 235 // If the operation has ended without error (backup::STATUS_FINISHED_OK) 236 // proceed by cleaning the object from database. MDL-29262. 237 $this->save_controller(false, true); 238 } else if ($status == backup::STATUS_FINISHED_ERR) { 239 // If the operation has ended with an error save the controller 240 // preserving the object in the database. We may want it for debugging. 241 $this->save_controller(); 242 } 243 } 244 245 public function set_execution($execution, $executiontime = 0) { 246 $this->log('setting controller execution', backup::LOG_DEBUG); 247 // TODO: Check valid execution mode. 248 // TODO: Check time in future. 249 // TODO: Check time = 0 if immediate. 250 $this->execution = $execution; 251 $this->executiontime = $executiontime; 252 253 // Default logger chain (based on interactive/execution) 254 $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid); 255 } 256 257 // checksumable interface methods 258 259 public function calculate_checksum() { 260 // Reset current checksum to take it out from calculations! 261 $this->checksum = ''; 262 // Init checksum 263 $tempchecksum = md5('tempdir-' . $this->tempdir . 264 'restoreid-' . $this->restoreid . 265 'courseid-' . $this->courseid . 266 'type-' . $this->type . 267 'format-' . $this->format . 268 'interactive-'. $this->interactive . 269 'mode-' . $this->mode . 270 'userid-' . $this->userid . 271 'target-' . $this->target . 272 'samesite-' . $this->samesite . 273 'operation-' . $this->operation . 274 'status-' . $this->status . 275 'precheck-' . backup_general_helper::array_checksum_recursive(array($this->precheck)) . 276 'execution-' . $this->execution . 277 'plan-' . backup_general_helper::array_checksum_recursive(array($this->plan)) . 278 'info-' . backup_general_helper::array_checksum_recursive(array($this->info)) . 279 'logger-' . backup_general_helper::array_checksum_recursive(array($this->logger))); 280 $this->log('calculating controller checksum', backup::LOG_DEBUG, $tempchecksum); 281 return $tempchecksum; 282 } 283 284 public function is_checksum_correct($checksum) { 285 return $this->checksum === $checksum; 286 } 287 288 public function get_tempdir() { 289 return $this->tempdir; 290 } 291 292 public function get_restoreid() { 293 return $this->restoreid; 294 } 295 296 public function get_type() { 297 return $this->type; 298 } 299 300 public function get_operation() { 301 return $this->operation; 302 } 303 304 public function get_courseid() { 305 return $this->courseid; 306 } 307 308 public function get_format() { 309 return $this->format; 310 } 311 312 public function get_interactive() { 313 return $this->interactive; 314 } 315 316 public function get_mode() { 317 return $this->mode; 318 } 319 320 public function get_userid() { 321 return $this->userid; 322 } 323 324 public function get_target() { 325 return $this->target; 326 } 327 328 public function is_samesite() { 329 return $this->samesite; 330 } 331 332 public function get_status() { 333 return $this->status; 334 } 335 336 public function get_execution() { 337 return $this->execution; 338 } 339 340 public function get_executiontime() { 341 return $this->executiontime; 342 } 343 344 /** 345 * Returns the restore plan 346 * @return restore_plan 347 */ 348 public function get_plan() { 349 return $this->plan; 350 } 351 /** 352 * Gets the value for the requested setting 353 * 354 * @param string $name 355 * @param bool $default 356 * @return mixed 357 */ 358 public function get_setting_value($name, $default = false) { 359 try { 360 return $this->get_plan()->get_setting($name)->get_value(); 361 } catch (Exception $e) { 362 debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER); 363 return $default; 364 } 365 } 366 367 /** 368 * For debug only. Get a simple test display of all the settings. 369 * 370 * @return string 371 */ 372 public function debug_display_all_settings_values(): string { 373 return $this->get_plan()->debug_display_all_settings_values(); 374 } 375 376 public function get_info() { 377 return $this->info; 378 } 379 380 public function execute_plan() { 381 // Basic/initial prevention against time/memory limits 382 core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted 383 raise_memory_limit(MEMORY_EXTRA); 384 385 // Release the session so other tabs in the same session are not blocked. 386 if ($this->get_releasesession() === backup::RELEASESESSION_YES) { 387 \core\session\manager::write_close(); 388 } 389 390 // Do course cleanup precheck, if required. This was originally in restore_ui. Moved to handle async backup/restore. 391 if ($this->get_target() == backup::TARGET_CURRENT_DELETING || $this->get_target() == backup::TARGET_EXISTING_DELETING) { 392 $options = array(); 393 $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments'); 394 $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings'); 395 $options['userid'] = $this->userid; 396 restore_dbops::delete_course_content($this->get_courseid(), $options); 397 } 398 // If this is not a course restore or single activity restore (e.g. duplicate), inform the plan we are not 399 // including all the activities for sure. This will affect any 400 // task/step executed conditionally to stop processing information 401 // for section and activity restore. MDL-28180. 402 if ($this->get_type() !== backup::TYPE_1COURSE && $this->get_type() !== backup::TYPE_1ACTIVITY) { 403 $this->log('notifying plan about excluded activities by type', backup::LOG_DEBUG); 404 $this->plan->set_excluding_activities(); 405 } 406 self::$executing++; 407 try { 408 $this->plan->execute(); 409 } catch (Exception $e) { 410 self::$executing--; 411 throw $e; 412 } 413 self::$executing--; 414 } 415 416 /** 417 * Checks whether restore is currently executing. Certain parts of code that 418 * is called during restore, but not directly part of the restore system, may 419 * need to behave differently during restore (e.g. do not bother resetting a 420 * cache because we know it will be reset at end of operation). 421 * 422 * @return bool True if any restore is currently executing 423 */ 424 public static function is_executing() { 425 return self::$executing > 0; 426 } 427 428 /** 429 * Execute the restore prechecks to detect any problem before proceed with restore 430 * 431 * This function checks various parts of the restore (versions, users, roles...) 432 * returning true if everything was ok or false if any warning/error was detected. 433 * Any warning/error is returned by the get_precheck_results() method. 434 * Note: if any problem is found it will, automatically, drop all the restore temp 435 * tables as far as the next step is to inform about the warning/errors. If no problem 436 * is found, then default behaviour is to keep the temp tables so, in the same request 437 * restore will be executed, saving a lot of checks to be executed again. 438 * Note: If for any reason (UI to show after prechecks...) you want to force temp tables 439 * to be dropped always, you can pass true to the $droptemptablesafter parameter 440 */ 441 public function execute_precheck($droptemptablesafter = false) { 442 if (is_array($this->precheck)) { 443 throw new restore_controller_exception('precheck_alredy_executed', $this->status); 444 } 445 if ($this->status != backup::STATUS_NEED_PRECHECK) { 446 throw new restore_controller_exception('cannot_precheck_wrong_status', $this->status); 447 } 448 // Basic/initial prevention against time/memory limits 449 core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted 450 raise_memory_limit(MEMORY_EXTRA); 451 $this->precheck = restore_prechecks_helper::execute_prechecks($this, $droptemptablesafter); 452 if (!array_key_exists('errors', $this->precheck)) { // No errors, can be executed 453 $this->set_status(backup::STATUS_AWAITING); 454 } 455 if (empty($this->precheck)) { // No errors nor warnings, return true 456 return true; 457 } 458 return false; 459 } 460 461 public function get_results() { 462 return $this->plan->get_results(); 463 } 464 465 /** 466 * Returns true if the prechecks have been executed 467 * @return bool 468 */ 469 public function precheck_executed() { 470 return (is_array($this->precheck)); 471 } 472 473 public function get_precheck_results() { 474 if (!is_array($this->precheck)) { 475 throw new restore_controller_exception('precheck_not_executed'); 476 } 477 return $this->precheck; 478 } 479 480 /** 481 * Save controller information 482 * 483 * @param bool $includeobj to decide if the object itself must be updated (true) or no (false) 484 * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false) 485 */ 486 public function save_controller($includeobj = true, $cleanobj = false) { 487 // Going to save controller to persistent storage, calculate checksum for later checks and save it 488 // TODO: flag the controller as NA. Any operation on it should be forbidden util loaded back 489 $this->log('saving controller to db', backup::LOG_DEBUG); 490 if ($includeobj ) { // Only calculate checksum if we are going to include the object. 491 $this->checksum = $this->calculate_checksum(); 492 } 493 restore_controller_dbops::save_controller($this, $this->checksum, $includeobj, $cleanobj); 494 } 495 496 public static function load_controller($restoreid) { 497 // Load controller from persistent storage 498 // TODO: flag the controller as available. Operations on it can continue 499 $controller = restore_controller_dbops::load_controller($restoreid); 500 $controller->log('loading controller from db', backup::LOG_DEBUG); 501 return $controller; 502 } 503 504 /** 505 * class method to provide pseudo random unique "correct" tempdir names 506 */ 507 public static function get_tempdir_name($courseid = 0, $userid = 0) { 508 // Current epoch time + courseid + userid + random bits 509 return md5(time() . '-' . $courseid . '-'. $userid . '-'. random_string(20)); 510 } 511 512 /** 513 * Converts from current format to backup::MOODLE format 514 */ 515 public function convert() { 516 global $CFG; 517 require_once($CFG->dirroot . '/backup/util/helper/convert_helper.class.php'); 518 519 // Basic/initial prevention against time/memory limits 520 core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted 521 raise_memory_limit(MEMORY_EXTRA); 522 $this->progress->start_progress('Backup format conversion'); 523 524 if ($this->status != backup::STATUS_REQUIRE_CONV) { 525 throw new restore_controller_exception('cannot_convert_not_required_status'); 526 } 527 528 $this->log('backup format conversion required', backup::LOG_INFO); 529 530 // Run conversion to the proper format 531 if (!convert_helper::to_moodle2_format($this->get_tempdir(), $this->format, $this->get_logger())) { 532 // todo - unable to find the conversion path, what to do now? 533 // throwing the exception as a temporary solution 534 throw new restore_controller_exception('unable_to_find_conversion_path'); 535 } 536 537 $this->log('backup format conversion successful', backup::LOG_INFO); 538 539 // If no exceptions were thrown, then we are in the proper format 540 $this->format = backup::FORMAT_MOODLE; 541 542 // Load plan, apply security and set status based on interactivity 543 $this->load_plan(); 544 545 // Perform all initial security checks and apply (2nd param) them to settings automatically 546 restore_check::check_security($this, true); 547 548 if ($this->interactive == backup::INTERACTIVE_YES) { 549 $this->set_status(backup::STATUS_SETTING_UI); 550 } else { 551 $this->set_status(backup::STATUS_NEED_PRECHECK); 552 } 553 $this->progress->end_progress(); 554 } 555 556 /** 557 * Do the necessary copy preparation actions. 558 * This method should only be called once the backup of a copy operation is completed. 559 * 560 * @throws restore_controller_exception 561 */ 562 public function prepare_copy(): void { 563 // Check that we are in the correct mode. 564 if ($this->mode != backup::MODE_COPY) { 565 throw new restore_controller_exception('cannot_prepare_copy_wrong_mode'); 566 } 567 568 $this->progress->start_progress('Prepare Copy'); 569 570 // If no exceptions were thrown, then we are in the proper format. 571 $this->format = backup::FORMAT_MOODLE; 572 573 // Load plan, apply security and set status based on interactivity. 574 $this->load_plan(); 575 576 $this->set_status(backup::STATUS_NEED_PRECHECK); 577 $this->progress->end_progress(); 578 } 579 580 /** 581 * Get the course copy data. 582 * 583 * @return \stdClass 584 */ 585 public function get_copy(): \stdClass { 586 if ($this->mode != backup::MODE_COPY) { 587 throw new restore_controller_exception('cannot_get_copy_wrong_mode'); 588 } 589 590 return $this->copy; 591 } 592 593 // Protected API starts here 594 595 protected function calculate_restoreid() { 596 // Current epoch time + tempdir + courseid + interactive + mode + userid + target + operation + random bits 597 $this->restoreid = md5(time() . '-' . $this->tempdir . '-' . $this->courseid . '-'. $this->interactive . '-' . 598 $this->mode . '-' . $this->userid . '-'. $this->target . '-' . $this->operation . '-' . 599 random_string(20)); 600 } 601 602 protected function load_plan() { 603 // First of all, we need to introspect the moodle_backup.xml file 604 // in order to detect all the required stuff. So, create the 605 // monster $info structure where everything will be defined 606 $this->log('loading backup info', backup::LOG_DEBUG); 607 $this->info = backup_general_helper::get_backup_information($this->tempdir); 608 609 // Set the controller type to the one found in the information 610 $this->type = $this->info->type; 611 612 // Set the controller samesite flag as needed 613 $this->samesite = backup_general_helper::backup_is_samesite($this->info); 614 615 // Now we load the plan that will be configured following the 616 // information provided by the $info 617 $this->log('loading controller plan', backup::LOG_DEBUG); 618 $this->plan = new restore_plan($this); 619 $this->plan->build(); // Build plan for this controller 620 $this->set_status(backup::STATUS_PLANNED); 621 } 622 623 /** 624 * Apply defaults from the global admin settings 625 */ 626 protected function apply_defaults() { 627 $this->log('applying restore defaults', backup::LOG_DEBUG); 628 restore_controller_dbops::apply_config_defaults($this); 629 $this->set_status(backup::STATUS_CONFIGURED); 630 } 631 } 632 633 /* 634 * Exception class used by all the @restore_controller stuff 635 */ 636 class restore_controller_exception extends backup_exception { 637 638 public function __construct($errorcode, $a=NULL, $debuginfo=null) { 639 parent::__construct($errorcode, $a, $debuginfo); 640 } 641 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body