forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlangimport.php
executable file
·387 lines (341 loc) · 14 KB
/
langimport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Fetches language packages from download.moodle.org server
*
* Language packages are available at http://download.moodle.org/langpack/2.0/
* in ZIP format together with a file languages.md5 containing their hashes
* and meta info.
* Locally, language packs are saved into $CFG->dataroot/lang/
*
* @package core
* @copyright 2005 Yu Zhang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(dirname(dirname(__FILE__)).'/config.php');
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
admin_externalpage_setup('langimport');
if (!empty($CFG->skiplangupgrade)) {
echo $OUTPUT->header();
echo $OUTPUT->box(get_string('langimportdisabled', 'admin'));
echo $OUTPUT->footer();
die;
}
$mode = optional_param('mode', 0, PARAM_INT); // action
$pack = optional_param('pack', array(), PARAM_SAFEDIR); // pack to install
$uninstalllang = optional_param('uninstalllang', '', PARAM_LANG); // installed pack to uninstall
$confirm = optional_param('confirm', 0, PARAM_BOOL); // uninstallation confirmation
define('INSTALLATION_OF_SELECTED_LANG', 2);
define('DELETION_OF_SELECTED_LANG', 4);
define('UPDATE_ALL_LANG', 5);
//reset and diagnose lang cache permissions
remove_dir($CFG->dataroot.'/cache/languages');
if (file_exists($CFG->dataroot.'/cache/languages')) {
print_error('cannotdeletelangcache', 'error');
}
get_string_manager()->reset_caches();
$notice_ok = array();
$notice_error = array();
if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($pack)) {
set_time_limit(0);
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.');
}
}
}
if ($mode == DELETION_OF_SELECTED_LANG and !empty($uninstalllang)) {
if ($uninstalllang == 'en') {
$notice_error[] = 'English language pack can not be uninstalled';
} else if (!$confirm and confirm_sesskey()) {
echo $OUTPUT->header();
echo $OUTPUT->confirm(get_string('uninstallconfirm', 'admin', $uninstalllang),
'langimport.php?mode='.DELETION_OF_SELECTED_LANG.'&uninstalllang='.$uninstalllang.'&confirm=1',
'langimport.php');
echo $OUTPUT->footer();
die;
} else if (confirm_sesskey()) {
$dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
$dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
$rm1 = false;
$rm2 = false;
if (file_exists($dest1)){
$rm1 = remove_dir($dest1);
}
if (file_exists($dest2)){
$rm2 = remove_dir($dest2);
}
if ($rm1 or $rm2) {
$notice_ok[] = get_string('langpackremoved','admin');
} else { //nothing deleted, possibly due to permission error
$notice_error[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
}
}
}
if ($mode == UPDATE_ALL_LANG) {
set_time_limit(0);
if (!$availablelangs = 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', 'admin', $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 (!is_installed_lang($ulang, $md5array[$ulang])) {
$neededlangs[] = $ulang;
}
}
make_upload_directory('temp');
make_upload_directory('lang');
$updated = false; // any packs updated?
foreach ($neededlangs as $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.';
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;
}
}
// 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;
}
}
}
if ($updated) {
$notice_ok[] = get_string('langupdatecomplete','admin');
} else {
$notice_ok[] = get_string('nolangupdateneeded','admin');
}
}
get_string_manager()->reset_caches();
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('langimport', 'admin'));
$installedlangs = get_string_manager()->get_list_of_translations(true);
$missingparents = array();
foreach ($installedlangs as $installedlang => $unused) {
$parent = get_parent_language($installedlang);
if (empty($parent) or ($parent === 'en')) {
continue;
}
if (!isset($installedlangs[$parent])) {
$missingparents[$installedlang] = $parent;
}
}
if ($availablelangs = get_remote_list_of_languages()) {
$remote = true;
} else {
$remote = false;
$availablelangs = array();
echo $OUTPUT->box_start();
print_string('remotelangnotavailable', 'admin', $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);
echo $OUTPUT->notification($info, 'notifyproblem');
}
if ($missingparents) {
foreach ($missingparents as $l=>$parent) {
$a = new stdClass();
$a->lang = $installedlangs[$l];
$a->parent = $parent;
foreach ($availablelangs as $alang) {
if ($alang[0] == $parent) {
$shortlang = $alang[0];
$a->parent = $alang[2].' ('.$shortlang.')';
}
}
$info = get_string('missinglangparent', 'admin', $a);
echo $OUTPUT->notification($info, 'notifyproblem');
}
}
echo $OUTPUT->box_start();
echo html_writer::start_tag('table');
echo html_writer::start_tag('tr');
// list of installed languages
$url = new moodle_url('/admin/langimport.php', array('mode' => DELETION_OF_SELECTED_LANG));
echo html_writer::start_tag('td', array('valign' => 'top'));
echo html_writer::start_tag('form', array('id' => 'uninstallform', 'action' => $url->out(), 'method' => 'post'));
echo html_writer::start_tag('fieldset');
echo html_writer::label(get_string('installedlangs','admin'), 'uninstalllang');
echo html_writer::empty_tag('br');
echo html_writer::select($installedlangs, 'uninstalllang', '', false, array('size' => 15));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
echo html_writer::empty_tag('br');
echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('uninstall','admin')));
echo html_writer::end_tag('fieldset');
echo html_writer::end_tag('form');
if ($remote) {
$url = new moodle_url('/admin/langimport.php', array('mode' => UPDATE_ALL_LANG));
echo html_writer::start_tag('form', array('id' => 'updateform', 'action' => $url->out(), 'method' => 'post'));
echo html_writer::tag('fieldset', html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('updatelangs','admin'))));
echo html_writer::end_tag('form');
}
echo html_writer::end_tag('td');
// list of available languages
$options = array();
foreach ($availablelangs as $alang) {
if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !is_installed_lang($alang[0], $alang[1])) {
$options[$alang[0]] = $alang[2].' ('.$alang[0].')';
}
}
if (!empty($options)) {
echo html_writer::start_tag('td', array('valign' => 'top'));
$url = new moodle_url('/admin/langimport.php', array('mode' => INSTALLATION_OF_SELECTED_LANG));
echo html_writer::start_tag('form', array('id' => 'installform', 'action' => $url->out(), 'method' => 'post'));
echo html_writer::start_tag('fieldset');
echo html_writer::label(get_string('availablelangs','install'), 'pack');
echo html_writer::empty_tag('br');
echo html_writer::select($options, 'pack[]', '', false, array('size' => 15, 'multiple' => 'multiple'));
echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
echo html_writer::empty_tag('br');
echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('install','admin')));
echo html_writer::end_tag('fieldset');
echo html_writer::end_tag('form');
echo html_writer::end_tag('td');
}
echo html_writer::end_tag('tr');
echo html_writer::end_tag('table');
echo $OUTPUT->box_end();
echo $OUTPUT->footer();
die();
////////////////////////////////////////////////////////////////////////////////
// Local functions /////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/**
* checks the md5 of the zip file, grabbed from download.moodle.org,
* against the md5 of the local language file from last update
* @param string $lang
* @param string $md5check
* @return bool
*/
function is_installed_lang($lang, $md5check) {
global $CFG;
$md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
if (file_exists($md5file)){
return (file_get_contents($md5file) == $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;
}
}