MediaWiki
REL1_21
|
00001 <?php 00023 abstract class Collation { 00024 static $instance; 00025 00029 static function singleton() { 00030 if ( !self::$instance ) { 00031 global $wgCategoryCollation; 00032 self::$instance = self::factory( $wgCategoryCollation ); 00033 } 00034 return self::$instance; 00035 } 00036 00042 static function factory( $collationName ) { 00043 switch( $collationName ) { 00044 case 'uppercase': 00045 return new UppercaseCollation; 00046 case 'identity': 00047 return new IdentityCollation; 00048 case 'uca-default': 00049 return new IcuCollation( 'root' ); 00050 default: 00051 $match = array(); 00052 if ( preg_match( '/^uca-([a-z-]+)$/', $collationName, $match ) ) { 00053 return new IcuCollation( $match[1] ); 00054 } 00055 00056 # Provide a mechanism for extensions to hook in. 00057 $collationObject = null; 00058 wfRunHooks( 'Collation::factory', array( $collationName, &$collationObject ) ); 00059 00060 if ( $collationObject instanceof Collation ) { 00061 return $collationObject; 00062 } 00063 00064 // If all else fails... 00065 throw new MWException( __METHOD__.": unknown collation type \"$collationName\"" ); 00066 } 00067 } 00068 00080 abstract function getSortKey( $string ); 00081 00105 abstract function getFirstLetter( $string ); 00106 } 00107 00108 class UppercaseCollation extends Collation { 00109 var $lang; 00110 function __construct() { 00111 // Get a language object so that we can use the generic UTF-8 uppercase 00112 // function there 00113 $this->lang = Language::factory( 'en' ); 00114 } 00115 00116 function getSortKey( $string ) { 00117 return $this->lang->uc( $string ); 00118 } 00119 00120 function getFirstLetter( $string ) { 00121 if ( $string[0] == "\0" ) { 00122 $string = substr( $string, 1 ); 00123 } 00124 return $this->lang->ucfirst( $this->lang->firstChar( $string ) ); 00125 } 00126 } 00127 00134 class IdentityCollation extends Collation { 00135 00136 function getSortKey( $string ) { 00137 return $string; 00138 } 00139 00140 function getFirstLetter( $string ) { 00141 global $wgContLang; 00142 // Copied from UppercaseCollation. 00143 // I'm kind of unclear on when this could happen... 00144 if ( $string[0] == "\0" ) { 00145 $string = substr( $string, 1 ); 00146 } 00147 return $wgContLang->firstChar( $string ); 00148 } 00149 } 00150 00151 class IcuCollation extends Collation { 00152 const FIRST_LETTER_VERSION = 1; 00153 00154 var $primaryCollator, $mainCollator, $locale; 00155 var $firstLetterData; 00156 00166 static $cjkBlocks = array( 00167 array( 0x2E80, 0x2EFF ), // CJK Radicals Supplement 00168 array( 0x2F00, 0x2FDF ), // Kangxi Radicals 00169 array( 0x2FF0, 0x2FFF ), // Ideographic Description Characters 00170 array( 0x3000, 0x303F ), // CJK Symbols and Punctuation 00171 array( 0x31C0, 0x31EF ), // CJK Strokes 00172 array( 0x3200, 0x32FF ), // Enclosed CJK Letters and Months 00173 array( 0x3300, 0x33FF ), // CJK Compatibility 00174 array( 0x3400, 0x4DBF ), // CJK Unified Ideographs Extension A 00175 array( 0x4E00, 0x9FFF ), // CJK Unified Ideographs 00176 array( 0xF900, 0xFAFF ), // CJK Compatibility Ideographs 00177 array( 0xFE30, 0xFE4F ), // CJK Compatibility Forms 00178 array( 0x20000, 0x2A6DF ), // CJK Unified Ideographs Extension B 00179 array( 0x2A700, 0x2B73F ), // CJK Unified Ideographs Extension C 00180 array( 0x2B740, 0x2B81F ), // CJK Unified Ideographs Extension D 00181 array( 0x2F800, 0x2FA1F ), // CJK Compatibility Ideographs Supplement 00182 ); 00183 00205 static $tailoringFirstLetters = array( 00206 // Verified by native speakers 00207 'be' => array( "Ё" ), 00208 'be-tarask' => array( "Ё" ), 00209 'en' => array(), 00210 'fi' => array( "Å", "Ä", "Ö" ), 00211 'hu' => array( "Cs", "Dz", "Dzs", "Gy", "Ly", "Ny", "Ö", "Sz", "Ty", "Ü", "Zs" ), 00212 'it' => array(), 00213 'pl' => array( "Ą", "Ć", "Ę", "Ł", "Ń", "Ó", "Ś", "Ź", "Ż" ), 00214 'pt' => array(), 00215 'ru' => array(), 00216 'sv' => array( "Å", "Ä", "Ö" ), 00217 'uk' => array( "Ґ", "Ь" ), 00218 'vi' => array( "Ă", "Â", "Đ", "Ê", "Ô", "Ơ", "Ư" ), 00219 // Not verified, but likely correct 00220 'af' => array(), 00221 'ast' => array( "Ch", "Ll", "Ñ" ), 00222 'az' => array( "Ç", "Ə", "Ğ", "İ", "Ö", "Ş", "Ü" ), 00223 'bg' => array(), 00224 'br' => array( "Ch", "C'h" ), 00225 'bs' => array( "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ), 00226 'ca' => array(), 00227 'co' => array(), 00228 'cs' => array( "Č", "Ch", "Ř", "Š", "Ž" ), 00229 'cy' => array( "Ch", "Dd", "Ff", "Ng", "Ll", "Ph", "Rh", "Th" ), 00230 'da' => array( "Æ", "Ø", "Å" ), 00231 'de' => array(), 00232 'dsb' => array( "Č", "Ć", "Dź", "Ě", "Ch", "Ł", "Ń", "Ŕ", "Š", "Ś", "Ž", "Ź" ), 00233 'el' => array(), 00234 'eo' => array( "Ĉ", "Ĝ", "Ĥ", "Ĵ", "Ŝ", "Ŭ" ), 00235 'es' => array( "Ñ" ), 00236 'et' => array( "Š", "Ž", "Õ", "Ä", "Ö", "Ü" ), 00237 'eu' => array( "Ñ" ), 00238 'fo' => array( "Á", "Ð", "Í", "Ó", "Ú", "Ý", "Æ", "Ø", "Å" ), 00239 'fr' => array(), 00240 'fur' => array( "À", "Á", "Â", "È", "Ì", "Ò", "Ù" ), 00241 'fy' => array(), 00242 'ga' => array(), 00243 'gd' => array(), 00244 'gl' => array( "Ch", "Ll", "Ñ" ), 00245 'hr' => array( "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ), 00246 'hsb' => array( "Č", "Dź", "Ě", "Ch", "Ł", "Ń", "Ř", "Š", "Ć", "Ž" ), 00247 'is' => array( "Á", "Ð", "É", "Í", "Ó", "Ú", "Ý", "Þ", "Æ", "Ö", "Å" ), 00248 'kk' => array( "Ү", "І" ), 00249 'kl' => array( "Æ", "Ø", "Å" ), 00250 'ku' => array( "Ç", "Ê", "Î", "Ş", "Û" ), 00251 'ky' => array( "Ё" ), 00252 'la' => array(), 00253 'lb' => array(), 00254 'lt' => array( "Č", "Š", "Ž" ), 00255 'lv' => array( "Č", "Ģ", "Ķ", "Ļ", "Ņ", "Š", "Ž" ), 00256 'mk' => array(), 00257 'mo' => array( "Ă", "Â", "Î", "Ş", "Ţ" ), 00258 'mt' => array( "Ċ", "Ġ", "Għ", "Ħ", "Ż" ), 00259 'nl' => array(), 00260 'no' => array( "Æ", "Ø", "Å" ), 00261 'oc' => array(), 00262 'rm' => array(), 00263 'ro' => array( "Ă", "Â", "Î", "Ş", "Ţ" ), 00264 'rup' => array( "Ă", "Â", "Î", "Ľ", "Ń", "Ş", "Ţ" ), 00265 'sco' => array(), 00266 'sk' => array( "Ä", "Č", "Ch", "Ô", "Š", "Ž" ), 00267 'sl' => array( "Č", "Š", "Ž" ), 00268 'smn' => array( "Á", "Č", "Đ", "Ŋ", "Š", "Ŧ", "Ž", "Æ", "Ø", "Å", "Ä", "Ö" ), 00269 'sq' => array( "Ç", "Dh", "Ë", "Gj", "Ll", "Nj", "Rr", "Sh", "Th", "Xh", "Zh" ), 00270 'sr' => array(), 00271 'tk' => array( "Ç", "Ä", "Ž", "Ň", "Ö", "Ş", "Ü", "Ý" ), 00272 'tl' => array( "Ñ", "Ng" ), 00273 'tr' => array( "Ç", "Ğ", "İ", "Ö", "Ş", "Ü" ), 00274 'tt' => array( "Ә", "Ө", "Ү", "Җ", "Ң", "Һ" ), 00275 'uz' => array( "Ch", "G'", "Ng", "O'", "Sh" ), 00276 ); 00277 00278 const RECORD_LENGTH = 14; 00279 00280 function __construct( $locale ) { 00281 if ( !extension_loaded( 'intl' ) ) { 00282 throw new MWException( 'An ICU collation was requested, ' . 00283 'but the intl extension is not available.' ); 00284 } 00285 $this->locale = $locale; 00286 $this->mainCollator = Collator::create( $locale ); 00287 if ( !$this->mainCollator ) { 00288 throw new MWException( "Invalid ICU locale specified for collation: $locale" ); 00289 } 00290 00291 $this->primaryCollator = Collator::create( $locale ); 00292 $this->primaryCollator->setStrength( Collator::PRIMARY ); 00293 } 00294 00295 function getSortKey( $string ) { 00296 // intl extension produces non null-terminated 00297 // strings. Appending '' fixes it so that it doesn't generate 00298 // a warning on each access in debug php. 00299 wfSuppressWarnings(); 00300 $key = $this->mainCollator->getSortKey( $string ) . ''; 00301 wfRestoreWarnings(); 00302 return $key; 00303 } 00304 00305 function getPrimarySortKey( $string ) { 00306 wfSuppressWarnings(); 00307 $key = $this->primaryCollator->getSortKey( $string ) . ''; 00308 wfRestoreWarnings(); 00309 return $key; 00310 } 00311 00312 function getFirstLetter( $string ) { 00313 $string = strval( $string ); 00314 if ( $string === '' ) { 00315 return ''; 00316 } 00317 00318 // Check for CJK 00319 $firstChar = mb_substr( $string, 0, 1, 'UTF-8' ); 00320 if ( ord( $firstChar ) > 0x7f 00321 && self::isCjk( utf8ToCodepoint( $firstChar ) ) ) 00322 { 00323 return $firstChar; 00324 } 00325 00326 $sortKey = $this->getPrimarySortKey( $string ); 00327 00328 // Do a binary search to find the correct letter to sort under 00329 $min = $this->findLowerBound( 00330 array( $this, 'getSortKeyByLetterIndex' ), 00331 $this->getFirstLetterCount(), 00332 'strcmp', 00333 $sortKey ); 00334 00335 if ( $min === false ) { 00336 // Before the first letter 00337 return ''; 00338 } 00339 return $this->getLetterByIndex( $min ); 00340 } 00341 00342 function getFirstLetterData() { 00343 if ( $this->firstLetterData !== null ) { 00344 return $this->firstLetterData; 00345 } 00346 00347 $cache = wfGetCache( CACHE_ANYTHING ); 00348 $cacheKey = wfMemcKey( 'first-letters', $this->locale ); 00349 $cacheEntry = $cache->get( $cacheKey ); 00350 00351 if ( $cacheEntry && isset( $cacheEntry['version'] ) 00352 && $cacheEntry['version'] == self::FIRST_LETTER_VERSION ) 00353 { 00354 $this->firstLetterData = $cacheEntry; 00355 return $this->firstLetterData; 00356 } 00357 00358 // Generate data from serialized data file 00359 00360 if ( isset ( self::$tailoringFirstLetters[$this->locale] ) ) { 00361 $letters = wfGetPrecompiledData( "first-letters-root.ser" ); 00362 // Append additional characters 00363 $letters = array_merge( $letters, self::$tailoringFirstLetters[$this->locale] ); 00364 // Remove unnecessary ones, if any 00365 if ( isset( self::$tailoringFirstLetters[ '-' . $this->locale ] ) ) { 00366 $letters = array_diff( $letters, self::$tailoringFirstLetters[ '-' . $this->locale ] ); 00367 } 00368 } else { 00369 $letters = wfGetPrecompiledData( "first-letters-{$this->locale}.ser" ); 00370 if ( $letters === false ) { 00371 throw new MWException( "MediaWiki does not support ICU locale " . 00372 "\"{$this->locale}\"" ); 00373 } 00374 } 00375 00376 // Sort the letters. 00377 // 00378 // It's impossible to have the precompiled data file properly sorted, 00379 // because the sort order changes depending on ICU version. If the 00380 // array is not properly sorted, the binary search will return random 00381 // results. 00382 // 00383 // We also take this opportunity to remove primary collisions. 00384 $letterMap = array(); 00385 foreach ( $letters as $letter ) { 00386 $key = $this->getPrimarySortKey( $letter ); 00387 if ( isset( $letterMap[$key] ) ) { 00388 // Primary collision 00389 // Keep whichever one sorts first in the main collator 00390 if ( $this->mainCollator->compare( $letter, $letterMap[$key] ) < 0 ) { 00391 $letterMap[$key] = $letter; 00392 } 00393 } else { 00394 $letterMap[$key] = $letter; 00395 } 00396 } 00397 ksort( $letterMap, SORT_STRING ); 00398 // Remove duplicate prefixes. Basically if something has a sortkey 00399 // which is a prefix of some other sortkey, then it is an 00400 // expansion and probably should not be considered a section 00401 // header. 00402 // 00403 // For example 'þ' is sometimes sorted as if it is the letters 00404 // 'th'. Other times it is its own primary element. Another 00405 // example is '₨'. Sometimes its a currency symbol. Sometimes it 00406 // is an 'R' followed by an 's'. 00407 // 00408 // Additionally an expanded element should always sort directly 00409 // after its first element due to they way sortkeys work. 00410 // 00411 // UCA sortkey elements are of variable length but no collation 00412 // element should be a prefix of some other element, so I think 00413 // this is safe. See: 00414 // * https://ssl.icu-project.org/repos/icu/icuhtml/trunk/design/collation/ICU_collation_design.htm 00415 // * http://site.icu-project.org/design/collation/uca-weight-allocation 00416 // 00417 // Additionally, there is something called primary compression to 00418 // worry about. Basically, if you have two primary elements that 00419 // are more than one byte and both start with the same byte then 00420 // the first byte is dropped on the second primary. Additionally 00421 // either \x03 or \xFF may be added to mean that the next primary 00422 // does not start with the first byte of the first primary. 00423 // 00424 // This shouldn't matter much, as the first primary is not 00425 // changed, and that is what we are comparing against. 00426 // 00427 // tl;dr: This makes some assumptions about how icu implements 00428 // collations. It seems incredibly unlikely these assumptions 00429 // will change, but nonetheless they are assumptions. 00430 00431 $prev = false; 00432 $duplicatePrefixes = array(); 00433 foreach( $letterMap as $key => $value ) { 00434 // Remove terminator byte. Otherwise the prefix 00435 // comparison will get hung up on that. 00436 $trimmedKey = rtrim( $key, "\0" ); 00437 if ( $prev === false || $prev === '' ) { 00438 $prev = $trimmedKey; 00439 // We don't yet have a collation element 00440 // to compare against, so continue. 00441 continue; 00442 } 00443 00444 // Due to the fact the array is sorted, we only have 00445 // to compare with the element directly previous 00446 // to the current element (skipping expansions). 00447 // An element "X" will always sort directly 00448 // before "XZ" (Unless we have "XY", but we 00449 // do not update $prev in that case). 00450 if ( substr( $trimmedKey, 0, strlen( $prev ) ) === $prev ) { 00451 $duplicatePrefixes[] = $key; 00452 // If this is an expansion, we don't want to 00453 // compare the next element to this element, 00454 // but to what is currently $prev 00455 continue; 00456 } 00457 $prev = $trimmedKey; 00458 } 00459 foreach( $duplicatePrefixes as $badKey ) { 00460 wfDebug( "Removing '{$letterMap[$badKey]}' from first letters." ); 00461 unset( $letterMap[$badKey] ); 00462 // This code assumes that unsetting does not change sort order. 00463 } 00464 $data = array( 00465 'chars' => array_values( $letterMap ), 00466 'keys' => array_keys( $letterMap ), 00467 'version' => self::FIRST_LETTER_VERSION, 00468 ); 00469 00470 // Reduce memory usage before caching 00471 unset( $letterMap ); 00472 00473 // Save to cache 00474 $this->firstLetterData = $data; 00475 $cache->set( $cacheKey, $data, 86400 * 7 /* 1 week */ ); 00476 return $data; 00477 } 00478 00479 function getLetterByIndex( $index ) { 00480 if ( $this->firstLetterData === null ) { 00481 $this->getFirstLetterData(); 00482 } 00483 return $this->firstLetterData['chars'][$index]; 00484 } 00485 00486 function getSortKeyByLetterIndex( $index ) { 00487 if ( $this->firstLetterData === null ) { 00488 $this->getFirstLetterData(); 00489 } 00490 return $this->firstLetterData['keys'][$index]; 00491 } 00492 00493 function getFirstLetterCount() { 00494 if ( $this->firstLetterData === null ) { 00495 $this->getFirstLetterData(); 00496 } 00497 return count( $this->firstLetterData['chars'] ); 00498 } 00499 00515 function findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target ) { 00516 if ( $valueCount === 0 ) { 00517 return false; 00518 } 00519 00520 $min = 0; 00521 $max = $valueCount; 00522 do { 00523 $mid = $min + ( ( $max - $min ) >> 1 ); 00524 $item = call_user_func( $valueCallback, $mid ); 00525 $comparison = call_user_func( $comparisonCallback, $target, $item ); 00526 if ( $comparison > 0 ) { 00527 $min = $mid; 00528 } elseif ( $comparison == 0 ) { 00529 $min = $mid; 00530 break; 00531 } else { 00532 $max = $mid; 00533 } 00534 } while ( $min < $max - 1 ); 00535 00536 if ( $min == 0 ) { 00537 $item = call_user_func( $valueCallback, $min ); 00538 $comparison = call_user_func( $comparisonCallback, $target, $item ); 00539 if ( $comparison < 0 ) { 00540 // Before the first item 00541 return false; 00542 } 00543 } 00544 return $min; 00545 } 00546 00547 static function isCjk( $codepoint ) { 00548 foreach ( self::$cjkBlocks as $block ) { 00549 if ( $codepoint >= $block[0] && $codepoint <= $block[1] ) { 00550 return true; 00551 } 00552 } 00553 return false; 00554 } 00555 00568 static function getICUVersion() { 00569 return defined( 'INTL_ICU_VERSION' ) ? INTL_ICU_VERSION : false; 00570 } 00571 00579 static function getUnicodeVersionForICU() { 00580 $icuVersion = IcuCollation::getICUVersion(); 00581 if ( !$icuVersion ) { 00582 return false; 00583 } 00584 00585 $versionPrefix = substr( $icuVersion, 0, 3 ); 00586 // Source: http://site.icu-project.org/download 00587 $map = array( 00588 '50.' => '6.2', 00589 '49.' => '6.1', 00590 '4.8' => '6.0', 00591 '4.6' => '6.0', 00592 '4.4' => '5.2', 00593 '4.2' => '5.1', 00594 '4.0' => '5.1', 00595 '3.8' => '5.0', 00596 '3.6' => '5.0', 00597 '3.4' => '4.1', 00598 ); 00599 00600 if ( isset( $map[$versionPrefix] ) ) { 00601 return $map[$versionPrefix]; 00602 } else { 00603 return false; 00604 } 00605 } 00606 }