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
26define('CLI_SCRIPT', true);
27
28require(__DIR__ . '/../../../../config.php');
29require_once("$CFG->libdir/clilib.php");
30require_once("$CFG->dirroot/$CFG->admin/tool/customlang/locallib.php");
31
32$usage = <<<EOF
33"Export custom language files to a target folder.
34Useful for uploading custom langstrings to AMOS or importing or syncing them to other moodle instances.
35
36Options:
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 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
44Examples:
45Export all custom language files to the default folder:
46\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php
47
48Export 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
51EOF;
52
53$dafaulttarget = "$CFG->tempdir/customlang/";
54
55// Now get cli options.
56list($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
67if ($unrecognized) {
68    $unrecognized = implode("\n  ", $unrecognized);
69    cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
70}
71
72if ($options['help']) {
73    echo $usage;
74    die;
75}
76if (!file_exists($options['target'])) {
77    mkdir($options['target'], 0777, true);
78}
79
80cli_writeln(get_string('cliexportheading', 'tool_customlang'));
81$langs = [];
82if ($options['lang']) {
83    $langs = explode(',', $options['lang']);
84} else {
85    // No language set. We export all installed languages.
86    $langs = array_keys(get_string_manager()->get_list_of_translations(true));
87}
88
89foreach ($langs as $lang) {
90    $filename = $options['target'] . get_string('exportzipfilename', 'tool_customlang', ['lang' => $lang]);
91    // If the file exists and we are not using the temp folder it requires an ovewrite.
92    if ($options['target'] != $dafaulttarget && file_exists($filename) && !$options['overwrite']) {
93        cli_problem(get_string('cliexportfileexists', 'tool_customlang', $lang));
94        continue;
95    }
96    cli_heading(get_string('cliexportstartexport', 'tool_customlang', $lang));
97    $langdir = tool_customlang_utils::get_localpack_location($lang);
98    if (!file_exists($langdir)) {
99        // No custom files set for this language set.
100        cli_writeln(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
101        continue;
102    }
103    $zipper = get_file_packer();
104    $tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
105    $filelist = [];
106    if ($options['components']) {
107        $components = explode(',', $options['components']);
108        foreach ($components as $component) {
109            $filepath = "$langdir/$component.php";
110            if (file_exists($filepath)) {
111                $filelist["$component.php"] = $filepath;
112            } else {
113                cli_problem(
114                    get_string('cliexportfilenotfoundforcomponent', 'tool_customlang', ['lang' => $lang, 'file' => $filepath])
115                );
116            }
117        }
118    } else {
119        $langfiles = scandir($langdir);
120        foreach ($langfiles as $file) {
121            if (substr($file, 0, 1) != '.') {
122                $filelist[$file] = "$langdir/$file";
123            }
124        }
125    }
126    if (empty($filelist)) {
127        cli_problem(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
128        continue;
129    }
130    if ($zipper->archive_to_pathname($filelist, $filename)) {
131        cli_writeln(get_string('cliexportzipdone', 'tool_customlang', $filename));
132    } else {
133        cli_problem(get_string('cliexportzipfail', 'tool_customlang', $filename));
134    }
135}
136