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