Single quotes
[mediawiki.git] / includes / normal / UtfNormalGenerate.php
blob65128b706e669ca338075611c44806f3c7dbbfa0
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>
3 # http://www.mediawiki.org/
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 /**
21 * This script generates UniNormalData.inc from the Unicode Character Database
22 * and supplementary files.
24 * @package UtfNormal
25 * @access private
28 /** */
29 require_once 'UtfNormalUtil.php';
31 $in = fopen("DerivedNormalizationProps.txt", "rt" );
32 if( !$in ) {
33 print "Can't open DerivedNormalizationProps.txt for reading.\n";
34 print "If necessary, fetch this file from the internet:\n";
35 print "http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt\n";
36 exit(-1);
38 print "Initializing normalization quick check tables...\n";
39 $checkNFC = array();
40 while( false !== ($line = fgets( $in ) ) ) {
41 if( preg_match( '/^([0-9A-F]+)(?:..([0-9A-F]+))?\s*;\s*(NFC_QC)\s*;\s*([MN])/', $line, $matches ) ) {
42 list( $junk, $first, $last, $prop, $value ) = $matches;
43 #print "$first $last $prop $value\n";
44 if( !$last ) $last = $first;
45 for( $i = hexdec( $first ); $i <= hexdec( $last ); $i++) {
46 $char = codepointToUtf8( $i );
47 $checkNFC[$char] = $value;
51 fclose( $in );
53 $in = fopen("CompositionExclusions.txt", "rt" );
54 if( !$in ) {
55 print "Can't open CompositionExclusions.txt for reading.\n";
56 print "If necessary, fetch this file from the internet:\n";
57 print "http://www.unicode.org/Public/UNIDATA/CompositionExclusions.txt\n";
58 exit(-1);
60 $exclude = array();
61 while( false !== ($line = fgets( $in ) ) ) {
62 if( preg_match( '/^([0-9A-F]+)/i', $line, $matches ) ) {
63 $codepoint = $matches[1];
64 $source = codepointToUtf8( hexdec( $codepoint ) );
65 $exclude[$source] = true;
68 fclose($in);
70 $in = fopen("UnicodeData.txt", "rt" );
71 if( !$in ) {
72 print "Can't open UnicodeData.txt for reading.\n";
73 print "If necessary, fetch this file from the internet:\n";
74 print "http://www.unicode.org/Public/UNIDATA/UnicodeData.txt\n";
75 exit(-1);
78 $compatibilityDecomp = array();
79 $canonicalDecomp = array();
80 $canonicalComp = array();
81 $combiningClass = array();
82 $total = 0;
83 $compat = 0;
84 $canon = 0;
86 print "Reading character definitions...\n";
87 while( false !== ($line = fgets( $in ) ) ) {
88 $columns = split(';', $line);
89 $codepoint = $columns[0];
90 $name = $columns[1];
91 $canonicalCombiningClass = $columns[3];
92 $decompositionMapping = $columns[5];
94 $source = codepointToUtf8( hexdec( $codepoint ) );
96 if( $canonicalCombiningClass != 0 ) {
97 $combiningClass[$source] = IntVal( $canonicalCombiningClass );
100 if( $decompositionMapping === '' ) continue;
101 if( preg_match( '/^<(.+)> (.*)$/', $decompositionMapping, $matches ) ) {
102 # Compatibility decomposition
103 $canonical = false;
104 $decompositionMapping = $matches[2];
105 $compat++;
106 } else {
107 $canonical = true;
108 $canon++;
110 $total++;
111 $dest = hexSequenceToUtf8( $decompositionMapping );
113 $compatibilityDecomp[$source] = $dest;
114 if( $canonical ) {
115 $canonicalDecomp[$source] = $dest;
116 if( empty( $exclude[$source] ) ) {
117 $canonicalComp[$dest] = $source;
120 #print "$codepoint | $canonicalCombiningClasses | $decompositionMapping\n";
122 fclose( $in );
124 print "Recursively expanding canonical mappings...\n";
125 $changed = 42;
126 $pass = 1;
127 while( $changed > 0 ) {
128 print "pass $pass\n";
129 $changed = 0;
130 foreach( $canonicalDecomp as $source => $dest ) {
131 $newDest = preg_replace_callback(
132 '/([\xc0-\xff][\x80-\xbf]+)/',
133 'callbackCanonical',
134 $dest);
135 if( $newDest === $dest ) continue;
136 $changed++;
137 $canonicalDecomp[$source] = $newDest;
139 $pass++;
142 print "Recursively expanding compatibility mappings...\n";
143 $changed = 42;
144 $pass = 1;
145 while( $changed > 0 ) {
146 print "pass $pass\n";
147 $changed = 0;
148 foreach( $compatibilityDecomp as $source => $dest ) {
149 $newDest = preg_replace_callback(
150 '/([\xc0-\xff][\x80-\xbf]+)/',
151 'callbackCompat',
152 $dest);
153 if( $newDest === $dest ) continue;
154 $changed++;
155 $compatibilityDecomp[$source] = $newDest;
157 $pass++;
160 print "$total decomposition mappings ($canon canonical, $compat compatibility)\n";
162 $out = fopen("UtfNormalData.inc", "wt");
163 if( $out ) {
164 $serCombining = escapeSingleString( serialize( $combiningClass ) );
165 $serComp = escapeSingleString( serialize( $canonicalComp ) );
166 $serCanon = escapeSingleString( serialize( $canonicalDecomp ) );
167 $serCheckNFC = escapeSingleString( serialize( $checkNFC ) );
168 $outdata = "<" . "?php
170 * This file was automatically generated -- do not edit!
171 * Run UtfNormalGenerate.php to create this file again (make clean && make)
172 * @package MediaWiki
174 /** */
175 global \$utfCombiningClass, \$utfCanonicalComp, \$utfCanonicalDecomp, \$utfCheckNFC;
176 \$utfCombiningClass = unserialize( '$serCombining' );
177 \$utfCanonicalComp = unserialize( '$serComp' );
178 \$utfCanonicalDecomp = unserialize( '$serCanon' );
179 \$utfCheckNFC = unserialize( '$serCheckNFC' );
180 ?" . ">\n";
181 fputs( $out, $outdata );
182 fclose( $out );
183 print "Wrote out UtfNormalData.inc\n";
184 } else {
185 print "Can't create file UtfNormalData.inc\n";
186 exit(-1);
190 $out = fopen("UtfNormalDataK.inc", "wt");
191 if( $out ) {
192 $serCompat = escapeSingleString( serialize( $compatibilityDecomp ) );
193 $outdata = "<" . "?php
195 * This file was automatically generated -- do not edit!
196 * Run UtfNormalGenerate.php to create this file again (make clean && make)
197 * @package MediaWiki
199 /** */
200 global \$utfCompatibilityDecomp;
201 \$utfCompatibilityDecomp = unserialize( '$serCompat' );
202 ?" . ">\n";
203 fputs( $out, $outdata );
204 fclose( $out );
205 print "Wrote out UtfNormalDataK.inc\n";
206 exit(0);
207 } else {
208 print "Can't create file UtfNormalDataK.inc\n";
209 exit(-1);
212 # ---------------
214 function callbackCanonical( $matches ) {
215 global $canonicalDecomp;
216 if( isset( $canonicalDecomp[$matches[1]] ) ) {
217 return $canonicalDecomp[$matches[1]];
219 return $matches[1];
222 function callbackCompat( $matches ) {
223 global $compatibilityDecomp;
224 if( isset( $compatibilityDecomp[$matches[1]] ) ) {
225 return $compatibilityDecomp[$matches[1]];
227 return $matches[1];