lm/lm.php
author Dirk Haun <dirk@haun-online.de>
Sun, 11 Oct 2009 19:15:09 +0200
branchHEAD
changeset 43 d70dcf7ba266
parent 38 797eb8143c9f
child 55 f1fc41e356ae
permissions -rwxr-xr-x
Add a \n after the closing ?>
     1 #!/usr/local/bin/php -q 
     2 <?php
     3 
     4 /* Reminder: always indent with 4 spaces (no tabs). */
     5 // +---------------------------------------------------------------------------+
     6 // | Geeklog 1.6                                                               |
     7 // +---------------------------------------------------------------------------+
     8 // | lm.php                                                                    |
     9 // |                                                                           |
    10 // | Update a language file by merging it with english.php                     |
    11 // +---------------------------------------------------------------------------+
    12 // | Copyright (C) 2004-2009 by the following authors:                         |
    13 // |                                                                           |
    14 // | Author:  Dirk Haun         - dirk AT haun-online DOT de                   |
    15 // +---------------------------------------------------------------------------+
    16 // |                                                                           |
    17 // | This program is free software; you can redistribute it and/or             |
    18 // | modify it under the terms of the GNU General Public License               |
    19 // | as published by the Free Software Foundation; either version 2            |
    20 // | of the License, or (at your option) any later version.                    |
    21 // |                                                                           |
    22 // | This program is distributed in the hope that it will be useful,           |
    23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
    24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
    25 // | GNU General Public License for more details.                              |
    26 // |                                                                           |
    27 // | You should have received a copy of the GNU General Public License         |
    28 // | along with this program; if not, write to the Free Software Foundation,   |
    29 // | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
    30 // |                                                                           |
    31 // +---------------------------------------------------------------------------+
    32 
    33 $VERSION = '1.0.3';
    34 
    35 // Prevent PHP from reporting uninitialized variables
    36 error_reporting( E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR );
    37 
    38 // name of the language file should be passed on the command line
    39 $langfile = $GLOBALS['argv'][1];
    40 if (empty ($langfile)) {
    41     echo "lm.php v{$VERSION}\n";
    42     echo "This is free software; see the source for copying conditions.\n\n";
    43     echo "Usage: {$GLOBALS['argv'][0]} langfile.php [module] > new-langfile.php\n\n";
    44     exit;
    45 }
    46 
    47 $module = '';
    48 if (!empty($GLOBALS['argv'][2])) {
    49     $module = $GLOBALS['argv'][2];
    50 }
    51 
    52 $mb = false;
    53 if (strpos($filename, '_utf-8') !== false) {
    54     $mb = true;
    55 
    56     if (!function_exists('mb_strpos')) {
    57         echo "Sorry, this script needs a PHP version that has multibyte support compiled in.\n\n";
    58         exit;
    59     } elseif (!function_exists('mb_ereg_replace')) {
    60         echo "Sorry, this script needs a PHP version with the mb_ereg_replace function compiled in.\n\n";
    61         exit;
    62     }
    63 
    64     mb_regex_encoding('UTF-8');
    65     mb_internal_encoding('UTF-8');
    66 }
    67 
    68 define('XHTML', '');
    69 
    70 // list of all variables accessed in the language file
    71 $_DB_mysqldump_path         = '{$_DB_mysqldump_path}';
    72 $_CONF['backup_path']       = '{$_CONF[\'backup_path\']}';
    73 $_CONF['commentspeedlimit'] = '{$_CONF[\'commentspeedlimit\']}';
    74 $_CONF['site_admin_url']    = '{$_CONF[\'site_admin_url\']}';
    75 $_CONF['site_name']         = '{$_CONF[\'site_name\']}';
    76 $_CONF['site_url']          = '{$_CONF[\'site_url\']}';
    77 $_CONF['speedlimit']        = '{$_CONF[\'speedlimit\']}';
    78 $_USER['username']          = '{$_USER[\'username\']}';
    79 
    80 $failures                   = '{$failures}';
    81 $from                       = '{$from}';
    82 $fromemail                  = '{$fromemail}';
    83 $qid                        = '{$qid}';
    84 $shortmsg                   = '{$shortmsg}';
    85 $successes                  = '{$successes}';
    86 $topic                      = '{$topic}';
    87 $type                       = '{$type}';
    88 
    89 // load the English language file
    90 if (empty($module)) {
    91     require_once 'language/english.php';
    92 } elseif ($module == 'install') {
    93     require_once 'public_html/admin/install/language/english.php';
    94 } else {
    95     require_once 'plugins/' . $module . '/language/english.php';
    96 }
    97 
    98 $lastp = strrpos($_SERVER['PHP_SELF'], DIRECTORY_SEPARATOR);
    99 if ($lastp === false) {
   100     $incpath = '.' . DIRECTORY_SEPARATOR;
   101 } else {
   102     $incpath = substr($_SERVER['PHP_SELF'], 0, $lastp + 1);
   103 }
   104 $incpath .= 'include' . DIRECTORY_SEPARATOR;
   105 
   106 function separator()
   107 {
   108     echo "###############################################################################\n";
   109 }
   110 
   111 function separatorThin()
   112 {
   113     echo "// +---------------------------------------------------------------------------+\n";
   114 }
   115 
   116 /**
   117 * My mb-save replacement for some(!) string replacements
   118 *
   119 */
   120 function my_str_replace($s1, $s2, $s3)
   121 {
   122     global $mb;
   123 
   124     if ($mb) {
   125         return mb_ereg_replace($s1, $s2, $s3);
   126     } else {
   127         return str_replace($s1, $s2, $s3);
   128     }
   129 }
   130 
   131 /**
   132 * My mb-save replacement for some(!) use cases of strpos
   133 *
   134 */
   135 function my_strpos($s1, $s2)
   136 {
   137     global $mb;
   138 
   139     if ($mb) {
   140         return mb_strpos($s1, $s2);
   141     } else {
   142         return strpos($s1, $s2);
   143     }
   144 }
   145 
   146 /**
   147 * Make <br> and <hr> tags XHTML compliant
   148 *
   149 */
   150 function makeXHTML($txt)
   151 {
   152     global $mb;
   153 
   154     // fix accidentally created <brXHTML> tags in some 1.5.0b1 language files
   155     $txt = my_str_replace('brXHTML', 'br', $txt);
   156 
   157     if ($mb) {
   158         $fc = mb_substr($txt, 0, 1);
   159     } else {
   160         $fc = substr($txt, 0, 1);
   161     }
   162 
   163     $txt = my_str_replace('<br>',
   164                           '<br' . $fc . ' . XHTML . ' . $fc . '>', $txt);
   165     $txt = my_str_replace('<hr>',
   166                           '<hr' . $fc . ' . XHTML . ' . $fc . '>', $txt);
   167 
   168     return $txt;
   169 }
   170 
   171 function prepareText($newtxt)
   172 {
   173     global $mb;
   174 
   175     if (my_strpos($newtxt, '{$') === false) {
   176         if (my_strpos($newtxt, '\n') === false) {
   177             // text contains neither variables nor line feeds,
   178             // so enclose it in single quotes
   179             $newtxt = my_str_replace("'", "\'", $newtxt);
   180             $quotedtext = "'" . $newtxt . "'";
   181         } else {
   182             // text contains line feeds - enclose in double quotes so
   183             // they can be interpreted
   184             $newtxt = my_str_replace('"', '\"', $newtxt);
   185             $quotedtext = '"' . $newtxt . '"';
   186         }
   187     } else {
   188         // text contains variables
   189         if ($mb) {
   190             $newtxt = mb_ereg_replace('\$', '\$', $newtxt);
   191             // backslash attack!
   192             $newtxt = mb_ereg_replace('\{\\\\\$', '{$', $newtxt);
   193             $newtxt = mb_ereg_replace('"', '\"', $newtxt);
   194         } else {
   195             $newtxt = str_replace('$', '\$', $newtxt);
   196             $newtxt = str_replace('{\$', '{$', $newtxt);
   197             $newtxt = str_replace('"', '\"', $newtxt);
   198         }
   199         $quotedtext = '"' . $newtxt . '"';
   200     }
   201 
   202     return $quotedtext;
   203 }
   204 
   205 /**
   206 * Merge two language arrays
   207 *
   208 * This function does all the work. Any missing text strings are copied
   209 * over from english.php. Also does some pretty-printing.
   210 *
   211 */
   212 function mergeArrays($ENG, $OTHER, $arrayName, $comment = '')
   213 {
   214     global $mb;
   215 
   216     $numElements = sizeof($ENG);
   217     $counter = 0;
   218 
   219     if ($comment !== false) {
   220         separator();
   221     }
   222     if (!empty ($comment)) {
   223         $comments = explode ("\n", $comment);
   224         foreach ($comments as $c) {
   225             echo "# $c\n";
   226         }
   227     }
   228     echo "\n\${$arrayName} = array(\n";
   229 
   230     foreach ($ENG as $key => $txt) {
   231         $counter++;
   232         if (is_numeric($key)) {
   233             echo "    $key => ";
   234         } else {
   235             echo "    '$key' => ";
   236         }
   237         $newtxt = '';
   238         if (empty($OTHER[$key])) {
   239             // string does not exist in other language - use English text
   240             $newtxt = $txt;
   241         } else {
   242             if (isset($ENG[$key]) && empty($ENG[$key])) {
   243                 // string is now empty in English language file - remove it
   244                 $newtxt = '';
   245             } else {
   246                 // string exists in other language - keep it
   247                 $newtxt = $OTHER[$key];
   248             }
   249         }
   250 
   251         if (!is_array($newtxt)) {
   252             $newtxt = my_str_replace("\n", '\n', $newtxt);
   253         }
   254 
   255         if (is_array($newtxt)) { // mainly for the config selects
   256             $quotedtext = 'array(';
   257             foreach ($newtxt as $nkey => $ntxt) {
   258                 $quotedtext .= "'" . my_str_replace("'", "\'", $nkey) . "' => ";
   259                 if ($ntxt === true) {
   260                     $quotedtext .= 'true';
   261                 } elseif ($ntxt === false) {
   262                     $quotedtext .= 'false';
   263                 } elseif (is_numeric($ntxt)) {
   264                     $quotedtext .= $ntxt;
   265                 } else {
   266                     $quotedtext .= "'" . my_str_replace("'", "\'", $ntxt) . "'";
   267                 }
   268                 $quotedtext .= ', ';
   269             }
   270             if ($mb) {
   271                 $quotedtext = mb_substr($quotedtext, 0, -2);
   272             } else {
   273                 $quotedtext = substr($quotedtext, 0, -2);
   274             }
   275             $quotedtext .= ')';
   276 
   277             // hack for this special case ...
   278             if ($quotedtext == "array('True' => 1, 'False' => '')") {
   279                 $quotedtext = "array('True' => TRUE, 'False' => FALSE)";
   280             }
   281 
   282             // ??? $quotedtext = mb_ereg_replace("\n", '\n', $quotedtext);
   283         } else {
   284             $quotedtext = prepareText($newtxt);
   285         }
   286 
   287         $quotedtext = makeXHTML($quotedtext);
   288 
   289         if ($counter != $numElements) {
   290             $quotedtext .= ',';
   291         }
   292         echo "$quotedtext\n";
   293     }
   294 
   295     if ($comment === false) {
   296         echo ");\n";
   297     } else {
   298         echo ");\n\n";
   299     }
   300 }
   301 
   302 function mergeString($eng, $other, $name)
   303 {
   304     global $mb;
   305 
   306     if (empty($other)) {
   307         $newtxt = $eng;
   308     } else {
   309         $newtxt = $other;
   310     }
   311 
   312     $quotedtext = prepareText($newtxt);
   313     $quotedtext = makeXHTML($quotedtext);
   314 
   315     echo "\$$name = $quotedtext;\n";
   316 }
   317 
   318 /**
   319 * Read the credits / copyright from the other language file.
   320 * Assumes that it starts and ends with a separator line of # signs.
   321 *
   322 */
   323 function readCredits($langfile)
   324 {
   325     $credits = array ();
   326 
   327     $firstcomment = false;
   328 
   329     $fh = fopen ($langfile, 'r');
   330     if ($fh !== false) {
   331         while (true) {
   332             $line = fgets ($fh);
   333             if ($firstcomment) {
   334                 $credits[] = $line;
   335                 if (strstr ($line, '#####') !== false) {
   336                     // end of credits reached
   337                     break;
   338                 } elseif (strstr($line, '*/') !== false) {
   339                     // end of credits reached, Spam-X style
   340                     break;
   341                 } elseif (strstr($line, '+-----') !== false) {
   342                     $nextline = fgets($fh);
   343                     $tst = trim($nextline);
   344                     if (empty($tst)) {
   345                         // end of credits reached, install script style
   346                         break;
   347                     } else {
   348                         $credits[] = $nextline;
   349                     }
   350                 }
   351             } else {
   352                 if (strstr ($line, '#####') !== false) {
   353                     // start of credits
   354                     $firstcomment = true;
   355                     $credits[] = $line;
   356                 } elseif (strstr($line, '/**') !== false) {
   357                     // start of credits, Spam-X style
   358                     $firstcomment = true;
   359                     $credits[] = $line;
   360                 } elseif (strstr($line, '/* Reminder:') !== false) {
   361                     // start of credits, install script style
   362                     $firstcomment = true;
   363                     $credits[] = $line;
   364                 }
   365             }
   366         }
   367         fclose ($fh);
   368     }
   369 
   370     return ($credits);
   371 }
   372 
   373 
   374 // MAIN
   375 
   376 $credits = readCredits($langfile);
   377 
   378 // output starts here ...
   379 
   380 echo "<?php\n\n";
   381 
   382 foreach ($credits as $c) {
   383     echo "$c"; // Note: linefeeds are part of the credits
   384 }
   385 
   386 // load the module file which does the rest
   387 
   388 if (empty($module)) {
   389     require_once $incpath . 'core.inc';
   390 } else {
   391     require_once $incpath . $module . '.inc';
   392 }
   393 
   394 echo "\n?>\n";
   395 
   396 ?>