public function test_component_installer() { global $CFG; $ci = new component_installer('http://download.moodle.org', 'unittest', 'downloadtests.zip'); $this->assertTrue($ci->check_requisites()); $destpath = $CFG->dataroot . '/downloadtests'; //carefully remove component files to enforce fresh installation @unlink($destpath . '/' . 'downloadtests.md5'); @unlink($destpath . '/' . 'test.html'); @unlink($destpath . '/' . 'test.jpg'); @rmdir($destpath); $this->assertEqual(COMPONENT_NEEDUPDATE, $ci->need_upgrade()); $status = $ci->install(); $this->assertEqual(COMPONENT_INSTALLED, $status); $this->assertEqual('9e94f74b3efb1ff6cf075dc6b2abf15c', $ci->get_component_md5()); //it's already installed, so Moodle should detect it's up to date $this->assertEqual(COMPONENT_UPTODATE, $ci->need_upgrade()); $status = $ci->install(); $this->assertEqual(COMPONENT_UPTODATE, $status); //check if correct files were downloaded $this->assertEqual('2af180e813dc3f446a9bb7b6af87ce24', md5_file($destpath . '/' . 'test.jpg')); $this->assertEqual('47250a973d1b88d9445f94db4ef2c97a', md5_file($destpath . '/' . 'test.html')); }
$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.'; continue; } } if (file_exists($dest2)) { if (!remove_dir($dest2)) { $notice_error[] = 'Could not delete old directory ' . $dest2 . ', update of ' . $pack . ' failed, please check permissions.'; continue; } } //2. copy & unzip into new if ($cd = new component_installer('http://download.moodle.org', 'lang16', $pack . '.zip', 'languages.md5', 'lang')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) switch ($status) { case COMPONENT_ERROR: if ($cd->get_error() == 'remotedownloaderror') { $a = new stdClass(); $a->url = 'http://download.moodle.org/lang16/' . $pack . '.zip'; $a->dest = $CFG->dataroot . '/lang'; print_error($cd->get_error(), 'error', "", $a); // not probable } else { print_error($cd->get_error(), 'error'); // not probable } break;
/** * Perform the actual language pack installation * * @uses component_installer * @param string $langcode * @return int return status */ protected function install_language_pack($langcode) { // initialise new component installer to process this language $installer = new component_installer('http://download.moodle.org', 'download.php/direct/langpack/' . $this->version, $langcode . '.zip', 'languages.md5', 'lang'); if (!$installer->requisitesok) { throw new lang_installer_exception('installer_requisites_check_failed'); } $status = $installer->install(); if ($status == COMPONENT_ERROR) { if ($installer->get_error() === 'remotedownloaderror') { return self::RESULT_DOWNLOADERROR; } else { throw new lang_installer_exception($installer->get_error(), $langcode); } } else { if ($status == COMPONENT_UPTODATE) { return self::RESULT_UPTODATE; } else { if ($status == COMPONENT_INSTALLED) { return self::RESULT_INSTALLED; } else { throw new lang_installer_exception('unexpected_installer_result', $status); } } } }
//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(); } } } } } // switch the string_manager instance to stop using install/lang/ $CFG->early_install_lang = false; $CFG->langotherroot = $CFG->dataroot . '/lang'; $CFG->langlocalroot = $CFG->dataroot . '/lang'; get_string_manager(true); // make sure we are installing stable release or require a confirmation if (isset($maturity)) { if ($maturity < MATURITY_STABLE and !$options['allow-unstable']) { $maturitylevel = get_string('maturity' . $maturity, 'admin');
$stradminhelpenvironment = get_string("adminhelpenvironment"); $strenvironment = get_string('environment', 'admin'); $strerror = get_string('error'); $strmoodleversion = get_string('moodleversion'); $strupdate = get_string('updatecomponent', 'admin'); $strupwards = get_string('upwards', 'admin'); $strmisc = get_string('miscellaneous'); /// Print the header stuff admin_externalpage_print_header(); /// Print the component download link echo '<div class="reportlink"><a href="environment.php?action=updatecomponent&sesskey=' . $USER->sesskey . '">' . $strupdate . '</a></div>'; print_heading($strenvironment); /// Handle the 'updatecomponent' action if ($action == 'updatecomponent' && confirm_sesskey()) { /// Create component installer and execute it if ($cd = new component_installer('http://download.moodle.org', 'environment', 'environment.zip')) { $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED switch ($status) { case ERROR: if ($cd->get_error() == 'remotedownloadnotallowed') { $a = new stdClass(); $a->url = 'http://download.moodle.org/environment/environment.zip'; $a->dest = $CFG->dataroot . '/'; print_simple_box(get_string($cd->get_error(), 'error', $a), 'center', '', '', 5, 'errorbox'); } else { print_simple_box(get_string($cd->get_error(), 'error'), 'center', '', '', 5, 'errorbox'); } break; case UPTODATE: print_simple_box(get_string($cd->get_error(), 'error'), 'center');
require_once $CFG->libdir . '/adminlib.php'; require_once $CFG->libdir . '/environmentlib.php'; require_once $CFG->libdir . '/componentlib.class.php'; // Parameters $action = optional_param('action', '', PARAM_ALPHANUMEXT); $version = optional_param('version', '', PARAM_FILE); // $extraurlparams = array(); if ($version) { $extraurlparams['version'] = $version; } admin_externalpage_setup('environment', '', $extraurlparams); // Handle the 'updatecomponent' action if ($action == 'updatecomponent' && confirm_sesskey()) { // Create component installer and execute it if ($cd = new component_installer('https://download.moodle.org', 'environment', 'environment.zip')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) switch ($status) { case COMPONENT_ERROR: if ($cd->get_error() == 'remotedownloaderror') { $a = new stdClass(); $a->url = 'https://download.moodle.org/environment/environment.zip'; $a->dest = $CFG->dataroot . '/'; print_error($cd->get_error(), 'error', $PAGE->url, $a); die; } else { print_error($cd->get_error(), 'error', $PAGE->url); die; } case COMPONENT_UPTODATE:
/** * Try to upgrade the given language pack (or current language) * If it doesn't work, fail silently and return false */ function upgrade_language_pack($lang = '') { global $CFG; if (empty($lang)) { $lang = current_language(); } if ($lang == 'en_utf8') { return true; // Nothing to do } notify(get_string('langimport', 'admin') . ': ' . $lang . ' ... ', 'notifysuccess'); @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'; if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang . '.zip', 'languages.md5', 'lang')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) if ($status == COMPONENT_INSTALLED) { debugging('Downloading successful: ' . $lang); @unlink($CFG->dataroot . '/cache/languages'); return true; } } return false; }
$notice_error = array(); switch ($mode) { case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack if (confirm_sesskey() and !empty($pack)) { set_time_limit(0); @mkdir($CFG->dataroot . '/temp/', $CFG->directorypermissions); //make it in case it's a fresh install, it might not be there @mkdir($CFG->dataroot . '/lang/', $CFG->directorypermissions); if (is_array($pack)) { $packs = $pack; } else { $packs = array($pack); } foreach ($packs as $pack) { if ($cd = new component_installer('http://download.moodle.org', 'lang16', $pack . '.zip', 'languages.md5', 'lang')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) switch ($status) { case COMPONENT_ERROR: if ($cd->get_error() == 'remotedownloaderror') { $a = new object(); $a->url = 'http://download.moodle.org/lang16/' . $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);
/** * Try to upgrade the given language pack (or current language) * @global object */ function upgrade_language_pack($lang = '') { global $CFG, $OUTPUT; if (empty($lang)) { $lang = current_language(); } if ($lang == 'en_utf8') { 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'; if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang . '.zip', 'languages.md5', 'lang')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) if ($status == COMPONENT_INSTALLED) { @unlink($CFG->dataroot . '/cache/languages'); if ($parentlang = get_parent_language($lang)) { if ($cd = new component_installer('http://download.moodle.org', 'lang16', $parentlang . '.zip', 'languages.md5', 'lang')) { $cd->install(); } } echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); } } print_upgrade_separator(); }
$nextstage = DATABASE; } } //==========================================================================// // Try to download the lang pack if it has been selected if ($INSTALL['stage'] == DOWNLOADLANG && $INSTALL['downloadlangpack']) { $downloadsuccess = false; $downloaderror = ''; error_reporting(0); // Hide errors /// Create necessary lang dir if (!make_upload_directory('lang', false)) { $downloaderror = get_string('cannotcreatelangdir', 'error'); } /// Download and install component if (($cd = new component_installer('http://download.moodle.org', 'lang16', $INSTALL['language'] . '.zip', 'languages.md5', 'lang')) && empty($errormsg)) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) switch ($status) { case COMPONENT_ERROR: if ($cd->get_error() == 'remotedownloaderror') { $a = new stdClass(); $a->url = 'http://download.moodle.org/lang16/' . $INSTALL['language'] . '.zip'; $a->dest = $CFG->dataroot . '/lang'; $downloaderror = get_string($cd->get_error(), 'error', $a); } else { $downloaderror = get_string($cd->get_error(), 'error'); } break; case COMPONENT_UPTODATE: case COMPONENT_INSTALLED:
$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.'; continue; } } if (file_exists($dest2)) { if (!remove_dir($dest2)) { $notice_error[] = 'Could not delete old directory ' . $dest2 . ', update of ' . $pack . ' failed, please check permissions.'; continue; } } //2. copy & unzip into new if ($cd = new component_installer('http://download.moodle.org', 'lang16', $pack . '.zip', 'languages.md5', 'lang')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) switch ($status) { case COMPONENT_ERROR: if ($cd->get_error() == 'remotedownloaderror') { $a = new stdClass(); $a->url = 'http://download.moodle.org/lang16/' . $pack . '.zip'; $a->dest = $CFG->dataroot . '/lang'; error(get_string($cd->get_error(), 'error', $a)); // not probable } else { error(get_string($cd->get_error(), 'error')); // not probable } break;
public function installLangPack($pack) { global $CFG; 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 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: 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; } } else { echo $OUTPUT->notification('Had an unspecified error with the component installer, sorry.'); } } }
$stradminhelpenvironment = get_string("adminhelpenvironment"); $strenvironment = get_string('environment', 'admin'); $strerror = get_string('error'); $strmoodleversion = get_string('moodleversion'); $strupdate = get_string('updatecomponent', 'admin'); $strupwards = get_string('upwards', 'admin'); $strmisc = get_string('miscellaneous'); /// Print the header stuff echo $OUTPUT->header(); /// Print the component download link echo '<div class="reportlink"><a href="environment.php?action=updatecomponent&sesskey=' . sesskey() . '">' . $strupdate . '</a></div>'; echo $OUTPUT->heading($strenvironment); /// Handle the 'updatecomponent' action if ($action == 'updatecomponent' && confirm_sesskey()) { /// Create component installer and execute it if ($cd = new component_installer('http://download.moodle.org', 'environment', 'environment.zip')) { $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED) switch ($status) { case COMPONENT_ERROR: if ($cd->get_error() == 'remotedownloaderror') { $a = new stdClass(); $a->url = 'http://download.moodle.org/environment/environment.zip'; $a->dest = $CFG->dataroot . '/'; echo $OUTPUT->box(get_string($cd->get_error(), 'error', $a), 'errorbox'); } else { echo $OUTPUT->box(get_string($cd->get_error(), 'error'), 'errorbox'); } break; case COMPONENT_UPTODATE: echo $OUTPUT->box(get_string($cd->get_error(), 'error'));
//download the language pack if it doesn't exist if ($interactive == CLI_FULL || $interactive == CLI_SEMI && !isset($INSTALL['downloadlangaugepack'])) { $site_langs = get_list_of_languages(); if (!key_exists($INSTALL['lang'], $site_langs)) { console_write("\n", '', false); console_write("\n", '', false); console_write('downloadlanguagepack', 'install'); $download_lang_pack = read_yes_no(); if ($download_lang_pack == 'yes') { $downloadsuccess = false; /// Create necessary lang dir if (!make_upload_directory('lang', false)) { console_write_error(get_string('cannotcreatelangdir', 'error'), false); } /// Download and install component if (($cd = new component_installer('http://download.moodle.org', 'lang16', $INSTALL['lang'] . '.zip', 'languages.md5', 'lang')) && empty($errormsg)) { $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED switch ($status) { case ERROR: if ($cd->get_error() == 'remotedownloadnotallowed') { $a = new stdClass(); $a->url = 'http://download.moodle.org/lang16/' . $pack . '.zip'; $a->dest = $CFG->dataroot . '/lang'; console_write(get_string($cd->get_error(), 'error', $a), false); } else { $downloaderror = get_string($cd->get_error(), 'error'); console_write(get_string($cd->get_error(), 'error'), false); } break; case UPTODATE: