NOBUG: Added support for after_restore() methods on restore. Will help some blocks.
[moodle.git] / backup / util / plan / restore_plan.class.php
CommitLineData
b5d45a04
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-plan
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 * Implementable class defining the needed stuf for one restore plan
27 *
28 * TODO: Finish phpdocs
29 */
30class restore_plan extends base_plan implements loggable {
31
785d6603
SH
32 /**
33 *
34 * @var restore_controller
35 */
b5d45a04
EL
36 protected $controller; // The restore controller building/executing this plan
37 protected $basepath; // Fullpath to dir where backup is available
482aac65 38 protected $preloaded; // When executing the plan, do we have preloaded (from checks) info
03307846 39 protected $decoder; // restore_decode_processor in charge of decoding all the interlinks
58328ce8
EL
40 protected $missingmodules; // to flag if restore has detected some missing module
41 protected $excludingdactivities; // to flag if restore settings are excluding any activity
b5d45a04
EL
42
43 /**
44 * Constructor - instantiates one object of this class
45 */
46 public function __construct($controller) {
47 global $CFG;
48
49 if (! $controller instanceof restore_controller) {
50 throw new restore_plan_exception('wrong_restore_controller_specified');
51 }
52 $this->controller = $controller;
c9d8234a 53 $this->basepath = $CFG->dataroot . '/temp/backup/' . $controller->get_tempdir();
482aac65 54 $this->preloaded = false;
03307846 55 $this->decoder = new restore_decode_processor($this->get_restoreid(), $this->get_info()->original_wwwroot, $CFG->wwwroot);
58328ce8
EL
56 $this->missingmodules = false;
57 $this->excludingdactivities = false;
58
b5d45a04
EL
59 parent::__construct('restore_plan');
60 }
61
62 public function build() {
63 restore_plan_builder::build_plan($this->controller); // We are moodle2 always, go straight to builder
64 $this->built = true;
65 }
66
67 public function get_restoreid() {
68 return $this->controller->get_restoreid();
69 }
70
71 public function get_courseid() {
72 return $this->controller->get_courseid();
73 }
74
dc1e4cce
EL
75 public function get_mode() {
76 return $this->controller->get_mode();
77 }
78
b5d45a04
EL
79 public function get_basepath() {
80 return $this->basepath;
81 }
82
83 public function get_logger() {
84 return $this->controller->get_logger();
85 }
86
c9d8234a
EL
87 public function get_info() {
88 return $this->controller->get_info();
89 }
90
a4e13312
EL
91 public function get_target() {
92 return $this->controller->get_target();
93 }
94
482aac65
EL
95 public function get_userid() {
96 return $this->controller->get_userid();
97 }
98
03307846
EL
99 public function get_decoder() {
100 return $this->decoder;
101 }
102
482aac65
EL
103 public function is_samesite() {
104 return $this->controller->is_samesite();
105 }
106
58328ce8
EL
107 public function is_missing_modules() {
108 return $this->missingmodules;
109 }
110
111 public function is_excluding_activities() {
112 return $this->excludingdactivities;
113 }
114
482aac65
EL
115 public function set_preloaded_information() {
116 $this->preloaded = true;
117 }
118
119 public function get_preloaded_information() {
120 return $this->preloaded;
121 }
122
b1eaf633
EL
123 public function get_tempdir() {
124 return $this->controller->get_tempdir();
125 }
126
58328ce8
EL
127 public function set_missing_modules() {
128 $this->missingmodules = true;
129 }
130
131 public function set_excluding_activities() {
132 $this->excludingdactivities = true;
133 }
134
b5d45a04
EL
135 public function log($message, $level, $a = null, $depth = null, $display = false) {
136 backup_helper::log($message, $level, $a, $depth, $display, $this->get_logger());
137 }
138
139 /**
140 * Function responsible for executing the tasks of any plan
141 */
142 public function execute() {
143 if ($this->controller->get_status() != backup::STATUS_AWAITING) {
144 throw new restore_controller_exception('restore_not_executable_awaiting_required', $this->controller->get_status());
145 }
146 $this->controller->set_status(backup::STATUS_EXECUTING);
147 parent::execute();
148 $this->controller->set_status(backup::STATUS_FINISHED_OK);
149 }
648a575e
EL
150
151 /**
152 * Execute the after_restore methods of all the executed tasks in the plan
153 */
154 public function execute_after_restore() {
155 // Simply iterate over each task in the plan and delegate to them the execution
156 foreach ($this->tasks as $task) {
157 $task->execute_after_restore();
158 }
159 }
b5d45a04
EL
160}
161
162/*
163 * Exception class used by all the @restore_plan stuff
164 */
165class restore_plan_exception extends base_plan_exception {
166
167 public function __construct($errorcode, $a=NULL, $debuginfo=null) {
168 parent::__construct($errorcode, $a, $debuginfo);
169 }
170}