Commit 6b7d7f79 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1625863: Adding cli for langpacks



This cli does:

1) Install / update langpacks by listing the languages by their code,
   eg de = German, fr = French like so

sudo -u www-data php admin/cli/sync_langpacks.php -l=de,fr

This will make a backup of the existing language (if exists) into the
dataroot langpacks_backup directory.

2) If you don't want to make backups then add the -b=false flag, eg

sudo -u www-data php admin/cli/sync_langpacks.php -l=de,fr -b=false

3) If you need to rollback the backups you can use the -r=true flag, eg

sudo -u www-data php admin/cli/sync_langpacks.php -l=de,fr -r=true

It will ignore trying to update the 'en' language as that is set
within the Mahara system itself

behatnotneeded

Change-Id: I4ec773126cc45a87fc78dec658e97856eec05338
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent dacb2049
<?php
/**
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('CLI', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once(get_config('docroot') . 'auth/lib.php');
require(get_config('libroot') . 'cli.php');
require(get_config('libroot') . 'file.php');
$cli = get_cli();
$options = array();
$options['langpacks'] = (object) array(
'shortoptions' => array('l'),
'description' => get_string('cli_langpack', 'admin'),
'required' => true,
'examplevalue' => 'de',
);
define('CLI_LANGPACKS_BACKUP_DEFAULT', -1);
$options['keepbackups'] = (object) array(
'shortoptions' => array('b'),
'description' => get_string('cli_langpack_backup', 'admin'),
'required' => false,
'defaultvalue' => CLI_LANGPACKS_BACKUP_DEFAULT,
);
define('CLI_LANGPACKS_REVERT_DEFAULT', -1);
$options['revertbackups'] = (object) array(
'shortoptions' => array('r'),
'description' => get_string('cli_langpack_revert', 'admin'),
'required' => false,
'defaultvalue' => CLI_LANGPACKS_REVERT_DEFAULT,
);
$settings = (object) array(
'info' => get_string('cli_langpack_info', 'admin'),
'options' => $options,
);
$cli->setup($settings);
// Get which language packs we are going to update
$langpacks = $cli->get_cli_param('langpacks');
$langpacks = explode(',', $langpacks);
// No need to update the English lang pack as English lang strings are default in Mahara
$key = array_search('en', $langpacks);
if ($key !== false) {
unset($langpacks[$key]);
$cli->cli_print(get_string('cli_langpack_en', 'admin'));
}
if (empty($langpacks)) {
$cli->cli_exit(get_string('cli_langpack_missing', 'admin'));
}
// Get installed languages
$languages = get_languages();
$series = get_config('series');
if (preg_match('/dev$/', get_config('release'))) {
$series = 'master';
$seriesfilesuffix = $series . '.tar.gz';
}
else {
$seriesfilesuffix = $series . '_STABLE.tar.gz';
}
$cli->cli_print(get_string('cli_lang_branch', 'admin', $series));
$tmpdir = get_config('dataroot') . 'temp';
if (!check_dir_exists($tmpdir) || !is_writable($tmpdir)) {
$cli->cli_exit(get_string('cli_tmpdir_notwritable', 'admin', $tmpdir));
}
$langdir = get_config('dataroot') . 'langpacks';
if (!check_dir_exists($langdir) || !is_writable($langdir)) {
$cli->cli_exit(get_string('cli_langdir_notwritable', 'admin', $langdir));
}
$backupdir = $langdir . '_backup';
if (!check_dir_exists($backupdir) || !is_writable($backupdir)) {
$cli->cli_exit(get_string('cli_backupdir_notwritable', 'admin', $backupdir));
}
$langdirownership = posix_getpwuid(fileowner($langdir));
if ($cli->get_cli_param('revertbackups') === CLI_LANGPACKS_REVERT_DEFAULT) {
$rollback = false;
}
else {
$rollback = $cli->get_cli_param_boolean('revertbackups');
}
foreach ($langpacks as $lang) {
$cli->cli_print('================== ' . $lang . ' ==================');
if ($rollback) {
if (is_dir($backupdir . '/' . $lang . '.utf8')) {
// To revert lang from langpacks_backup back to langpacks
if (!copyr($backupdir . '/' . $lang . '.utf8', $langdir . '/' . $lang . '.utf8')) {
$cli->cli_print(get_string('cli_restore_warning', 'admin', $lang));
}
$cli->cli_print(get_string('cli_restore_done', 'admin', $lang));
}
else {
$cli->cli_print(get_string('cli_restore_warning', 'admin', $lang));
continue;
}
}
else {
// Install/update lang from langpacks.mahara.org to langpacks
// First: work out what we should be doing
$dobackup = false;
$langexists = (array_key_exists($lang . '.utf8', $languages)) ? true : false;
$cli->cli_print(get_string('cli_language_status', 'admin', $lang, ($langexists ? 'true' : 'false')));
if ($cli->get_cli_param('keepbackups') === CLI_LANGPACKS_BACKUP_DEFAULT) {
// no backup specified so do if lang already exists
if ($langexists) {
$dobackup = true;
}
}
else {
$dobackup = (!$langexists) ? false : $cli->get_cli_param_boolean('keepbackups');
}
$cli->cli_print(get_string('cli_language_make_backup', 'admin', ($dobackup ? 'true' : 'false')));
// fetch the lang packs we need and save them to tmp
$langpackurl = 'http://langpacks.mahara.org/';
$filename = $lang . '-' . $seriesfilesuffix;
$langurl = $langpackurl . $filename;
$cli->cli_print(get_string('cli_langpack_url', 'admin', $langurl));
$checklang = mahara_http_request(
array(
CURLOPT_URL => $langurl,
CURLOPT_HEADER => false,
),
true
);
if ($checklang->info['http_code'] != '200') {
$cli->cli_print(get_string('cli_langpack_url_failed', 'admin', $lang, $checklang->info['http_code']));
continue;
}
$file = $checklang->data;
file_put_contents($tmpdir . '/' . $filename, $file);
$cli->cli_print(get_string('cli_langpack_upload', 'admin', $filename));
// if we need to make a backup - do it now
if ($dobackup) {
if (!copyr($langdir . '/' . $lang . '.utf8', $backupdir . '/' . $lang . '.utf8')) {
$cli->cli_print(get_string('cli_langpack_backup_failed', 'admin', $lang));
}
$cli->cli_print(get_string('cli_langpack_backup_done', 'admin', $lang));
}
// extract contents of langpack into langpacks dir
$filenametmp = substr($filename, 0, stripos($filename, '.gz'));
// Try to decompress the langpack file
try {
// Need to remove old decompressed file
if (file_exists($tmpdir . '/' . $filenametmp)) {
unlink($tmpdir . '/' . $filenametmp);
}
$phargz = new PharData($tmpdir . '/' . $filename);
$phargz->decompress();
}
catch (Exception $e) {
$cli->cli_print(get_string('cli_langpack_extract_failed', 'admin', $filename));
}
// Extract to langpacks dir
try {
$phar = new PharData($tmpdir . '/' . $filenametmp);
$phar->extractTo($langdir, null, true); // extract all files, and overwrite
$cli->cli_print(get_string('cli_langpack_extract_done', 'admin', $lang));
}
catch (Exception $e) {
// handle errors
$cli->cli_print(get_string('cli_langpack_extract_failed', 'admin', $filenametmp, $e->getMessage()));
}
}
$cli->cli_print('------------------------------------------');
}
$cli->cli_exit(get_string('done'));
\ No newline at end of file
......@@ -1266,3 +1266,40 @@ $string['cli_pwreset_typo'] = 'Sorry, passwords do not match.';
$string['cli_close_site_info'] = 'This command-line PHP script allows you to close your site to non-admin users, and open it again. (This is the same as pressing the "Close site" button on the Administration homepage.)';
$string['cli_close_site_siteclosed'] = 'Site closed for maintenance.';
$string['cli_close_site_siteopen'] = 'Site open.';
// Languange update script
$string['cli_backupdir_notwritable'] = 'Language backup directory "%s" is not writable.';
$string['cli_langpack'] = 'List by language code eg "de,pt_BR,fr"';
$string['cli_langpack_backup'] = 'Backup the language directory before updating (optional).';
$string['cli_langpack_backup_done'] = 'Backup of language "%s" done';
$string['cli_langpack_backup_failed'] = '*** WARNING *** Unable to make backup of "%s"';
$string['cli_langpack_en'] = 'No need to update english langpack as the strings are defined in Mahara itself. You can set special lang strings in local/lang/';
$string['cli_langpack_extract_done'] = 'Copied new "%s" langpack into place';
$string['cli_langpack_extract_failed'] = '*** WARNING *** Unable to extract files from "%s"
- reason: %s';
$string['cli_langpack_info'] = 'To update your language packs via the command line.
You can:
1) Install / update langpacks by listing the languages by their code, eg de = German, fr = French like so
sudo -u www-data php admin/cli/sync_langpacks.php -l=de,fr
This will make a backup of the existing language (if exists) into the dataroot langpacks_backup directory.
2) If you don\'t want to make backups then add the -b=false flag, eg
sudo -u www-data php admin/cli/sync_langpacks.php -l=de,fr -b=false
3) If you need to rollback the backups you can use the -r=true flag, eg
sudo -u www-data php admin/cli/sync_langpacks.php -l=de,fr -r=true
It will ignore trying to update the \'en\' language as that is set within the Mahara system itself';
$string['cli_langpack_missing'] = 'No langpacks specified';
$string['cli_langpack_revert'] = 'Revert to last backed up version of language.';
$string['cli_langdir_notwritable'] = 'Language directory "%s" is not writable.';
$string['cli_language_make_backup'] = 'Make a backup? %s';
$string['cli_language_status'] = 'Language "%s" already installed: %s';
$string['cli_langpack_url'] = 'Language pack URL "%s"';
$string['cli_langpack_url_failed'] = '*** WARNING *** Language pack for "%s" does not exist/can not be downloaded. Return code: %s';
$string['cli_langpack_upload'] = 'Saving language pack "%s" to temp directory';
$string['cli_restore_done'] = 'Restored backup of "%s" done';
$string['cli_restore_warning'] = '*** WARNING *** Unable to restore backup of "%s"';
$string['cli_tmpdir_notwritable'] = 'Temporary upload directory "%s" is not writable.';
$string['cli_lang_branch'] = 'Mahara series version to fetch langpacks for series "%s"';
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment