Merge branch 'MDL-63730-34-enfix' of git://github.com/mudrd8mz/moodle into MOODLE_34_...
[moodle.git] / backup / converter / imscc11 / backuplib.php
blob2d749bda4f8909fd70318a5b086d8cc361e69857
1 <?php
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.
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/>.
18 /**
19 * Provides {@link imscc11_export_converter} class
21 * @package core
22 * @subpackage backup-convert
23 * @copyright 2011 Darko Miletic <dmiletic@moodlerooms.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/backup/converter/convertlib.php');
31 class imscc11_export_converter extends base_converter {
32 static public function get_deps() {
33 global $CFG;
34 require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php');
35 return array(
36 'users' => setting_dependency::DISABLED_VALUE,
37 'filters' => setting_dependency::DISABLED_VALUE,
38 'blocks' => setting_dependency::DISABLED_VALUE
42 protected function execute() {
45 public static function description() {
47 return array(
48 'from' => backup::FORMAT_MOODLE,
49 'to' => backup::FORMAT_IMSCC11,
50 'cost' => 10
57 class imscc11_store_backup_file extends backup_execution_step {
59 protected function define_execution() {
61 // Get basepath
62 $basepath = $this->get_basepath();
64 // Calculate the zip fullpath (in OS temp area it's always backup.imscc)
65 $zipfile = $basepath . '/backup.imscc';
67 // Perform storage and return it (TODO: shouldn't be array but proper result object)
68 // Let's send the file to file storage, everything already defined
69 // First of all, get some information from the backup_controller to help us decide
70 list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid());
72 // Extract useful information to decide
73 $file = $sinfo['filename']->value;
74 $filename = basename($file,'.'.pathinfo($file, PATHINFO_EXTENSION)).'.imscc'; // Backup filename
75 $userid = $dinfo[0]->userid; // User->id executing the backup
76 $id = $dinfo[0]->id; // Id of activity/section/course (depends of type)
77 $courseid = $dinfo[0]->courseid; // Id of the course
79 $ctxid = context_user::instance($userid)->id;
80 $component = 'user';
81 $filearea = 'backup';
82 $itemid = 0;
83 $fs = get_file_storage();
84 $fr = array(
85 'contextid' => $ctxid,
86 'component' => $component,
87 'filearea' => $filearea,
88 'itemid' => $itemid,
89 'filepath' => '/',
90 'filename' => $filename,
91 'userid' => $userid,
92 'timecreated' => time(),
93 'timemodified'=> time());
94 // If file already exists, delete if before
95 // creating it again. This is BC behaviour - copy()
96 // overwrites by default
97 if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
98 $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
99 $sf = $fs->get_file_by_hash($pathnamehash);
100 $sf->delete();
103 return array('backup_destination' => $fs->create_file_from_pathname($fr, $zipfile));
107 class imscc11_zip_contents extends backup_execution_step {
109 protected function define_execution() {
111 // Get basepath
112 $basepath = $this->get_basepath();
114 // Get the list of files in directory
115 $filestemp = get_directory_list($basepath, '', false, true, true);
116 $files = array();
117 foreach ($filestemp as $file) {
118 // Add zip paths and fs paths to all them
119 $files[$file] = $basepath . '/' . $file;
122 // Calculate the zip fullpath (in OS temp area it's always backup.mbz)
123 $zipfile = $basepath . '/backup.imscc';
125 // Get the zip packer
126 $zippacker = get_file_packer('application/zip');
128 // Zip files
129 $zippacker->archive_to_pathname($files, $zipfile);
133 class imscc11_backup_convert extends backup_execution_step {
135 protected function define_execution() {
136 global $CFG;
137 // Get basepath
138 $basepath = $this->get_basepath();
140 require_once($CFG->dirroot . '/backup/cc/cc_includes.php');
142 $tempdir = $CFG->tempdir . '/backup/' . uniqid('', true);
144 if (mkdir($tempdir, $CFG->directorypermissions, true)) {
146 cc_convert_moodle2::convert($basepath, $tempdir);
147 //Switch the directories
148 if (empty($CFG->keeptempdirectoriesonbackup)) {
149 fulldelete($basepath);
150 } else {
151 if (!rename($basepath, $basepath . '_moodle2_source')) {
152 throw new backup_task_exception('failed_rename_source_tempdir');
156 if (!rename($tempdir, $basepath)) {
157 throw new backup_task_exception('failed_move_converted_into_place');