Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This script allows to restore a course from CLI.
19
 *
20
 * @package    core
21
 * @subpackage cli
22
 * @copyright  2020 Catalyst IT
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
define('CLI_SCRIPT', 1);
27
 
28
require(__DIR__ . '/../../config.php');
29
require_once($CFG->libdir . '/clilib.php');
30
require_once($CFG->dirroot . "/backup/util/includes/restore_includes.php");
31
 
32
list($options, $unrecognized) = cli_get_params([
33
    'file' => '',
34
    'categoryid' => '',
35
    'courseid' => '',
36
    'showdebugging' => false,
37
    'help' => false,
38
], [
39
    'f' => 'file',
40
    'c' => 'categoryid',
41
    'C' => 'courseid',
42
    's' => 'showdebugging',
43
    'h' => 'help',
44
]);
45
 
46
if ($unrecognized) {
47
    $unrecognized = implode("\n  ", $unrecognized);
48
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
49
}
50
 
51
if ($options['help'] || !($options['file']) || !($options['categoryid'] || $options['courseid'])) {
52
    $help = <<<EOL
53
Restore backup into provided category or course.
54
If courseid is set, course module/s will be added into the course.
55
 
56
Options:
57
-f, --file=STRING       Path to the backup file.
58
-c, --categoryid=INT    ID of the course category to restore to.
59
-C, --courseid=INT      ID of the course to restore to. This option is ignored if categoryid is set.
60
-s, --showdebugging     Show developer level debugging information
61
-h, --help              Print out this help.
62
 
63
Example:
64
\$sudo -u www-data /usr/bin/php admin/cli/restore_backup.php --file=/path/to/backup/file.mbz --categoryid=1\n
65
EOL;
66
 
67
    echo $help;
68
    exit(0);
69
}
70
 
71
if ($options['showdebugging']) {
72
    set_debugging(DEBUG_DEVELOPER, true);
73
}
74
 
75
if (!$admin = get_admin()) {
76
    throw new \moodle_exception('noadmins');
77
}
78
 
79
if (!file_exists($options['file'])) {
80
    throw new \moodle_exception('filenotfound');
81
}
82
 
83
if ($options['categoryid']) {
84
    if (!$category = $DB->get_record('course_categories', ['id' => $options['categoryid']], 'id')) {
85
        throw new \moodle_exception('invalidcategoryid');
86
    }
87
} else if ($options['courseid']) {
88
    if (!$course = $DB->get_record('course', ['id' => $options['courseid']], 'id')) {
89
        throw new \moodle_exception('invalidcourseid');
90
    }
91
} else {
92
    throw new \moodle_exception('invalidoption');
93
}
94
 
95
$backupdir = restore_controller::get_tempdir_name(SITEID, $USER->id);
96
$path = make_backup_temp_directory($backupdir);
97
 
98
cli_heading(get_string('extractingbackupfileto', 'backup', $path));
99
$fp = get_file_packer('application/vnd.moodle.backup');
100
$fp->extract_to_pathname($options['file'], $path);
101
 
102
cli_heading(get_string('preprocessingbackupfile'));
103
try {
104
    list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoringcourse', 'backup'),
105
        get_string('restoringcourseshortname', 'backup'));
106
 
107
    if (!empty($course)) {
108
        $courseid = $course->id;
109
        $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
110
            backup::MODE_GENERAL, $admin->id, backup::TARGET_EXISTING_ADDING);
111
    } else {
112
        $courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
113
        $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
114
            backup::MODE_GENERAL, $admin->id, backup::TARGET_NEW_COURSE);
115
    }
116
    $rc->execute_precheck();
117
    $rc->execute_plan();
118
    $rc->destroy();
119
 
120
    // Rename course name if the backup is from course module and restore to category.
121
    if (empty($course)) {
122
        $course = get_course($courseid);
123
        list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoretonewcourse', 'backup'),
124
            get_string('newcourse'));
125
        $course->fullname = $fullname;
126
        $course->shortname = $shortname;
127
        $course->visible = 1;
128
        $DB->update_record('course', $course);
129
    }
130
} catch (Exception $e) {
131
    cli_heading(get_string('cleaningtempdata'));
132
    fulldelete($path);
133
    throw new \moodle_exception('generalexceptionmessage', 'error', '', $e->getMessage());
134
}
135
 
136
cli_heading(get_string('restoredcourseid', 'backup', $courseid));
137
exit(0);