Skip to content

Commit

Permalink
MDL-26914 When installing a lang pack, install all the grandparents too
Browse files Browse the repository at this point in the history
All places where a language pack is installed now use the new language
package installer that takes case of the parental dependencies.
  • Loading branch information
mudrd8mz committed Mar 31, 2011
1 parent 15e16f0 commit 74a4c9a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 159 deletions.
29 changes: 10 additions & 19 deletions admin/cli/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,25 +376,16 @@
}
}

//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));
}
}
}
Expand Down
29 changes: 10 additions & 19 deletions admin/cli/install_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,26 +121,17 @@
}
$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));
}
}
}
Expand Down
141 changes: 50 additions & 91 deletions admin/langimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -33,8 +33,6 @@
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)) {
Expand Down Expand Up @@ -63,41 +61,22 @@
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;
}
}
}
Expand Down Expand Up @@ -136,7 +115,9 @@
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
Expand Down Expand Up @@ -171,8 +152,8 @@
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;
}
Expand All @@ -185,41 +166,38 @@
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;
}
}

Expand All @@ -228,6 +206,8 @@
} else {
$notice_ok[] = get_string('nolangupdateneeded','admin');
}

unset($installer);
}
get_string_manager()->reset_caches();

Expand All @@ -239,15 +219,17 @@
$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])) {
$missingparents[$installedlang] = $parent;
}
}

if ($availablelangs = get_remote_list_of_languages()) {
$installer = new lang_installer();

if ($availablelangs = $installer->get_remote_list_of_languages()) {
$remote = true;
} else {
$remote = false;
Expand Down Expand Up @@ -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;
}
}
27 changes: 9 additions & 18 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,24 +354,15 @@
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);
}
}

Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@
$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';
Expand Down
26 changes: 14 additions & 12 deletions lib/upgradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 74a4c9a

Please sign in to comment.