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