3 abstract class Collation
{
9 static function singleton() {
10 if ( !self
::$instance ) {
11 global $wgCategoryCollation;
12 self
::$instance = self
::factory( $wgCategoryCollation );
14 return self
::$instance;
19 * @param $collationName string
22 static function factory( $collationName ) {
23 switch( $collationName ) {
25 return new UppercaseCollation
;
27 return new IcuCollation( 'root' );
29 throw new MWException( __METHOD__
.": unknown collation type \"$collationName\"" );
34 * Given a string, convert it to a (hopefully short) key that can be used
35 * for efficient sorting. A binary sort according to the sortkeys
36 * corresponds to a logical sort of the corresponding strings. Current
37 * code expects that a line feed character should sort before all others, but
38 * has no other particular expectations (and that one can be changed if
41 * @param string $string UTF-8 string
42 * @return string Binary sortkey
44 abstract function getSortKey( $string );
47 * Given a string, return the logical "first letter" to be used for
48 * grouping on category pages and so on. This has to be coordinated
49 * carefully with convertToSortkey(), or else the sorted list might jump
50 * back and forth between the same "initial letters" or other pathological
51 * behavior. For instance, if you just return the first character, but "a"
52 * sorts the same as "A" based on getSortKey(), then you might get a
64 * etc., assuming for the sake of argument that $wgCapitalLinks is false.
66 * @param string $string UTF-8 string
67 * @return string UTF-8 string corresponding to the first letter of input
69 abstract function getFirstLetter( $string );
72 class UppercaseCollation
extends Collation
{
74 function __construct() {
75 // Get a language object so that we can use the generic UTF-8 uppercase
77 $this->lang
= Language
::factory( 'en' );
80 function getSortKey( $string ) {
81 return $this->lang
->uc( $string );
84 function getFirstLetter( $string ) {
85 if ( $string[0] == "\0" ) {
86 $string = substr( $string, 1 );
88 return $this->lang
->ucfirst( $this->lang
->firstChar( $string ) );
92 class IcuCollation
extends Collation
{
93 var $primaryCollator, $mainCollator, $locale;
99 * The same definition of a CJK block must be used for both Collation and
100 * generateCollationData.php. These blocks are omitted from the first
101 * letter data, as an optimisation measure and because the default UCA table
102 * is pretty useless for sorting Chinese text anyway. Japanese and Korean
103 * blocks are not included here, because they are smaller and more useful.
105 static $cjkBlocks = array(
106 array( 0x2E80, 0x2EFF ), // CJK Radicals Supplement
107 array( 0x2F00, 0x2FDF ), // Kangxi Radicals
108 array( 0x2FF0, 0x2FFF ), // Ideographic Description Characters
109 array( 0x3000, 0x303F ), // CJK Symbols and Punctuation
110 array( 0x31C0, 0x31EF ), // CJK Strokes
111 array( 0x3200, 0x32FF ), // Enclosed CJK Letters and Months
112 array( 0x3300, 0x33FF ), // CJK Compatibility
113 array( 0x3400, 0x4DBF ), // CJK Unified Ideographs Extension A
114 array( 0x4E00, 0x9FFF ), // CJK Unified Ideographs
115 array( 0xF900, 0xFAFF ), // CJK Compatibility Ideographs
116 array( 0xFE30, 0xFE4F ), // CJK Compatibility Forms
117 array( 0x20000, 0x2A6DF ), // CJK Unified Ideographs Extension B
118 array( 0x2A700, 0x2B73F ), // CJK Unified Ideographs Extension C
119 array( 0x2B740, 0x2B81F ), // CJK Unified Ideographs Extension D
120 array( 0x2F800, 0x2FA1F ), // CJK Compatibility Ideographs Supplement
123 const RECORD_LENGTH
= 14;
125 function __construct( $locale ) {
126 if ( !extension_loaded( 'intl' ) ) {
127 throw new MWException( 'An ICU collation was requested, ' .
128 'but the intl extension is not available.' );
130 $this->locale
= $locale;
131 $this->mainCollator
= Collator
::create( $locale );
132 if ( !$this->mainCollator
) {
133 throw new MWException( "Invalid ICU locale specified for collation: $locale" );
136 $this->primaryCollator
= Collator
::create( $locale );
137 $this->primaryCollator
->setStrength( Collator
::PRIMARY
);
140 function getSortKey( $string ) {
141 // intl extension produces non null-terminated
142 // strings. Appending '' fixes it so that it doesn't generate
143 // a warning on each access in debug php.
144 wfSuppressWarnings();
145 $key = $this->mainCollator
->getSortKey( $string ) . '';
150 function getPrimarySortKey( $string ) {
151 wfSuppressWarnings();
152 $key = $this->primaryCollator
->getSortKey( $string ) . '';
157 function getFirstLetter( $string ) {
158 $string = strval( $string );
159 if ( $string === '' ) {
164 $firstChar = mb_substr( $string, 0, 1, 'UTF-8' );
165 if ( ord( $firstChar ) > 0x7f
166 && self
::isCjk( utf8ToCodepoint( $firstChar ) ) )
171 $sortKey = $this->getPrimarySortKey( $string );
173 // Do a binary search to find the correct letter to sort under
174 $min = $this->findLowerBound(
175 array( $this, 'getSortKeyByLetterIndex' ),
176 $this->getFirstLetterCount(),
180 if ( $min === false ) {
181 // Before the first letter
184 return $this->getLetterByIndex( $min );
187 function getFirstLetterData() {
188 if ( $this->firstLetterData
!== null ) {
189 return $this->firstLetterData
;
192 $cache = wfGetCache( CACHE_ANYTHING
);
193 $cacheKey = wfMemcKey( 'first-letters', $this->locale
);
194 $cacheEntry = $cache->get( $cacheKey );
197 $this->firstLetterData
= $cacheEntry;
198 return $this->firstLetterData
;
201 // Generate data from serialized data file
203 $letters = wfGetPrecompiledData( "first-letters-{$this->locale}.ser" );
204 if ( $letters === false ) {
205 throw new MWException( "MediaWiki does not support ICU locale " .
206 "\"{$this->locale}\"" );
211 // It's impossible to have the precompiled data file properly sorted,
212 // because the sort order changes depending on ICU version. If the
213 // array is not properly sorted, the binary search will return random
216 // We also take this opportunity to remove primary collisions.
217 $letterMap = array();
218 foreach ( $letters as $letter ) {
219 $key = $this->getPrimarySortKey( $letter );
220 if ( isset( $letterMap[$key] ) ) {
222 // Keep whichever one sorts first in the main collator
223 if ( $this->mainCollator
->compare( $letter, $letterMap[$key] ) < 0 ) {
224 $letterMap[$key] = $letter;
227 $letterMap[$key] = $letter;
230 ksort( $letterMap, SORT_STRING
);
232 'chars' => array_values( $letterMap ),
233 'keys' => array_keys( $letterMap )
236 // Reduce memory usage before caching
240 $this->firstLetterData
= $data;
241 $cache->set( $cacheKey, $data, 86400 * 7 /* 1 week */ );
245 function getLetterByIndex( $index ) {
246 if ( $this->firstLetterData
=== null ) {
247 $this->getFirstLetterData();
249 return $this->firstLetterData
['chars'][$index];
252 function getSortKeyByLetterIndex( $index ) {
253 if ( $this->firstLetterData
=== null ) {
254 $this->getFirstLetterData();
256 return $this->firstLetterData
['keys'][$index];
259 function getFirstLetterCount() {
260 if ( $this->firstLetterData
=== null ) {
261 $this->getFirstLetterData();
263 return count( $this->firstLetterData
['chars'] );
267 * Do a binary search, and return the index of the largest item that sorts
268 * less than or equal to the target value.
270 * @param $valueCallback array A function to call to get the value with
271 * a given array index.
272 * @param $valueCount int The number of items accessible via $valueCallback,
273 * indexed from 0 to $valueCount - 1
274 * @param $comparisonCallback array A callback to compare two values, returning
275 * -1, 0 or 1 in the style of strcmp().
276 * @param $target string The target value to find.
278 * @return The item index of the lower bound, or false if the target value
279 * sorts before all items.
281 function findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target ) {
283 $max = $valueCount - 1;
285 $mid = $min +
( ( $max - $min ) >> 1 );
286 $item = call_user_func( $valueCallback, $mid );
287 $comparison = call_user_func( $comparisonCallback, $target, $item );
288 if ( $comparison > 0 ) {
290 } elseif ( $comparison == 0 ) {
296 } while ( $min < $max - 1 );
298 if ( $min == 0 && $max == 0 && $comparison > 0 ) {
299 // Before the first item
306 static function isCjk( $codepoint ) {
307 foreach ( self
::$cjkBlocks as $block ) {
308 if ( $codepoint >= $block[0] && $codepoint <= $block[1] ) {