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.
   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   * This script imports TinyMCE lang strings into Moodle English lang pack.
  19   *
  20   * @package    editor_tinymce
  21   * @copyright  2009 Petr Skoda (http://skodak.org)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('CLI_SCRIPT', true);
  26  
  27  require __DIR__ . '/../../../../config.php';
  28  
  29  if (!$CFG->debugdeveloper) {
  30      die('Only for developers!!!!!');
  31  }
  32  
  33  // Current strings in our lang pack.
  34  $old_strings = editor_tinymce_get_all_strings();
  35  ksort($old_strings);
  36  
  37  // Upstream strings.
  38  $parsed = editor_tinymce_parse_js_files();
  39  ksort($parsed);
  40  
  41  // Our modifications and upstream changes in existing strings.
  42  $tweaked = array();
  43  
  44  // Detect changes and new additions - ignore case difference, no UTF-8 here.
  45  foreach ($parsed as $key=>$value) {
  46      if (array_key_exists($key, $old_strings)) {
  47          $oldvalue = $old_strings[$key];
  48          if (strtolower($oldvalue) === strtolower($value)) {
  49              $parsed[$key] = $oldvalue;
  50          } else {
  51              $tweaked[$key] = $oldvalue;
  52          }
  53          unset($old_strings[$key]);
  54      }
  55  }
  56  
  57  if (!$handle = fopen("$CFG->dirroot/lib/editor/tinymce/lang/en/editor_tinymce.php", 'w')) {
  58       echo "Cannot write to $filename !!";
  59       exit(1);
  60  }
  61  
  62  $header = <<<EOT
  63  <?php
  64  // This file is part of Moodle - http://moodle.org/
  65  //
  66  // Moodle is free software: you can redistribute it and/or modify
  67  // it under the terms of the GNU General Public License as published by
  68  // the Free Software Foundation, either version 3 of the License, or
  69  // (at your option) any later version.
  70  //
  71  // Moodle is distributed in the hope that it will be useful,
  72  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  73  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  74  // GNU General Public License for more details.
  75  //
  76  // You should have received a copy of the GNU General Public License
  77  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  78  
  79  /**
  80   * Strings for component 'editor_tinymce', language 'en'.
  81   *
  82   * Note: use editor/tinymce/extra/tools/update_lang_files.php script to import strings from upstream JS lang files.
  83   *
  84   * @package    editor_tinymce
  85   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  86   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  87   */
  88  
  89  EOT;
  90  
  91  fwrite($handle, $header);
  92  
  93  fwrite($handle, "\n\n//== Custom Moodle strings that are not part of upstream TinyMCE ==\n");
  94  foreach ($old_strings as $key=>$value) {
  95      fwrite($handle, editor_tinymce_encode_stringline($key, $value));
  96  }
  97  
  98  fwrite($handle, "\n\n// == TinyMCE upstream lang strings from all standard upstream plugins ==\n");
  99  foreach ($parsed as $key=>$value) {
 100      fwrite($handle, editor_tinymce_encode_stringline($key, $value, isset($tweaked[$key])));
 101  }
 102  
 103  if ($tweaked) {
 104      fwrite($handle, "\n\n// == Our modifications or upstream changes ==\n");
 105      foreach ($tweaked as $key=>$value) {
 106          fwrite($handle, editor_tinymce_encode_stringline($key, $value));
 107      }
 108  }
 109  
 110  fclose($handle);
 111  
 112  get_string_manager()->reset_caches();
 113  die("\nFinished update of EN lang pack (other langs have to be imported via AMOS)\n\n");
 114  
 115  
 116  
 117  /// ============ Utility functions ========================
 118  
 119  function editor_tinymce_encode_stringline($key, $value, $commentedout=false) {
 120      $return = "\$string['$key'] = ".var_export($value, true).";";
 121      if ($commentedout) {
 122          $return = "/* $return */";
 123      }
 124      return $return."\n";
 125  }
 126  
 127  function editor_tinymce_get_all_strings() {
 128      $sm = get_string_manager();
 129      return $sm->load_component_strings('editor_tinymce', 'en', true, true);
 130  }
 131  
 132  function editor_tinymce_parse_js_files() {
 133      global $CFG;
 134  
 135      require_once("$CFG->libdir/editorlib.php");
 136      $editor = get_texteditor('tinymce');
 137      $basedir = "$CFG->libdir/editor/tinymce/tiny_mce/$editor->version";
 138  
 139      $files = array();
 140      $strings = array();
 141  
 142      $files['simple'] = "$basedir/themes/simple/langs/en.js";
 143      $files['advanced'] = "$basedir/themes/advanced/langs/en.js";
 144      $files['advanced_dlg'] = "$basedir/themes/advanced/langs/en_dlg.js";
 145  
 146      $items = new DirectoryIterator("$basedir/plugins/");
 147      foreach ($items as $item) {
 148          if ($item->isDot() or !$item->isDir()) {
 149              continue;
 150          }
 151          $plugin = $item->getFilename();
 152          if ($plugin === 'example') {
 153              continue;
 154          }
 155          if (file_exists("$basedir/plugins/$plugin/langs/en.js")) {
 156              $files[$plugin] = "$basedir/plugins/$plugin/langs/en.js";
 157          }
 158          if (file_exists("$basedir/plugins/$plugin/langs/en_dlg.js")) {
 159              $files[$plugin.'_dlg'] = "$basedir/plugins/$plugin/langs/en_dlg.js";
 160          }
 161          unset($item);
 162      }
 163      unset($items);
 164  
 165      // It would be too easy if TinyMCE used standard JSON in lang files...
 166  
 167      // Core upstream pack.
 168      $content = file_get_contents("$basedir/langs/en.js");
 169      $content = trim($content);
 170      $content = preg_replace("/^tinyMCE.addI18n\(\{en:/", '', $content);
 171      $content = preg_replace("/\}\);$/", '', $content);
 172      $content = preg_replace("/([\{,])([a-zA-Z0-9_]+):/", '$1"$2":', $content);
 173      $content = preg_replace("/:'([^']*)'/", ':"$1"', $content);
 174      $content = str_replace("\\'", "'", $content);
 175      $maindata = json_decode($content, true);
 176  
 177      if (is_null($maindata) or json_last_error() != 0) {
 178          echo "error processing main lang file\n";
 179          echo $content."\n\n";
 180          exit(1);
 181      }
 182      foreach($maindata as $component=>$data) {
 183          foreach ($data as $key=>$value) {
 184              $strings["$component:$key"] = $value;
 185          }
 186      }
 187      unset($content);
 188      unset($maindata);
 189  
 190      // Upstream plugins.
 191      foreach($files as $plugin=>$path) {
 192          $content = file_get_contents($path);
 193          $content = trim($content);
 194          $content = preg_replace("/^tinyMCE\.addI18n\('en\.[a-z09_]*',\s*/", '', $content);
 195          $content = preg_replace("/\);$/", '', $content);
 196  
 197          $content = preg_replace('/(\{|"\s*,)\s*([a-z0-9_]+)\s*:\s*"/m', '$1"$2":"', $content);
 198          $content = str_replace("\\'", "'", $content);
 199  
 200          $data = json_decode($content, true);
 201          if (is_null($data) or json_last_error() != 0) {
 202              echo "error processing $path lang file\n";
 203              echo $content."\n\n";
 204              exit(1);
 205          }
 206          foreach ($data as $key=>$value) {
 207              if ($key === '_empty_') {
 208                  continue;
 209              }
 210              $strings["$plugin:$key"] = $value;
 211          }
 212      }
 213  
 214      return $strings;
 215  }