Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * @package    moodlecore
  20   * @subpackage backup-helper
  21   * @copyright  2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Collection of helper functions to handle files
  27   *
  28   * This class implements various functions related with moodle storage
  29   * handling (get file from storage, put it there...) and some others
  30   * to use the zip/unzip facilities.
  31   *
  32   * Note: It's supposed that, some day, files implementation will offer
  33   * those functions without needeing to know storage internals at all.
  34   * That day, we'll move related functions here to proper file api ones.
  35   *
  36   * TODO: Finish phpdocs
  37   */
  38  class backup_file_manager {
  39  
  40      /**
  41       * Returns the full path to backup storage base dir
  42       */
  43      public static function get_backup_storage_base_dir($backupid) {
  44          global $CFG;
  45  
  46          $backupiddir = make_backup_temp_directory($backupid);
  47          return $backupiddir . '/files';
  48      }
  49  
  50      /**
  51       * Given one file content hash, returns the path (relative to filedir)
  52       * to the file.
  53       */
  54      public static function get_backup_content_file_location($contenthash) {
  55          $l1 = $contenthash[0].$contenthash[1];
  56          return "$l1/$contenthash";
  57      }
  58  
  59      /**
  60       * Copy one file from moodle storage to backup storage
  61       */
  62      public static function copy_file_moodle2backup($backupid, $filerecorid) {
  63          global $DB;
  64  
  65          if (!backup_controller_dbops::backup_includes_files($backupid)) {
  66              // Only include the files if required by the controller.
  67              return;
  68          }
  69  
  70          // Normalise param
  71          if (!is_object($filerecorid)) {
  72              $filerecorid = $DB->get_record('files', array('id' => $filerecorid));
  73          }
  74  
  75          // Directory, nothing to do
  76          if ($filerecorid->filename === '.') {
  77              return;
  78          }
  79  
  80          $fs = get_file_storage();
  81          $file = $fs->get_file_instance($filerecorid);
  82          // If the file is external file, skip copying.
  83          if ($file->is_external_file()) {
  84              return;
  85          }
  86  
  87          // Calculate source and target paths (use same subdirs strategy for both)
  88          $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' .
  89                            self::get_backup_content_file_location($filerecorid->contenthash);
  90  
  91          // Create target dir if necessary
  92          if (!file_exists(dirname($targetfilepath))) {
  93              if (!check_dir_exists(dirname($targetfilepath), true, true)) {
  94                  throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath));
  95              }
  96          }
  97  
  98          // And copy the file (if doesn't exist already)
  99          if (!file_exists($targetfilepath)) {
 100              $file->copy_content_to($targetfilepath);
 101          }
 102      }
 103  }