MDL-73926 backup: Add hook to exclude events for triggering backup

This commit is contained in:
Tomo Tsuyuki 2023-12-14 10:01:47 +11:00
parent a891866cbd
commit f4597c9096
4 changed files with 90 additions and 2 deletions

View File

@ -7,6 +7,7 @@ information provided here is intended especially for developers.
async backups (See MDL-69983).
* During restore the function create_included_users has been updated to convert backups containing
legacy MD5 hashed passwords to the new password hashing scheme (See MDL-79134).
* New get_excluded_events hook for excluding events from automated backup has been implemented (See MDL-73926).
=== 4.1 ===

View File

@ -778,18 +778,31 @@ abstract class backup_cron_automated_helper {
* intentional, since we cannot reliably determine if any modification was made or not.
*/
protected static function is_course_modified($courseid, $since) {
global $DB;
$logmang = get_log_manager();
$readers = $logmang->get_readers('core\log\sql_reader');
$params = array('courseid' => $courseid, 'since' => $since);
// Exclude events defined by hook.
$hook = new \core\hook\backup\get_excluded_events();
\core\hook\manager::get_instance()->dispatch($hook);
$excludedevents = $hook->get_events();
foreach ($readers as $readerpluginname => $reader) {
$where = "courseid = :courseid and timecreated > :since and crud <> 'r'";
$excludeevents = [];
// Prevent logs of prevous backups causing a false positive.
if ($readerpluginname != 'logstore_legacy') {
$where .= " and target <> 'course_backup'";
$excludeevents[] = '\core\event\course_backup_created';
}
$excludeevents = array_merge($excludeevents, $excludedevents);
if ($excludeevents) {
list($notinsql, $notinparams) = $DB->get_in_or_equal($excludeevents, SQL_PARAMS_NAMED, 'eventname', false);
$where .= 'AND eventname ' . $notinsql;
$params = array_merge($params, $notinparams);
}
if ($reader->get_events_select_exists($where, $params)) {
return true;
}

View File

@ -0,0 +1,74 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\hook\backup;
use core\hook\described_hook;
/**
* Get a list of event names which are excluded to trigger from course changes in automated backup.
*
* @package core
* @copyright 2023 Tomo Tsuyuki <tomotsuyuki@catalyst-au.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class get_excluded_events implements described_hook {
/**
* @var string[] Array of event names.
*/
private $events = [];
/**
* Describes the hook purpose.
*
* @return string
*/
public static function get_hook_description(): string {
return 'Get a list of event names which are excluded to trigger from course changes in automated backup.';
}
/**
* List of tags that describe this hook.
*
* @return string[]
*/
public static function get_hook_tags(): array {
return ['backup'];
}
/**
* Add an array of event names which are excluded to trigger from course changes in automated backup.
* This is set from plugin hook.
* e.g. ['\local_course\event\update', '\local_course\event\sync']
*
* @param string[] $events Array of event name strings
* @return void
*/
public function add_events(array $events): void {
$this->events = array_merge($this->events, $events);
}
/**
* Get an array of event names which are excluded to trigger from course changes in automated backup.
* This is called after dispatch for the hook and use values to exclude events for backup.
*
* @return array
*/
public function get_events(): array {
return $this->events;
}
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2023120700.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2023120700.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.4dev (Build: 20231207)'; // Human-friendly version name