Merge branch 'MDL-81449-main' of https://github.com/lucaboesch/moodle
[moodle.git] / backup / controller / backup_controller.class.php
blob8fb30a78257bd6b44ee7a34dccc0c3def8eb7e1c
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Backup controller and related exception classes.
20 * @package core_backup
21 * @subpackage backup-controller
22 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 /**
27 * Class implementing the controller of any backup process
29 * This final class is in charge of controlling all the backup architecture, for any
30 * type of backup. Based in type, format, interactivity and target, it stores the
31 * whole execution plan and settings that will be used later by the @backup_worker,
32 * applies all the defaults, performs all the security contraints and is in charge
33 * of handling the ui if necessary. Also logging strategy is defined here.
35 * Note the class is 100% neutral and usable for *any* backup. It just stores/requests
36 * all the needed information from other backup classes in order to have everything well
37 * structured in order to allow the @backup_worker classes to do their job.
39 * In other words, a mammoth class, but don't worry, practically everything is delegated/
40 * aggregated!)
42 class backup_controller extends base_controller {
43 /** @var string Unique identifier for this backup */
44 protected $backupid;
46 /**
47 * Type of item that is being stored in the backup.
49 * Should be selected from one of the backup::TYPE_ constants
50 * for example backup::TYPE_1ACTIVITY
52 * @var string
54 protected $type;
56 /** @var int Course/section/course_module id to backup */
57 protected $id;
59 /** @var false|int The id of the course the backup belongs to, or false if no course. */
60 protected $courseid;
62 /**
63 * Format of backup (moodle, imscc).
65 * Should be one of the backup::FORMAT_ constants.
66 * for example backup::FORMAT_MOODLE
68 * @var string
70 protected $format;
72 /**
73 * Whether this backup will require user interaction.
75 * Should be one of backup::INTERACTIVE_YES or INTERACTIVE_NO
77 * @var bool
79 protected $interactive;
81 /**
82 * Purpose of the backup (default settings)
84 * Should be one of the the backup::MODE_ constants,
85 * for example backup::MODE_GENERAL
87 * @var int
89 protected $mode;
91 /** @var int The id of the user executing the backup. */
92 protected $userid;
94 /**
95 * Type of operation (backup/restore)
97 * Should be selected from: backup::OPERATION_BACKUP or OPERATION_RESTORE
99 * @var string
101 protected $operation;
104 * Current status of the controller (created, planned, configured...)
106 * It should be one of the backup::STATUS_ constants,
107 * for example backup::STATUS_AWAITING.
109 * @var int
111 protected $status;
113 /** @var backup_plan Backup execution plan. */
114 protected $plan;
116 /** @var int Whether this backup includes files (1) or not (0). */
117 protected $includefiles;
120 * Immediate/delayed execution type.
121 * @var int
123 protected $execution;
125 /** @var int Epoch time when we want the backup to be executed (requires cron to run). */
126 protected $executiontime;
128 /** @var null Destination chain object (fs_moodle, fs_os, db, email...). */
129 protected $destination;
131 /** @var string Cache {@see \checksumable} results for lighter {@see \backup_controller::is_checksum_correct()} uses. */
132 protected $checksum;
135 * The role ids to keep in a copy operation.
136 * @var array
138 protected $keptroles = array();
141 * Constructor for the backup controller class.
143 * @param string $type Type of the backup; One of backup::TYPE_1COURSE, TYPE_1SECTION, TYPE_1ACTIVITY
144 * @param int $id The ID of the item to backup; e.g the course id
145 * @param string $format The backup format to use; Most likely backup::FORMAT_MOODLE
146 * @param bool $interactive Whether this backup will require user interaction; backup::INTERACTIVE_YES or INTERACTIVE_NO
147 * @param int $mode One of backup::MODE_GENERAL, MODE_IMPORT, MODE_SAMESITE, MODE_HUB, MODE_AUTOMATED
148 * @param int $userid The id of the user making the backup
149 * @param bool $releasesession Should release the session? backup::RELEASESESSION_YES or backup::RELEASESESSION_NO
151 public function __construct($type, $id, $format, $interactive, $mode, $userid, $releasesession = backup::RELEASESESSION_NO) {
152 $this->type = $type;
153 $this->id = $id;
154 $this->courseid = backup_controller_dbops::get_courseid_from_type_id($this->type, $this->id);
155 $this->format = $format;
156 $this->interactive = $interactive;
157 $this->mode = $mode;
158 $this->userid = $userid;
159 $this->releasesession = $releasesession;
161 // Apply some defaults
162 $this->operation = backup::OPERATION_BACKUP;
163 $this->executiontime = 0;
164 $this->checksum = '';
166 // Set execution based on backup mode.
167 if ($mode == backup::MODE_ASYNC || $mode == backup::MODE_COPY) {
168 $this->execution = backup::EXECUTION_DELAYED;
169 } else {
170 $this->execution = backup::EXECUTION_INMEDIATE;
173 // Apply current backup version and release if necessary
174 backup_controller_dbops::apply_version_and_release();
176 // Check format and type are correct
177 backup_check::check_format_and_type($this->format, $this->type);
179 // Check id is correct
180 backup_check::check_id($this->type, $this->id);
182 // Check user is correct
183 backup_check::check_user($this->userid);
185 // Calculate unique $backupid
186 $this->calculate_backupid();
188 // Default logger chain (based on interactive/execution)
189 $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->backupid);
191 // By default there is no progress reporter. Interfaces that wish to
192 // display progress must set it.
193 $this->progress = new \core\progress\none();
195 // Instantiate the output_controller singleton and active it if interactive and immediate.
196 $oc = output_controller::get_instance();
197 if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) {
198 $oc->set_active(true);
201 $this->log('instantiating backup controller', backup::LOG_INFO, $this->backupid);
203 // Default destination chain (based on type/mode/execution)
204 $this->destination = backup_factory::get_destination_chain($this->type, $this->id, $this->mode, $this->execution);
206 // Set initial status
207 $this->set_status(backup::STATUS_CREATED);
209 // Load plan (based on type/format)
210 $this->load_plan();
212 // Apply all default settings (based on type/format/mode)
213 $this->apply_defaults();
215 // Perform all initial security checks and apply (2nd param) them to settings automatically
216 backup_check::check_security($this, true);
218 // Set status based on interactivity
219 if ($this->interactive == backup::INTERACTIVE_YES) {
220 $this->set_status(backup::STATUS_SETTING_UI);
221 } else {
222 $this->set_status(backup::STATUS_AWAITING);
227 * Clean structures used by the backup_controller
229 * This method clean various structures used by the backup_controller,
230 * destroying them in an ordered way, so their memory will be gc properly
231 * by PHP (mainly circular references).
233 * Note that, while it's not mandatory to execute this method, it's highly
234 * recommended to do so, specially in scripts performing multiple operations
235 * (like the automated backups) or the system will run out of memory after
236 * a few dozens of backups)
238 public function destroy() {
239 // Only need to destroy circulars under the plan. Delegate to it.
240 $this->plan->destroy();
241 // Loggers may have also chained references, destroy them. Also closing resources when needed.
242 $this->logger->destroy();
246 * Declare that all user interaction with the backup controller is complete.
248 * After this the backup controller is waiting for processing.
250 public function finish_ui() {
251 if ($this->status != backup::STATUS_SETTING_UI) {
252 throw new backup_controller_exception('cannot_finish_ui_if_not_setting_ui');
254 $this->set_status(backup::STATUS_AWAITING);
258 * Validates the backup is valid after any user changes.
260 * A backup_controller_exception will be thrown if there is an issue.
262 public function process_ui_event() {
264 // Perform security checks throwing exceptions (2nd param) if something is wrong
265 backup_check::check_security($this, false);
269 * Sets the new status of the backup.
271 * @param int $status
273 public function set_status($status) {
274 // Note: never save_controller() with the object info after STATUS_EXECUTING or the whole controller,
275 // containing all the steps will be sent to DB. 100% (monster) useless.
276 $this->log('setting controller status to', backup::LOG_DEBUG, $status);
277 // TODO: Check it's a correct status.
278 $this->status = $status;
279 // Ensure that, once set to backup::STATUS_AWAITING, controller is stored in DB.
280 // Also save if executing so we can better track progress.
281 if ($status == backup::STATUS_AWAITING || $status == backup::STATUS_EXECUTING) {
282 $this->save_controller();
283 $tbc = self::load_controller($this->backupid);
284 $this->logger = $tbc->logger; // wakeup loggers
285 $tbc->plan->destroy(); // Clean plan controller structures, keeping logger alive.
287 } else if ($status == backup::STATUS_FINISHED_OK) {
288 // If the operation has ended without error (backup::STATUS_FINISHED_OK)
289 // proceed by cleaning the object from database. MDL-29262.
290 $this->save_controller(false, true);
291 } else if ($status == backup::STATUS_FINISHED_ERR) {
292 // If the operation has ended with an error save the controller
293 // preserving the object in the database. We may want it for debugging.
294 $this->save_controller();
299 * Sets if the backup will be processed immediately, or later.
301 * @param int $execution Use backup::EXECUTION_INMEDIATE or backup::EXECUTION_DELAYED
302 * @param int $executiontime The timestamp in the future when the task should be executed, or 0 for immediately.
304 public function set_execution($execution, $executiontime = 0) {
305 $this->log('setting controller execution', backup::LOG_DEBUG);
306 // TODO: Check valid execution mode.
307 // TODO: Check time in future.
308 // TODO: Check time = 0 if immediate.
309 $this->execution = $execution;
310 $this->executiontime = $executiontime;
312 // Default destination chain (based on type/mode/execution)
313 $this->destination = backup_factory::get_destination_chain($this->type, $this->id, $this->mode, $this->execution);
315 // Default logger chain (based on interactive/execution)
316 $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->backupid);
319 // checksumable interface methods
321 public function calculate_checksum() {
322 // Reset current checksum to take it out from calculations!
323 $this->checksum = '';
324 // Init checksum
325 $tempchecksum = md5('backupid-' . $this->backupid .
326 'type-' . $this->type .
327 'id-' . $this->id .
328 'format-' . $this->format .
329 'interactive-'. $this->interactive .
330 'mode-' . $this->mode .
331 'userid-' . $this->userid .
332 'operation-' . $this->operation .
333 'status-' . $this->status .
334 'execution-' . $this->execution .
335 'plan-' . backup_general_helper::array_checksum_recursive(array($this->plan)) .
336 'destination-'. backup_general_helper::array_checksum_recursive(array($this->destination)) .
337 'logger-' . backup_general_helper::array_checksum_recursive(array($this->logger)));
338 $this->log('calculating controller checksum', backup::LOG_DEBUG, $tempchecksum);
339 return $tempchecksum;
342 public function is_checksum_correct($checksum) {
343 return $this->checksum === $checksum;
347 * Gets the unique identifier for this backup controller.
349 * @return string
351 public function get_backupid() {
352 return $this->backupid;
356 * Gets the type of backup to be performed.
358 * Use {@see \backup_controller::get_id()} to find the instance being backed up.
360 * @return string
362 public function get_type() {
363 return $this->type;
367 * Returns the current value of the include_files setting.
368 * This setting is intended to ensure that files are not included in
369 * generated backups.
371 * @return int Indicates whether files should be included in backups.
373 public function get_include_files() {
374 return $this->includefiles;
378 * Returns the default value for $this->includefiles before we consider any settings.
380 * @return bool
381 * @throws dml_exception
383 protected function get_include_files_default(): bool {
384 // We normally include files.
385 $includefiles = true;
387 // In an import, we don't need to include files.
388 if ($this->get_mode() === backup::MODE_IMPORT) {
389 $includefiles = false;
392 // When a backup is intended for the same site, we don't need to include the files.
393 // Note, this setting is only used for duplication of an entire course.
394 if ($this->get_mode() === backup::MODE_SAMESITE || $this->get_mode() === backup::MODE_COPY) {
395 $includefiles = false;
398 // If backup is automated and we have set auto backup config to exclude
399 // files then set them to be excluded here.
400 $backupautofiles = (bool) get_config('backup', 'backup_auto_files');
401 if ($this->get_mode() === backup::MODE_AUTOMATED && !$backupautofiles) {
402 $includefiles = false;
405 return $includefiles;
409 * Gets if this is a backup or restore.
411 * @return string
413 public function get_operation() {
414 return $this->operation;
418 * Gets the instance id of the item being backed up.
420 * It's meaning is related to the type of backup {@see \backup_controller::get_type()}.
422 * @return int
424 public function get_id() {
425 return $this->id;
429 * Gets the course that the item being backed up is in.
431 * @return false|int
433 public function get_courseid() {
434 return $this->courseid;
438 * Gets the format the backup is stored in.
440 * @return string
442 public function get_format() {
443 return $this->format;
447 * Gets if user interaction is expected during the backup.
449 * @return bool
451 public function get_interactive() {
452 return $this->interactive;
456 * Gets the mode that the backup will be performed in.
458 * @return int
460 public function get_mode() {
461 return $this->mode;
465 * Get the id of the user who started the backup.
467 * @return int
469 public function get_userid() {
470 return $this->userid;
474 * Get the current status of the backup.
476 * @return int
478 public function get_status() {
479 return $this->status;
483 * Get if the backup will be executed immediately, or later.
485 * @return int
487 public function get_execution() {
488 return $this->execution;
492 * Get when the backup will be executed.
494 * @return int 0 means now, otherwise a Unix timestamp
496 public function get_executiontime() {
497 return $this->executiontime;
501 * Gets the plan that will be run during the backup.
503 * @return backup_plan
505 public function get_plan() {
506 return $this->plan;
510 * For debug only. Get a simple test display of all the settings.
512 * @return string
514 public function debug_display_all_settings_values(): string {
515 return $this->get_plan()->debug_display_all_settings_values();
519 * Sets the user roles that should be kept in the destination course
520 * for a course copy operation.
522 * @param array $roleids
523 * @throws backup_controller_exception
525 public function set_kept_roles(array $roleids): void {
526 // Only allow of keeping user roles when controller is in copy mode.
527 if ($this->mode != backup::MODE_COPY) {
528 throw new backup_controller_exception('cannot_set_keep_roles_wrong_mode');
531 $this->keptroles = $roleids;
535 * Executes the backup
536 * @return void Throws and exception of completes
538 public function execute_plan() {
539 // Basic/initial prevention against time/memory limits
540 core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted
541 raise_memory_limit(MEMORY_EXTRA);
543 // Release the session so other tabs in the same session are not blocked.
544 if ($this->get_releasesession() === backup::RELEASESESSION_YES) {
545 \core\session\manager::write_close();
548 // If the controller has decided that we can include files, then check the setting, otherwise do not include files.
549 if ($this->get_include_files()) {
550 $this->set_include_files((bool) $this->get_plan()->get_setting('files')->get_value());
553 // If this is not a course backup, or single activity backup (e.g. duplicate) inform the plan we are not
554 // including all the activities for sure. This will affect any
555 // task/step executed conditionally to stop including information
556 // for section and activity backup. MDL-28180.
557 if ($this->get_type() !== backup::TYPE_1COURSE && $this->get_type() !== backup::TYPE_1ACTIVITY) {
558 $this->log('notifying plan about excluded activities by type', backup::LOG_DEBUG);
559 $this->plan->set_excluding_activities();
562 // Handle copy operation specific settings.
563 if ($this->mode == backup::MODE_COPY) {
564 $this->plan->set_kept_roles($this->keptroles);
567 return $this->plan->execute();
571 * Gets the results of the plan execution for this backup.
573 * @return array
575 public function get_results() {
576 return $this->plan->get_results();
580 * Save controller information
582 * @param bool $includeobj to decide if the object itself must be updated (true) or no (false)
583 * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false)
585 public function save_controller($includeobj = true, $cleanobj = false) {
586 // Going to save controller to persistent storage, calculate checksum for later checks and save it.
587 // TODO: flag the controller as NA. Any operation on it should be forbidden until loaded back.
588 $this->log('saving controller to db', backup::LOG_DEBUG);
589 if ($includeobj ) { // Only calculate checksum if we are going to include the object.
590 $this->checksum = $this->calculate_checksum();
592 backup_controller_dbops::save_controller($this, $this->checksum, $includeobj, $cleanobj);
596 * Loads a backup controller from the database.
598 * @param string $backupid The id of the backup controller.
599 * @return \backup_controller
601 public static function load_controller($backupid) {
602 // Load controller from persistent storage
603 // TODO: flag the controller as available. Operations on it can continue
604 $controller = backup_controller_dbops::load_controller($backupid);
605 $controller->log('loading controller from db', backup::LOG_DEBUG);
606 return $controller;
609 // Protected API starts here
612 * Creates a unique id for this backup.
614 protected function calculate_backupid() {
615 // Current epoch time + type + id + format + interactive + mode + userid + operation
616 // should be unique enough. Add one random part at the end
617 $this->backupid = md5(time() . '-' . $this->type . '-' . $this->id . '-' . $this->format . '-' .
618 $this->interactive . '-' . $this->mode . '-' . $this->userid . '-' .
619 $this->operation . '-' . random_string(20));
623 * Builds the plan for this backup job so that it may be executed.
625 protected function load_plan() {
626 $this->log('loading controller plan', backup::LOG_DEBUG);
627 $this->plan = new backup_plan($this);
628 $this->plan->build(); // Build plan for this controller
629 $this->set_status(backup::STATUS_PLANNED);
633 * Sets default values for the backup controller.
635 protected function apply_defaults() {
636 $this->log('applying plan defaults', backup::LOG_DEBUG);
637 backup_controller_dbops::apply_config_defaults($this);
638 $this->set_status(backup::STATUS_CONFIGURED);
639 $this->set_include_files($this->get_include_files_default());
643 * Set the initial value for the include_files setting.
645 * @param bool $includefiles
646 * @see backup_controller::get_include_files for further information on the purpose of this setting.
648 protected function set_include_files(bool $includefiles) {
649 $this->log("setting file inclusion to {$this->includefiles}", backup::LOG_DEBUG);
650 $this->includefiles = (int) $includefiles;
655 * Exception class used by all the @backup_controller stuff
657 class backup_controller_exception extends backup_exception {
659 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
660 parent::__construct($errorcode, $a, $debuginfo);