/** * Downloads a langpack and fakes it being outdated * * @param string $langcode The language code (e.g. en) * @Given /^outdated langpack \'([^\']*)\' is installed$/ */ public function outdated_langpack_is_installed($langcode) { global $CFG; require_once $CFG->libdir . '/componentlib.class.php'; // Download the langpack. $dir = make_upload_directory('lang'); $installer = new lang_installer($langcode); $result = $installer->run(); if ($result[$langcode] !== lang_installer::RESULT_INSTALLED) { throw new coding_exception("Failed to install langpack '{$langcode}'"); } $path = "{$dir}/{$langcode}/{$langcode}.md5"; if (!file_exists($path)) { throw new coding_exception("Failed to find '{$langcode}' checksum"); } file_put_contents($path, '000000'); }
/** * Called during upgrade, we need up-to-date lang pack * because it may be used during upgrade... * * @param string $lang * @return void */ function tool_langimport_preupgrade_update($lang) { global $CFG, $OUTPUT; require_once $CFG->libdir . '/componentlib.class.php'; echo $OUTPUT->heading(get_string('langimport', 'tool_langimport') . ': ' . $lang); @mkdir($CFG->tempdir . '/'); //make it in case it's a fresh install, it might not be there @mkdir($CFG->dataroot . '/lang/'); $installer = new lang_installer($lang); $results = $installer->run(); foreach ($results as $langcode => $langstatus) { switch ($langstatus) { case lang_installer::RESULT_DOWNLOADERROR: echo $OUTPUT->notification($langcode . '.zip'); break; case lang_installer::RESULT_INSTALLED: echo $OUTPUT->notification(get_string('langpackinstalled', 'tool_langimport', $langcode), 'notifysuccess'); break; case lang_installer::RESULT_UPTODATE: echo $OUTPUT->notification(get_string('langpackuptodate', 'tool_langimport', $langcode), 'notifysuccess'); break; } } }
/** * 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)) { $this->info[] = 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; } } try { $updated = $this->install_languagepacks($neededlangs, true); } catch (\moodle_exception $e) { $this->errors[] = 'An exception occurred while installing language packs: ' . $e->getMessage(); return false; } if ($updated) { $this->info[] = get_string('langupdatecomplete', 'tool_langimport'); // The strings have been changed so we need to purge their cache to ensure users see the changes. get_string_manager()->reset_caches(); } else { $this->info[] = get_string('nolangupdateneeded', 'tool_langimport'); } return true; }
if ($config->stage == INSTALL_DATABASETYPE) { // skip db selection if distro package supports only one db if (!empty($distro->dbtype)) { $config->stage = INSTALL_DATABASE; } } if ($config->stage == INSTALL_DOWNLOADLANG) { $downloaderror = ''; // 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); } } if ($downloaderror !== '') { install_print_header($config, get_string('language'), get_string('langdownloaderror', 'install', $CFG->lang), $downloaderror); install_print_footer($config); die; } else {
private function update_all_languages($lang) { // TODO: Use above $lang to update a single language pack. global $CFG; require_once $CFG->libdir . '/filelib.php'; require_once $CFG->libdir . '/componentlib.class.php'; \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 (!$this->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); get_string_manager()->reset_caches(); }
} get_string_manager()->reset_caches(); echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('langimport', 'tool_langimport')); $installedlangs = get_string_manager()->get_list_of_translations(true); $missingparents = array(); foreach ($installedlangs as $installedlang => $unused) { $parent = get_parent_language($installedlang); if (empty($parent)) { continue; } if (!isset($installedlangs[$parent])) { $missingparents[$installedlang] = $parent; } } $installer = new lang_installer(); if ($availablelangs = $installer->get_remote_list_of_languages()) { $remote = true; } else { $remote = false; $availablelangs = array(); echo $OUTPUT->box_start(); print_string('remotelangnotavailable', 'tool_langimport', $CFG->dataroot . '/lang/'); echo $OUTPUT->box_end(); } if ($notice_ok) { $info = implode('<br />', $notice_ok); echo $OUTPUT->notification($info, 'notifysuccess'); } if ($notice_error) { $info = implode('<br />', $notice_error);
/** * Returns the URL where a given language pack can be downloaded * * Alternatively, if the parameter is empty, returns URL of the page with the * list of all available language packs. * * @param string $langcode language code like 'cs' or empty for unknown * @return string URL */ public function lang_pack_url($langcode = '') { return $this->installer->lang_pack_url($langcode); }
/** * Try to upgrade the given language pack (or current language) * * @param string $lang the code of the language to update, defaults to the current language */ function upgrade_language_pack($lang = '') { global $CFG, $OUTPUT; get_string_manager()->reset_caches(); if (empty($lang)) { $lang = current_language(); } if ($lang == 'en') { return true; // Nothing to do } upgrade_started(false); echo $OUTPUT->heading(get_string('langimport', 'admin') . ': ' . $lang); @mkdir($CFG->dataroot . '/temp/'); //make it in case it's a fresh install, it might not be there @mkdir($CFG->dataroot . '/lang/'); require_once $CFG->libdir . '/componentlib.class.php'; $installer = new lang_installer($lang); $results = $installer->run(); foreach ($results as $langcode => $langstatus) { switch ($langstatus) { case lang_installer::RESULT_DOWNLOADERROR: echo $OUTPUT->notification($langcode . '.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; } } get_string_manager()->reset_caches(); print_upgrade_separator(); }
/** * 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, true); } 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; }