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