MDL-46210 langimport: Rearrange code

Factoring code into a controller class so that the update
can be put into a scheduled task (and be a bit tider).
This commit is contained in:
Dan Poltawski 2014-09-11 21:28:14 +01:00
parent 516ea6cf8b
commit 816b3e0176
4 changed files with 248 additions and 170 deletions

View File

@ -0,0 +1,229 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Lang import controller
*
* @package tool_langimport
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_langimport;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/componentlib.class.php');
/**
* Lang import controller
*
* @package tool_langimport
* @copyright 2014 Dan Poltawski <dan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class controller {
/** @var array list of informational messages */
public $info;
/** @var array list of error messages */
public $errors;
/** @var \lang_installer */
private $installer;
/** @var array languages available on the remote server */
public $availablelangs;
/**
* Constructor.
*/
public function __construct() {
make_temp_directory('');
make_upload_directory('lang');
$this->info = array();
$this->errors = array();
$this->installer = new \lang_installer();
$this->availablelangs = $this->installer->get_remote_list_of_languages();
}
/**
* Install language packs provided
*
* @param string|array $langs array of langcodes or individual langcodes
* @return int false if an error encountered or
* @throws \moodle_exception when error is encountered installing langpack
*/
public function install_languagepacks($langs) {
global $CFG;
$this->installer->set_queue($langs);
$results = $this->installer->run();
$updatedpacks = 0;
foreach ($results as $langcode => $langstatus) {
switch ($langstatus) {
case \lang_installer::RESULT_DOWNLOADERROR:
$a = new \stdClass();
$a->url = $this->installer->lang_pack_url($langcode);
$a->dest = $CFG->dataroot.'/lang';
$this->errors[] = get_string('remotedownloaderror', 'error', $a);
throw new \moodle_exception('remotedownloaderror', 'error', $a);
break;
case \lang_installer::RESULT_INSTALLED:
$this->info[] = get_string('langpackinstalled', 'tool_langimport', $langcode);
$updatedpacks++;
break;
case \lang_installer::RESULT_UPTODATE:
$this->info[] = get_string('langpackuptodate', 'tool_langimport', $langcode);
break;
}
}
return $updatedpacks;
}
/**
* Uninstall language pack
*
* @param string $lang language code
* @return bool true if language succesfull installed
*/
public function uninstall_language($lang) {
global $CFG;
$dest1 = $CFG->dataroot.'/lang/'.$lang;
$dest2 = $CFG->dirroot.'/lang/'.$lang;
$rm1 = false;
$rm2 = false;
if (file_exists($dest1)) {
$rm1 = remove_dir($dest1);
}
if (file_exists($dest2)) {
$rm2 = remove_dir($dest2);
}
if ($rm1 or $rm2) {
$this->info[] = get_string('langpackremoved', 'tool_langimport', $lang);
return true;
} else { // Nothing deleted, possibly due to permission error.
$this->errors[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
return false;
}
}
/**
* Updated all install language packs with the latest found on servre
*
* @return bool true if languages succesfully updated.
*/
public function update_all_installed_languages() {
global $CFG;
if (!$availablelangs = $this->installer->get_remote_list_of_languages()) {
$this->errors[] = get_string('cannotdownloadlanguageupdatelist', 'error');
return false;
}
$md5array = array(); // Convert to (string)langcode => (string)md5.
foreach ($availablelangs as $alang) {
$md5array[$alang[0]] = $alang[1];
}
// Filter out unofficial packs.
$currentlangs = array_keys(get_string_manager()->get_list_of_translations(true));
$updateablelangs = array();
foreach ($currentlangs as $clang) {
if (!array_key_exists($clang, $md5array)) {
$noticeok[] = get_string('langpackupdateskipped', 'tool_langimport', $clang);
continue;
}
$dest1 = $CFG->dataroot.'/lang/'.$clang;
$dest2 = $CFG->dirroot.'/lang/'.$clang;
if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')) {
$updateablelangs[] = $clang;
}
}
// Filter out packs that have the same md5 key.
$neededlangs = array();
foreach ($updateablelangs as $ulang) {
if (!$this->is_installed_lang($ulang, $md5array[$ulang])) {
$neededlangs[] = $ulang;
}
}
// Clean-up currently installed versions of the packs.
foreach ($neededlangs as $packindex => $pack) {
if ($pack == 'en') {
continue;
}
// Delete old directories.
$dest1 = $CFG->dataroot.'/lang/'.$pack;
$dest2 = $CFG->dirroot.'/lang/'.$pack;
if (file_exists($dest1)) {
if (!remove_dir($dest1)) {
$noticeerror[] = 'Could not delete old directory '.$dest1.', update of '.$pack
.' failed, please check permissions.';
unset($neededlangs[$packindex]);
continue;
}
}
if (file_exists($dest2)) {
if (!remove_dir($dest2)) {
$noticeerror[] = 'Could not delete old directory '.$dest2.', update of '.$pack
.' failed, please check permissions.';
unset($neededlangs[$packindex]);
continue;
}
}
}
try {
$updated = $this->install_languagepacks($neededlangs);
} catch (\moodle_exception $e) {
return false;
}
if ($updated) {
$this->info[] = get_string('langupdatecomplete', 'tool_langimport');
} else {
$this->info[] = get_string('nolangupdateneeded', 'tool_langimport');
}
return true;
}
/**
* checks the md5 of the zip file, grabbed from download.moodle.org,
* against the md5 of the local language file from last update
* @param string $lang language code
* @param string $md5check md5 to check
* @return bool true if installed
*/
public function is_installed_lang($lang, $md5check) {
global $CFG;
$md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
if (file_exists($md5file)) {
return (file_get_contents($md5file) == $md5check);
}
return false;
}
}

View File

@ -28,10 +28,8 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php');
require(__DIR__.'/../../../config.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/componentlib.class.php');
admin_externalpage_setup('toollangimport');
@ -65,37 +63,17 @@ define('UPDATE_ALL_LANG', 5);
get_string_manager()->reset_caches();
$notice_ok = array();
$notice_error = array();
$controller = new tool_langimport\controller();
if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($pack)) {
core_php_time_limit::raise();
make_temp_directory('');
make_upload_directory('lang');
$installer = new lang_installer($pack);
$results = $installer->run();
foreach ($results as $langcode => $langstatus) {
switch ($langstatus) {
case lang_installer::RESULT_DOWNLOADERROR:
$a = new stdClass();
$a->url = $installer->lang_pack_url($langcode);
$a->dest = $CFG->dataroot.'/lang';
print_error('remotedownloaderror', 'error', 'index.php', $a);
break;
case lang_installer::RESULT_INSTALLED:
$notice_ok[] = get_string('langpackinstalled', 'tool_langimport', $langcode);
break;
case lang_installer::RESULT_UPTODATE:
$notice_ok[] = get_string('langpackuptodate', 'tool_langimport', $langcode);
break;
}
}
$controller->install_languagepacks($pack);
}
if ($mode == DELETION_OF_SELECTED_LANG and !empty($uninstalllang)) {
if ($uninstalllang == 'en') {
$notice_error[] = 'English language pack can not be uninstalled';
// TODO.
$controller->errors[] = 'English language pack can not be uninstalled';
} else if (!$confirm and confirm_sesskey()) {
echo $OUTPUT->header();
@ -106,120 +84,13 @@ if ($mode == DELETION_OF_SELECTED_LANG and !empty($uninstalllang)) {
die;
} else if (confirm_sesskey()) {
$dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
$dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
$rm1 = false;
$rm2 = false;
if (file_exists($dest1)){
$rm1 = remove_dir($dest1);
}
if (file_exists($dest2)){
$rm2 = remove_dir($dest2);
}
if ($rm1 or $rm2) {
$notice_ok[] = get_string('langpackremoved', 'tool_langimport');
} else { //nothing deleted, possibly due to permission error
$notice_error[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
}
$controller->uninstall_language($uninstalllang);
}
}
if ($mode == UPDATE_ALL_LANG) {
core_php_time_limit::raise();
$installer = new lang_installer();
if (!$availablelangs = $installer->get_remote_list_of_languages()) {
print_error('cannotdownloadlanguageupdatelist', 'error');
}
$md5array = array(); // (string)langcode => (string)md5
foreach ($availablelangs as $alang) {
$md5array[$alang[0]] = $alang[1];
}
// filter out unofficial packs
$currentlangs = array_keys(get_string_manager()->get_list_of_translations(true));
$updateablelangs = array();
foreach ($currentlangs as $clang) {
if (!array_key_exists($clang, $md5array)) {
$notice_ok[] = get_string('langpackupdateskipped', 'tool_langimport', $clang);
continue;
}
$dest1 = $CFG->dataroot.'/lang/'.$clang;
$dest2 = $CFG->dirroot.'/lang/'.$clang;
if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
$updateablelangs[] = $clang;
}
}
// then filter out packs that have the same md5 key
$neededlangs = array(); // all the packs that needs updating
foreach ($updateablelangs as $ulang) {
if (!is_installed_lang($ulang, $md5array[$ulang])) {
$neededlangs[] = $ulang;
}
}
make_temp_directory('');
make_upload_directory('lang');
// clean-up currently installed versions of the packs
foreach ($neededlangs as $packindex => $pack) {
if ($pack == 'en') {
continue;
}
// delete old directories
$dest1 = $CFG->dataroot.'/lang/'.$pack;
$dest2 = $CFG->dirroot.'/lang/'.$pack;
$rm1 = false;
$rm2 = false;
if (file_exists($dest1)) {
if (!remove_dir($dest1)) {
$notice_error[] = 'Could not delete old directory '.$dest1.', update of '.$pack.' failed, please check permissions.';
unset($neededlangs[$packindex]);
continue;
}
}
if (file_exists($dest2)) {
if (!remove_dir($dest2)) {
$notice_error[] = 'Could not delete old directory '.$dest2.', update of '.$pack.' failed, please check permissions.';
unset($neededlangs[$packindex]);
continue;
}
}
}
// install all needed language packs
$installer->set_queue($neededlangs);
$results = $installer->run();
$updated = false; // any packs updated?
foreach ($results as $langcode => $langstatus) {
switch ($langstatus) {
case lang_installer::RESULT_DOWNLOADERROR:
$a = new stdClass();
$a->url = $installer->lang_pack_url($langcode);
$a->dest = $CFG->dataroot.'/lang';
print_error('remotedownloaderror', 'error', 'index.php', $a);
break;
case lang_installer::RESULT_INSTALLED:
$updated = true;
$notice_ok[] = get_string('langpackinstalled', 'tool_langimport', $langcode);
break;
case lang_installer::RESULT_UPTODATE:
$notice_ok[] = get_string('langpackuptodate', 'tool_langimport', $langcode);
break;
}
}
if ($updated) {
$notice_ok[] = get_string('langupdatecomplete', 'tool_langimport');
} else {
$notice_ok[] = get_string('nolangupdateneeded', 'tool_langimport');
}
unset($installer);
$controller->update_all_installed_languages();
}
get_string_manager()->reset_caches();
@ -239,9 +110,7 @@ foreach ($installedlangs as $installedlang => $unused) {
}
}
$installer = new lang_installer();
if ($availablelangs = $installer->get_remote_list_of_languages()) {
if ($availablelangs = $controller->availablelangs) {
$remote = true;
} else {
$remote = false;
@ -251,18 +120,18 @@ if ($availablelangs = $installer->get_remote_list_of_languages()) {
echo $OUTPUT->box_end();
}
if ($notice_ok) {
$info = implode('<br />', $notice_ok);
if ($controller->info) {
$info = implode('<br />', $controller->info);
echo $OUTPUT->notification($info, 'notifysuccess');
}
if ($notice_error) {
$info = implode('<br />', $notice_error);
if ($controller->errors) {
$info = implode('<br />', $controller->errors);
echo $OUTPUT->notification($info, 'notifyproblem');
}
if ($missingparents) {
foreach ($missingparents as $l=>$parent) {
foreach ($missingparents as $l => $parent) {
$a = new stdClass();
$a->lang = $installedlangs[$l];
$a->parent = $parent;
@ -306,7 +175,7 @@ echo html_writer::end_tag('td');
// list of available languages
$options = array();
foreach ($availablelangs as $alang) {
if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !is_installed_lang($alang[0], $alang[1])) {
if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !$controller->is_installed_lang($alang[0], $alang[1])) {
$options[$alang[0]] = $alang[2].' ('.$alang[0].')';
}
}
@ -331,23 +200,3 @@ echo html_writer::end_tag('table');
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
die();
////////////////////////////////////////////////////////////////////////////////
// Local functions /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* checks the md5 of the zip file, grabbed from download.moodle.org,
* against the md5 of the local language file from last update
* @param string $lang
* @param string $md5check
* @return bool
*/
function is_installed_lang($lang, $md5check) {
global $CFG;
$md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
if (file_exists($md5file)){
return (file_get_contents($md5file) == $md5check);
}
return false;
}

View File

@ -27,10 +27,10 @@ $string['install'] = 'Install selected language pack(s)';
$string['installedlangs'] = 'Installed language packs';
$string['langimport'] = 'Language import utility';
$string['langimportdisabled'] = 'Language import feature has been disabled. You have to update your language packs manually at the file-system level. Do not forget to purge string caches after you do so.';
$string['langpackinstalled'] = 'Language pack {$a} was successfully installed';
$string['langpackremoved'] = 'Language pack was uninstalled';
$string['langpackupdateskipped'] = 'Update of {$a} language pack skipped';
$string['langpackuptodate'] = 'Language pack {$a} is up-to-date';
$string['langpackinstalled'] = 'Language pack \'{$a}\' was successfully installed';
$string['langpackremoved'] = 'Language pack \'{$a}\' was uninstalled';
$string['langpackupdateskipped'] = 'Update of \'{$a}\' language pack skipped';
$string['langpackuptodate'] = 'Language pack \'{$a}\' is up-to-date';
$string['langupdatecomplete'] = 'Language pack update completed';
$string['missingcfglangotherroot'] = 'Missing configuration value $CFG->langotherroot';
$string['missinglangparent'] = 'Missing parent language <em>{$a->parent}</em> of <em>{$a->lang}</em>.';

View File

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014051200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2014051202; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014050800; // Requires this Moodle version
$plugin->component = 'tool_langimport'; // Full name of the plugin (used for diagnostics)