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
 * Export custom language strings to zip files.
19
 *
20
 * @package    tool_customlang
21
 * @subpackage customlang
22
 * @copyright  2020 Thomas Wedekind <thomas.wedekind@univie.ac.at>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
define('CLI_SCRIPT', true);
27
 
28
require(__DIR__ . '/../../../../config.php');
29
require_once("$CFG->libdir/clilib.php");
30
require_once("$CFG->dirroot/$CFG->admin/tool/customlang/locallib.php");
31
 
32
$usage = <<<EOF
33
"Export custom language files to a target folder.
34
Useful for uploading custom langstrings to AMOS or importing or syncing them to other moodle instances.
35
 
36
Options:
37
-l, --lang              Comma seperated language ids to export, default: all
38
-c, --components        Comma seperated components to export, default: all
39
-t, --target            Target directory to copy the zip files to, default: $CFG->tempdir/customlang
40
-o, --overwrite         Overwrite existing files in the target folder.
41
                            Note: If the target is not set, the files are always overwritten!
42
-h, --help              Print out this help
43
 
44
Examples:
45
Export all custom language files to the default folder:
46
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php
47
 
48
Export just the english files of moodle core and the activity 'quiz' in a subfolder in my home folder:
49
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php --lang='en' --components='moodle,quiz' --target='~/customdir'
50
 
51
EOF;
52
 
53
$dafaulttarget = "$CFG->tempdir/customlang/";
54
 
55
// Now get cli options.
56
list($options, $unrecognized) = cli_get_params(
57
    [
58
        'lang' => '',
59
        'components' => '',
60
        'target' => $dafaulttarget,
61
        'overwrite' => false,
62
        'help' => false,
63
    ],
64
    ['h' => 'help', 'c' => 'components', 't' => 'target', 'o' => 'overwrite']
65
);
66
 
67
if ($unrecognized) {
68
    $unrecognized = implode("\n  ", $unrecognized);
69
    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
70
}
71
 
72
if ($options['help']) {
73
    echo $usage;
74
    die;
75
}
76
 
77
// Ensure target directory exists.
78
$options['target'] = rtrim($options['target'], '/') . '/';
79
check_dir_exists($options['target']);
80
 
81
cli_writeln(get_string('cliexportheading', 'tool_customlang'));
82
$langs = [];
83
if ($options['lang']) {
84
    $langs = explode(',', $options['lang']);
85
} else {
86
    // No language set. We export all installed languages.
87
    $langs = array_keys(get_string_manager()->get_list_of_translations(true));
88
}
89
 
90
foreach ($langs as $lang) {
91
    $filename = $options['target'] . "customlang_{$lang}.zip";
92
    // If the file exists and we are not using the temp folder it requires an ovewrite.
93
    if ($options['target'] != $dafaulttarget && file_exists($filename) && !$options['overwrite']) {
94
        cli_problem(get_string('cliexportfileexists', 'tool_customlang', ['lang' => $lang]));
95
        continue;
96
    }
97
    cli_heading(get_string('cliexportstartexport', 'tool_customlang', $lang));
98
    $langdir = tool_customlang_utils::get_localpack_location($lang);
99
    if (!file_exists($langdir)) {
100
        // No custom files set for this language set.
101
        cli_writeln(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
102
        continue;
103
    }
104
    $zipper = get_file_packer();
105
    $tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
106
    $filelist = [];
107
    if ($options['components']) {
108
        $components = explode(',', $options['components']);
109
        foreach ($components as $component) {
110
            $filepath = "$langdir/$component.php";
111
            if (file_exists($filepath)) {
112
                $filelist["$component.php"] = $filepath;
113
            } else {
114
                cli_problem(
115
                    get_string('cliexportfilenotfoundforcomponent', 'tool_customlang', ['lang' => $lang, 'file' => $filepath])
116
                );
117
            }
118
        }
119
    } else {
120
        $langfiles = scandir($langdir);
121
        foreach ($langfiles as $file) {
122
            if (substr($file, 0, 1) != '.') {
123
                $filelist[$file] = "$langdir/$file";
124
            }
125
        }
126
    }
127
    if (empty($filelist)) {
128
        cli_problem(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
129
        continue;
130
    }
131
    if ($zipper->archive_to_pathname($filelist, $filename)) {
132
        cli_writeln(get_string('cliexportzipdone', 'tool_customlang', $filename));
133
    } else {
134
        cli_problem(get_string('cliexportzipfail', 'tool_customlang', $filename));
135
    }
136
}