[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/util/helper/ -> backup_file_manager.class.php (source)

   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          return $CFG->tempdir . '/backup/' . $backupid . '/files';
  47      }
  48  
  49      /**
  50       * Given one file content hash, returns the path (relative to filedir)
  51       * to the file.
  52       */
  53      public static function get_backup_content_file_location($contenthash) {
  54          $l1 = $contenthash[0].$contenthash[1];
  55          return "$l1/$contenthash";
  56      }
  57  
  58      /**
  59       * Copy one file from moodle storage to backup storage
  60       */
  61      public static function copy_file_moodle2backup($backupid, $filerecorid) {
  62          global $DB;
  63  
  64          if (!backup_controller_dbops::backup_includes_files($backupid)) {
  65              // Only include the files if required by the controller.
  66              return;
  67          }
  68  
  69          // Normalise param
  70          if (!is_object($filerecorid)) {
  71              $filerecorid = $DB->get_record('files', array('id' => $filerecorid));
  72          }
  73  
  74          // Directory, nothing to do
  75          if ($filerecorid->filename === '.') {
  76              return;
  77          }
  78  
  79          $fs = get_file_storage();
  80          $file = $fs->get_file_instance($filerecorid);
  81          // If the file is external file, skip copying.
  82          if ($file->is_external_file()) {
  83              return;
  84          }
  85  
  86          // Calculate source and target paths (use same subdirs strategy for both)
  87          $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' .
  88                            self::get_backup_content_file_location($filerecorid->contenthash);
  89  
  90          // Create target dir if necessary
  91          if (!file_exists(dirname($targetfilepath))) {
  92              if (!check_dir_exists(dirname($targetfilepath), true, true)) {
  93                  throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath));
  94              }
  95          }
  96  
  97          // And copy the file (if doesn't exist already)
  98          if (!file_exists($targetfilepath)) {
  99              $file->copy_content_to($targetfilepath);
 100          }
 101      }
 102  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1