Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / admin / langimport.php
blob6ace99dcc43027c96498864dc0d3e28fb1de102d
1 <?php //$Id$
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
16 $displaylang = $pack;
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');
28 $title = $strlang;
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
37 $notice_ok = array();
38 $notice_error = array();
40 switch ($mode){
42 case INSTALLATION_OF_SELECTED_LANG: ///installation of selected language pack
44 if (confirm_sesskey() and !empty($pack)) {
45 set_time_limit(0);
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)) {
50 $packs = $pack;
51 } else {
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)
59 switch ($status) {
61 case COMPONENT_ERROR:
62 if ($cd->get_error() == 'remotedownloaderror') {
63 $a = new object();
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);
67 } else {
68 print_error($cd->get_error(), 'error', 'langimport.php');
70 break;
72 case COMPONENT_INSTALLED:
73 $notice_ok[] = get_string('langpackinstalled','admin',$pack);
74 break;
76 case COMPONENT_UPTODATE:
77 break;
80 } else {
81 notify('Had an unspecified error with the component installer, sorry.');
85 break;
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.'&amp;uninstalllang='.$uninstalllang.'&amp;confirm=1&amp;sesskey='.sesskey(),
96 'langimport.php');
97 print_footer();
98 die;
100 } else if (confirm_sesskey()) {
101 $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
102 $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
103 $rm1 = false;
104 $rm2 = false;
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
113 if ($rm1 or $rm2) {
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';
119 break;
121 case UPDATE_ALL_LANG: //1 click update for all updatable language packs
122 set_time_limit(0);
124 //0th pull a list from download.moodle.org,
125 //key = langname, value = md5
126 $md5array = array();
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);
146 continue;
148 $dest1 = $CFG->dataroot.'/lang/'.$clang;
149 $dest2 = $CFG->dirroot.'/lang/'.$clang;
151 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
152 $lang16[] = $clang;
156 //then filter out packs that have the same md5 key
157 foreach ($lang16 as $clang) {
158 if (!is_installed_lang($clang, $md5array[$clang])){
159 $packs[] = $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
167 continue;
170 //1. delete old director(ies)
172 $dest1 = $CFG->dataroot.'/lang/'.$pack;
173 $dest2 = $CFG->dirroot.'/lang/'.$pack;
174 $rm1 = false;
175 $rm2 = false;
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.';
179 continue;
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.';
185 continue;
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)
194 switch ($status) {
196 case COMPONENT_ERROR:
197 if ($cd->get_error() == 'remotedownloaderror') {
198 $a = new stdClass();
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
202 } else {
203 print_error($cd->get_error(), 'error'); // not probable
205 break;
206 case COMPONENT_UPTODATE:
207 //Print error string or whatever you want to do
208 break;
209 case COMPONENT_INSTALLED:
210 $notice_ok[] = get_string('langpackupdated', 'admin', $pack);
211 $updated = true;
212 //Print/do whatever you want
213 break;
214 default:
216 } else {
221 if ($updated) {
222 $notice_ok[] = get_string('langupdatecomplete','admin');
223 } else {
224 $notice_ok[] = get_string('nolangupdateneeded','admin');
227 break;
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) {
239 $SESSION->lang = $l;
240 $parent = get_string('parentlanguage');
241 if ($parent == 'en_utf8') {
242 continue;
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;
253 } else {
254 unset($SESSION->lang);
257 if ($availablelangs = get_remote_list_of_languages()) {
258 $remote = 1;
259 } else {
260 $remote = 0; //flag for reading from remote or local
261 $availablelangs = get_local_list_of_languages();
264 if (!$remote) {
265 print_box_start();
266 print_string('remotelangnotavailable', 'admin', $CFG->dataroot.'/lang/');
267 print_box_end();
270 if ($notice_ok) {
271 $info = implode('<br />', $notice_ok);
272 notify($info, 'notifysuccess');
275 if ($notice_error) {
276 $info = implode('<br />', $notice_error);
277 notify($info, 'notifyproblem');
280 if ($missingparents) {
281 foreach ($missingparents as $l=>$parent) {
282 $a = new object();
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);
289 } else {
290 $shortlang = $alang[0];
292 $a->parent = $alang[2].' ('.$shortlang.')';
295 $info = get_string('missinglangparent', 'admin', $a);
296 notify($info, 'notifyproblem');
300 print_box_start();
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>';
314 echo '</select>';
315 echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
316 echo '</fieldset>';
317 echo '</form>';
319 if ($remote) {
320 echo '<form id="updateform" action="langimport.php?mode='.UPDATE_ALL_LANG.'" method="post">';
321 echo '<div>';
322 echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
323 echo '</div>';
324 echo '</form>';
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";
341 if ($remote) {
342 echo '<select name="pack[]" id="pack" size="15" multiple="multiple">';
345 foreach ($availablelangs as $alang) {
346 if ($alang[0] == '') {
347 continue;
349 if (trim($alang[0]) != "en_utf8") {
350 if ($remote) {
351 if (substr($alang[0], -5) == '_utf8') { //Remove the _utf8 suffix from the lang to show
352 $shortlang = substr($alang[0], 0, -5);
353 } else {
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>';
362 $empty = 0;
365 if ($remote) {
366 echo '</select>';
367 echo '<br /><input type="submit" value="'.$THEME->larrow.' '.get_string('install','admin').'" />';
369 echo '</fieldset>';
370 echo '</form>';
372 if ($empty) {
373 echo '<br />';
374 print_string('nolanguagetodownload','admin');
377 //close available langs table
378 echo '</td></tr></table>';
379 print_box_end();
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.
387 * @return array
389 function get_local_list_of_languages() {
390 global $CFG;
391 $source = $CFG->dirroot.'/lib/languages.md5';
392 $availablelangs = array();
393 if ($fp = fopen($source, 'r')) {
394 while(!feof ($fp)) {
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
406 * @return bool
408 function is_installed_lang($lang, $md5check) {
409 global $CFG;
410 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
411 if (file_exists($md5file)){
412 return (file_get_contents($md5file) == $md5check);
414 return false;
418 * Returns the latest list of available language packs from
419 * moodle.org
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) {
429 if (!empty($line)){
430 $availablelangs[] = split(',', $line);
433 return $availablelangs;
435 } else {
436 return false;