PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/util/dbops/restore_controller_dbops.class.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 265 lines | 169 code | 20 blank | 76 comment | 37 complexity | c8887af494ff5280d90910a76df5c9bb MD5 | raw file
  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.
  13. //
  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/>.
  16. /**
  17. * @package moodlecore
  18. * @subpackage backup-dbops
  19. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. /**
  23. * Non instantiable helper class providing DB support to the @restore_controller
  24. *
  25. * This class contains various static methods available for all the DB operations
  26. * performed by the restore_controller class
  27. *
  28. * TODO: Finish phpdocs
  29. */
  30. abstract class restore_controller_dbops extends restore_dbops {
  31. /**
  32. * Send one restore controller to DB
  33. *
  34. * @param restore_controller $controller controller to send to DB
  35. * @param string $checksum hash of the controller to be checked
  36. * @param bool $includeobj to decide if the object itself must be updated (true) or no (false)
  37. * @param bool $cleanobj to decide if the object itself must be cleaned (true) or no (false)
  38. * @return int id of the controller record in the DB
  39. * @throws backup_controller_exception|restore_dbops_exception
  40. */
  41. public static function save_controller($controller, $checksum, $includeobj = true, $cleanobj = false) {
  42. global $DB;
  43. // Check we are going to save one backup_controller
  44. if (! $controller instanceof restore_controller) {
  45. throw new backup_controller_exception('restore_controller_expected');
  46. }
  47. // Check checksum is ok. Only if we are including object info. Sounds silly but it isn't ;-).
  48. if ($includeobj and !$controller->is_checksum_correct($checksum)) {
  49. throw new restore_dbops_exception('restore_controller_dbops_saving_checksum_mismatch');
  50. }
  51. // Cannot request to $includeobj and $cleanobj at the same time.
  52. if ($includeobj and $cleanobj) {
  53. throw new restore_dbops_exception('restore_controller_dbops_saving_cannot_include_and_delete');
  54. }
  55. // Get all the columns
  56. $rec = new stdclass();
  57. $rec->backupid = $controller->get_restoreid();
  58. $rec->operation = $controller->get_operation();
  59. $rec->type = $controller->get_type();
  60. $rec->itemid = $controller->get_courseid();
  61. $rec->format = $controller->get_format();
  62. $rec->interactive = $controller->get_interactive();
  63. $rec->purpose = $controller->get_mode();
  64. $rec->userid = $controller->get_userid();
  65. $rec->status = $controller->get_status();
  66. $rec->execution = $controller->get_execution();
  67. $rec->executiontime= $controller->get_executiontime();
  68. $rec->checksum = $checksum;
  69. // Serialize information
  70. if ($includeobj) {
  71. $rec->controller = base64_encode(serialize($controller));
  72. } else if ($cleanobj) {
  73. $rec->controller = '';
  74. }
  75. // Send it to DB
  76. if ($recexists = $DB->get_record('backup_controllers', array('backupid' => $rec->backupid))) {
  77. $rec->id = $recexists->id;
  78. $rec->timemodified = time();
  79. $DB->update_record('backup_controllers', $rec);
  80. } else {
  81. $rec->timecreated = time();
  82. $rec->timemodified = 0;
  83. $rec->id = $DB->insert_record('backup_controllers', $rec);
  84. }
  85. return $rec->id;
  86. }
  87. public static function load_controller($restoreid) {
  88. global $DB;
  89. if (! $controllerrec = $DB->get_record('backup_controllers', array('backupid' => $restoreid))) {
  90. throw new backup_dbops_exception('restore_controller_dbops_nonexisting');
  91. }
  92. $controller = unserialize(base64_decode($controllerrec->controller));
  93. if (!is_object($controller)) {
  94. // The controller field of the table did not contain a serialized object.
  95. // It is made empty after it has been used successfully, it is likely that
  96. // the user has pressed the browser back button at some point.
  97. throw new backup_dbops_exception('restore_controller_dbops_loading_invalid_controller');
  98. }
  99. // Check checksum is ok. Sounds silly but it isn't ;-)
  100. if (!$controller->is_checksum_correct($controllerrec->checksum)) {
  101. throw new backup_dbops_exception('restore_controller_dbops_loading_checksum_mismatch');
  102. }
  103. return $controller;
  104. }
  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
  108. if ($dbman->table_exists('backup_ids_temp')) { // Table exists, from restore prechecks
  109. // TODO: Improve this by inserting/selecting some record to see there is restoreid match
  110. // TODO: If not match, exception, table corresponds to another backup/restore operation
  111. return true;
  112. }
  113. backup_controller_dbops::create_backup_ids_temp_table($restoreid);
  114. backup_controller_dbops::create_backup_files_temp_table($restoreid);
  115. return false;
  116. }
  117. public static function drop_restore_temp_tables($backupid) {
  118. global $DB;
  119. $dbman = $DB->get_manager(); // We are going to use database_manager services
  120. $targettablenames = array('backup_ids_temp', 'backup_files_temp');
  121. foreach ($targettablenames as $targettablename) {
  122. $table = new xmldb_table($targettablename);
  123. $dbman->drop_table($table); // And drop it
  124. }
  125. // Invalidate the backup_ids caches.
  126. restore_dbops::reset_backup_ids_cached();
  127. }
  128. /**
  129. * Sets the default values for the settings in a restore operation
  130. *
  131. * @param restore_controller $controller
  132. */
  133. public static function apply_config_defaults(restore_controller $controller) {
  134. $settings = array(
  135. 'restore_general_users' => 'users',
  136. 'restore_general_enrolments' => 'enrolments',
  137. 'restore_general_role_assignments' => 'role_assignments',
  138. 'restore_general_activities' => 'activities',
  139. 'restore_general_blocks' => 'blocks',
  140. 'restore_general_filters' => 'filters',
  141. 'restore_general_comments' => 'comments',
  142. 'restore_general_badges' => 'badges',
  143. 'restore_general_calendarevents' => 'calendarevents',
  144. 'restore_general_userscompletion' => 'userscompletion',
  145. 'restore_general_logs' => 'logs',
  146. 'restore_general_histories' => 'grade_histories',
  147. 'restore_general_questionbank' => 'questionbank',
  148. 'restore_general_groups' => 'groups',
  149. 'restore_general_competencies' => 'competencies'
  150. );
  151. self::apply_admin_config_defaults($controller, $settings, true);
  152. $target = $controller->get_target();
  153. if ($target == backup::TARGET_EXISTING_ADDING || $target == backup::TARGET_CURRENT_ADDING) {
  154. $settings = array(
  155. 'restore_merge_overwrite_conf' => 'overwrite_conf',
  156. 'restore_merge_course_fullname' => 'course_fullname',
  157. 'restore_merge_course_shortname' => 'course_shortname',
  158. 'restore_merge_course_startdate' => 'course_startdate',
  159. );
  160. self::apply_admin_config_defaults($controller, $settings, true);
  161. }
  162. if ($target == backup::TARGET_EXISTING_DELETING || $target == backup::TARGET_CURRENT_DELETING) {
  163. $settings = array(
  164. 'restore_replace_overwrite_conf' => 'overwrite_conf',
  165. 'restore_replace_course_fullname' => 'course_fullname',
  166. 'restore_replace_course_shortname' => 'course_shortname',
  167. 'restore_replace_course_startdate' => 'course_startdate',
  168. 'restore_replace_keep_roles_and_enrolments' => 'keep_roles_and_enrolments',
  169. 'restore_replace_keep_groups_and_groupings' => 'keep_groups_and_groupings',
  170. );
  171. self::apply_admin_config_defaults($controller, $settings, true);
  172. }
  173. // Add some dependencies.
  174. $plan = $controller->get_plan();
  175. if ($plan->setting_exists('overwrite_conf')) {
  176. /** @var restore_course_overwrite_conf_setting $overwriteconf */
  177. $overwriteconf = $plan->get_setting('overwrite_conf');
  178. if ($overwriteconf->get_visibility()) {
  179. foreach (['course_fullname', 'course_shortname', 'course_startdate'] as $settingname) {
  180. if ($plan->setting_exists($settingname)) {
  181. $setting = $plan->get_setting($settingname);
  182. $overwriteconf->add_dependency($setting, setting_dependency::DISABLED_FALSE,
  183. array('defaultvalue' => $setting->get_value()));
  184. }
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Returns the default value to be used for a setting from the admin restore config
  191. *
  192. * @param string $config
  193. * @param backup_setting $setting
  194. * @return mixed
  195. */
  196. private static function get_setting_default($config, $setting) {
  197. $value = get_config('restore', $config);
  198. if (in_array($setting->get_name(), ['course_fullname', 'course_shortname', 'course_startdate']) &&
  199. $setting->get_ui() instanceof backup_setting_ui_defaultcustom) {
  200. // Special case - admin config settings course_fullname, etc. are boolean and the restore settings are strings.
  201. $value = (bool)$value;
  202. if ($value) {
  203. $attributes = $setting->get_ui()->get_attributes();
  204. $value = $attributes['customvalue'];
  205. }
  206. }
  207. if ($setting->get_ui() instanceof backup_setting_ui_select) {
  208. // Make sure the value is a valid option in the select element, otherwise just pick the first from the options list.
  209. // Example: enrolments dropdown may not have the "enrol_withusers" option because users info can not be restored.
  210. $options = array_keys($setting->get_ui()->get_values());
  211. if (!in_array($value, $options)) {
  212. $value = reset($options);
  213. }
  214. }
  215. return $value;
  216. }
  217. /**
  218. * Sets the controller settings default values from the admin config.
  219. *
  220. * @param restore_controller $controller
  221. * @param array $settings a map from admin config names to setting names (Config name => Setting name)
  222. * @param boolean $uselocks whether "locked" admin settings should be honoured
  223. */
  224. private static function apply_admin_config_defaults(restore_controller $controller, array $settings, $uselocks) {
  225. $plan = $controller->get_plan();
  226. foreach ($settings as $config => $settingname) {
  227. if ($plan->setting_exists($settingname)) {
  228. $setting = $plan->get_setting($settingname);
  229. $value = self::get_setting_default($config, $setting);
  230. $locked = (get_config('restore', $config . '_locked') == true);
  231. // We can only update the setting if it isn't already locked by config or permission.
  232. if ($setting->get_status() != base_setting::LOCKED_BY_CONFIG
  233. && $setting->get_status() != base_setting::LOCKED_BY_PERMISSION
  234. && $setting->get_ui()->is_changeable()) {
  235. $setting->set_value($value);
  236. if ($uselocks && $locked) {
  237. $setting->set_status(base_setting::LOCKED_BY_CONFIG);
  238. }
  239. }
  240. } else {
  241. $controller->log('Unknown setting: ' . $settingname, BACKUP::LOG_DEBUG);
  242. }
  243. }
  244. }
  245. }