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   * CLI customlang import tool.
  19   *
  20   * @package    tool_customlang
  21   * @copyright  2020 Ferran Recio <ferran@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use tool_customlang\local\importer;
  26  use core\output\notification;
  27  
  28  define('CLI_SCRIPT', true);
  29  
  30  require(__DIR__ . '/../../../../config.php');
  31  require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
  32  require_once("$CFG->libdir/clilib.php");
  33  
  34  $usage =
  35  "Import lang customization.
  36  
  37  It can get a single file or a folder.
  38  If no lang is provided it will try to infere from the filename
  39  
  40  Options:
  41  --lang                  The target language (will get from filename if not provided)
  42  --source=path           File or folder of the custom lang files (zip or php files)
  43  --mode                  What string should be imported. Options are:
  44                              - all: all string will be imported (default)
  45                              - new: only string with no previous customisation
  46                              - update: only strings already modified
  47  --checkin               Save strings to the language pack
  48  -h, --help              Print out this help
  49  
  50  Examples:
  51  \$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --lang=en --source=customlangs.zip
  52  
  53  \$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --source=/tmp/customlangs --checkin
  54  
  55  \$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --lang=en --source=/tmp/customlangs
  56  
  57  ";
  58  
  59  list($options, $unrecognized) = cli_get_params(
  60      [
  61          'help' => false,
  62          'lang' => false,
  63          'source' => false,
  64          'mode' => 'all',
  65          'checkin' => false,
  66      ],
  67      ['h' => 'help']
  68  );
  69  
  70  if ($unrecognized) {
  71      $unrecognized = implode("\n  ", $unrecognized);
  72      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  73  }
  74  
  75  if ($options['help']) {
  76      cli_write($usage);
  77      exit(0);
  78  }
  79  
  80  $source = $options['source'] ?? null;
  81  $lang = $options['lang'] ?? null;
  82  $modeparam = $options['mode'] ?? 'all';
  83  $checkin = $options['checkin'] ?? false;
  84  
  85  $modes = [
  86      'all' => importer::IMPORTALL,
  87      'update' => importer::IMPORTUPDATE,
  88      'new' => importer::IMPORTNEW,
  89  ];
  90  if (!isset($modes[$modeparam])) {
  91      cli_error(get_string('climissingmode', 'tool_customlang'));
  92  }
  93  $mode = $modes[$modeparam];
  94  
  95  if (empty($source)) {
  96      $source = $CFG->dataroot.'/temp/customlang';
  97  }
  98  
  99  if (!file_exists($source)) {
 100      cli_error(get_string('climissingsource', 'tool_customlang'));
 101  }
 102  
 103  // Emulate normal session - we use admin account by default.
 104  cron_setup_user();
 105  
 106  // Get the file list.
 107  $files = [];
 108  $langfiles = [];
 109  
 110  if (is_file($source)) {
 111      $files[] = $source;
 112  }
 113  if (is_dir($source)) {
 114      $filelist = glob("$source/*");
 115      foreach ($filelist as $filename) {
 116          $files[] = "$filename";
 117      }
 118  }
 119  
 120  $countfiles = 0;
 121  foreach ($files as $filepath) {
 122      // Try to get the lang.
 123      $filelang = $lang;
 124      // Get component from filename.
 125      $pathparts = pathinfo($filepath);
 126      $filename = $pathparts['filename'];
 127      $extension = $pathparts['extension'];
 128      if ($extension == 'zip') {
 129          if (!$filelang) {
 130              // Try to get the lang from the filename.
 131              if (strrpos($filename, 'customlang_') === 0) {
 132                  $parts = explode('_', $filename);
 133                  if (!empty($parts[1])) {
 134                      $filelang = $parts[1];
 135                  }
 136              }
 137          }
 138      } else if ($extension != 'php') {
 139          // Ignore any other file extension.
 140          continue;
 141      }
 142      if (empty($filelang)) {
 143          cli_error(get_string('climissinglang', 'tool_customlang'));
 144      }
 145      if (!isset($langfiles[$filelang])) {
 146          $langfiles[$filelang] = [];
 147      }
 148      $langfiles[$filelang][] = $filepath;
 149      $countfiles ++;
 150  }
 151  
 152  if (!$countfiles) {
 153      cli_error(get_string('climissingfiles', 'tool_customlang'));
 154  }
 155  
 156  foreach ($langfiles as $lng => $files) {
 157      $importer = new importer($lng, $mode);
 158      $storedfiles = [];
 159      $fs = get_file_storage();
 160  
 161      cli_heading(get_string('clifiles', 'tool_customlang', $lng));
 162  
 163      foreach ($files as $file) {
 164          // Generate a valid stored_file from this file.
 165          $record = (object)[
 166              'filearea' => 'draft',
 167              'component' => 'user',
 168              'filepath' => '/',
 169              'itemid'   => file_get_unused_draft_itemid(),
 170              'license'  => $CFG->sitedefaultlicense,
 171              'author'   => '',
 172              'filename' => clean_param(basename($file), PARAM_FILE),
 173              'contextid' => \context_user::instance($USER->id)->id,
 174              'userid' => $USER->id,
 175          ];
 176          cli_writeln($file);
 177          $storedfiles[] = $fs->create_file_from_pathname($record, $file);
 178      }
 179      cli_writeln("");
 180  
 181      // Import files.
 182      cli_heading(get_string('cliimporting', 'tool_customlang', $modeparam));
 183      $importer->import($storedfiles);
 184      // Display logs.
 185      $log = $importer->get_log();
 186      if (empty($log)) {
 187          cli_problem(get_string('clinolog', 'tool_customlang', $lng));
 188      }
 189      foreach ($log as $message) {
 190          if ($message->errorlevel == notification::NOTIFY_ERROR) {
 191              cli_problem($message->get_message());
 192          } else {
 193              cli_writeln($message->get_message());
 194          }
 195      }
 196      // Do the checkin if necessary.
 197      if ($checkin) {
 198          tool_customlang_utils::checkin($lng);
 199          cli_writeln(get_string('savecheckin', 'tool_customlang'));
 200      }
 201      cli_writeln("");
 202  }
 203  
 204  exit(0);