PageRenderTime 65ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/backup/util/helper/restore_logs_processor.class.php

https://bitbucket.org/moodle/moodle
PHP | 144 lines | 73 code | 16 blank | 55 comment | 12 complexity | 7a2127395ba10ab30acb6ca8ffe2bc4f MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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-helper
  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. * TODO: Finish phpdocs
  23. */
  24. /**
  25. * This class is one varying singleton that, for all the logs corresponding to
  26. * one task, is in charge of storing all its {@link restore_log_rule} rules,
  27. * dispatching to the correct one and insert/log the resulting information.
  28. *
  29. * Each time the task getting the instance changes, the rules are completely
  30. * reloaded with the ones in the new task. And all rules are informed with
  31. * new fixed values if explicity set.
  32. *
  33. * This class adopts the singleton pattern to be able to provide some persistency
  34. * of rules along the restore of all the logs corresponding to one restore_task
  35. */
  36. class restore_logs_processor {
  37. private static $instance; // The current instance of restore_logs_processor
  38. private static $task; // The current restore_task instance this processor belongs to
  39. private $rules; // Array of restore_log_rule rules (module-action being keys), supports multiple per key
  40. private function __construct($values) { // Private constructor
  41. // Constructor has been called, so we need to reload everything
  42. // Process rules
  43. $this->rules = array();
  44. $rules = call_user_func(array(self::$task, 'define_restore_log_rules'));
  45. foreach ($rules as $rule) {
  46. // TODO: Check it is one restore_log_rule
  47. // Set rule restoreid
  48. $rule->set_restoreid(self::$task->get_restoreid());
  49. // Set rule fixed values if needed
  50. if (is_array($values) and !empty($values)) {
  51. $rule->set_fixed_values($values);
  52. }
  53. // Add the rule to the associative array
  54. if (array_key_exists($rule->get_key_name(), $this->rules)) {
  55. $this->rules[$rule->get_key_name()][] = $rule;
  56. } else {
  57. $this->rules[$rule->get_key_name()] = array($rule);
  58. }
  59. }
  60. }
  61. public static function get_instance($task, $values) {
  62. // If the singleton isn't set or if the task is another one, create new instance
  63. if (!isset(self::$instance) || self::$task !== $task) {
  64. self::$task = $task;
  65. self::$instance = new restore_logs_processor($values);
  66. }
  67. return self::$instance;
  68. }
  69. public function process_log_record($log) {
  70. // Check we have one restore_log_rule for this log record
  71. $keyname = $log->module . '-' . $log->action;
  72. if (array_key_exists($keyname, $this->rules)) {
  73. // Try it for each rule available
  74. foreach ($this->rules[$keyname] as $rule) {
  75. $newlog = $rule->process($log);
  76. // Some rule has been able to perform the conversion, exit from loop
  77. if (!empty($newlog)) {
  78. break;
  79. }
  80. }
  81. // Arrived here log is empty, no rule was able to perform the conversion, log the problem
  82. if (empty($newlog)) {
  83. self::$task->log('Log module-action "' . $keyname . '" process problem. Not restored. ' .
  84. json_encode($log), backup::LOG_DEBUG);
  85. }
  86. } else { // Action not found log the problem
  87. self::$task->log('Log module-action "' . $keyname . '" unknown. Not restored. '.json_encode($log), backup::LOG_DEBUG);
  88. $newlog = false;
  89. }
  90. return $newlog;
  91. }
  92. /**
  93. * Adds all the activity {@link restore_log_rule} rules
  94. * defined in activity task but corresponding to log
  95. * records at course level (cmid = 0).
  96. */
  97. public static function register_log_rules_for_course() {
  98. $tasks = array(); // To get the list of tasks having log rules for course
  99. $rules = array(); // To accumulate rules for course
  100. // Add the module tasks
  101. $mods = core_component::get_plugin_list('mod');
  102. foreach ($mods as $mod => $moddir) {
  103. if (class_exists('restore_' . $mod . '_activity_task')) {
  104. $tasks[$mod] = 'restore_' . $mod . '_activity_task';
  105. }
  106. }
  107. foreach ($tasks as $mod => $classname) {
  108. if (!method_exists($classname, 'define_restore_log_rules_for_course')) {
  109. continue; // This method is optional
  110. }
  111. // Get restore_log_rule array and accumulate
  112. $taskrules = call_user_func(array($classname, 'define_restore_log_rules_for_course'));
  113. if (!is_array($taskrules)) {
  114. throw new restore_logs_processor_exception('define_restore_log_rules_for_course_not_array', $classname);
  115. }
  116. $rules = array_merge($rules, $taskrules);
  117. }
  118. return $rules;
  119. }
  120. }
  121. /*
  122. * Exception class used by all the @restore_logs_processor stuff
  123. */
  124. class restore_logs_processor_exception extends backup_exception {
  125. public function __construct($errorcode, $a=NULL, $debuginfo=null) {
  126. return parent::__construct($errorcode, $a, $debuginfo);
  127. }
  128. }