Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'MDL-26914-lang-grand-parents' of git://github.com/mudrd…

…8mz/moodle
  • Loading branch information...
commit 3d3ead8e3591f1165375723a598098dfcc28f355 2 parents 1f67888 + 74a4c9a
Petr Skoda skodak authored
29 admin/cli/install.php
@@ -376,25 +376,16 @@
376 376 }
377 377 }
378 378
379   -//download lang pack with optional notification
380   -if ($CFG->lang != 'en') {
381   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
382   - if ($cd->install() == COMPONENT_ERROR) {
383   - if ($cd->get_error() == 'remotedownloaderror') {
384   - $a = new stdClass();
385   - $a->url = 'http://download.moodle.org/langpack/2.0/'.$CFG->lang.'.zip';
386   - $a->dest = $CFG->dataroot.'/lang';
387   - cli_problem(get_string($cd->get_error(), 'error', $a));
388   - } else {
389   - cli_problem(get_string($cd->get_error(), 'error'));
390   - }
391   - } else {
392   - // install parent lang if defined
393   - if ($parentlang = get_parent_language()) {
394   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
395   - $cd->install();
396   - }
397   - }
  379 +// download required lang packs
  380 +if ($CFG->lang !== 'en') {
  381 + $installer = new lang_installer($CFG->lang);
  382 + $results = $installer->run();
  383 + foreach ($results as $langcode => $langstatus) {
  384 + if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
  385 + $a = new stdClass();
  386 + $a->url = $installer->lang_pack_url($langcode);
  387 + $a->dest = $CFG->dataroot.'/lang';
  388 + cli_problem(get_string('remotedownloaderror', 'error', $a));
398 389 }
399 390 }
400 391 }
29 admin/cli/install_database.php
@@ -121,26 +121,17 @@
121 121 }
122 122 $CFG->lang = $options['lang'];
123 123
124   -//download lang pack with optional notification
125   -if ($CFG->lang != 'en') {
  124 +// download required lang packs
  125 +if ($CFG->lang !== 'en') {
126 126 make_upload_directory('lang');
127   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
128   - if ($cd->install() == COMPONENT_ERROR) {
129   - if ($cd->get_error() == 'remotedownloaderror') {
130   - $a = new stdClass();
131   - $a->url = 'http://download.moodle.org/langpack/2.0/'.$CFG->lang.'.zip';
132   - $a->dest = $CFG->dataroot.'/lang';
133   - cli_problem(get_string($cd->get_error(), 'error', $a));
134   - } else {
135   - cli_problem(get_string($cd->get_error(), 'error'));
136   - }
137   - } else {
138   - // install parent lang if defined
139   - if ($parentlang = get_parent_language()) {
140   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
141   - $cd->install();
142   - }
143   - }
  127 + $installer = new lang_installer($CFG->lang);
  128 + $results = $installer->run();
  129 + foreach ($results as $langcode => $langstatus) {
  130 + if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
  131 + $a = new stdClass();
  132 + $a->url = $installer->lang_pack_url($langcode);
  133 + $a->dest = $CFG->dataroot.'/lang';
  134 + cli_problem(get_string('remotedownloaderror', 'error', $a));
144 135 }
145 136 }
146 137 }
141 admin/langimport.php
@@ -18,7 +18,7 @@
18 18 /**
19 19 * Fetches language packages from download.moodle.org server
20 20 *
21   - * Language packages are available at http://download.moodle.org/langpack/2.0/
  21 + * Language packages are available at http://download.moodle.org/langpack/
22 22 * in ZIP format together with a file languages.md5 containing their hashes
23 23 * and meta info.
24 24 * Locally, language packs are saved into $CFG->dataroot/lang/
@@ -33,8 +33,6 @@
33 33 require_once($CFG->libdir.'/filelib.php');
34 34 require_once($CFG->libdir.'/componentlib.class.php');
35 35
36   -$thisversion = '2.0'; // TODO this information should be taken from version.php or similar source
37   -
38 36 admin_externalpage_setup('langimport');
39 37
40 38 if (!empty($CFG->skiplangupgrade)) {
@@ -63,41 +61,22 @@
63 61 make_upload_directory('temp');
64 62 make_upload_directory('lang');
65 63
66   - if (is_array($pack)) {
67   - $packs = $pack;
68   - } else {
69   - $packs = array($pack);
70   - }
71   -
72   - foreach ($packs as $pack) {
73   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/'.$thisversion, $pack.'.zip', 'languages.md5', 'lang')) {
74   - $status = $cd->install();
75   - switch ($status) {
76   - case COMPONENT_ERROR:
77   - if ($cd->get_error() == 'remotedownloaderror') {
78   - $a = new stdClass();
79   - $a->url = 'http://download.moodle.org/langpack/'.$thisversion.'/'.$pack.'.zip';
80   - $a->dest = $CFG->dataroot.'/lang';
81   - print_error($cd->get_error(), 'error', 'langimport.php', $a);
82   - } else {
83   - print_error($cd->get_error(), 'error', 'langimport.php');
84   - }
85   - break;
86   - case COMPONENT_INSTALLED:
87   - $notice_ok[] = get_string('langpackinstalled','admin',$pack);
88   - if ($parentlang = get_parent_language($pack)) {
89   - // install also parent pack if specified
90   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/'.$thisversion,
91   - $parentlang.'.zip', 'languages.md5', 'lang')) {
92   - $cd->install();
93   - }
94   - }
95   - break;
96   - case COMPONENT_UPTODATE:
97   - break;
98   - }
99   - } else {
100   - echo $OUTPUT->notification('Had an unspecified error with the component installer, sorry.');
  64 + $installer = new lang_installer($pack);
  65 + $results = $installer->run();
  66 + foreach ($results as $langcode => $langstatus) {
  67 + switch ($langstatus) {
  68 + case lang_installer::RESULT_DOWNLOADERROR:
  69 + $a = new stdClass();
  70 + $a->url = $installer->lang_pack_url($langcode);
  71 + $a->dest = $CFG->dataroot.'/lang';
  72 + print_error('remotedownloaderror', 'error', 'langimport.php', $a);
  73 + break;
  74 + case lang_installer::RESULT_INSTALLED:
  75 + $notice_ok[] = get_string('langpackinstalled', 'admin', $langcode);
  76 + break;
  77 + case lang_installer::RESULT_UPTODATE:
  78 + $notice_ok[] = get_string('langpackuptodate', 'admin', $langcode);
  79 + break;
101 80 }
102 81 }
103 82 }
@@ -136,7 +115,9 @@
136 115 if ($mode == UPDATE_ALL_LANG) {
137 116 set_time_limit(0);
138 117
139   - if (!$availablelangs = get_remote_list_of_languages()) {
  118 + $installer = new lang_installer();
  119 +
  120 + if (!$availablelangs = $installer->get_remote_list_of_languages()) {
140 121 print_error('cannotdownloadlanguageupdatelist', 'error');
141 122 }
142 123 $md5array = array(); // (string)langcode => (string)md5
@@ -171,8 +152,8 @@
171 152 make_upload_directory('temp');
172 153 make_upload_directory('lang');
173 154
174   - $updated = false; // any packs updated?
175   - foreach ($neededlangs as $pack) {
  155 + // clean-up currently installed versions of the packs
  156 + foreach ($neededlangs as $packindex => $pack) {
176 157 if ($pack == 'en') {
177 158 continue;
178 159 }
@@ -185,41 +166,38 @@
185 166 if (file_exists($dest1)) {
186 167 if (!remove_dir($dest1)) {
187 168 $notice_error[] = 'Could not delete old directory '.$dest1.', update of '.$pack.' failed, please check permissions.';
  169 + unset($neededlangs[$packindex]);
188 170 continue;
189 171 }
190 172 }
191 173 if (file_exists($dest2)) {
192 174 if (!remove_dir($dest2)) {
193 175 $notice_error[] = 'Could not delete old directory '.$dest2.', update of '.$pack.' failed, please check permissions.';
  176 + unset($neededlangs[$packindex]);
194 177 continue;
195 178 }
196 179 }
  180 + }
197 181
198   - // copy and unzip new version
199   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/'.$thisversion, $pack.'.zip', 'languages.md5', 'lang')) {
200   - $status = $cd->install();
201   - switch ($status) {
202   -
203   - case COMPONENT_ERROR:
204   - if ($cd->get_error() == 'remotedownloaderror') {
205   - $a = new stdClass();
206   - $a->url = 'http://download.moodle.org/langpack/'.$thisversion.'/'.$pack.'.zip';
207   - $a->dest = $CFG->dataroot.'/lang';
208   - print_error($cd->get_error(), 'error', 'langimport.php', $a);
209   - } else {
210   - print_error($cd->get_error(), 'error', 'langimport.php');
211   - }
212   - break;
213   -
214   - case COMPONENT_UPTODATE:
215   - // should not get here
216   - break;
217   -
218   - case COMPONENT_INSTALLED:
219   - $notice_ok[] = get_string('langpackupdated', 'admin', $pack);
220   - $updated = true;
221   - break;
222   - }
  182 + // install all needed language packs
  183 + $installer->set_queue($neededlangs);
  184 + $results = $installer->run();
  185 + $updated = false; // any packs updated?
  186 + foreach ($results as $langcode => $langstatus) {
  187 + switch ($langstatus) {
  188 + case lang_installer::RESULT_DOWNLOADERROR:
  189 + $a = new stdClass();
  190 + $a->url = $installer->lang_pack_url($langcode);
  191 + $a->dest = $CFG->dataroot.'/lang';
  192 + print_error('remotedownloaderror', 'error', 'langimport.php', $a);
  193 + break;
  194 + case lang_installer::RESULT_INSTALLED:
  195 + $updated = true;
  196 + $notice_ok[] = get_string('langpackinstalled', 'admin', $langcode);
  197 + break;
  198 + case lang_installer::RESULT_UPTODATE:
  199 + $notice_ok[] = get_string('langpackuptodate', 'admin', $langcode);
  200 + break;
223 201 }
224 202 }
225 203
@@ -228,6 +206,8 @@
228 206 } else {
229 207 $notice_ok[] = get_string('nolangupdateneeded','admin');
230 208 }
  209 +
  210 + unset($installer);
231 211 }
232 212 get_string_manager()->reset_caches();
233 213
@@ -239,7 +219,7 @@
239 219 $missingparents = array();
240 220 foreach ($installedlangs as $installedlang => $unused) {
241 221 $parent = get_parent_language($installedlang);
242   - if (empty($parent) or ($parent === 'en')) {
  222 + if (empty($parent)) {
243 223 continue;
244 224 }
245 225 if (!isset($installedlangs[$parent])) {
@@ -247,7 +227,9 @@
247 227 }
248 228 }
249 229
250   -if ($availablelangs = get_remote_list_of_languages()) {
  230 +$installer = new lang_installer();
  231 +
  232 +if ($availablelangs = $installer->get_remote_list_of_languages()) {
251 233 $remote = true;
252 234 } else {
253 235 $remote = false;
@@ -357,26 +339,3 @@ function is_installed_lang($lang, $md5check) {
357 339 }
358 340 return false;
359 341 }
360   -
361   -/**
362   - * Returns the list of available language packs from download.moodle.org
363   - *
364   - * @return array|bool false if can not download
365   - */
366   -function get_remote_list_of_languages() {
367   - $source = 'http://download.moodle.org/langpack/2.0/languages.md5';
368   - $availablelangs = array();
369   -
370   - if ($content = download_file_content($source)) {
371   - $alllines = explode("\n", $content);
372   - foreach($alllines as $line) {
373   - if (!empty($line)){
374   - $availablelangs[] = explode(',', $line);
375   - }
376   - }
377   - return $availablelangs;
378   -
379   - } else {
380   - return false;
381   - }
382   -}
27 install.php
@@ -354,24 +354,15 @@
354 354 if ($config->stage == INSTALL_DOWNLOADLANG) {
355 355 $downloaderror = '';
356 356
357   -// Download and install lang component, lang dir was already created in install_init_dataroot
358   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $CFG->lang.'.zip', 'languages.md5', 'lang')) {
359   - if ($cd->install() == COMPONENT_ERROR) {
360   - if ($cd->get_error() == 'remotedownloaderror') {
361   - $a = new stdClass();
362   - $a->url = 'http://download.moodle.org/langpack/2.0/'.$config->lang.'.zip';
363   - $a->dest = $CFG->dataroot.'/lang';
364   - $downloaderror = get_string($cd->get_error(), 'error', $a);
365   - } else {
366   - $downloaderror = get_string($cd->get_error(), 'error');
367   - }
368   - } else {
369   - // install parent lang if defined
370   - if ($parentlang = get_parent_language()) {
371   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
372   - $cd->install();
373   - }
374   - }
  357 + // download and install required lang packs, the lang dir has already been created in install_init_dataroot
  358 + $installer = new lang_installer($CFG->lang);
  359 + $results = $installer->run();
  360 + foreach ($results as $langcode => $langstatus) {
  361 + if ($langstatus === lang_installer::RESULT_DOWNLOADERROR) {
  362 + $a = new stdClass();
  363 + $a->url = $installer->lang_pack_url($langcode);
  364 + $a->dest = $CFG->dataroot.'/lang';
  365 + $downloaderror = get_string('remotedownloaderror', 'error', $a);
375 366 }
376 367 }
377 368
1  lang/en/admin.php
@@ -648,6 +648,7 @@
648 648 $string['langpacks'] = 'Language packs';
649 649 $string['langpackupdated'] = 'Language pack {$a} was successfully updated';
650 650 $string['langpackupdateskipped'] = 'Update of {$a} language pack skipped';
  651 +$string['langpackuptodate'] = 'Language pack {$a} is up-to-date';
651 652 $string['langpackwillbeupdated'] = 'NOTE: Moodle will try to download updates for your language packs during the upgrade.';
652 653 $string['langrmyourself'] = 'To prevent data loss, lang.php is not able to overwrite existing file with empty content. Please, remove the file manually in order to get rid of it.';
653 654 $string['langstringcache'] = 'Cache all language strings';
264 lib/componentlib.class.php
@@ -549,3 +549,267 @@ function get_extra_md5_field() {
549 549 }
550 550
551 551 } /// End of component_installer class
  552 +
  553 +
  554 +/**
  555 + * Language packs installer
  556 + *
  557 + * This class wraps the functionality provided by {@link component_installer}
  558 + * and adds support for installing a set of language packs.
  559 + *
  560 + * Given an array of required language packs, this class fetches them all
  561 + * and installs them. It detects eventual dependencies and installs
  562 + * all parent languages, too.
  563 + *
  564 + * @copyright 2011 David Mudrak <david@moodle.com>
  565 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  566 + */
  567 +class lang_installer {
  568 +
  569 + /** lang pack was successfully downloaded and deployed */
  570 + const RESULT_INSTALLED = 'installed';
  571 + /** lang pack was up-to-date so no download was needed */
  572 + const RESULT_UPTODATE = 'uptodate';
  573 + /** there was a problem with downloading the lang pack */
  574 + const RESULT_DOWNLOADERROR = 'downloaderror';
  575 +
  576 + /** @var array of languages to install */
  577 + protected $queue = array();
  578 + /** @var string the code of language being currently installed */
  579 + protected $current;
  580 + /** @var array of languages already installed by this instance */
  581 + protected $done = array();
  582 + /** @var string this Moodle major version */
  583 + protected $version;
  584 +
  585 + /**
  586 + * Prepare the installer
  587 + *
  588 + * @todo Moodle major version is hardcoded here, should be obtained from version.php or so
  589 + * @param string|array $langcode a code of the language to install
  590 + */
  591 + public function __construct($langcode = '') {
  592 + global $CFG;
  593 +
  594 + $this->set_queue($langcode);
  595 + $this->version = '2.0';
  596 +
  597 + if (!empty($CFG->langotherroot) and $CFG->langotherroot !== $CFG->dataroot . '/lang') {
  598 + debugging('The in-built language pack installer does not support alternative location ' .
  599 + 'of languages root directory. You are supposed to install and update your language '.
  600 + 'packs on your own.');
  601 + }
  602 + }
  603 +
  604 + /**
  605 + * Sets the queue of language packs to be installed
  606 + *
  607 + * @param string|array $langcodes language code like 'cs' or a list of them
  608 + */
  609 + public function set_queue($langcodes) {
  610 + if (is_array($langcodes)) {
  611 + $this->queue = $langcodes;
  612 + } else if (!empty($langcodes)) {
  613 + $this->queue = array($langcodes);
  614 + }
  615 + }
  616 +
  617 + /**
  618 + * Runs the installer
  619 + *
  620 + * This method calls {@link self::install_language_pack} for every language in the
  621 + * queue. If a dependency is detected, the parent language is added to the queue.
  622 + *
  623 + * @return array results, array of self::RESULT_xxx constants indexed by language code
  624 + */
  625 + public function run() {
  626 +
  627 + $results = array();
  628 +
  629 + while ($this->current = array_shift($this->queue)) {
  630 +
  631 + if ($this->was_processed($this->current)) {
  632 + // do not repeat yourself
  633 + continue;
  634 + }
  635 +
  636 + if ($this->current === 'en') {
  637 + $this->mark_processed($this->current);
  638 + continue;
  639 + }
  640 +
  641 + $results[$this->current] = $this->install_language_pack($this->current);
  642 +
  643 + if (in_array($results[$this->current], array(self::RESULT_INSTALLED, self::RESULT_UPTODATE))) {
  644 + if ($parentlang = $this->get_parent_language($this->current)) {
  645 + if (!$this->is_queued($parentlang) and !$this->was_processed($parentlang)) {
  646 + $this->add_to_queue($parentlang);
  647 + }
  648 + }
  649 + }
  650 +
  651 + $this->mark_processed($this->current);
  652 + }
  653 +
  654 + return $results;
  655 + }
  656 +
  657 + /**
  658 + * Returns the URL where a given language pack can be downloaded
  659 + *
  660 + * Alternatively, if the parameter is empty, returns URL of the page with the
  661 + * list of all available language packs.
  662 + *
  663 + * @param string $langcode language code like 'cs' or empty for unknown
  664 + * @return string URL
  665 + */
  666 + public function lang_pack_url($langcode = '') {
  667 +
  668 + $baseurl = 'http://download.moodle.org/langpack/' . $this->version . '/';
  669 +
  670 + if (empty($langcode)) {
  671 + return $baseurl;
  672 + } else {
  673 + return $baseurl . $langcode . '.zip';
  674 + }
  675 + }
  676 +
  677 + /**
  678 + * Returns the list of available language packs from download.moodle.org
  679 + *
  680 + * @return array|bool false if can not download
  681 + */
  682 + public function get_remote_list_of_languages() {
  683 + $source = 'http://download.moodle.org/langpack/' . $this->version . '/languages.md5';
  684 + $availablelangs = array();
  685 +
  686 + if ($content = download_file_content($source)) {
  687 + $alllines = explode("\n", $content);
  688 + foreach($alllines as $line) {
  689 + if (!empty($line)){
  690 + $availablelangs[] = explode(',', $line);
  691 + }
  692 + }
  693 + return $availablelangs;
  694 +
  695 + } else {
  696 + return false;
  697 + }
  698 + }
  699 +
  700 + // Internal implementation /////////////////////////////////////////////////
  701 +
  702 + /**
  703 + * Adds a language pack (or a list of them) to the queue
  704 + *
  705 + * @param string|array $langcodes code of the language to install or a list of them
  706 + */
  707 + protected function add_to_queue($langcodes) {
  708 + if (is_array($langcodes)) {
  709 + $this->queue = array_merge($this->queue, $langcodes);
  710 + } else if (!empty($langcodes)) {
  711 + $this->queue[] = $langcodes;
  712 + }
  713 + }
  714 +
  715 + /**
  716 + * Checks if the given language is queued or if the queue is empty
  717 + *
  718 + * @example $installer->is_queued('es'); // is Spanish going to be installed?
  719 + * @example $installer->is_queued(); // is there a language queued?
  720 + *
  721 + * @param string $langcode language code or empty string for "any"
  722 + * @return boolean
  723 + */
  724 + protected function is_queued($langcode = '') {
  725 +
  726 + if (empty($langcode)) {
  727 + return !empty($this->queue);
  728 +
  729 + } else {
  730 + return in_array($langcode, $this->queue);
  731 + }
  732 + }
  733 +
  734 + /**
  735 + * Checks if the given language has already been processed by this instance
  736 + *
  737 + * @see self::mark_processed()
  738 + * @param string $langcode
  739 + * @return boolean
  740 + */
  741 + protected function was_processed($langcode) {
  742 + return isset($this->done[$langcode]);
  743 + }
  744 +
  745 + /**
  746 + * Mark the given language pack as processed
  747 + *
  748 + * @see self::was_processed()
  749 + * @param string $langcode
  750 + */
  751 + protected function mark_processed($langcode) {
  752 + $this->done[$langcode] = 1;
  753 + }
  754 +
  755 + /**
  756 + * Returns a parent language of the given installed language
  757 + *
  758 + * @param string $langcode
  759 + * @return string parent language's code
  760 + */
  761 + protected function get_parent_language($langcode) {
  762 + return get_parent_language($langcode);
  763 + }
  764 +
  765 + /**
  766 + * Perform the actual language pack installation
  767 + *
  768 + * @uses component_installer
  769 + * @param string $langcode
  770 + * @return int return status
  771 + */
  772 + protected function install_language_pack($langcode) {
  773 +
  774 + // initialise new component installer to process this language
  775 + $installer = new component_installer('http://download.moodle.org', 'langpack/' . $this->version,
  776 + $langcode . '.zip', 'languages.md5', 'lang');
  777 +
  778 + if (!$installer->requisitesok) {
  779 + throw new lang_installer_exception('installer_requisites_check_failed');
  780 + }
  781 +
  782 + $status = $installer->install();
  783 +
  784 + if ($status == COMPONENT_ERROR) {
  785 + if ($installer->get_error() === 'remotedownloaderror') {
  786 + return self::RESULT_DOWNLOADERROR;
  787 + } else {
  788 + throw new lang_installer_exception($installer->get_error(), $langcode);
  789 + }
  790 +
  791 + } else if ($status == COMPONENT_UPTODATE) {
  792 + return self::RESULT_UPTODATE;
  793 +
  794 + } else if ($status == COMPONENT_INSTALLED) {
  795 + return self::RESULT_INSTALLED;
  796 +
  797 + } else {
  798 + throw new lang_installer_exception('unexpected_installer_result', $status);
  799 + }
  800 + }
  801 +}
  802 +
  803 +
  804 +/**
  805 + * Exception thrown by {@link lang_installer}
  806 + *
  807 + * @copyright 2011 David Mudrak <david@moodle.com>
  808 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  809 + */
  810 +class lang_installer_exception extends moodle_exception {
  811 +
  812 + public function __construct($errorcode, $debuginfo = null) {
  813 + parent::__construct($errorcode, 'error', '', null, $debuginfo);
  814 + }
  815 +}
115 lib/simpletest/testcomponentlib.php
@@ -58,6 +58,121 @@ public function test_component_installer() {
58 58 //check if correct files were downloaded
59 59 $this->assertEqual('2af180e813dc3f446a9bb7b6af87ce24', md5_file($destpath.'/'.'test.jpg'));
60 60 $this->assertEqual('47250a973d1b88d9445f94db4ef2c97a', md5_file($destpath.'/'.'test.html'));
  61 + }
  62 +
  63 + /**
  64 + * Test the public API of the {@link lang_installer} class
  65 + */
  66 + public function test_lang_installer() {
  67 +
  68 + // test the manipulation with the download queue
  69 + $installer = new testable_lang_installer();
  70 + $this->assertFalse($installer->protected_is_queued());
  71 + $installer->protected_add_to_queue('cs');
  72 + $installer->protected_add_to_queue(array('cs', 'sk'));
  73 + $this->assertTrue($installer->protected_is_queued());
  74 + $this->assertTrue($installer->protected_is_queued('cs'));
  75 + $this->assertTrue($installer->protected_is_queued('sk'));
  76 + $this->assertFalse($installer->protected_is_queued('de_kids'));
  77 + $installer->set_queue('de_kids');
  78 + $this->assertFalse($installer->protected_is_queued('cs'));
  79 + $this->assertFalse($installer->protected_is_queued('sk'));
  80 + $this->assertFalse($installer->protected_is_queued('de'));
  81 + $this->assertFalse($installer->protected_is_queued('de_du'));
  82 + $this->assertTrue($installer->protected_is_queued('de_kids'));
  83 + $installer->set_queue(array('cs', 'de_kids'));
  84 + $this->assertTrue($installer->protected_is_queued('cs'));
  85 + $this->assertFalse($installer->protected_is_queued('sk'));
  86 + $this->assertFalse($installer->protected_is_queued('de'));
  87 + $this->assertFalse($installer->protected_is_queued('de_du'));
  88 + $this->assertTrue($installer->protected_is_queued('de_kids'));
  89 + $installer->set_queue(array());
  90 + $this->assertFalse($installer->protected_is_queued());
  91 + unset($installer);
  92 +
  93 + // install a set of lang packs
  94 + $installer = new testable_lang_installer(array('cs', 'de_kids', 'xx'));
  95 + $result = $installer->run();
  96 + $this->assertEqual($result['cs'], lang_installer::RESULT_UPTODATE);
  97 + $this->assertEqual($result['de_kids'], lang_installer::RESULT_INSTALLED);
  98 + $this->assertEqual($result['xx'], lang_installer::RESULT_DOWNLOADERROR);
  99 + // the following two were automatically added to the queue
  100 + $this->assertEqual($result['de_du'], lang_installer::RESULT_INSTALLED);
  101 + $this->assertEqual($result['de'], lang_installer::RESULT_UPTODATE);
  102 +
  103 + // exception throwing
  104 + $installer = new testable_lang_installer(array('yy'));
  105 + $this->expectException('lang_installer_exception');
  106 + $installer->run();
  107 + }
  108 +}
  109 +
  110 +/**
  111 + * Testable lang_installer subclass that does not actually install anything
  112 + * and provides access to the protected methods of the parent class
  113 + *
  114 + * @copyright 2011 David Mudrak <david@moodle.com>
  115 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  116 + */
  117 +class testable_lang_installer extends lang_installer {
  118 +
  119 + /**
  120 + * @see parent::is_queued()
  121 + */
  122 + public function protected_is_queued($langcode = '') {
  123 + return $this->is_queued($langcode);
  124 + }
  125 +
  126 + /**
  127 + * @see parent::add_to_queue()
  128 + */
  129 + public function protected_add_to_queue($langcodes) {
  130 + return $this->add_to_queue($langcodes);
  131 + }
  132 +
  133 + /**
  134 + * Simulate lang pack installation via component_installer
  135 + *
  136 + * Language packages 'de_du' and 'de_kids' reported as installed
  137 + * Language packages 'cs' and 'de' reported as up-to-date
  138 + * Language package 'xx' returns download error
  139 + * All other language packages will throw an unknown exception
  140 + *
  141 + * @see parent::install_language_pack()
  142 + */
  143 + protected function install_language_pack($langcode) {
  144 +
  145 + switch ($langcode) {
  146 + case 'de_du':
  147 + case 'de_kids':
  148 + return self::RESULT_INSTALLED;
  149 +
  150 + case 'cs':
  151 + case 'de':
  152 + return self::RESULT_UPTODATE;
  153 +
  154 + case 'xx':
  155 + return self::RESULT_DOWNLOADERROR;
  156 +
  157 + default:
  158 + throw new lang_installer_exception('testing-unknown-exception', $langcode);
  159 + }
  160 + }
  161 +
  162 + /**
  163 + * Simulate detection of parent languge
  164 + *
  165 + * @see parent::get_parent_language()
  166 + */
  167 + protected function get_parent_language($langcode) {
61 168
  169 + switch ($langcode) {
  170 + case 'de_kids':
  171 + return 'de_du';
  172 + case 'de_du':
  173 + return 'de';
  174 + default:
  175 + return '';
  176 + }
62 177 }
63 178 }
26 lib/upgradelib.php
@@ -1259,11 +1259,10 @@ function upgrade_init_javascript() {
1259 1259 $PAGE->requires->js_init_code($js);
1260 1260 }
1261 1261
1262   -
1263 1262 /**
1264 1263 * Try to upgrade the given language pack (or current language)
1265 1264 *
1266   - * @todo hardcoded Moodle version here - shall be provided by version.php or similar script
  1265 + * @param string $lang the code of the language to update, defaults to the current language
1267 1266 */
1268 1267 function upgrade_language_pack($lang='') {
1269 1268 global $CFG, $OUTPUT;
@@ -1286,16 +1285,19 @@ function upgrade_language_pack($lang='') {
1286 1285
1287 1286 require_once($CFG->libdir.'/componentlib.class.php');
1288 1287
1289   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $lang.'.zip', 'languages.md5', 'lang')) {
1290   - $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
1291   -
1292   - if ($status == COMPONENT_INSTALLED) {
1293   - if ($parentlang = get_parent_language($lang)) {
1294   - if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
1295   - $cd->install();
1296   - }
1297   - }
1298   - echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
  1288 + $installer = new lang_installer($pack);
  1289 + $results = $installer->run();
  1290 + foreach ($results as $langcode => $langstatus) {
  1291 + switch ($langstatus) {
  1292 + case lang_installer::RESULT_DOWNLOADERROR:
  1293 + echo $OUTPUT->notification($pack . '.zip');
  1294 + break;
  1295 + case lang_installer::RESULT_INSTALLED:
  1296 + echo $OUTPUT->notification(get_string('langpackinstalled', 'admin', $langcode), 'notifysuccess');
  1297 + break;
  1298 + case lang_installer::RESULT_UPTODATE:
  1299 + echo $OUTPUT->notification(get_string('langpackuptodate', 'admin', $langcode), 'notifysuccess');
  1300 + break;
1299 1301 }
1300 1302 }
1301 1303

0 comments on commit 3d3ead8

Please sign in to comment.
Something went wrong with that request. Please try again.