MediaWiki
REL1_20
|
00001 <?php 00024 require_once( __DIR__ .'/../Maintenance.php' ); 00025 00031 class GenerateCollationData extends Maintenance { 00033 var $dataDir; 00034 00036 var $weights; 00037 00043 var $mappedChars; 00044 00045 var $debugOutFile; 00046 00050 const NORMAL_UPPERCASE = 0x08; 00051 const NORMAL_HIRAGANA = 0X0E; 00052 00053 public function __construct() { 00054 parent::__construct(); 00055 $this->addOption( 'data-dir', 'A directory on the local filesystem ' . 00056 'containing allkeys.txt and ucd.all.grouped.xml from unicode.org', 00057 false, true ); 00058 $this->addOption( 'debug-output', 'Filename for sending debug output to', 00059 false, true ); 00060 } 00061 00062 public function execute() { 00063 $this->dataDir = $this->getOption( 'data-dir', '.' ); 00064 if ( !file_exists( "{$this->dataDir}/allkeys.txt" ) ) { 00065 $this->error( "Unable to find allkeys.txt. Please download it from " . 00066 "http://www.unicode.org/Public/UCA/latest/allkeys.txt and specify " . 00067 "its location with --data-dir=<DIR>" ); 00068 exit( 1 ); 00069 } 00070 if ( !file_exists( "{$this->dataDir}/ucd.all.grouped.xml" ) ) { 00071 $this->error( "Unable to find ucd.all.grouped.xml. Please download it " . 00072 "from http://www.unicode.org/Public/6.0.0/ucdxml/ucd.all.grouped.zip " . 00073 "and specify its location with --data-dir=<DIR>" ); 00074 exit( 1 ); 00075 } 00076 $debugOutFileName = $this->getOption( 'debug-output' ); 00077 if ( $debugOutFileName ) { 00078 $this->debugOutFile = fopen( $debugOutFileName, 'w' ); 00079 if ( !$this->debugOutFile ) { 00080 $this->error( "Unable to open debug output file for writing" ); 00081 exit( 1 ); 00082 } 00083 } 00084 $this->loadUcd(); 00085 $this->generateFirstChars(); 00086 } 00087 00088 function loadUcd() { 00089 $uxr = new UcdXmlReader( "{$this->dataDir}/ucd.all.grouped.xml" ); 00090 $uxr->readChars( array( $this, 'charCallback' ) ); 00091 } 00092 00093 function charCallback( $data ) { 00094 // Skip non-printable characters, 00095 // but do not skip a normal space (U+0020) since 00096 // people like to use that as a fake no header symbol. 00097 $category = substr( $data['gc'], 0, 1 ); 00098 if ( strpos( 'LNPS', $category ) === false 00099 && $data['cp'] !== '0020' ) { 00100 return; 00101 } 00102 $cp = hexdec( $data['cp'] ); 00103 00104 // Skip the CJK ideograph blocks, as an optimisation measure. 00105 // UCA doesn't sort them properly anyway, without tailoring. 00106 if ( IcuCollation::isCjk( $cp ) ) { 00107 return; 00108 } 00109 00110 // Skip the composed Hangul syllables, we will use the bare Jamo 00111 // as first letters 00112 if ( $data['block'] == 'Hangul Syllables' ) { 00113 return; 00114 } 00115 00116 // Calculate implicit weight per UTS #10 v6.0.0, sec 7.1.3 00117 if ( $data['UIdeo'] === 'Y' ) { 00118 if ( $data['block'] == 'CJK Unified Ideographs' 00119 || $data['block'] == 'CJK Compatibility Ideographs' ) 00120 { 00121 $base = 0xFB40; 00122 } else { 00123 $base = 0xFB80; 00124 } 00125 } else { 00126 $base = 0xFBC0; 00127 } 00128 $a = $base + ( $cp >> 15 ); 00129 $b = ( $cp & 0x7fff ) | 0x8000; 00130 00131 $this->weights[$cp] = sprintf( ".%04X.%04X", $a, $b ); 00132 00133 if ( $data['dm'] !== '#' ) { 00134 $this->mappedChars[$cp] = true; 00135 } 00136 00137 if ( $cp % 4096 == 0 ) { 00138 print "{$data['cp']}\n"; 00139 } 00140 } 00141 00142 function generateFirstChars() { 00143 $file = fopen( "{$this->dataDir}/allkeys.txt", 'r' ); 00144 if ( !$file ) { 00145 $this->error( "Unable to open allkeys.txt" ); 00146 exit( 1 ); 00147 } 00148 global $IP; 00149 $outFile = fopen( "$IP/serialized/first-letters-root.ser", 'w' ); 00150 if ( !$outFile ) { 00151 $this->error( "Unable to open output file first-letters-root.ser" ); 00152 exit( 1 ); 00153 } 00154 00155 $goodTertiaryChars = array(); 00156 00157 // For each character with an entry in allkeys.txt, overwrite the implicit 00158 // entry in $this->weights that came from the UCD. 00159 // Also gather a list of tertiary weights, for use in selecting the group header 00160 while ( false !== ( $line = fgets( $file ) ) ) { 00161 // We're only interested in single-character weights, pick them out with a regex 00162 $line = trim( $line ); 00163 if ( !preg_match( '/^([0-9A-F]+)\s*;\s*([^#]*)/', $line, $m ) ) { 00164 continue; 00165 } 00166 00167 $cp = hexdec( $m[1] ); 00168 $allWeights = trim( $m[2] ); 00169 $primary = ''; 00170 $tertiary = ''; 00171 00172 if ( !isset( $this->weights[$cp] ) ) { 00173 // Non-printable, ignore 00174 continue; 00175 } 00176 foreach ( StringUtils::explode( '[', $allWeights ) as $weightStr ) { 00177 preg_match_all( '/[*.]([0-9A-F]+)/', $weightStr, $m ); 00178 if ( !empty( $m[1] ) ) { 00179 if ( $m[1][0] !== '0000' ) { 00180 $primary .= '.' . $m[1][0]; 00181 } 00182 if ( $m[1][2] !== '0000' ) { 00183 $tertiary .= '.' . $m[1][2]; 00184 } 00185 } 00186 } 00187 $this->weights[$cp] = $primary; 00188 if ( $tertiary === '.0008' 00189 || $tertiary === '.000E' ) 00190 { 00191 $goodTertiaryChars[$cp] = true; 00192 } 00193 } 00194 fclose( $file ); 00195 00196 // Identify groups of characters with the same primary weight 00197 $this->groups = array(); 00198 asort( $this->weights, SORT_STRING ); 00199 $prevWeight = reset( $this->weights ); 00200 $group = array(); 00201 foreach ( $this->weights as $cp => $weight ) { 00202 if ( $weight !== $prevWeight ) { 00203 $this->groups[$prevWeight] = $group; 00204 $prevWeight = $weight; 00205 if ( isset( $this->groups[$weight] ) ) { 00206 $group = $this->groups[$weight]; 00207 } else { 00208 $group = array(); 00209 } 00210 } 00211 $group[] = $cp; 00212 } 00213 if ( $group ) { 00214 $this->groups[$prevWeight] = $group; 00215 } 00216 00217 // If one character has a given primary weight sequence, and a second 00218 // character has a longer primary weight sequence with an initial 00219 // portion equal to the first character, then remove the second 00220 // character. This avoids having characters like U+A732 (double A) 00221 // polluting the basic latin sort area. 00222 00223 foreach ( $this->groups as $weight => $group ) { 00224 if ( preg_match( '/(\.[0-9A-F]*)\./', $weight, $m ) ) { 00225 if ( isset( $this->groups[$m[1]] ) ) { 00226 unset( $this->groups[$weight] ); 00227 } 00228 } 00229 } 00230 00231 ksort( $this->groups, SORT_STRING ); 00232 00233 // Identify the header character in each group 00234 $headerChars = array(); 00235 $prevChar = "\000"; 00236 $tertiaryCollator = new Collator( 'root' ); 00237 $primaryCollator = new Collator( 'root' ); 00238 $primaryCollator->setStrength( Collator::PRIMARY ); 00239 $numOutOfOrder = 0; 00240 foreach ( $this->groups as $weight => $group ) { 00241 $uncomposedChars = array(); 00242 $goodChars = array(); 00243 foreach ( $group as $cp ) { 00244 if ( isset( $goodTertiaryChars[$cp] ) ) { 00245 $goodChars[] = $cp; 00246 } 00247 if ( !isset( $this->mappedChars[$cp] ) ) { 00248 $uncomposedChars[] = $cp; 00249 } 00250 } 00251 $x = array_intersect( $goodChars, $uncomposedChars ); 00252 if ( !$x ) { 00253 $x = $uncomposedChars; 00254 if ( !$x ) { 00255 $x = $group; 00256 } 00257 } 00258 00259 // Use ICU to pick the lowest sorting character in the selection 00260 $tertiaryCollator->sort( $x ); 00261 $cp = $x[0]; 00262 00263 $char = codepointToUtf8( $cp ); 00264 $headerChars[] = $char; 00265 if ( $primaryCollator->compare( $char, $prevChar ) <= 0 ) { 00266 $numOutOfOrder ++; 00267 /* 00268 printf( "Out of order: U+%05X > U+%05X\n", 00269 utf8ToCodepoint( $prevChar ), 00270 utf8ToCodepoint( $char ) ); 00271 */ 00272 } 00273 $prevChar = $char; 00274 00275 if ( $this->debugOutFile ) { 00276 fwrite( $this->debugOutFile, sprintf( "%05X %s %s (%s)\n", $cp, $weight, $char, 00277 implode( ' ', array_map( 'codepointToUtf8', $group ) ) ) ); 00278 } 00279 } 00280 00281 print "Out of order: $numOutOfOrder / " . count( $headerChars ) . "\n"; 00282 00283 fwrite( $outFile, serialize( $headerChars ) ); 00284 } 00285 } 00286 00287 class UcdXmlReader { 00288 var $fileName; 00289 var $callback; 00290 var $groupAttrs; 00291 var $xml; 00292 var $blocks = array(); 00293 var $currentBlock; 00294 00295 function __construct( $fileName ) { 00296 $this->fileName = $fileName; 00297 } 00298 00299 public function readChars( $callback ) { 00300 $this->getBlocks(); 00301 $this->currentBlock = reset( $this->blocks ); 00302 $xml = $this->open(); 00303 $this->callback = $callback; 00304 00305 while ( $xml->name !== 'repertoire' && $xml->next() ); 00306 00307 while ( $xml->read() ) { 00308 if ( $xml->nodeType == XMLReader::ELEMENT ) { 00309 if ( $xml->name === 'group' ) { 00310 $this->groupAttrs = $this->readAttributes(); 00311 } elseif ( $xml->name === 'char' ) { 00312 $this->handleChar(); 00313 } 00314 } elseif ( $xml->nodeType === XMLReader::END_ELEMENT ) { 00315 if ( $xml->name === 'group' ) { 00316 $this->groupAttrs = array(); 00317 } 00318 } 00319 } 00320 $xml->close(); 00321 } 00322 00323 protected function open() { 00324 $this->xml = new XMLReader; 00325 $this->xml->open( $this->fileName ); 00326 if ( !$this->xml ) { 00327 throw new MWException( __METHOD__.": unable to open {$this->fileName}" ); 00328 } 00329 while ( $this->xml->name !== 'ucd' && $this->xml->read() ); 00330 $this->xml->read(); 00331 return $this->xml; 00332 } 00333 00339 protected function readAttributes() { 00340 $attrs = array(); 00341 while ( $this->xml->moveToNextAttribute() ) { 00342 $attrs[$this->xml->name] = $this->xml->value; 00343 } 00344 return $attrs; 00345 } 00346 00347 protected function handleChar() { 00348 $attrs = $this->readAttributes() + $this->groupAttrs; 00349 if ( isset( $attrs['cp'] ) ) { 00350 $first = $last = hexdec( $attrs['cp'] ); 00351 } else { 00352 $first = hexdec( $attrs['first-cp'] ); 00353 $last = hexdec( $attrs['last-cp'] ); 00354 unset( $attrs['first-cp'] ); 00355 unset( $attrs['last-cp'] ); 00356 } 00357 00358 for ( $cp = $first; $cp <= $last; $cp++ ) { 00359 $hexCp = sprintf( "%04X", $cp ); 00360 foreach ( array( 'na', 'na1' ) as $nameProp ) { 00361 if ( isset( $attrs[$nameProp] ) ) { 00362 $attrs[$nameProp] = str_replace( '#', $hexCp, $attrs[$nameProp] ); 00363 } 00364 } 00365 00366 while ( $this->currentBlock ) { 00367 if ( $cp < $this->currentBlock[0] ) { 00368 break; 00369 } elseif ( $cp <= $this->currentBlock[1] ) { 00370 $attrs['block'] = key( $this->blocks ); 00371 break; 00372 } else { 00373 $this->currentBlock = next( $this->blocks ); 00374 } 00375 } 00376 00377 $attrs['cp'] = $hexCp; 00378 call_user_func( $this->callback, $attrs ); 00379 } 00380 } 00381 00382 public function getBlocks() { 00383 if ( $this->blocks ) { 00384 return $this->blocks; 00385 } 00386 00387 $xml = $this->open(); 00388 while ( $xml->name !== 'blocks' && $xml->read() ); 00389 00390 while ( $xml->read() ) { 00391 if ( $xml->nodeType == XMLReader::ELEMENT ) { 00392 if ( $xml->name === 'block' ) { 00393 $attrs = $this->readAttributes(); 00394 $first = hexdec( $attrs['first-cp'] ); 00395 $last = hexdec( $attrs['last-cp'] ); 00396 $this->blocks[$attrs['name']] = array( $first, $last ); 00397 } 00398 } 00399 } 00400 $xml->close(); 00401 return $this->blocks; 00402 } 00403 00404 } 00405 00406 $maintClass = 'GenerateCollationData'; 00407 require_once( RUN_MAINTENANCE_IF_MAIN );