Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Fetches language packages from download.moodle.org server
  19   *
  20   * Language packages are available at https://download.moodle.org/langpack/
  21   * in ZIP format together with a file languages.md5 containing their hashes
  22   * and meta info.
  23   * Locally, language packs are saved into $CFG->dataroot/lang/
  24   *
  25   * @package    tool
  26   * @subpackage langimport
  27   * @copyright  2005 Yu Zhang
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  
  31  require(__DIR__.'/../../../config.php');
  32  require_once($CFG->libdir.'/adminlib.php');
  33  
  34  admin_externalpage_setup('toollangimport');
  35  
  36  if (empty($CFG->langotherroot)) {
  37      throw new moodle_exception('missingcfglangotherroot', 'tool_langimport');
  38  }
  39  
  40  $mode               = optional_param('mode', 0, PARAM_INT);              // action
  41  $pack               = optional_param_array('pack', array(), PARAM_SAFEDIR);    // pack to install
  42  $uninstalllang      = optional_param_array('uninstalllang', array(), PARAM_LANG);// installed pack to uninstall
  43  $confirmtounistall  = optional_param('confirmtouninstall', '', PARAM_SAFEPATH);  // uninstallation confirmation
  44  $purgecaches        = optional_param('purgecaches', false, PARAM_BOOL);  // explicit caches reset
  45  
  46  if ($purgecaches) {
  47      require_sesskey();
  48      get_string_manager()->reset_caches();
  49      redirect($PAGE->url);
  50  }
  51  
  52  if (!empty($CFG->skiplangupgrade)) {
  53      echo $OUTPUT->header();
  54      echo $OUTPUT->box(get_string('langimportdisabled', 'tool_langimport'));
  55      echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('purgecaches' => 1)), get_string('purgestringcaches', 'tool_langimport'));
  56      echo $OUTPUT->footer();
  57      die;
  58  }
  59  
  60  define('INSTALLATION_OF_SELECTED_LANG', 2);
  61  define('DELETION_OF_SELECTED_LANG', 4);
  62  define('UPDATE_ALL_LANG', 5);
  63  
  64  get_string_manager()->reset_caches();
  65  
  66  $controller = new tool_langimport\controller();
  67  
  68  if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($pack)) {
  69      if (is_array($pack) && count($pack) > 1) {
  70          // Installing multiple languages can take a while - perform it asynchronously in the background.
  71          $controller->schedule_languagepacks_installation($pack);
  72  
  73      } else {
  74          // Single language pack to be installed synchronously. It should be reasonably quick and can be used for debugging, too.
  75          core_php_time_limit::raise();
  76          $controller->install_languagepacks($pack);
  77      }
  78  }
  79  
  80  if ($mode == DELETION_OF_SELECTED_LANG and (!empty($uninstalllang) or !empty($confirmtounistall))) {
  81      // Actually deleting languages, languages to delete are passed as GET parameter as string
  82      // ...need to populate them to array.
  83      if (empty($uninstalllang)) {
  84          $uninstalllang = explode('/', $confirmtounistall);
  85      }
  86  
  87      if (in_array('en', $uninstalllang)) {
  88          // TODO.
  89          $controller->errors[] = get_string('noenglishuninstall', 'tool_langimport');
  90  
  91      } else if (empty($confirmtounistall) and confirm_sesskey()) { // User chose langs to be deleted, show confirmation.
  92          echo $OUTPUT->header();
  93          echo $OUTPUT->confirm(get_string('uninstallconfirm', 'tool_langimport', implode(', ', $uninstalllang)),
  94              new moodle_url($PAGE->url, array(
  95                  'mode' => DELETION_OF_SELECTED_LANG,
  96                  'confirmtouninstall' => implode('/', $uninstalllang),
  97              )), $PAGE->url);
  98          echo $OUTPUT->footer();
  99          die;
 100  
 101      } else if (confirm_sesskey()) {   // Deleting languages.
 102          foreach ($uninstalllang as $ulang) {
 103              $controller->uninstall_language($ulang);
 104          }
 105  
 106      }
 107  }
 108  
 109  if ($mode == UPDATE_ALL_LANG) {
 110      core_php_time_limit::raise();
 111      $controller->update_all_installed_languages();
 112  }
 113  get_string_manager()->reset_caches();
 114  
 115  echo $OUTPUT->header();
 116  echo $OUTPUT->heading(get_string('langimport', 'tool_langimport'));
 117  
 118  $installedlangs = get_string_manager()->get_list_of_translations(true);
 119  $locale = new \tool_langimport\locale();
 120  
 121  
 122  if ($availablelangs = $controller->availablelangs) {
 123      $remote = true;
 124  } else {
 125      $remote = false;
 126      $availablelangs = array();
 127      $a = [
 128          'src' => $controller->lang_pack_url(),
 129          'dest' => $CFG->dataroot.'/lang/',
 130      ];
 131      $errormessage = get_string('downloadnotavailable', 'tool_langimport', $a);
 132      \core\notification::error($errormessage);
 133  }
 134  
 135  $missinglocales = '';
 136  $missingparents = array();
 137  foreach ($installedlangs as $installedlang => $langpackname) {
 138      // Check locale availability.
 139      if (!$locale->check_locale_availability($installedlang)) {
 140          $missinglocales .= '<li>'.$langpackname.'</li>';
 141      }
 142  
 143      // This aligns the name of the language to match the available languages using
 144      // both the name for the language and the localized name for the language.
 145      $alang = array_filter($availablelangs, function($k) use ($installedlang) {
 146          return $k[0] == $installedlang;
 147      });
 148      $alang = array_pop($alang);
 149      if (!empty($alang[0]) and trim($alang[0]) !== 'en') {
 150          $installedlangs[$installedlang] = $alang[2] . ' &lrm;(' . $alang[0] . ')&lrm;';
 151      }
 152  
 153      $parent = get_parent_language($installedlang);
 154      if (empty($parent)) {
 155          continue;
 156      }
 157      if (!isset($installedlangs[$parent])) {
 158          $missingparents[$installedlang] = $parent;
 159      }
 160  }
 161  
 162  if (!empty($missinglocales)) {
 163      // There is at least one missing locale.
 164      $a = new stdClass();
 165      $a->globallocale = moodle_getlocale();
 166      $a->missinglocales = $missinglocales;
 167      $controller->errors[] = get_string('langunsupported', 'tool_langimport', $a);
 168  }
 169  
 170  if ($controller->info) {
 171      $info = implode('<br />', $controller->info);
 172      \core\notification::success($info);
 173  }
 174  
 175  if ($controller->errors) {
 176      $info = implode('<br />', $controller->errors);
 177      \core\notification::error($info);
 178  }
 179  
 180  // Inform about pending language packs installations.
 181  foreach (\core\task\manager::get_adhoc_tasks('\tool_langimport\task\install_langpacks') as $installtask) {
 182      $installtaskdata = $installtask->get_custom_data();
 183  
 184      if (!empty($installtaskdata->langs)) {
 185          \core\notification::info(get_string('installpending', 'tool_langimport', implode(', ', $installtaskdata->langs)));
 186      }
 187  }
 188  
 189  if ($missingparents) {
 190      foreach ($missingparents as $l => $parent) {
 191          $a = new stdClass();
 192          $a->lang   = $installedlangs[$l];
 193          $a->parent = $parent;
 194          foreach ($availablelangs as $alang) {
 195              if ($alang[0] == $parent) {
 196                  $shortlang = $alang[0];
 197                  $a->parent = $alang[2].' ('.$shortlang.')';
 198              }
 199          }
 200          $info = get_string('missinglangparent', 'tool_langimport', $a);
 201          \core\notification::error($info);
 202      }
 203  }
 204  
 205  $uninstallurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => DELETION_OF_SELECTED_LANG));
 206  $updateurl = null;
 207  if ($remote) {
 208      $updateurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => UPDATE_ALL_LANG));
 209  }
 210  $installurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => INSTALLATION_OF_SELECTED_LANG));
 211  
 212  // List of available languages.
 213  $options = array();
 214  foreach ($availablelangs as $alang) {
 215      if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !$controller->is_installed_lang($alang[0], $alang[1])) {
 216          $options[$alang[0]] = $alang[2].' &lrm;('.$alang[0].')&lrm;';
 217      }
 218  }
 219  
 220  $renderable = new \tool_langimport\output\langimport_page($installedlangs, $options, $uninstallurl, $updateurl, $installurl);
 221  $output = $PAGE->get_renderer('tool_langimport');
 222  echo $output->render($renderable);
 223  
 224  $PAGE->requires->strings_for_js(array('uninstallconfirm', 'uninstall', 'selectlangs', 'noenglishuninstall'),
 225                                  'tool_langimport');
 226  $PAGE->requires->yui_module('moodle-core-languninstallconfirm',
 227                              'Y.M.core.languninstallconfirm.init',
 228                               array(array('uninstallUrl' => $uninstallurl->out()))
 229                              );
 230  echo $OUTPUT->footer();