MDL-24283 improved stored_file encapsulation, this should allow us to hack with store...
[moodle.git] / backup / util / helper / backup_file_manager.class.php
CommitLineData
69dd0c8c
EL
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 *
69dd0c8c
EL
36 * TODO: Finish phpdocs
37 */
38class backup_file_manager {
39
69dd0c8c
EL
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->dataroot . '/temp/backup/' . $backupid . '/files';
47 }
48
49 /**
50 * Given one file content hash, returns the path (relative to filedir)
51 * to the file.
52 */
2b199e7c 53 public static function get_backup_content_file_location($contenthash) {
69dd0c8c 54 $l1 = $contenthash[0].$contenthash[1];
2b199e7c 55 return "$l1/$contenthash";
69dd0c8c
EL
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 // Normalise param
65 if (!is_object($filerecorid)) {
66 $filerecorid = $DB->get_record('files', array('id' => $filerecorid));
67 }
68
69 // Directory, nothing to do
70 if ($filerecorid->filename === '.') {
71 return;
72 }
73
2b199e7c 74 $fs = get_file_storage();
693ef3a8 75 $file = $fs->get_file_instance($filerecorid);
2b199e7c 76
69dd0c8c 77 // Calculate source and target paths (use same subdirs strategy for both)
69dd0c8c 78 $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' .
2b199e7c 79 self::get_backup_content_file_location($filerecorid->contenthash);
69dd0c8c
EL
80
81 // Create target dir if necessary
82 if (!file_exists(dirname($targetfilepath))) {
83 if (!check_dir_exists(dirname($targetfilepath), true, true)) {
84 throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath));
85 }
86 }
87
88 // And copy the file (if doesn't exist already)
89 if (!file_exists($targetfilepath)) {
2b199e7c 90 $file->copy_content_to($targetfilepath);
69dd0c8c
EL
91 }
92 }
69dd0c8c 93}