2 ///This file only manages the installation of 1.6 lang packs.
3 ///in downloads.moodle.org, they are store in separate directory /lang16
4 ///in local server, they are stored in $CFG->dataroot/lang
5 ///This helps to avoid confusion.
7 require_once('../config.php');
8 require_once($CFG->libdir
.'/adminlib.php');
9 require_once($CFG->libdir
.'/filelib.php');
10 require_once($CFG->libdir
.'/componentlib.class.php');
12 admin_externalpage_setup('langimport');
14 $mode = optional_param('mode', 0, PARAM_INT
); //phase
15 $pack = optional_param('pack', array(), PARAM_FILE
); //pack to install
17 $uninstalllang = optional_param('uninstalllang', '', PARAM_FILE
);
18 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
19 $sitelang = optional_param('sitelangconfig', '', PARAM_FILE
);
21 define('INSTALLATION_OF_SELECTED_LANG', 2);
22 define('DELETION_OF_SELECTED_LANG', 4);
23 define('UPDATE_ALL_LANG', 5);
25 $strlang = get_string('langimport','admin');
26 $strlanguage = get_string('language');
27 $strthislanguage = get_string('thislanguage');
30 //reset and diagnose lang cache permissions
31 @unlink
($CFG->dataroot
.'/cache/languages');
32 if (file_exists($CFG->dataroot
.'/cache/languages')) {
33 error('Language cache can not be deleted, please fix permissions in dataroot/cache/languages!');
35 get_list_of_languages(true); //refresh lang cache
38 $notice_error = array();
42 case INSTALLATION_OF_SELECTED_LANG
: ///installation of selected language pack
44 if (confirm_sesskey() and !empty($pack)) {
46 @mkdir
($CFG->dataroot
.'/temp/'); //make it in case it's a fresh install, it might not be there
47 @mkdir
($CFG->dataroot
.'/lang/');
49 if (is_array($pack)) {
52 $packs = array($pack);
55 foreach ($packs as $pack) {
56 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
57 $pack.'.zip', 'languages.md5', 'lang')) {
58 $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
62 if ($cd->get_error() == 'remotedownloaderror') {
64 $a->url
= 'http://download.moodle.org/lang16/'.$pack.'.zip';
65 $a->dest
= $CFG->dataroot
.'/lang';
66 print_error($cd->get_error(), 'error', 'langimport.php', $a);
68 print_error($cd->get_error(), 'error', 'langimport.php');
72 case COMPONENT_INSTALLED
:
73 $notice_ok[] = get_string('langpackinstalled','admin',$pack);
76 case COMPONENT_UPTODATE
:
81 notify('Had an unspecified error with the component installer, sorry.');
87 case DELETION_OF_SELECTED_LANG
: //delete a directory(ies) containing a lang pack completely
89 if ($uninstalllang == 'en_utf8') {
90 $notice_error[] = 'en_utf8 can not be uninstalled!';
92 } else if (!$confirm && confirm_sesskey()) {
93 admin_externalpage_print_header();
94 notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang),
95 'langimport.php?mode='.DELETION_OF_SELECTED_LANG
.'&uninstalllang='.$uninstalllang.'&confirm=1&sesskey='.sesskey(),
100 } else if (confirm_sesskey()) {
101 $dest1 = $CFG->dataroot
.'/lang/'.$uninstalllang;
102 $dest2 = $CFG->dirroot
.'/lang/'.$uninstalllang;
105 if (file_exists($dest1)){
106 $rm1 = remove_dir($dest1);
108 if (file_exists($dest2)){
109 $rm2 = remove_dir($dest2);
111 get_list_of_languages(true); //refresh lang cache
112 //delete the direcotries
114 $notice_ok[] = get_string('langpackremoved','admin');
115 } else { //nothing deleted, possibly due to permission error
116 $notice_error[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
121 case UPDATE_ALL_LANG
: //1 click update for all updatable language packs
124 //0th pull a list from download.moodle.org,
125 //key = langname, value = md5
127 $updated = 0; //any packs updated?
128 $alllangs = array_keys(get_list_of_languages(false, true)); //get all available langs
129 $lang16 = array(); //all the Moodle 1.6 unicode lang packs (updated and not updated)
130 $packs = array(); //all the packs that needs updating
133 if (!$availablelangs = get_remote_list_of_languages()) {
134 print_error('cannotdownloadlanguageupdatelist');
137 //and build an associative array
138 foreach ($availablelangs as $alang) {
139 $md5array[$alang[0]] = $alang[1];
142 //filtering out non-16 and unofficial packs
143 foreach ($alllangs as $clang) {
144 if (!array_key_exists($clang, $md5array)) {
145 $notice_ok[] = get_string('langpackupdateskipped', 'admin', $clang);
148 $dest1 = $CFG->dataroot
.'/lang/'.$clang;
149 $dest2 = $CFG->dirroot
.'/lang/'.$clang;
151 if (file_exists($dest1.'/langconfig.php') ||
file_exists($dest2.'/langconfig.php')){
156 //then filter out packs that have the same md5 key
157 foreach ($lang16 as $clang) {
158 if (!is_installed_lang($clang, $md5array[$clang])){
163 @mkdir
($CFG->dataroot
.'/temp/');
164 @mkdir
($CFG->dataroot
.'/lang/');
165 foreach ($packs as $pack){ //for each of the remaining in the list, we
166 if ($pack == 'en_utf8') { // no update for en_utf8
170 //1. delete old director(ies)
172 $dest1 = $CFG->dataroot
.'/lang/'.$pack;
173 $dest2 = $CFG->dirroot
.'/lang/'.$pack;
176 if (file_exists($dest1)) {
177 if (!remove_dir($dest1)) {
178 $notice_error[] = 'Could not delete old directory '.$dest1.', update of '.$pack.' failed, please check permissions.';
182 if (file_exists($dest2)) {
183 if (!remove_dir($dest2)) {
184 $notice_error[] = 'Could not delete old directory '.$dest2.', update of '.$pack.' failed, please check permissions.';
189 //2. copy & unzip into new
191 if ($cd = new component_installer('http://download.moodle.org', 'lang16',
192 $pack.'.zip', 'languages.md5', 'lang')) {
193 $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
196 case COMPONENT_ERROR
:
197 if ($cd->get_error() == 'remotedownloaderror') {
199 $a->url
= 'http://download.moodle.org/lang16/'.$pack.'.zip';
200 $a->dest
= $CFG->dataroot
.'/lang';
201 print_error($cd->get_error(), 'error', "", $a); // not probable
203 print_error($cd->get_error(), 'error'); // not probable
206 case COMPONENT_UPTODATE
:
207 //Print error string or whatever you want to do
209 case COMPONENT_INSTALLED
:
210 $notice_ok[] = get_string('langpackupdated', 'admin', $pack);
212 //Print/do whatever you want
222 $notice_ok[] = get_string('langupdatecomplete','admin');
224 $notice_ok[] = get_string('nolangupdateneeded','admin');
228 } //close of main switch
231 admin_externalpage_print_header();
233 $installedlangs = get_list_of_languages(true, true);
235 $missingparents = array();
236 $oldlang = isset($SESSION->lang
) ?
$SESSION->lang
: null; // override current lang
238 foreach($installedlangs as $l=>$unused) {
240 $parent = get_string('parentlanguage');
241 if ($parent == 'en_utf8') {
244 if (strpos($parent, '[[') !== false) {
245 continue; // no parent
247 if (!isset($installedlangs[$parent])) {
248 $missingparents[$l] = $parent;
251 if (isset($oldlang)) {
252 $SESSION->lang
= $oldlang;
254 unset($SESSION->lang
);
257 if ($availablelangs = get_remote_list_of_languages()) {
260 $remote = 0; //flag for reading from remote or local
261 $availablelangs = get_local_list_of_languages();
266 print_string('remotelangnotavailable', 'admin', $CFG->dataroot
.'/lang/');
271 $info = implode('<br />', $notice_ok);
272 notify($info, 'notifysuccess');
276 $info = implode('<br />', $notice_error);
277 notify($info, 'notifyproblem');
280 if ($missingparents) {
281 foreach ($missingparents as $l=>$parent) {
283 $a->lang
= $installedlangs[$l];
284 $a->parent
= $parent;
285 foreach ($availablelangs as $alang) {
286 if ($alang[0] == $parent) {
287 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
288 $shortlang = substr($alang[0], 0, -5);
290 $shortlang = $alang[0];
292 $a->parent
= $alang[2].' ('.$shortlang.')';
295 $info = get_string('missinglangparent', 'admin', $a);
296 notify($info, 'notifyproblem');
301 echo '<table summary="">';
302 echo '<tr><td align="center" valign="top">';
303 echo '<form id="uninstallform" action="langimport.php?mode='.DELETION_OF_SELECTED_LANG
.'" method="post">';
304 echo '<fieldset class="invisiblefieldset">';
305 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
307 /// display installed langs here
309 echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n";
310 echo '<select name="uninstalllang" id="uninstalllang" size="15">';
311 foreach ($installedlangs as $clang =>$ilang){
312 echo '<option value="'.$clang.'">'.$ilang.'</option>';
315 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
320 echo '<form id="updateform" action="langimport.php?mode='.UPDATE_ALL_LANG
.'" method="post">';
322 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
327 /// Display option to change site language
329 /// display to be installed langs here
331 echo '</td><td align="center" valign="top">';
332 //availabe langs table
333 $empty = 1; //something to pring
335 /// if this language pack is not already installed, then we allow installation
337 echo '<form id="installform" method="post" action="langimport.php?mode='.INSTALLATION_OF_SELECTED_LANG
.'">';
338 echo '<fieldset class="invisiblefieldset">';
339 echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
340 echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n";
342 echo '<select name="pack[]" id="pack" size="15" multiple="multiple">';
345 foreach ($availablelangs as $alang) {
346 if ($alang[0] == '') {
349 if (trim($alang[0]) != "en_utf8") {
351 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
352 $shortlang = substr($alang[0], 0, -5);
354 $shortlang = $alang[0];
356 if (!is_installed_lang($alang[0], $alang[1])){ //if not already installed
357 echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
359 } else { //print list in local format, and instruction to install
360 echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>';
367 echo '<br /><input type="submit" value="'.$THEME->larrow
.' '.get_string('install','admin').'" />';
374 print_string('nolanguagetodownload','admin');
377 //close available langs table
378 echo '</td></tr></table>';
381 admin_externalpage_print_footer();
384 * Returns a list of available language packs from a
385 * local copy shipped with standard moodle distro
386 * this is for site that can't download components.
389 function get_local_list_of_languages() {
391 $source = $CFG->dirroot
.'/lib/languages.md5';
392 $availablelangs = array();
393 if ($fp = fopen($source, 'r')) {
395 $availablelangs[] = split(',', fgets($fp,1024));
398 return $availablelangs;
402 * checks the md5 of the zip file, grabbed from download.moodle.org,
403 * against the md5 of the local language file from last update
404 * @param string $lang
405 * @param string $md5check
408 function is_installed_lang($lang, $md5check) {
410 $md5file = $CFG->dataroot
.'/lang/'.$lang.'/'.$lang.'.md5';
411 if (file_exists($md5file)){
412 return (file_get_contents($md5file) == $md5check);
418 * Returns the latest list of available language packs from
420 * @return array or false if can not download
422 function get_remote_list_of_languages() {
423 $source = 'http://download.moodle.org/lang16/languages.md5';
424 $availablelangs = array();
426 if ($content = download_file_content($source)) {
427 $alllines = split("\n", $content);
428 foreach($alllines as $line) {
430 $availablelangs[] = split(',', $line);
433 return $availablelangs;