From d7cadcc2442eba904d1e66c9b3c8e83103df1178 Mon Sep 17 00:00:00 2001 From: Ferran Recio Date: Tue, 25 Aug 2020 16:20:16 +0200 Subject: [PATCH] MDL-69583 tool_customlang: add CLI import --- admin/tool/customlang/cli/import.php | 204 ++++++++++++++++++ .../customlang/lang/en/tool_customlang.php | 7 + 2 files changed, 211 insertions(+) create mode 100644 admin/tool/customlang/cli/import.php diff --git a/admin/tool/customlang/cli/import.php b/admin/tool/customlang/cli/import.php new file mode 100644 index 00000000000..4daff057fdf --- /dev/null +++ b/admin/tool/customlang/cli/import.php @@ -0,0 +1,204 @@ +. + +/** + * CLI customlang import tool. + * + * @package tool_customlang + * @copyright 2020 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +use tool_customlang\local\importer; +use core\output\notification; + +define('CLI_SCRIPT', true); + +require(__DIR__ . '/../../../../config.php'); +require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php'); +require_once("$CFG->libdir/clilib.php"); + +$usage = +"Import lang customization. + +It can get a single file or a folder. +If no lang is provided it will try to infere from the filename + +Options: +--lang The target language (will get from filename if not provided) +--source=path File or folder of the custom lang files (zip or php files) +--mode What string should be imported. Options are: + - all: all string will be imported (default) + - new: only string with no previous customisation + - update: only strings already modified +--checkin Save strings to the language pack +-h, --help Print out this help + +Examples: +\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --lang=en --source=customlangs.zip + +\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --source=/tmp/customlangs --checkin + +\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --lang=en --source=/tmp/customlangs + +"; + +list($options, $unrecognized) = cli_get_params( + [ + 'help' => false, + 'lang' => false, + 'source' => false, + 'mode' => 'all', + 'checkin' => false, + ], + ['h' => 'help'] +); + +if ($unrecognized) { + $unrecognized = implode("\n ", $unrecognized); + cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); +} + +if ($options['help']) { + cli_write($usage); + exit(0); +} + +$source = $options['source'] ?? null; +$lang = $options['lang'] ?? null; +$modeparam = $options['mode'] ?? 'all'; +$checkin = $options['checkin'] ?? false; + +$modes = [ + 'all' => importer::IMPORTALL, + 'update' => importer::IMPORTUPDATE, + 'new' => importer::IMPORTNEW, +]; +if (!isset($modes[$modeparam])) { + cli_error(get_string('climissingmode', 'tool_customlang')); +} +$mode = $modes[$modeparam]; + +if (empty($source)) { + $source = $CFG->dataroot.'/temp/customlang'; +} + +if (!file_exists($source)) { + cli_error(get_string('climissingsource', 'tool_customlang')); +} + +// Emulate normal session - we use admin account by default. +cron_setup_user(); + +// Get the file list. +$files = []; +$langfiles = []; + +if (is_file($source)) { + $files[] = $source; +} +if (is_dir($source)) { + $filelist = glob("$source/*"); + foreach ($filelist as $filename) { + $files[] = "$filename"; + } +} + +$countfiles = 0; +foreach ($files as $filepath) { + // Try to get the lang. + $filelang = $lang; + // Get component from filename. + $pathparts = pathinfo($filepath); + $filename = $pathparts['filename']; + $extension = $pathparts['extension']; + if ($extension == 'zip') { + if (!$filelang) { + // Try to get the lang from the filename. + if (strrpos($filename, 'customlang_') === 0) { + $parts = explode('_', $filename); + if (!empty($parts[1])) { + $filelang = $parts[1]; + } + } + } + } else if ($extension != 'php') { + // Ignore any other file extension. + continue; + } + if (empty($filelang)) { + cli_error(get_string('climissinglang', 'tool_customlang')); + } + if (!isset($langfiles[$filelang])) { + $langfiles[$filelang] = []; + } + $langfiles[$filelang][] = $filepath; + $countfiles ++; +} + +if (!$countfiles) { + cli_error(get_string('climissingfiles', 'tool_customlang')); +} + +foreach ($langfiles as $lng => $files) { + $importer = new importer($lng, $mode); + $storedfiles = []; + $fs = get_file_storage(); + + cli_heading(get_string('clifiles', 'tool_customlang', $lng)); + + foreach ($files as $file) { + // Generate a valid stored_file from this file. + $record = (object)[ + 'filearea' => 'draft', + 'component' => 'user', + 'filepath' => '/', + 'itemid' => file_get_unused_draft_itemid(), + 'license' => $CFG->sitedefaultlicense, + 'author' => '', + 'filename' => clean_param(basename($file), PARAM_FILE), + 'contextid' => \context_user::instance($USER->id)->id, + 'userid' => $USER->id, + ]; + cli_writeln($file); + $storedfiles[] = $fs->create_file_from_pathname($record, $file); + } + cli_writeln(""); + + // Import files. + cli_heading(get_string('cliimporting', 'tool_customlang', $modeparam)); + $importer->import($storedfiles); + // Display logs. + $log = $importer->get_log(); + if (empty($log)) { + cli_problem(get_string('clinolog', 'tool_customlang', $lng)); + } + foreach ($log as $message) { + if ($message->errorlevel == notification::NOTIFY_ERROR) { + cli_problem($message->get_message()); + } else { + cli_writeln($message->get_message()); + } + } + // Do the checkin if necessary. + if ($checkin) { + tool_customlang_utils::checkin($lng); + cli_writeln(get_string('savecheckin', 'tool_customlang')); + } + cli_writeln(""); +} + +exit(0); diff --git a/admin/tool/customlang/lang/en/tool_customlang.php b/admin/tool/customlang/lang/en/tool_customlang.php index 513b2f7877d..29dd1e28b65 100644 --- a/admin/tool/customlang/lang/en/tool_customlang.php +++ b/admin/tool/customlang/lang/en/tool_customlang.php @@ -37,6 +37,13 @@ $string['cliexportfilenotfoundforcomponent'] = 'File {$a->filepath} not found fo $string['cliexportstartexport'] = 'Exporting language "{$a}"'; $string['cliexportzipdone'] = 'Zip created: {$a}'; $string['cliexportzipfail'] = 'Cannot create zip {$a}'; +$string['clifiles'] = 'Files to import into {$a}'; +$string['cliimporting'] = 'Import files string (mode {$a})'; +$string['clinolog'] = 'Nothing to import into {$a}'; +$string['climissinglang'] = 'Missing language'; +$string['climissingfiles'] = 'Missing valid files'; +$string['climissingmode'] = 'Missing or invalid mode (valid is all, new or update)'; +$string['climissingsource'] = 'Missing file or folder'; $string['confirmcheckin'] = 'You are about to save modifications to your local language pack. This will export the customised strings from the translator into your site data directory and your site will start using the modified strings. Press \'Continue\' to proceed with saving.'; $string['customlang:edit'] = 'Edit local translation'; $string['customlang:export'] = 'Export local translation'; -- 2.43.0