* Standardised file description headers
[lhc/web/wiklou.git] / includes / normal / Utf8CaseGenerate.php
1 <?php
2 /**
3 * This script generates Utf8Case.php from the Unicode Character Database
4 * and supplementary files.
5 *
6 * Copyright © 2004,2008 Brion Vibber <brion@pobox.com>
7 * http://www.mediawiki.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup UtfNormal
26 */
27
28 if( php_sapi_name() != 'cli' ) {
29 die( "Run me from the command line please.\n" );
30 }
31
32 require_once 'UtfNormalUtil.php';
33
34 $in = fopen("UnicodeData.txt", "rt" );
35 if( !$in ) {
36 print "Can't open UnicodeData.txt for reading.\n";
37 print "If necessary, fetch this file from the internet:\n";
38 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
39 exit(-1);
40 }
41 $wikiUpperChars = array();
42 $wikiLowerChars = array();
43
44 print "Reading character definitions...\n";
45 while( false !== ($line = fgets( $in ) ) ) {
46 $columns = explode(';', $line);
47 $codepoint = $columns[0];
48 $name = $columns[1];
49 $simpleUpper = $columns[12];
50 $simpleLower = $columns[13];
51
52 $source = codepointToUtf8( hexdec( $codepoint ) );
53 if( $simpleUpper ) {
54 $wikiUpperChars[$source] = codepointToUtf8( hexdec( $simpleUpper ) );
55 }
56 if( $simpleLower ) {
57 $wikiLowerChars[$source] = codepointToUtf8( hexdec( $simpleLower ) );
58 }
59 }
60 fclose( $in );
61
62 $out = fopen("Utf8Case.php", "wt");
63 if( $out ) {
64 $outUpperChars = escapeArray( $wikiUpperChars );
65 $outLowerChars = escapeArray( $wikiLowerChars );
66 $outdata = "<" . "?php
67 /**
68 * Simple 1:1 upper/lowercase switching arrays for utf-8 text.
69 * Won't get context-sensitive things yet.
70 *
71 * Hack for bugs in ucfirst() and company
72 *
73 * These are pulled from memcached if possible, as this is faster than filling
74 * up a big array manually.
75 *
76 * @file
77 * @ingroup Language
78 */
79
80 /**
81 * Translation array to get upper case character
82 */
83 \$wikiUpperChars = $outUpperChars;
84
85 /**
86 * Translation array to get lower case character
87 */
88 \$wikiLowerChars = $outLowerChars;\n";
89 fputs( $out, $outdata );
90 fclose( $out );
91 print "Wrote out Utf8Case.php\n";
92 } else {
93 print "Can't create file Utf8Case.php\n";
94 exit(-1);
95 }
96
97
98 function escapeArray( $arr ) {
99 return "array(\n" .
100 implode( ",\n",
101 array_map( "escapeLine",
102 array_keys( $arr ),
103 array_values( $arr ) ) ) .
104 "\n)";
105 }
106
107 function escapeLine( $key, $val ) {
108 $encKey = escapeSingleString( $key );
109 $encVal = escapeSingleString( $val );
110 return "\t'$encKey' => '$encVal'";
111 }