(bug 24898) MediaWiki uses /tmp even if a vHost-specific tempdir is set, also make...
[mediawiki.git] / includes / normal / Utf8CaseGenerate.php
blob8dbff1dbd406bcc490b07e593b6425a0fe478b58
1 <?php
2 /**
3 * This script generates Utf8Case.php from the Unicode Character Database
4 * and supplementary files.
6 * Copyright © 2004,2008 Brion Vibber <brion@pobox.com>
7 * http://www.mediawiki.org/
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.
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.
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
24 * @file
25 * @ingroup UtfNormal
28 if( php_sapi_name() != 'cli' ) {
29 die( "Run me from the command line please.\n" );
32 require_once 'UtfNormalUtil.php';
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);
41 $wikiUpperChars = array();
42 $wikiLowerChars = array();
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];
52 $source = codepointToUtf8( hexdec( $codepoint ) );
53 if( $simpleUpper ) {
54 $wikiUpperChars[$source] = codepointToUtf8( hexdec( $simpleUpper ) );
56 if( $simpleLower ) {
57 $wikiLowerChars[$source] = codepointToUtf8( hexdec( $simpleLower ) );
60 fclose( $in );
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.
71 * Hack for bugs in ucfirst() and company
73 * These are pulled from memcached if possible, as this is faster than filling
74 * up a big array manually.
76 * @file
77 * @ingroup Language
80 /**
81 * Translation array to get upper case character
83 \$wikiUpperChars = $outUpperChars;
85 /**
86 * Translation array to get lower case character
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);
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)";
107 function escapeLine( $key, $val ) {
108 $encKey = escapeSingleString( $key );
109 $encVal = escapeSingleString( $val );
110 return "\t'$encKey' => '$encVal'";