2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Lang import controller
20 * @package tool_langimport
21 * @copyright 2014 Dan Poltawski <dan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace tool_langimport;
27 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/filelib.php');
29 require_once($CFG->libdir.'/componentlib.class.php');
32 * Lang import controller
34 * @package tool_langimport
35 * @copyright 2014 Dan Poltawski <dan@moodle.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 /** @var array list of informational messages */
41 /** @var array list of error messages */
43 /** @var \lang_installer */
45 /** @var array languages available on the remote server */
46 public $availablelangs;
51 public function __construct() {
52 make_temp_directory('');
53 make_upload_directory('lang');
55 $this->info = array();
56 $this->errors = array();
57 $this->installer = new \lang_installer();
59 $this->availablelangs = $this->installer->get_remote_list_of_languages();
63 * Install language packs provided
65 * @param string|array $langs array of langcodes or individual langcodes
66 * @return int false if an error encountered or
67 * @throws \moodle_exception when error is encountered installing langpack
69 public function install_languagepacks($langs) {
72 $this->installer->set_queue($langs);
73 $results = $this->installer->run();
77 foreach ($results as $langcode => $langstatus) {
78 switch ($langstatus) {
79 case \lang_installer::RESULT_DOWNLOADERROR:
81 $a->url = $this->installer->lang_pack_url($langcode);
82 $a->dest = $CFG->dataroot.'/lang';
83 $this->errors[] = get_string('remotedownloaderror', 'error', $a);
84 throw new \moodle_exception('remotedownloaderror', 'error', $a);
86 case \lang_installer::RESULT_INSTALLED:
87 $this->info[] = get_string('langpackinstalled', 'tool_langimport', $langcode);
90 case \lang_installer::RESULT_UPTODATE:
91 $this->info[] = get_string('langpackuptodate', 'tool_langimport', $langcode);
100 * Uninstall language pack
102 * @param string $lang language code
103 * @return bool true if language succesfull installed
105 public function uninstall_language($lang) {
108 $dest1 = $CFG->dataroot.'/lang/'.$lang;
109 $dest2 = $CFG->dirroot.'/lang/'.$lang;
112 if (file_exists($dest1)) {
113 $rm1 = remove_dir($dest1);
115 if (file_exists($dest2)) {
116 $rm2 = remove_dir($dest2);
120 $this->info[] = get_string('langpackremoved', 'tool_langimport', $lang);
122 } else { // Nothing deleted, possibly due to permission error.
123 $this->errors[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
129 * Updated all install language packs with the latest found on servre
131 * @return bool true if languages succesfully updated.
133 public function update_all_installed_languages() {
136 if (!$availablelangs = $this->installer->get_remote_list_of_languages()) {
137 $this->errors[] = get_string('cannotdownloadlanguageupdatelist', 'error');
141 $md5array = array(); // Convert to (string)langcode => (string)md5.
142 foreach ($availablelangs as $alang) {
143 $md5array[$alang[0]] = $alang[1];
146 // Filter out unofficial packs.
147 $currentlangs = array_keys(get_string_manager()->get_list_of_translations(true));
148 $updateablelangs = array();
149 foreach ($currentlangs as $clang) {
150 if (!array_key_exists($clang, $md5array)) {
151 $noticeok[] = get_string('langpackupdateskipped', 'tool_langimport', $clang);
154 $dest1 = $CFG->dataroot.'/lang/'.$clang;
155 $dest2 = $CFG->dirroot.'/lang/'.$clang;
157 if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')) {
158 $updateablelangs[] = $clang;
162 // Filter out packs that have the same md5 key.
163 $neededlangs = array();
164 foreach ($updateablelangs as $ulang) {
165 if (!$this->is_installed_lang($ulang, $md5array[$ulang])) {
166 $neededlangs[] = $ulang;
170 // Clean-up currently installed versions of the packs.
171 foreach ($neededlangs as $packindex => $pack) {
176 // Delete old directories.
177 $dest1 = $CFG->dataroot.'/lang/'.$pack;
178 $dest2 = $CFG->dirroot.'/lang/'.$pack;
179 if (file_exists($dest1)) {
180 if (!remove_dir($dest1)) {
181 $noticeerror[] = 'Could not delete old directory '.$dest1.', update of '.$pack
182 .' failed, please check permissions.';
183 unset($neededlangs[$packindex]);
187 if (file_exists($dest2)) {
188 if (!remove_dir($dest2)) {
189 $noticeerror[] = 'Could not delete old directory '.$dest2.', update of '.$pack
190 .' failed, please check permissions.';
191 unset($neededlangs[$packindex]);
198 $updated = $this->install_languagepacks($neededlangs);
199 } catch (\moodle_exception $e) {
204 $this->info[] = get_string('langupdatecomplete', 'tool_langimport');
206 $this->info[] = get_string('nolangupdateneeded', 'tool_langimport');
213 * checks the md5 of the zip file, grabbed from download.moodle.org,
214 * against the md5 of the local language file from last update
215 * @param string $lang language code
216 * @param string $md5check md5 to check
217 * @return bool true if installed
219 public function is_installed_lang($lang, $md5check) {
221 $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
222 if (file_exists($md5file)) {
223 return (file_get_contents($md5file) == $md5check);