MDL-21432 backup - move all the files handling on restore to use new backup_files_tem...
[moodle.git] / backup / util / dbops / restore_controller_dbops.class.php
CommitLineData
fbc2778d
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-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
23 */
24
25/**
26 * Non instantiable helper class providing DB support to the @restore_controller
27 *
28 * This class contains various static methods available for all the DB operations
29 * performed by the restore_controller class
30 *
31 * TODO: Finish phpdocs
32 */
33abstract class restore_controller_dbops extends restore_dbops {
34
35 public static function save_controller($controller, $checksum) {
36 global $DB;
37 // Check we are going to save one backup_controller
38 if (! $controller instanceof restore_controller) {
39 throw new backup_controller_exception('restore_controller_expected');
40 }
41 // Check checksum is ok. Sounds silly but it isn't ;-)
42 if (!$controller->is_checksum_correct($checksum)) {
43 throw new restore_dbops_exception('restore_controller_dbops_saving_checksum_mismatch');
44 }
45 // Get all the columns
46 $rec = new stdclass();
47 $rec->backupid = $controller->get_restoreid();
48 $rec->operation = $controller->get_operation();
49 $rec->type = $controller->get_type();
50 $rec->itemid = $controller->get_courseid();
51 $rec->format = $controller->get_format();
52 $rec->interactive = $controller->get_interactive();
53 $rec->purpose = $controller->get_mode();
54 $rec->userid = $controller->get_userid();
55 $rec->status = $controller->get_status();
56 $rec->execution = $controller->get_execution();
57 $rec->executiontime= $controller->get_executiontime();
58 $rec->checksum = $checksum;
59 // Serialize information
60 $rec->controller = base64_encode(serialize($controller));
61 // Send it to DB
62 if ($recexists = $DB->get_record('backup_controllers', array('backupid' => $rec->backupid))) {
63 $rec->id = $recexists->id;
64 $rec->timemodified = time();
65 $DB->update_record('backup_controllers', $rec);
66 } else {
67 $rec->timecreated = time();
68 $rec->timemodified = 0;
69 $rec->id = $DB->insert_record('backup_controllers', $rec);
70 }
71 return $rec->id;
72 }
73
74 public static function load_controller($restoreid) {
75 global $DB;
76 if (! $controllerrec = $DB->get_record('backup_controllers', array('backupid' => $restoreid))) {
77 throw new backup_dbops_exception('restore_controller_dbops_nonexisting');
78 }
79 $controller = unserialize(base64_decode($controllerrec->controller));
80 // Check checksum is ok. Sounds silly but it isn't ;-)
81 if (!$controller->is_checksum_correct($controllerrec->checksum)) {
82 throw new backup_dbops_exception('restore_controller_dbops_loading_checksum_mismatch');
83 }
84 return $controller;
85 }
482aac65 86
b8bb45b0 87 public static function create_restore_temp_tables($restoreid) {
482aac65
EL
88 global $CFG, $DB;
89 $dbman = $DB->get_manager(); // We are going to use database_manager services
90
91 if ($dbman->table_exists('backup_ids_temp')) { // Table exists, from restore prechecks
92 // TODO: Improve this by inserting/selecting some record to see there is restoreid match
93 // TODO: If not match, exception, table corresponds to another backup/restore operation
94 return true;
95 }
b8bb45b0
EL
96 backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_ids_template', 'backup_ids_temp');
97 backup_controller_dbops::create_temptable_from_real_table($restoreid, 'backup_files_template', 'backup_files_temp');
482aac65
EL
98 return false;
99 }
b8bb45b0
EL
100
101 public static function drop_restore_temp_tables($backupid) {
102 global $DB;
103 $dbman = $DB->get_manager(); // We are going to use database_manager services
104
105 $targettablenames = array('backup_ids_temp', 'backup_files_temp');
106 foreach ($targettablenames as $targettablename) {
107 $table = new xmldb_table($targettablename);
108 $dbman->drop_temp_table($table); // And drop it
109 }
110 }
fbc2778d 111}