From 74a4c9a9ee7fb1ae67d27c0a8aed00a653c47967 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Tue, 29 Mar 2011 21:09:59 +0200 Subject: [PATCH] MDL-26914 When installing a lang pack, install all the grandparents too All places where a language pack is installed now use the new language package installer that takes case of the parental dependencies. --- admin/cli/install.php | 29 +++---- admin/cli/install_database.php | 29 +++---- admin/langimport.php | 141 ++++++++++++--------------------- install.php | 27 +++---- lang/en/admin.php | 1 + lib/upgradelib.php | 26 +++--- 6 files changed, 94 insertions(+), 159 deletions(-) diff --git a/admin/cli/install.php b/admin/cli/install.php index fc643b53265..1c37f549057 100644 --- a/admin/cli/install.php +++ b/admin/cli/install.php @@ -376,25 +376,16 @@ if ($interactive) { } } -//download lang pack with optional notification -if ($CFG->lang != 'en') { - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) { - if ($cd->install() == COMPONENT_ERROR) { - if ($cd->get_error() == 'remotedownloaderror') { - $a = new stdClass(); - $a->url = 'http://download.moodle.org/langpack/2.0/'.$CFG->lang.'.zip'; - $a->dest = $CFG->dataroot.'/lang'; - cli_problem(get_string($cd->get_error(), 'error', $a)); - } else { - cli_problem(get_string($cd->get_error(), 'error')); - } - } else { - // install parent lang if defined - if ($parentlang = get_parent_language()) { - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) { - $cd->install(); - } - } +// download required lang packs +if ($CFG->lang !== 'en') { + $installer = new lang_installer($CFG->lang); + $results = $installer->run(); + foreach ($results as $langcode => $langstatus) { + if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) { + $a = new stdClass(); + $a->url = $installer->lang_pack_url($langcode); + $a->dest = $CFG->dataroot.'/lang'; + cli_problem(get_string('remotedownloaderror', 'error', $a)); } } } diff --git a/admin/cli/install_database.php b/admin/cli/install_database.php index 17f6d59309d..ac58928f6a6 100644 --- a/admin/cli/install_database.php +++ b/admin/cli/install_database.php @@ -121,26 +121,17 @@ if (!file_exists($CFG->dirroot.'/install/lang/'.$options['lang'])) { } $CFG->lang = $options['lang']; -//download lang pack with optional notification -if ($CFG->lang != 'en') { +// download required lang packs +if ($CFG->lang !== 'en') { make_upload_directory('lang'); - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) { - if ($cd->install() == COMPONENT_ERROR) { - if ($cd->get_error() == 'remotedownloaderror') { - $a = new stdClass(); - $a->url = 'http://download.moodle.org/langpack/2.0/'.$CFG->lang.'.zip'; - $a->dest = $CFG->dataroot.'/lang'; - cli_problem(get_string($cd->get_error(), 'error', $a)); - } else { - cli_problem(get_string($cd->get_error(), 'error')); - } - } else { - // install parent lang if defined - if ($parentlang = get_parent_language()) { - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) { - $cd->install(); - } - } + $installer = new lang_installer($CFG->lang); + $results = $installer->run(); + foreach ($results as $langcode => $langstatus) { + if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) { + $a = new stdClass(); + $a->url = $installer->lang_pack_url($langcode); + $a->dest = $CFG->dataroot.'/lang'; + cli_problem(get_string('remotedownloaderror', 'error', $a)); } } } diff --git a/admin/langimport.php b/admin/langimport.php index 3ebb9deef43..4aa614b2193 100644 --- a/admin/langimport.php +++ b/admin/langimport.php @@ -18,7 +18,7 @@ /** * Fetches language packages from download.moodle.org server * - * Language packages are available at http://download.moodle.org/langpack/2.0/ + * Language packages are available at http://download.moodle.org/langpack/ * in ZIP format together with a file languages.md5 containing their hashes * and meta info. * Locally, language packs are saved into $CFG->dataroot/lang/ @@ -33,8 +33,6 @@ require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/filelib.php'); require_once($CFG->libdir.'/componentlib.class.php'); -$thisversion = '2.0'; // TODO this information should be taken from version.php or similar source - admin_externalpage_setup('langimport'); if (!empty($CFG->skiplangupgrade)) { @@ -63,41 +61,22 @@ if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($p make_upload_directory('temp'); make_upload_directory('lang'); - if (is_array($pack)) { - $packs = $pack; - } else { - $packs = array($pack); - } - - foreach ($packs as $pack) { - if ($cd = new component_installer('http://download.moodle.org', 'langpack/'.$thisversion, $pack.'.zip', 'languages.md5', 'lang')) { - $status = $cd->install(); - switch ($status) { - case COMPONENT_ERROR: - if ($cd->get_error() == 'remotedownloaderror') { - $a = new stdClass(); - $a->url = 'http://download.moodle.org/langpack/'.$thisversion.'/'.$pack.'.zip'; - $a->dest = $CFG->dataroot.'/lang'; - print_error($cd->get_error(), 'error', 'langimport.php', $a); - } else { - print_error($cd->get_error(), 'error', 'langimport.php'); - } - break; - case COMPONENT_INSTALLED: - $notice_ok[] = get_string('langpackinstalled','admin',$pack); - if ($parentlang = get_parent_language($pack)) { - // install also parent pack if specified - if ($cd = new component_installer('http://download.moodle.org', 'langpack/'.$thisversion, - $parentlang.'.zip', 'languages.md5', 'lang')) { - $cd->install(); - } - } - break; - case COMPONENT_UPTODATE: - break; - } - } else { - echo $OUTPUT->notification('Had an unspecified error with the component installer, sorry.'); + $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', 'langimport.php', $a); + break; + case lang_installer::RESULT_INSTALLED: + $notice_ok[] = get_string('langpackinstalled', 'admin', $langcode); + break; + case lang_installer::RESULT_UPTODATE: + $notice_ok[] = get_string('langpackuptodate', 'admin', $langcode); + break; } } } @@ -136,7 +115,9 @@ if ($mode == DELETION_OF_SELECTED_LANG and !empty($uninstalllang)) { if ($mode == UPDATE_ALL_LANG) { set_time_limit(0); - if (!$availablelangs = get_remote_list_of_languages()) { + $installer = new lang_installer(); + + if (!$availablelangs = $installer->get_remote_list_of_languages()) { print_error('cannotdownloadlanguageupdatelist', 'error'); } $md5array = array(); // (string)langcode => (string)md5 @@ -171,8 +152,8 @@ if ($mode == UPDATE_ALL_LANG) { make_upload_directory('temp'); make_upload_directory('lang'); - $updated = false; // any packs updated? - foreach ($neededlangs as $pack) { + // clean-up currently installed versions of the packs + foreach ($neededlangs as $packindex => $pack) { if ($pack == 'en') { continue; } @@ -185,41 +166,38 @@ if ($mode == UPDATE_ALL_LANG) { 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; } } + } - // copy and unzip new version - if ($cd = new component_installer('http://download.moodle.org', 'langpack/'.$thisversion, $pack.'.zip', 'languages.md5', 'lang')) { - $status = $cd->install(); - switch ($status) { - - case COMPONENT_ERROR: - if ($cd->get_error() == 'remotedownloaderror') { - $a = new stdClass(); - $a->url = 'http://download.moodle.org/langpack/'.$thisversion.'/'.$pack.'.zip'; - $a->dest = $CFG->dataroot.'/lang'; - print_error($cd->get_error(), 'error', 'langimport.php', $a); - } else { - print_error($cd->get_error(), 'error', 'langimport.php'); - } - break; - - case COMPONENT_UPTODATE: - // should not get here - break; - - case COMPONENT_INSTALLED: - $notice_ok[] = get_string('langpackupdated', 'admin', $pack); - $updated = true; - break; - } + // 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', 'langimport.php', $a); + break; + case lang_installer::RESULT_INSTALLED: + $updated = true; + $notice_ok[] = get_string('langpackinstalled', 'admin', $langcode); + break; + case lang_installer::RESULT_UPTODATE: + $notice_ok[] = get_string('langpackuptodate', 'admin', $langcode); + break; } } @@ -228,6 +206,8 @@ if ($mode == UPDATE_ALL_LANG) { } else { $notice_ok[] = get_string('nolangupdateneeded','admin'); } + + unset($installer); } get_string_manager()->reset_caches(); @@ -239,7 +219,7 @@ $installedlangs = get_string_manager()->get_list_of_translations(true); $missingparents = array(); foreach ($installedlangs as $installedlang => $unused) { $parent = get_parent_language($installedlang); - if (empty($parent) or ($parent === 'en')) { + if (empty($parent)) { continue; } if (!isset($installedlangs[$parent])) { @@ -247,7 +227,9 @@ foreach ($installedlangs as $installedlang => $unused) { } } -if ($availablelangs = get_remote_list_of_languages()) { +$installer = new lang_installer(); + +if ($availablelangs = $installer->get_remote_list_of_languages()) { $remote = true; } else { $remote = false; @@ -357,26 +339,3 @@ function is_installed_lang($lang, $md5check) { } return false; } - -/** - * Returns the list of available language packs from download.moodle.org - * - * @return array|bool false if can not download - */ -function get_remote_list_of_languages() { - $source = 'http://download.moodle.org/langpack/2.0/languages.md5'; - $availablelangs = array(); - - if ($content = download_file_content($source)) { - $alllines = explode("\n", $content); - foreach($alllines as $line) { - if (!empty($line)){ - $availablelangs[] = explode(',', $line); - } - } - return $availablelangs; - - } else { - return false; - } -} diff --git a/install.php b/install.php index 4a4896ad0f1..b615587b7d4 100644 --- a/install.php +++ b/install.php @@ -354,24 +354,15 @@ if ($config->stage == INSTALL_DATABASETYPE) { if ($config->stage == INSTALL_DOWNLOADLANG) { $downloaderror = ''; -// Download and install lang component, lang dir was already created in install_init_dataroot - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) { - if ($cd->install() == COMPONENT_ERROR) { - if ($cd->get_error() == 'remotedownloaderror') { - $a = new stdClass(); - $a->url = 'http://download.moodle.org/langpack/2.0/'.$config->lang.'.zip'; - $a->dest = $CFG->dataroot.'/lang'; - $downloaderror = get_string($cd->get_error(), 'error', $a); - } else { - $downloaderror = get_string($cd->get_error(), 'error'); - } - } else { - // install parent lang if defined - if ($parentlang = get_parent_language()) { - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) { - $cd->install(); - } - } + // download and install required lang packs, the lang dir has already been created in install_init_dataroot + $installer = new lang_installer($CFG->lang); + $results = $installer->run(); + foreach ($results as $langcode => $langstatus) { + if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) { + $a = new stdClass(); + $a->url = $installer->lang_pack_url($langcode); + $a->dest = $CFG->dataroot.'/lang'; + $downloaderror = get_string('remotedownloaderror', 'error', $a); } } diff --git a/lang/en/admin.php b/lang/en/admin.php index 7c67ea9a5e6..874535ba6e4 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -648,6 +648,7 @@ $string['langpackremoved'] = 'Language pack was uninstalled'; $string['langpacks'] = 'Language packs'; $string['langpackupdated'] = 'Language pack {$a} was successfully updated'; $string['langpackupdateskipped'] = 'Update of {$a} language pack skipped'; +$string['langpackuptodate'] = 'Language pack {$a} is up-to-date'; $string['langpackwillbeupdated'] = 'NOTE: Moodle will try to download updates for your language packs during the upgrade.'; $string['langrmyourself'] = 'To prevent data loss, lang.php is not able to overwrite existing file with empty content. Please, remove the file manually in order to get rid of it.'; $string['langstringcache'] = 'Cache all language strings'; diff --git a/lib/upgradelib.php b/lib/upgradelib.php index e0ba25c3028..a526068897c 100644 --- a/lib/upgradelib.php +++ b/lib/upgradelib.php @@ -1259,11 +1259,10 @@ function upgrade_init_javascript() { $PAGE->requires->js_init_code($js); } - /** * Try to upgrade the given language pack (or current language) * - * @todo hardcoded Moodle version here - shall be provided by version.php or similar script + * @param string $lang the code of the language to update, defaults to the current language */ function upgrade_language_pack($lang='') { global $CFG, $OUTPUT; @@ -1286,16 +1285,19 @@ function upgrade_language_pack($lang='') { require_once($CFG->libdir.'/componentlib.class.php'); - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $lang.'.zip', 'languages.md5', 'lang')) { - $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) - - if ($status == COMPONENT_INSTALLED) { - if ($parentlang = get_parent_language($lang)) { - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) { - $cd->install(); - } - } - echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); + $installer = new lang_installer($pack); + $results = $installer->run(); + foreach ($results as $langcode => $langstatus) { + switch ($langstatus) { + case lang_installer::RESULT_DOWNLOADERROR: + echo $OUTPUT->notification($pack . '.zip'); + break; + case lang_installer::RESULT_INSTALLED: + echo $OUTPUT->notification(get_string('langpackinstalled', 'admin', $langcode), 'notifysuccess'); + break; + case lang_installer::RESULT_UPTODATE: + echo $OUTPUT->notification(get_string('langpackuptodate', 'admin', $langcode), 'notifysuccess'); + break; } }