MDL-31558 add missing mimetype to send_temp_file()
[moodle.git] / backup / util / dbops / restore_controller_dbops.class.php
blobce84652fd1ae8faa8a677fe6a68227220c0b904a
1 <?php
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.
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/>.
18 /**
19 * @package moodlecore
20 * @subpackage backup-dbops
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
25 /**
26 * Non instantiable helper class providing DB support to the @restore_controller
28 * This class contains various static methods available for all the DB operations
29 * performed by the restore_controller class
31 * TODO: Finish phpdocs
33 abstract class restore_controller_dbops extends restore_dbops {
35 /**
36 * Send one restore controller to DB
38 * @param restore_controller $controller controller to send to DB
39 * @param string $checksum hash of the controller to be checked
40 * @param bool $includeobj to decide if the object itself must be updated (true) or no (false)
41 * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false)
42 * @return int id of the controller record in the DB
43 * @throws backup_controller_exception|restore_dbops_exception
45 public static function save_controller($controller, $checksum, $includeobj = true, $cleanobj = false) {
46 global $DB;
47 // Check we are going to save one backup_controller
48 if (! $controller instanceof restore_controller) {
49 throw new backup_controller_exception('restore_controller_expected');
51 // Check checksum is ok. Only if we are including object info. Sounds silly but it isn't ;-).
52 if ($includeobj and !$controller->is_checksum_correct($checksum)) {
53 throw new restore_dbops_exception('restore_controller_dbops_saving_checksum_mismatch');
55 // Cannot request to $includeobj and $cleanobj at the same time.
56 if ($includeobj and $cleanobj) {
57 throw new restore_dbops_exception('restore_controller_dbops_saving_cannot_include_and_delete');
59 // Get all the columns
60 $rec = new stdclass();
61 $rec->backupid = $controller->get_restoreid();
62 $rec->operation = $controller->get_operation();
63 $rec->type = $controller->get_type();
64 $rec->itemid = $controller->get_courseid();
65 $rec->format = $controller->get_format();
66 $rec->interactive = $controller->get_interactive();
67 $rec->purpose = $controller->get_mode();
68 $rec->userid = $controller->get_userid();
69 $rec->status = $controller->get_status();
70 $rec->execution = $controller->get_execution();
71 $rec->executiontime= $controller->get_executiontime();
72 $rec->checksum = $checksum;
73 // Serialize information
74 if ($includeobj) {
75 $rec->controller = base64_encode(serialize($controller));
76 } else if ($cleanobj) {
77 $rec->controller = '';
79 // Send it to DB
80 if ($recexists = $DB->get_record('backup_controllers', array('backupid' => $rec->backupid))) {
81 $rec->id = $recexists->id;
82 $rec->timemodified = time();
83 $DB->update_record('backup_controllers', $rec);
84 } else {
85 $rec->timecreated = time();
86 $rec->timemodified = 0;
87 $rec->id = $DB->insert_record('backup_controllers', $rec);
89 return $rec->id;
92 public static function load_controller($restoreid) {
93 global $DB;
94 if (! $controllerrec = $DB->get_record('backup_controllers', array('backupid' => $restoreid))) {
95 throw new backup_dbops_exception('restore_controller_dbops_nonexisting');
97 $controller = unserialize(base64_decode($controllerrec->controller));
98 // Check checksum is ok. Sounds silly but it isn't ;-)
99 if (!$controller->is_checksum_correct($controllerrec->checksum)) {
100 throw new backup_dbops_exception('restore_controller_dbops_loading_checksum_mismatch');
102 return $controller;
105 public static function create_restore_temp_tables($restoreid) {
106 global $CFG, $DB;
107 $dbman = $DB->get_manager(); // We are going to use database_manager services
109 if ($dbman->table_exists('backup_ids_temp')) { // Table exists, from restore prechecks
110 // TODO: Improve this by inserting/selecting some record to see there is restoreid match
111 // TODO: If not match, exception, table corresponds to another backup/restore operation
112 return true;
114 backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_ids_template', 'backup_ids_temp');
115 backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_files_template', 'backup_files_temp');
116 return false;
119 public static function drop_restore_temp_tables($backupid) {
120 global $DB;
121 $dbman = $DB->get_manager(); // We are going to use database_manager services
123 $targettablenames = array('backup_ids_temp', 'backup_files_temp');
124 foreach ($targettablenames as $targettablename) {
125 $table = new xmldb_table($targettablename);
126 $dbman->drop_temp_table($table); // And drop it