MDL-35773 core_backup: controller has full responsibility for files
authorMark Nelson <mdjnelson@gmail.com>
Fri, 26 Jul 2019 05:18:01 +0000 (13:18 +0800)
committerMark Nelson <mdjnelson@gmail.com>
Fri, 26 Jul 2019 09:00:40 +0000 (17:00 +0800)
backup/controller/backup_controller.class.php
backup/moodle2/backup_custom_fields.php
backup/moodle2/backup_stepslib.php

index 7180a3a..cf5fcf9 100644 (file)
@@ -271,6 +271,37 @@ class backup_controller extends base_controller {
         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;
     }
@@ -326,6 +357,12 @@ class backup_controller extends base_controller {
         // 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
@@ -386,42 +423,19 @@ class backup_controller extends base_controller {
         $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;
     }
-
 }
 
 /*
index 75bab78..9de5ce6 100644 (file)
@@ -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)) {
@@ -259,36 +254,25 @@ class file_nested_element extends backup_nested_element {
         // 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;
-    }
 }
 
 /**
index 9cebbe8..11dd4ce 100644 (file)
@@ -1794,9 +1794,6 @@ class backup_final_files_structure_step extends backup_structure_step {
             '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);
@@ -1836,8 +1833,7 @@ class backup_main_structure_step extends backup_structure_step {
         $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;