Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/editor/tinymce/extra/tools/update_lang_files.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 define('CLI_SCRIPT', true);
00028 require_once dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))).'/config.php';
00029 
00030 if (!debugging('', DEBUG_DEVELOPER)) {
00031     die('Only for developers!!!!!');
00032 }
00033 
00034 $langconversion = array(
00035     // mapping of TinyMCE lang codes to Moodle codes
00036     'nb'    => 'no',
00037     'sr'    => 'sr_lt',
00038 
00039     // ignore the following files due to known errors
00040     'ch'    => false,   // XML parsing error, Moodle does not seem to have Chamorro yet anyway
00041     'zh'    => false,   // XML parsing error, use 'zh' => 'zh_tw' when sorted out
00042 );
00043 
00044 $targetlangdir = "$CFG->dirroot/lib/editor/tinymce/extra/tools/temp/langs"; // change if needed
00045 $tempdir       = "$CFG->dirroot/lib/editor/tinymce/extra/tools/temp/tinylangs";
00046 $enfile        = "$CFG->dirroot/lib/editor/tinymce/lang/en/editor_tinymce.php";
00047 
00048 
00050 if (!file_exists("$tempdir/en.xml")) {
00051     die('Missing temp/tinylangs/en.xml! Did you download langs?');
00052 }
00053 $old_strings = editor_tinymce_get_all_strings('en');
00054 ksort($old_strings);
00055 
00056 // our modifications and upstream changes in existing strings
00057 $tweaked = array();
00058 
00059 //detect changes and new additions
00060 $parsed = editor_tinymce_parse_xml_lang("$tempdir/en.xml");
00061 ksort($parsed);
00062 foreach ($parsed as $key=>$value) {
00063     if (array_key_exists($key, $old_strings)) {
00064         $oldvalue = $old_strings[$key];
00065         if ($oldvalue !== $value) {
00066             $tweaked[$key] = $oldvalue;
00067         }
00068         unset($old_strings[$key]);
00069     }
00070 }
00071 
00072 if (!$handle = fopen($enfile, 'w')) {
00073      echo "Cannot write to $filename !!";
00074      exit;
00075 }
00076 
00077 $header = <<<EOT
00078 <?php
00079 
00080 // This file is part of Moodle - http://moodle.org/
00081 //
00082 // Moodle is free software: you can redistribute it and/or modify
00083 // it under the terms of the GNU General Public License as published by
00084 // the Free Software Foundation, either version 3 of the License, or
00085 // (at your option) any later version.
00086 //
00087 // Moodle is distributed in the hope that it will be useful,
00088 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00089 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00090 // GNU General Public License for more details.
00091 //
00092 // You should have received a copy of the GNU General Public License
00093 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00094 
00104 EOT;
00105 
00106 fwrite($handle, $header);
00107 
00108 fwrite($handle, "\n\n//== Custom Moodle strings that are not part of upstream TinyMCE ==\n");
00109 foreach ($old_strings as $key=>$value) {
00110     fwrite($handle, editor_tinymce_encode_stringline($key, $value));
00111 }
00112 
00113 fwrite($handle, "\n\n// == TinyMCE upstream lang strings from all plugins ==\n");
00114 foreach ($parsed as $key=>$value) {
00115     fwrite($handle, editor_tinymce_encode_stringline($key, $value));
00116 }
00117 
00118 if ($tweaked) {
00119     fwrite($handle, "\n\n// == Our modifications or upstream changes ==\n");
00120     foreach ($tweaked as $key=>$value) {
00121         fwrite($handle, editor_tinymce_encode_stringline($key, $value));
00122     }
00123 }
00124 
00125 fclose($handle);
00126 
00127 //now update all other langs
00128 $en_strings = editor_tinymce_get_all_strings('en');
00129 if (!file_exists($targetlangdir)) {
00130     echo "Can not find target lang dir: $targetlangdir !!";
00131 }
00132 
00133 $xmlfiles = new DirectoryIterator($tempdir);
00134 foreach ($xmlfiles as $xmlfile) {
00135     if ($xmlfile->isDot() or $xmlfile->isLink() or $xmlfile->isDir()) {
00136         continue;
00137     }
00138 
00139     $filename = $xmlfile->getFilename();
00140 
00141     if ($filename == 'en.xml') {
00142         continue;
00143     }
00144     if (substr($filename, -4) !== '.xml') {
00145         continue;
00146     }
00147 
00148     $xmllang = substr($filename, 0, strlen($filename) - 4);
00149 
00150     echo "Processing $xmllang ...\n";
00151 
00152     if (array_key_exists($xmllang, $langconversion)) {
00153         $lang = $langconversion[$xmllang];
00154         if (empty($lang)) {
00155             echo "  Ignoring: $xmllang\n";
00156             continue;
00157         } else {
00158             echo "  Mapped to: $lang\n";
00159         }
00160     } else {
00161         $lang = $xmllang;
00162     }
00163 
00164     $langfile = "$targetlangdir/$lang/editor_tinymce.php";
00165     if (!file_exists(dirname($langfile))) {
00166         mkdir(dirname($langfile), 0755, true);
00167     }
00168 
00169     if (file_exists($langfile)) {
00170         unlink($langfile);
00171     }
00172 
00173     $parsed = editor_tinymce_parse_xml_lang("$tempdir/$xmlfile");
00174     ksort($parsed);
00175 
00176     if (!$handle = fopen($langfile, 'w')) {
00177          echo "*** Cannot write to $langfile !!\n";
00178          continue;
00179     }
00180 
00181     fwrite($handle, "<?php\n\n// upload this file into the AMOS stage, rebase the stage, review the changes and commit\n");
00182     foreach ($parsed as $key=>$value) {
00183         fwrite($handle, editor_tinymce_encode_stringline($key, $value));
00184     }
00185 
00186     fclose($handle);
00187 }
00188 unset($xmlfiles);
00189 
00190 
00191 die("\nFinished!\n\n");
00192 
00193 
00194 
00195 
00196 
00197 
00198 
00200 
00201 function editor_tinymce_encode_stringline($key, $value) {
00202         return "\$string['$key'] = ".var_export($value, true).";\n";
00203 }
00204 
00205 function editor_tinymce_parse_xml_lang($file) {
00206     $result = array();
00207 
00208     $doc = new DOMDocument();
00209     $doc->load($file);
00210     $groups = $doc->getElementsByTagName('group');
00211     foreach($groups as $group) {
00212         $section = $group->getAttribute('target');
00213         $items = $group->getElementsByTagName('item');
00214         foreach($items as $item) {
00215             $name  = $item->getAttribute('name');
00216             $value = $item->textContent;
00217             //undo quoted stuff
00218             $value = str_replace('\n', "\n", $value);
00219             $value = str_replace('\'', "'", $value);
00220             $value = str_replace('\\\\', '\\', $value);
00221             $result["$section:$name"] = $value;
00222         }
00223     }
00224     return $result;
00225 }
00226 
00227 function editor_tinymce_get_all_strings($lang) {
00228     $sm = get_string_manager();
00229     return $sm->load_component_strings('editor_tinymce', $lang);
00230 }
 All Data Structures Namespaces Files Functions Variables Enumerations