Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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      core_php_time_limit::raise();
  70      $controller->install_languagepacks($pack);
  71  }
  72  
  73  if ($mode == DELETION_OF_SELECTED_LANG and (!empty($uninstalllang) or !empty($confirmtounistall))) {
  74      // Actually deleting languages, languages to delete are passed as GET parameter as string
  75      // ...need to populate them to array.
  76      if (empty($uninstalllang)) {
  77          $uninstalllang = explode('/', $confirmtounistall);
  78      }
  79  
  80      if (in_array('en', $uninstalllang)) {
  81          // TODO.
  82          $controller->errors[] = get_string('noenglishuninstall', 'tool_langimport');
  83  
  84      } else if (empty($confirmtounistall) and confirm_sesskey()) { // User chose langs to be deleted, show confirmation.
  85          echo $OUTPUT->header();
  86          echo $OUTPUT->confirm(get_string('uninstallconfirm', 'tool_langimport', implode(', ', $uninstalllang)),
  87              new moodle_url($PAGE->url, array(
  88                  'mode' => DELETION_OF_SELECTED_LANG,
  89                  'confirmtouninstall' => implode('/', $uninstalllang),
  90              )), $PAGE->url);
  91          echo $OUTPUT->footer();
  92          die;
  93  
  94      } else if (confirm_sesskey()) {   // Deleting languages.
  95          foreach ($uninstalllang as $ulang) {
  96              $controller->uninstall_language($ulang);
  97          }
  98  
  99      }
 100  }
 101  
 102  if ($mode == UPDATE_ALL_LANG) {
 103      core_php_time_limit::raise();
 104      $controller->update_all_installed_languages();
 105  }
 106  get_string_manager()->reset_caches();
 107  
 108  echo $OUTPUT->header();
 109  echo $OUTPUT->heading(get_string('langimport', 'tool_langimport'));
 110  
 111  $installedlangs = get_string_manager()->get_list_of_translations(true);
 112  $locale = new \tool_langimport\locale();
 113  
 114  $missinglocales = '';
 115  $missingparents = array();
 116  foreach ($installedlangs as $installedlang => $langpackname) {
 117      // Check locale availability.
 118      if (!$locale->check_locale_availability($installedlang)) {
 119          $missinglocales .= '<li>'.$langpackname.'</li>';
 120      }
 121  
 122      $parent = get_parent_language($installedlang);
 123      if (empty($parent)) {
 124          continue;
 125      }
 126      if (!isset($installedlangs[$parent])) {
 127          $missingparents[$installedlang] = $parent;
 128      }
 129  }
 130  
 131  if (!empty($missinglocales)) {
 132      // There is at least one missing locale.
 133      $a = new stdClass();
 134      $a->globallocale = moodle_getlocale();
 135      $a->missinglocales = $missinglocales;
 136      $controller->errors[] = get_string('langunsupported', 'tool_langimport', $a);
 137  }
 138  
 139  if ($availablelangs = $controller->availablelangs) {
 140      $remote = true;
 141  } else {
 142      $remote = false;
 143      $availablelangs = array();
 144      $a = [
 145          'src' => $controller->lang_pack_url(),
 146          'dest' => $CFG->dataroot.'/lang/',
 147      ];
 148      $errormessage = get_string('downloadnotavailable', 'tool_langimport', $a);
 149      \core\notification::error($errormessage);
 150  }
 151  
 152  if ($controller->info) {
 153      $info = implode('<br />', $controller->info);
 154      \core\notification::success($info);
 155  }
 156  
 157  if ($controller->errors) {
 158      $info = implode('<br />', $controller->errors);
 159      \core\notification::error($info);
 160  }
 161  
 162  if ($missingparents) {
 163      foreach ($missingparents as $l => $parent) {
 164          $a = new stdClass();
 165          $a->lang   = $installedlangs[$l];
 166          $a->parent = $parent;
 167          foreach ($availablelangs as $alang) {
 168              if ($alang[0] == $parent) {
 169                  $shortlang = $alang[0];
 170                  $a->parent = $alang[2].' ('.$shortlang.')';
 171              }
 172          }
 173          $info = get_string('missinglangparent', 'tool_langimport', $a);
 174          \core\notification::error($info);
 175      }
 176  }
 177  
 178  $uninstallurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => DELETION_OF_SELECTED_LANG));
 179  $updateurl = null;
 180  if ($remote) {
 181      $updateurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => UPDATE_ALL_LANG));
 182  }
 183  $installurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => INSTALLATION_OF_SELECTED_LANG));
 184  
 185  // List of available languages.
 186  $options = array();
 187  foreach ($availablelangs as $alang) {
 188      if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !$controller->is_installed_lang($alang[0], $alang[1])) {
 189          $options[$alang[0]] = $alang[2].' &lrm;('.$alang[0].')&lrm;';
 190      }
 191  }
 192  
 193  $renderable = new \tool_langimport\output\langimport_page($installedlangs, $options, $uninstallurl, $updateurl, $installurl);
 194  $output = $PAGE->get_renderer('tool_langimport');
 195  echo $output->render($renderable);
 196  
 197  $PAGE->requires->strings_for_js(array('uninstallconfirm', 'uninstall', 'selectlangs', 'noenglishuninstall'),
 198                                  'tool_langimport');
 199  $PAGE->requires->yui_module('moodle-core-languninstallconfirm',
 200                              'Y.M.core.languninstallconfirm.init',
 201                               array(array('uninstallUrl' => $uninstallurl->out()))
 202                              );
 203  echo $OUTPUT->footer();