Skip to content

Commit

Permalink
Merge branch 'MDL-81044-main' of https://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
junpataleta committed Mar 7, 2024
2 parents fd7a6b4 + 62ae5b6 commit f364dd5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
5 changes: 4 additions & 1 deletion backup/upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
This files describes API changes in /backup/*,
information provided here is intended especially for developers.

=== 4.4 ===

* New \core_backup\hook\before_course_modified_check hook which can be used to exclude events from automated backup has been implemented (See MDL-73926 and MDL-81044).

=== 4.3 ===

* The function get_async_backup_links_backup has a new param of $backupid and is part of a fix to
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 ===

Expand Down
33 changes: 15 additions & 18 deletions backup/util/helper/backup_cron_helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
/**
* Utility helper for automated backups run through cron.
*
* This class is an abstract class with methods that can be called to aid the
* running of automated backups over cron.
*
* @package core
* @subpackage backup
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* This class is an abstract class with methods that can be called to aid the
* running of automated backups over cron.
*/
abstract class backup_cron_automated_helper {

/** Automated backups are active and ready to run */
const STATE_OK = 0;
/** Automated backups are disabled and will not be run */
Expand Down Expand Up @@ -796,27 +791,29 @@ protected static function get_backups_to_delete($backupfiles, $now) {
*/
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);

/** @var \core\log\sql_reader[] */
$readers = get_log_manager()->get_readers('core\log\sql_reader');

// Exclude events defined by hook.
$hook = new \core\hook\backup\get_excluded_events();
$hook = new \core_backup\hook\before_course_modified_check();
\core\hook\manager::get_instance()->dispatch($hook);
$excludedevents = $hook->get_events();

foreach ($readers as $readerpluginname => $reader) {
$params = [
'courseid' => $courseid,
'since' => $since,
];
$where = "courseid = :courseid and timecreated > :since and crud <> 'r'";

$excludeevents = [];
// Prevent logs of prevous backups causing a false positive.
if ($readerpluginname != 'logstore_legacy') {
$excludeevents = $hook->get_excluded_events();
// Prevent logs of previous backups causing a false positive.
if ($readerpluginname !== 'logstore_legacy') {
$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);
[$notinsql, $notinparams] = $DB->get_in_or_equal($excludeevents, SQL_PARAMS_NAMED, 'eventname', false);
$where .= 'AND eventname ' . $notinsql;
$params = array_merge($params, $notinparams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
// 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;
namespace core_backup\hook;

use core\hook\described_hook;

/**
* Get a list of event names which are excluded to trigger from course changes in automated backup.
*
* @package core
* @package core_backup
* @copyright 2023 Tomo Tsuyuki <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
final class get_excluded_events implements described_hook {

final class before_course_modified_check implements described_hook {
/**
* @var string[] Array of event names.
*/
Expand All @@ -52,13 +51,10 @@ public static function get_hook_tags(): array {

/**
* 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
* @param string $events,... Array of event name strings
*/
public function add_events(array $events): void {
public function exclude_events(string ...$events): void {
$this->events = array_merge($this->events, $events);
}

Expand All @@ -68,7 +64,7 @@ public function add_events(array $events): void {
*
* @return array
*/
public function get_events(): array {
public function get_excluded_events(): array {
return $this->events;
}
}

0 comments on commit f364dd5

Please sign in to comment.