Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 402 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  // Filter the uninstall language list.
  47  // If the list contains a language which is not installed, it is replaced with an empty string.
  48  // When we try to uninstall an empty string, we uninstall every language.
  49  $uninstalllang = array_filter($uninstalllang, function($lang) {
  50      return !empty($lang);
  51  });
  52  
  53  if ($purgecaches) {
  54      require_sesskey();
  55      get_string_manager()->reset_caches();
  56      redirect($PAGE->url);
  57  }
  58  
  59  if (!empty($CFG->skiplangupgrade)) {
  60      echo $OUTPUT->header();
  61      echo $OUTPUT->box(get_string('langimportdisabled', 'tool_langimport'));
  62      echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('purgecaches' => 1)), get_string('purgestringcaches', 'tool_langimport'));
  63      echo $OUTPUT->footer();
  64      die;
  65  }
  66  
  67  define('INSTALLATION_OF_SELECTED_LANG', 2);
  68  define('DELETION_OF_SELECTED_LANG', 4);
  69  define('UPDATE_ALL_LANG', 5);
  70  
  71  get_string_manager()->reset_caches();
  72  
  73  $controller = new tool_langimport\controller();
  74  
  75  if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($pack)) {
  76      if (is_array($pack) && count($pack) > 1) {
  77          // Installing multiple languages can take a while - perform it asynchronously in the background.
  78          $controller->schedule_languagepacks_installation($pack);
  79          $controller->redirect($PAGE->url);
  80      } else {
  81          // Single language pack to be installed synchronously. It should be reasonably quick and can be used for debugging, too.
  82          core_php_time_limit::raise();
  83          $controller->install_languagepacks($pack);
  84          $controller->redirect($PAGE->url);
  85      }
  86  }
  87  
  88  if ($mode == DELETION_OF_SELECTED_LANG and (!empty($uninstalllang) or !empty($confirmtounistall))) {
  89      // Actually deleting languages, languages to delete are passed as GET parameter as string
  90      // ...need to populate them to array.
  91      if (empty($uninstalllang)) {
  92          $uninstalllang = explode('/', $confirmtounistall);
  93      }
  94  
  95      if (in_array('en', $uninstalllang)) {
  96          // TODO.
  97          $controller->errors[] = get_string('noenglishuninstall', 'tool_langimport');
  98  
  99      } else if (empty($confirmtounistall) and confirm_sesskey()) { // User chose langs to be deleted, show confirmation.
 100          echo $OUTPUT->header();
 101          echo $OUTPUT->confirm(get_string('uninstallconfirm', 'tool_langimport', implode(', ', $uninstalllang)),
 102              new moodle_url($PAGE->url, array(
 103                  'mode' => DELETION_OF_SELECTED_LANG,
 104                  'confirmtouninstall' => implode('/', $uninstalllang),
 105              )), $PAGE->url);
 106          echo $OUTPUT->footer();
 107          die;
 108  
 109      } else if (confirm_sesskey()) {   // Deleting languages.
 110          foreach ($uninstalllang as $ulang) {
 111              $controller->uninstall_language($ulang);
 112          }
 113          $controller->redirect($PAGE->url);
 114      }
 115  }
 116  
 117  if ($mode == UPDATE_ALL_LANG) {
 118      core_php_time_limit::raise();
 119      $controller->update_all_installed_languages();
 120  }
 121  get_string_manager()->reset_caches();
 122  
 123  $PAGE->set_primary_active_tab('siteadminnode');
 124  
 125  echo $OUTPUT->header();
 126  echo $OUTPUT->heading(get_string('langimport', 'tool_langimport'));
 127  
 128  $installedlangs = get_string_manager()->get_list_of_translations(true);
 129  $locale = new \tool_langimport\locale();
 130  
 131  
 132  if ($availablelangs = $controller->availablelangs) {
 133      $remote = true;
 134  } else {
 135      $remote = false;
 136      $availablelangs = array();
 137      $a = [
 138          'src' => $controller->lang_pack_url(),
 139          'dest' => $CFG->dataroot.'/lang/',
 140      ];
 141      $errormessage = get_string('downloadnotavailable', 'tool_langimport', $a);
 142      \core\notification::error($errormessage);
 143  }
 144  
 145  $missinglocales = '';
 146  $missingparents = array();
 147  foreach ($installedlangs as $installedlang => $langpackname) {
 148      // Check locale availability.
 149      if (!$locale->check_locale_availability($installedlang)) {
 150          $missinglocales .= '<li>'.$langpackname.'</li>';
 151      }
 152  
 153      // This aligns the name of the language to match the available languages using
 154      // both the name for the language and the localized name for the language.
 155      $alang = array_filter($availablelangs, function($k) use ($installedlang) {
 156          return $k[0] == $installedlang;
 157      });
 158      $alang = array_pop($alang);
 159      if (!empty($alang[0]) and trim($alang[0]) !== 'en') {
 160          $installedlangs[$installedlang] = $alang[2] . ' &lrm;(' . $alang[0] . ')&lrm;';
 161      }
 162  
 163      $parent = get_parent_language($installedlang);
 164      if (empty($parent)) {
 165          continue;
 166      }
 167      if (!isset($installedlangs[$parent])) {
 168          $missingparents[$installedlang] = $parent;
 169      }
 170  }
 171  
 172  if (!empty($missinglocales)) {
 173      // There is at least one missing locale.
 174      $a = new stdClass();
 175      $a->globallocale = moodle_getlocale();
 176      $a->missinglocales = $missinglocales;
 177      $controller->errors[] = get_string('langunsupported', 'tool_langimport', $a);
 178  }
 179  
 180  if ($controller->info) {
 181      $info = implode('<br />', $controller->info);
 182      \core\notification::success($info);
 183  }
 184  
 185  if ($controller->errors) {
 186      $info = implode('<br />', $controller->errors);
 187      \core\notification::error($info);
 188  }
 189  
 190  // Inform about pending language packs installations.
 191  foreach (\core\task\manager::get_adhoc_tasks('\tool_langimport\task\install_langpacks') as $installtask) {
 192      $installtaskdata = $installtask->get_custom_data();
 193  
 194      if (!empty($installtaskdata->langs)) {
 195          \core\notification::info(get_string('installpending', 'tool_langimport', implode(', ', $installtaskdata->langs)));
 196      }
 197  }
 198  
 199  if ($missingparents) {
 200      foreach ($missingparents as $l => $parent) {
 201          $a = new stdClass();
 202          $a->lang   = $installedlangs[$l];
 203          $a->parent = $parent;
 204          foreach ($availablelangs as $alang) {
 205              if ($alang[0] == $parent) {
 206                  $shortlang = $alang[0];
 207                  $a->parent = $alang[2].' ('.$shortlang.')';
 208              }
 209          }
 210          $info = get_string('missinglangparent', 'tool_langimport', $a);
 211          \core\notification::error($info);
 212      }
 213  }
 214  
 215  $uninstallurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => DELETION_OF_SELECTED_LANG));
 216  $updateurl = null;
 217  if ($remote) {
 218      $updateurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => UPDATE_ALL_LANG));
 219  }
 220  $installurl = new moodle_url('/admin/tool/langimport/index.php', array('mode' => INSTALLATION_OF_SELECTED_LANG));
 221  
 222  // List of available languages.
 223  $options = array();
 224  foreach ($availablelangs as $alang) {
 225      if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !$controller->is_installed_lang($alang[0], $alang[1])) {
 226          $options[$alang[0]] = $alang[2].' &lrm;('.$alang[0].')&lrm;';
 227      }
 228  }
 229  
 230  $renderable = new \tool_langimport\output\langimport_page($installedlangs, $options, $uninstallurl, $updateurl, $installurl);
 231  $output = $PAGE->get_renderer('tool_langimport');
 232  echo $output->render($renderable);
 233  
 234  $PAGE->requires->strings_for_js(array('uninstallconfirm', 'uninstall', 'selectlangs', 'noenglishuninstall'),
 235                                  'tool_langimport');
 236  $PAGE->requires->yui_module('moodle-core-languninstallconfirm',
 237                              'Y.M.core.languninstallconfirm.init',
 238                               array(array('uninstallUrl' => $uninstallurl->out()))
 239                              );
 240  echo $OUTPUT->footer();