MediaWiki
REL1_24
|
00001 <?php 00024 require_once __DIR__ . '/../Maintenance.php'; 00025 00031 class GenerateCollationData extends Maintenance { 00033 public $dataDir; 00034 00036 public $weights; 00037 00043 public $mappedChars; 00044 00045 public $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 00065 $allkeysPresent = file_exists( "{$this->dataDir}/allkeys.txt" ); 00066 $ucdallPresent = file_exists( "{$this->dataDir}/ucd.all.grouped.xml" ); 00067 00068 // As of January 2013, these links work for all versions of Unicode 00069 // between 5.1 and 6.2, inclusive. 00070 $allkeysURL = "http://www.unicode.org/Public/UCA/<Unicode version>/allkeys.txt"; 00071 $ucdallURL = "http://www.unicode.org/Public/<Unicode version>/ucdxml/ucd.all.grouped.zip"; 00072 00073 if ( !$allkeysPresent || !$ucdallPresent ) { 00074 $icuVersion = IcuCollation::getICUVersion(); 00075 $unicodeVersion = IcuCollation::getUnicodeVersionForICU(); 00076 00077 $error = ""; 00078 00079 if ( !$allkeysPresent ) { 00080 $error .= "Unable to find allkeys.txt. " 00081 . "Download it and specify its location with --data-dir=<DIR>. " 00082 . "\n\n"; 00083 } 00084 if ( !$ucdallPresent ) { 00085 $error .= "Unable to find ucd.all.grouped.xml. " 00086 . "Download it, unzip, and specify its location with --data-dir=<DIR>. " 00087 . "\n\n"; 00088 } 00089 00090 $versionKnown = false; 00091 if ( !$icuVersion ) { 00092 // Unknown version - either very old intl, 00093 // or PHP < 5.3.7 which does not expose this information 00094 $error .= "As MediaWiki could not determine the version of ICU library used by your PHP's " 00095 . "intl extension it can't suggest which file version to download. " 00096 . "This can be caused by running a very old version of intl or PHP < 5.3.7. " 00097 . "If you are sure everything is all right, find out the ICU version " 00098 . "by running phpinfo(), check what is the Unicode version it is using " 00099 . "at http://site.icu-project.org/download, then try finding appropriate data file(s) at:"; 00100 } elseif ( version_compare( $icuVersion, "4.0", "<" ) ) { 00101 // Extra old version 00102 $error .= "You are using outdated version of ICU ($icuVersion), intended for " 00103 . ( $unicodeVersion ? "Unicode $unicodeVersion" : "an unknown version of Unicode" ) 00104 . "; this file might not be avalaible for it, and it's not supported by MediaWiki. " 00105 . " You are on your own; consider upgrading PHP's intl extension or try " 00106 . "one of the files available at:"; 00107 } elseif ( version_compare( $icuVersion, "51.0", ">=" ) ) { 00108 // Extra recent version 00109 $error .= "You are using ICU $icuVersion, released after this script was last updated. " 00110 . "Check what is the Unicode version it is using at http://site.icu-project.org/download . " 00111 . "It can't be guaranteed everything will work, but appropriate file(s) should " 00112 . "be available at:"; 00113 } else { 00114 // ICU 4.0 to 50.x 00115 $versionKnown = true; 00116 $error .= "You are using ICU $icuVersion, intended for " 00117 . ( $unicodeVersion ? "Unicode $unicodeVersion" : "an unknown version of Unicode" ) 00118 . ". Appropriate file(s) should be available at:"; 00119 } 00120 $error .= "\n"; 00121 00122 if ( $versionKnown && $unicodeVersion ) { 00123 $allkeysURL = str_replace( "<Unicode version>", "$unicodeVersion.0", $allkeysURL ); 00124 $ucdallURL = str_replace( "<Unicode version>", "$unicodeVersion.0", $ucdallURL ); 00125 } 00126 00127 if ( !$allkeysPresent ) { 00128 $error .= "* $allkeysURL\n"; 00129 } 00130 if ( !$ucdallPresent ) { 00131 $error .= "* $ucdallURL\n"; 00132 } 00133 00134 $this->error( $error ); 00135 exit( 1 ); 00136 } 00137 00138 $debugOutFileName = $this->getOption( 'debug-output' ); 00139 if ( $debugOutFileName ) { 00140 $this->debugOutFile = fopen( $debugOutFileName, 'w' ); 00141 if ( !$this->debugOutFile ) { 00142 $this->error( "Unable to open debug output file for writing" ); 00143 exit( 1 ); 00144 } 00145 } 00146 $this->loadUcd(); 00147 $this->generateFirstChars(); 00148 } 00149 00150 function loadUcd() { 00151 $uxr = new UcdXmlReader( "{$this->dataDir}/ucd.all.grouped.xml" ); 00152 $uxr->readChars( array( $this, 'charCallback' ) ); 00153 } 00154 00155 function charCallback( $data ) { 00156 // Skip non-printable characters, 00157 // but do not skip a normal space (U+0020) since 00158 // people like to use that as a fake no header symbol. 00159 $category = substr( $data['gc'], 0, 1 ); 00160 if ( strpos( 'LNPS', $category ) === false 00161 && $data['cp'] !== '0020' 00162 ) { 00163 return; 00164 } 00165 $cp = hexdec( $data['cp'] ); 00166 00167 // Skip the CJK ideograph blocks, as an optimisation measure. 00168 // UCA doesn't sort them properly anyway, without tailoring. 00169 if ( IcuCollation::isCjk( $cp ) ) { 00170 return; 00171 } 00172 00173 // Skip the composed Hangul syllables, we will use the bare Jamo 00174 // as first letters 00175 if ( $data['block'] == 'Hangul Syllables' ) { 00176 return; 00177 } 00178 00179 // Calculate implicit weight per UTS #10 v6.0.0, sec 7.1.3 00180 if ( $data['UIdeo'] === 'Y' ) { 00181 if ( $data['block'] == 'CJK Unified Ideographs' 00182 || $data['block'] == 'CJK Compatibility Ideographs' 00183 ) { 00184 $base = 0xFB40; 00185 } else { 00186 $base = 0xFB80; 00187 } 00188 } else { 00189 $base = 0xFBC0; 00190 } 00191 $a = $base + ( $cp >> 15 ); 00192 $b = ( $cp & 0x7fff ) | 0x8000; 00193 00194 $this->weights[$cp] = sprintf( ".%04X.%04X", $a, $b ); 00195 00196 if ( $data['dm'] !== '#' ) { 00197 $this->mappedChars[$cp] = true; 00198 } 00199 00200 if ( $cp % 4096 == 0 ) { 00201 print "{$data['cp']}\n"; 00202 } 00203 } 00204 00205 function generateFirstChars() { 00206 $file = fopen( "{$this->dataDir}/allkeys.txt", 'r' ); 00207 if ( !$file ) { 00208 $this->error( "Unable to open allkeys.txt" ); 00209 exit( 1 ); 00210 } 00211 global $IP; 00212 $outFile = fopen( "$IP/serialized/first-letters-root.ser", 'w' ); 00213 if ( !$outFile ) { 00214 $this->error( "Unable to open output file first-letters-root.ser" ); 00215 exit( 1 ); 00216 } 00217 00218 $goodTertiaryChars = array(); 00219 00220 // For each character with an entry in allkeys.txt, overwrite the implicit 00221 // entry in $this->weights that came from the UCD. 00222 // Also gather a list of tertiary weights, for use in selecting the group header 00223 while ( false !== ( $line = fgets( $file ) ) ) { 00224 // We're only interested in single-character weights, pick them out with a regex 00225 $line = trim( $line ); 00226 if ( !preg_match( '/^([0-9A-F]+)\s*;\s*([^#]*)/', $line, $m ) ) { 00227 continue; 00228 } 00229 00230 $cp = hexdec( $m[1] ); 00231 $allWeights = trim( $m[2] ); 00232 $primary = ''; 00233 $tertiary = ''; 00234 00235 if ( !isset( $this->weights[$cp] ) ) { 00236 // Non-printable, ignore 00237 continue; 00238 } 00239 foreach ( StringUtils::explode( '[', $allWeights ) as $weightStr ) { 00240 preg_match_all( '/[*.]([0-9A-F]+)/', $weightStr, $m ); 00241 if ( !empty( $m[1] ) ) { 00242 if ( $m[1][0] !== '0000' ) { 00243 $primary .= '.' . $m[1][0]; 00244 } 00245 if ( $m[1][2] !== '0000' ) { 00246 $tertiary .= '.' . $m[1][2]; 00247 } 00248 } 00249 } 00250 $this->weights[$cp] = $primary; 00251 if ( $tertiary === '.0008' 00252 || $tertiary === '.000E' 00253 ) { 00254 $goodTertiaryChars[$cp] = true; 00255 } 00256 } 00257 fclose( $file ); 00258 00259 // Identify groups of characters with the same primary weight 00260 $this->groups = array(); 00261 asort( $this->weights, SORT_STRING ); 00262 $prevWeight = reset( $this->weights ); 00263 $group = array(); 00264 foreach ( $this->weights as $cp => $weight ) { 00265 if ( $weight !== $prevWeight ) { 00266 $this->groups[$prevWeight] = $group; 00267 $prevWeight = $weight; 00268 if ( isset( $this->groups[$weight] ) ) { 00269 $group = $this->groups[$weight]; 00270 } else { 00271 $group = array(); 00272 } 00273 } 00274 $group[] = $cp; 00275 } 00276 if ( $group ) { 00277 $this->groups[$prevWeight] = $group; 00278 } 00279 00280 // If one character has a given primary weight sequence, and a second 00281 // character has a longer primary weight sequence with an initial 00282 // portion equal to the first character, then remove the second 00283 // character. This avoids having characters like U+A732 (double A) 00284 // polluting the basic latin sort area. 00285 00286 foreach ( $this->groups as $weight => $group ) { 00287 if ( preg_match( '/(\.[0-9A-F]*)\./', $weight, $m ) ) { 00288 if ( isset( $this->groups[$m[1]] ) ) { 00289 unset( $this->groups[$weight] ); 00290 } 00291 } 00292 } 00293 00294 ksort( $this->groups, SORT_STRING ); 00295 00296 // Identify the header character in each group 00297 $headerChars = array(); 00298 $prevChar = "\000"; 00299 $tertiaryCollator = new Collator( 'root' ); 00300 $primaryCollator = new Collator( 'root' ); 00301 $primaryCollator->setStrength( Collator::PRIMARY ); 00302 $numOutOfOrder = 0; 00303 foreach ( $this->groups as $weight => $group ) { 00304 $uncomposedChars = array(); 00305 $goodChars = array(); 00306 foreach ( $group as $cp ) { 00307 if ( isset( $goodTertiaryChars[$cp] ) ) { 00308 $goodChars[] = $cp; 00309 } 00310 if ( !isset( $this->mappedChars[$cp] ) ) { 00311 $uncomposedChars[] = $cp; 00312 } 00313 } 00314 $x = array_intersect( $goodChars, $uncomposedChars ); 00315 if ( !$x ) { 00316 $x = $uncomposedChars; 00317 if ( !$x ) { 00318 $x = $group; 00319 } 00320 } 00321 00322 // Use ICU to pick the lowest sorting character in the selection 00323 $tertiaryCollator->sort( $x ); 00324 $cp = $x[0]; 00325 00326 $char = codepointToUtf8( $cp ); 00327 $headerChars[] = $char; 00328 if ( $primaryCollator->compare( $char, $prevChar ) <= 0 ) { 00329 $numOutOfOrder++; 00330 /* 00331 printf( "Out of order: U+%05X > U+%05X\n", 00332 utf8ToCodepoint( $prevChar ), 00333 utf8ToCodepoint( $char ) ); 00334 */ 00335 } 00336 $prevChar = $char; 00337 00338 if ( $this->debugOutFile ) { 00339 fwrite( $this->debugOutFile, sprintf( "%05X %s %s (%s)\n", $cp, $weight, $char, 00340 implode( ' ', array_map( 'codepointToUtf8', $group ) ) ) ); 00341 } 00342 } 00343 00344 print "Out of order: $numOutOfOrder / " . count( $headerChars ) . "\n"; 00345 00346 fwrite( $outFile, serialize( $headerChars ) ); 00347 } 00348 } 00349 00350 class UcdXmlReader { 00351 public $fileName; 00352 public $callback; 00353 public $groupAttrs; 00354 public $xml; 00355 public $blocks = array(); 00356 public $currentBlock; 00357 00358 function __construct( $fileName ) { 00359 $this->fileName = $fileName; 00360 } 00361 00362 public function readChars( $callback ) { 00363 $this->getBlocks(); 00364 $this->currentBlock = reset( $this->blocks ); 00365 $xml = $this->open(); 00366 $this->callback = $callback; 00367 00368 while ( $xml->name !== 'repertoire' && $xml->next() ); 00369 00370 while ( $xml->read() ) { 00371 if ( $xml->nodeType == XMLReader::ELEMENT ) { 00372 if ( $xml->name === 'group' ) { 00373 $this->groupAttrs = $this->readAttributes(); 00374 } elseif ( $xml->name === 'char' ) { 00375 $this->handleChar(); 00376 } 00377 } elseif ( $xml->nodeType === XMLReader::END_ELEMENT ) { 00378 if ( $xml->name === 'group' ) { 00379 $this->groupAttrs = array(); 00380 } 00381 } 00382 } 00383 $xml->close(); 00384 } 00385 00386 protected function open() { 00387 $this->xml = new XMLReader; 00388 $this->xml->open( $this->fileName ); 00389 if ( !$this->xml ) { 00390 throw new MWException( __METHOD__ . ": unable to open {$this->fileName}" ); 00391 } 00392 while ( $this->xml->name !== 'ucd' && $this->xml->read() ); 00393 $this->xml->read(); 00394 00395 return $this->xml; 00396 } 00397 00403 protected function readAttributes() { 00404 $attrs = array(); 00405 while ( $this->xml->moveToNextAttribute() ) { 00406 $attrs[$this->xml->name] = $this->xml->value; 00407 } 00408 00409 return $attrs; 00410 } 00411 00412 protected function handleChar() { 00413 $attrs = $this->readAttributes() + $this->groupAttrs; 00414 if ( isset( $attrs['cp'] ) ) { 00415 $first = $last = hexdec( $attrs['cp'] ); 00416 } else { 00417 $first = hexdec( $attrs['first-cp'] ); 00418 $last = hexdec( $attrs['last-cp'] ); 00419 unset( $attrs['first-cp'] ); 00420 unset( $attrs['last-cp'] ); 00421 } 00422 00423 for ( $cp = $first; $cp <= $last; $cp++ ) { 00424 $hexCp = sprintf( "%04X", $cp ); 00425 foreach ( array( 'na', 'na1' ) as $nameProp ) { 00426 if ( isset( $attrs[$nameProp] ) ) { 00427 $attrs[$nameProp] = str_replace( '#', $hexCp, $attrs[$nameProp] ); 00428 } 00429 } 00430 00431 while ( $this->currentBlock ) { 00432 if ( $cp < $this->currentBlock[0] ) { 00433 break; 00434 } elseif ( $cp <= $this->currentBlock[1] ) { 00435 $attrs['block'] = key( $this->blocks ); 00436 break; 00437 } else { 00438 $this->currentBlock = next( $this->blocks ); 00439 } 00440 } 00441 00442 $attrs['cp'] = $hexCp; 00443 call_user_func( $this->callback, $attrs ); 00444 } 00445 } 00446 00447 public function getBlocks() { 00448 if ( $this->blocks ) { 00449 return $this->blocks; 00450 } 00451 00452 $xml = $this->open(); 00453 while ( $xml->name !== 'blocks' && $xml->read() ); 00454 00455 while ( $xml->read() ) { 00456 if ( $xml->nodeType == XMLReader::ELEMENT ) { 00457 if ( $xml->name === 'block' ) { 00458 $attrs = $this->readAttributes(); 00459 $first = hexdec( $attrs['first-cp'] ); 00460 $last = hexdec( $attrs['last-cp'] ); 00461 $this->blocks[$attrs['name']] = array( $first, $last ); 00462 } 00463 } 00464 } 00465 $xml->close(); 00466 00467 return $this->blocks; 00468 } 00469 } 00470 00471 $maintClass = 'GenerateCollationData'; 00472 require_once RUN_MAINTENANCE_IF_MAIN;