Skip to content

Commit

Permalink
MDL-35773 core_backup: controller has full responsibility for files
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Jul 26, 2019
1 parent 9bfdfbb commit 56e28c3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 64 deletions.
68 changes: 41 additions & 27 deletions backup/controller/backup_controller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,37 @@ public function get_include_files() {
return $this->includefiles;
}

/**
* Returns the default value for $this->includefiles before we consider any settings.
*
* @return bool
* @throws dml_exception
*/
protected function get_include_files_default() : bool {
// We normally include files.
$includefiles = true;

// In an import, we don't need to include files.
if ($this->get_mode() === backup::MODE_IMPORT) {
$includefiles = false;
}

// When a backup is intended for the same site, we don't need to include the files.
// Note, this setting is only used for duplication of an entire course.
if ($this->get_mode() === backup::MODE_SAMESITE) {
$includefiles = false;
}

// If backup is automated and we have set auto backup config to exclude
// files then set them to be excluded here.
$backupautofiles = (bool) get_config('backup', 'backup_auto_files');
if ($this->get_mode() === backup::MODE_AUTOMATED && !$backupautofiles) {
$includefiles = false;
}

return $includefiles;
}

public function get_operation() {
return $this->operation;
}
Expand Down Expand Up @@ -326,6 +357,12 @@ public function execute_plan() {
// Basic/initial prevention against time/memory limits
core_php_time_limit::raise(1 * 60 * 60); // 1 hour for 1 course initially granted
raise_memory_limit(MEMORY_EXTRA);

// If the controller has decided that we can include files, then check the setting, otherwise do not include files.
if ($this->get_include_files()) {
$this->set_include_files((bool) $this->get_plan()->get_setting('files')->get_value());
}

// If this is not a course backup, or single activity backup (e.g. duplicate) inform the plan we are not
// including all the activities for sure. This will affect any
// task/step executed conditionally to stop including information
Expand Down Expand Up @@ -386,42 +423,19 @@ protected function apply_defaults() {
$this->log('applying plan defaults', backup::LOG_DEBUG);
backup_controller_dbops::apply_config_defaults($this);
$this->set_status(backup::STATUS_CONFIGURED);
$this->set_include_files();
$this->set_include_files($this->get_include_files_default());
}

/**
* Set the initial value for the include_files setting.
*
* @param bool $includefiles
* @see backup_controller::get_include_files for further information on the purpose of this setting.
* @return int Indicates whether files should be included in backups.
*/
protected function set_include_files() {
// We normally include files.
$includefiles = true;

// In an import, we don't need to include files.
if ($this->get_mode() === backup::MODE_IMPORT) {
$includefiles = false;
}

// When a backup is intended for the same site, we don't need to include the files.
// Note, this setting is only used for duplication of an entire course.
if ($this->get_mode() === backup::MODE_SAMESITE) {
$includefiles = false;
}

// If backup is automated and we have set auto backup config to exclude
// files then set them to be excluded here.
$backupautofiles = (bool)get_config('backup', 'backup_auto_files');
if ($this->get_mode() === backup::MODE_AUTOMATED && !$backupautofiles) {
$includefiles = false;
}

$this->includefiles = (int) $includefiles;
protected function set_include_files(bool $includefiles) {
$this->log("setting file inclusion to {$this->includefiles}", backup::LOG_DEBUG);
return $this->includefiles;
$this->includefiles = (int) $includefiles;
}

}

/*
Expand Down
48 changes: 16 additions & 32 deletions backup/moodle2/backup_custom_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,6 @@ class file_nested_element extends backup_nested_element {

protected $backupid;

/**
* @var bool Are we including the files?
*/
protected $includefiles = true;

public function process($processor) {
// Get current backupid from processor, we'll need later
if (is_null($this->backupid)) {
Expand All @@ -259,36 +254,25 @@ public function fill_values($values) {
// Fill values
parent::fill_values($values);
// Do our own tasks (copy file from moodle to backup)
if ($this->includefiles) {
try {
backup_file_manager::copy_file_moodle2backup($this->backupid, $values);
} catch (file_exception $e) {
$this->add_result(array('missing_files_in_pool' => true));

// Build helpful log message with all information necessary to identify
// file location.
$context = context::instance_by_id($values->contextid, IGNORE_MISSING);
$contextname = '';
if ($context) {
$contextname = ' \'' . $context->get_context_name() . '\'';
}
$message = 'Missing file in pool: ' . $values->filepath . $values->filename .
' (context ' . $values->contextid . $contextname . ', component ' .
$values->component . ', filearea ' . $values->filearea . ', itemid ' .
$values->itemid . ') [' . $e->debuginfo . ']';
$this->add_log($message, backup::LOG_WARNING);
try {
backup_file_manager::copy_file_moodle2backup($this->backupid, $values);
} catch (file_exception $e) {
$this->add_result(array('missing_files_in_pool' => true));

// Build helpful log message with all information necessary to identify
// file location.
$context = context::instance_by_id($values->contextid, IGNORE_MISSING);
$contextname = '';
if ($context) {
$contextname = ' \'' . $context->get_context_name() . '\'';
}
$message = 'Missing file in pool: ' . $values->filepath . $values->filename .
' (context ' . $values->contextid . $contextname . ', component ' .
$values->component . ', filearea ' . $values->filearea . ', itemid ' .
$values->itemid . ') [' . $e->debuginfo . ']';
$this->add_log($message, backup::LOG_WARNING);
}
}

/**
* Sets the value for includefiles.
*
* @param bool $value The value to set it to
*/
public function set_include_files(bool $value) {
$this->includefiles = $value;
}
}

/**
Expand Down
6 changes: 1 addition & 5 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1794,9 +1794,6 @@ protected function define_structure() {
'source', 'author', 'license', 'sortorder',
'repositorytype', 'repositoryid', 'reference'));

// Set whether we want to store the actual files in the backup.
$file->set_include_files($this->get_setting_value('files'));

// Build the tree

$files->add_child($file);
Expand Down Expand Up @@ -1836,8 +1833,7 @@ protected function define_structure() {
$info['backup_date'] = time();
$info['backup_uniqueid']= $this->get_backupid();
$info['mnet_remoteusers']=backup_controller_dbops::backup_includes_mnet_remote_users($this->get_backupid());
$info['include_files'] = $this->get_setting_value('files') &&
backup_controller_dbops::backup_includes_files($this->get_backupid());
$info['include_files'] = backup_controller_dbops::backup_includes_files($this->get_backupid());
$info['include_file_references_to_external_content'] =
backup_controller_dbops::backup_includes_file_references($this->get_backupid());
$info['original_wwwroot']=$CFG->wwwroot;
Expand Down

0 comments on commit 56e28c3

Please sign in to comment.