Fix PHP Fatal error: Call to undefined method Title::getPrefixedTitle().
[lhc/web/wiklou.git] / includes / Collation.php
1 <?php
2 /**
3 * Database row sorting.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 abstract class Collation {
24 static $instance;
25
26 /**
27 * @return Collation
28 */
29 static function singleton() {
30 if ( !self::$instance ) {
31 global $wgCategoryCollation;
32 self::$instance = self::factory( $wgCategoryCollation );
33 }
34 return self::$instance;
35 }
36
37 /**
38 * @throws MWException
39 * @param $collationName string
40 * @return Collation
41 */
42 static function factory( $collationName ) {
43 switch ( $collationName ) {
44 case 'uppercase':
45 return new UppercaseCollation;
46 case 'identity':
47 return new IdentityCollation;
48 case 'uca-default':
49 return new IcuCollation( 'root' );
50 case 'xx-uca-ckb':
51 return new CollationCkb;
52 default:
53 $match = array();
54 if ( preg_match( '/^uca-([a-z@=-]+)$/', $collationName, $match ) ) {
55 return new IcuCollation( $match[1] );
56 }
57
58 # Provide a mechanism for extensions to hook in.
59 $collationObject = null;
60 wfRunHooks( 'Collation::factory', array( $collationName, &$collationObject ) );
61
62 if ( $collationObject instanceof Collation ) {
63 return $collationObject;
64 }
65
66 // If all else fails...
67 throw new MWException( __METHOD__ . ": unknown collation type \"$collationName\"" );
68 }
69 }
70
71 /**
72 * Given a string, convert it to a (hopefully short) key that can be used
73 * for efficient sorting. A binary sort according to the sortkeys
74 * corresponds to a logical sort of the corresponding strings. Current
75 * code expects that a line feed character should sort before all others, but
76 * has no other particular expectations (and that one can be changed if
77 * necessary).
78 *
79 * @param string $string UTF-8 string
80 * @return string Binary sortkey
81 */
82 abstract function getSortKey( $string );
83
84 /**
85 * Given a string, return the logical "first letter" to be used for
86 * grouping on category pages and so on. This has to be coordinated
87 * carefully with convertToSortkey(), or else the sorted list might jump
88 * back and forth between the same "initial letters" or other pathological
89 * behavior. For instance, if you just return the first character, but "a"
90 * sorts the same as "A" based on getSortKey(), then you might get a
91 * list like
92 *
93 * == A ==
94 * * [[Aardvark]]
95 *
96 * == a ==
97 * * [[antelope]]
98 *
99 * == A ==
100 * * [[Ape]]
101 *
102 * etc., assuming for the sake of argument that $wgCapitalLinks is false.
103 *
104 * @param string $string UTF-8 string
105 * @return string UTF-8 string corresponding to the first letter of input
106 */
107 abstract function getFirstLetter( $string );
108 }
109
110 class UppercaseCollation extends Collation {
111 var $lang;
112 function __construct() {
113 // Get a language object so that we can use the generic UTF-8 uppercase
114 // function there
115 $this->lang = Language::factory( 'en' );
116 }
117
118 function getSortKey( $string ) {
119 return $this->lang->uc( $string );
120 }
121
122 function getFirstLetter( $string ) {
123 if ( $string[0] == "\0" ) {
124 $string = substr( $string, 1 );
125 }
126 return $this->lang->ucfirst( $this->lang->firstChar( $string ) );
127 }
128 }
129
130 /**
131 * Collation class that's essentially a no-op.
132 *
133 * Does sorting based on binary value of the string.
134 * Like how things were pre 1.17.
135 */
136 class IdentityCollation extends Collation {
137
138 function getSortKey( $string ) {
139 return $string;
140 }
141
142 function getFirstLetter( $string ) {
143 global $wgContLang;
144 // Copied from UppercaseCollation.
145 // I'm kind of unclear on when this could happen...
146 if ( $string[0] == "\0" ) {
147 $string = substr( $string, 1 );
148 }
149 return $wgContLang->firstChar( $string );
150 }
151 }
152
153 class IcuCollation extends Collation {
154 const FIRST_LETTER_VERSION = 2;
155
156 var $primaryCollator, $mainCollator, $locale, $digitTransformLanguage;
157 var $firstLetterData;
158
159 /**
160 * Unified CJK blocks.
161 *
162 * The same definition of a CJK block must be used for both Collation and
163 * generateCollationData.php. These blocks are omitted from the first
164 * letter data, as an optimisation measure and because the default UCA table
165 * is pretty useless for sorting Chinese text anyway. Japanese and Korean
166 * blocks are not included here, because they are smaller and more useful.
167 */
168 static $cjkBlocks = array(
169 array( 0x2E80, 0x2EFF ), // CJK Radicals Supplement
170 array( 0x2F00, 0x2FDF ), // Kangxi Radicals
171 array( 0x2FF0, 0x2FFF ), // Ideographic Description Characters
172 array( 0x3000, 0x303F ), // CJK Symbols and Punctuation
173 array( 0x31C0, 0x31EF ), // CJK Strokes
174 array( 0x3200, 0x32FF ), // Enclosed CJK Letters and Months
175 array( 0x3300, 0x33FF ), // CJK Compatibility
176 array( 0x3400, 0x4DBF ), // CJK Unified Ideographs Extension A
177 array( 0x4E00, 0x9FFF ), // CJK Unified Ideographs
178 array( 0xF900, 0xFAFF ), // CJK Compatibility Ideographs
179 array( 0xFE30, 0xFE4F ), // CJK Compatibility Forms
180 array( 0x20000, 0x2A6DF ), // CJK Unified Ideographs Extension B
181 array( 0x2A700, 0x2B73F ), // CJK Unified Ideographs Extension C
182 array( 0x2B740, 0x2B81F ), // CJK Unified Ideographs Extension D
183 array( 0x2F800, 0x2FA1F ), // CJK Compatibility Ideographs Supplement
184 );
185
186 /**
187 * Additional characters (or character groups) to be considered separate
188 * letters for given languages, or to be removed from the list of such
189 * letters (denoted by keys starting with '-').
190 *
191 * These are additions to (or subtractions from) the data stored in the
192 * first-letters-root.ser file (which among others includes full basic latin,
193 * cyrillic and greek alphabets).
194 *
195 * "Separate letter" is a letter that would have a separate heading/section
196 * for it in a dictionary or a phone book in this language. This data isn't
197 * used for sorting (the ICU library handles that), only for deciding which
198 * characters (or character groups) to use as headings.
199 *
200 * Initially generated based on the primary level of Unicode collation
201 * tailorings available at http://developer.mimer.com/charts/tailorings.htm ,
202 * later modified.
203 *
204 * Empty arrays are intended; this signifies that the data for the language is
205 * available and that there are, in fact, no additional letters to consider.
206 */
207 static $tailoringFirstLetters = array(
208 // Verified by native speakers
209 'be' => array( "Ё" ),
210 'be-tarask' => array( "Ё" ),
211 'en' => array(),
212 'fi' => array( "Å", "Ä", "Ö" ),
213 'hu' => array( "Cs", "Dz", "Dzs", "Gy", "Ly", "Ny", "Ö", "Sz", "Ty", "Ü", "Zs" ),
214 'it' => array(),
215 'pl' => array( "Ą", "Ć", "Ę", "Ł", "Ń", "Ó", "Ś", "Ź", "Ż" ),
216 'pt' => array(),
217 'ru' => array(),
218 'sv' => array( "Å", "Ä", "Ö" ),
219 'sv@collation=standard' => array( "Å", "Ä", "Ö" ),
220 'uk' => array( "Ґ", "Ь" ),
221 'vi' => array( "Ă", "Â", "Đ", "Ê", "Ô", "Ơ", "Ư" ),
222 // Not verified, but likely correct
223 'af' => array(),
224 'ast' => array( "Ch", "Ll", "Ñ" ),
225 'az' => array( "Ç", "Ə", "Ğ", "İ", "Ö", "Ş", "Ü" ),
226 'bg' => array(),
227 'br' => array( "Ch", "C'h" ),
228 'bs' => array( "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ),
229 'ca' => array(),
230 'co' => array(),
231 'cs' => array( "Č", "Ch", "Ř", "Š", "Ž" ),
232 'cy' => array( "Ch", "Dd", "Ff", "Ng", "Ll", "Ph", "Rh", "Th" ),
233 'da' => array( "Æ", "Ø", "Å" ),
234 'de' => array(),
235 'dsb' => array( "Č", "Ć", "Dź", "Ě", "Ch", "Ł", "Ń", "Ŕ", "Š", "Ś", "Ž", "Ź" ),
236 'el' => array(),
237 'eo' => array( "Ĉ", "Ĝ", "Ĥ", "Ĵ", "Ŝ", "Ŭ" ),
238 'es' => array( "Ñ" ),
239 'et' => array( "Š", "Ž", "Õ", "Ä", "Ö", "Ü" ),
240 'eu' => array( "Ñ" ),
241 'fa' => array( "آ", "ء", "ه" ),
242 'fo' => array( "Á", "Ð", "Í", "Ó", "Ú", "Ý", "Æ", "Ø", "Å" ),
243 'fr' => array(),
244 'fur' => array( "À", "Á", "Â", "È", "Ì", "Ò", "Ù" ),
245 'fy' => array(),
246 'ga' => array(),
247 'gd' => array(),
248 'gl' => array( "Ch", "Ll", "Ñ" ),
249 'hr' => array( "Č", "Ć", "Dž", "Đ", "Lj", "Nj", "Š", "Ž" ),
250 'hsb' => array( "Č", "Dź", "Ě", "Ch", "Ł", "Ń", "Ř", "Š", "Ć", "Ž" ),
251 'is' => array( "Á", "Ð", "É", "Í", "Ó", "Ú", "Ý", "Þ", "Æ", "Ö", "Å" ),
252 'kk' => array( "Ү", "І" ),
253 'kl' => array( "Æ", "Ø", "Å" ),
254 'ku' => array( "Ç", "Ê", "Î", "Ş", "Û" ),
255 'ky' => array( "Ё" ),
256 'la' => array(),
257 'lb' => array(),
258 'lt' => array( "Č", "Š", "Ž" ),
259 'lv' => array( "Č", "Ģ", "Ķ", "Ļ", "Ņ", "Š", "Ž" ),
260 'mk' => array(),
261 'mo' => array( "Ă", "Â", "Î", "Ş", "Ţ" ),
262 'mt' => array( "Ċ", "Ġ", "Għ", "Ħ", "Ż" ),
263 'nl' => array(),
264 'no' => array( "Æ", "Ø", "Å" ),
265 'oc' => array(),
266 'rm' => array(),
267 'ro' => array( "Ă", "Â", "Î", "Ş", "Ţ" ),
268 'rup' => array( "Ă", "Â", "Î", "Ľ", "Ń", "Ş", "Ţ" ),
269 'sco' => array(),
270 'sk' => array( "Ä", "Č", "Ch", "Ô", "Š", "Ž" ),
271 'sl' => array( "Č", "Š", "Ž" ),
272 'smn' => array( "Á", "Č", "Đ", "Ŋ", "Š", "Ŧ", "Ž", "Æ", "Ø", "Å", "Ä", "Ö" ),
273 'sq' => array( "Ç", "Dh", "Ë", "Gj", "Ll", "Nj", "Rr", "Sh", "Th", "Xh", "Zh" ),
274 'sr' => array(),
275 'tk' => array( "Ç", "Ä", "Ž", "Ň", "Ö", "Ş", "Ü", "Ý" ),
276 'tl' => array( "Ñ", "Ng" ),
277 'tr' => array( "Ç", "Ğ", "İ", "Ö", "Ş", "Ü" ),
278 'tt' => array( "Ә", "Ө", "Ү", "Җ", "Ң", "Һ" ),
279 'uz' => array( "Ch", "G'", "Ng", "O'", "Sh" ),
280 );
281
282 const RECORD_LENGTH = 14;
283
284 function __construct( $locale ) {
285 if ( !extension_loaded( 'intl' ) ) {
286 throw new MWException( 'An ICU collation was requested, ' .
287 'but the intl extension is not available.' );
288 }
289
290 $this->locale = $locale;
291 // Drop everything after the '@' in locale's name
292 $localeParts = explode( '@', $locale );
293 $this->digitTransformLanguage = Language::factory( $locale === 'root' ? 'en' : $localeParts[0] );
294
295 $this->mainCollator = Collator::create( $locale );
296 if ( !$this->mainCollator ) {
297 throw new MWException( "Invalid ICU locale specified for collation: $locale" );
298 }
299
300 $this->primaryCollator = Collator::create( $locale );
301 $this->primaryCollator->setStrength( Collator::PRIMARY );
302 }
303
304 function getSortKey( $string ) {
305 // intl extension produces non null-terminated
306 // strings. Appending '' fixes it so that it doesn't generate
307 // a warning on each access in debug php.
308 wfSuppressWarnings();
309 $key = $this->mainCollator->getSortKey( $string ) . '';
310 wfRestoreWarnings();
311 return $key;
312 }
313
314 function getPrimarySortKey( $string ) {
315 wfSuppressWarnings();
316 $key = $this->primaryCollator->getSortKey( $string ) . '';
317 wfRestoreWarnings();
318 return $key;
319 }
320
321 function getFirstLetter( $string ) {
322 $string = strval( $string );
323 if ( $string === '' ) {
324 return '';
325 }
326
327 // Check for CJK
328 $firstChar = mb_substr( $string, 0, 1, 'UTF-8' );
329 if ( ord( $firstChar ) > 0x7f && self::isCjk( utf8ToCodepoint( $firstChar ) ) ) {
330 return $firstChar;
331 }
332
333 $sortKey = $this->getPrimarySortKey( $string );
334
335 // Do a binary search to find the correct letter to sort under
336 $min = ArrayUtils::findLowerBound(
337 array( $this, 'getSortKeyByLetterIndex' ),
338 $this->getFirstLetterCount(),
339 'strcmp',
340 $sortKey );
341
342 if ( $min === false ) {
343 // Before the first letter
344 return '';
345 }
346 return $this->getLetterByIndex( $min );
347 }
348
349 function getFirstLetterData() {
350 if ( $this->firstLetterData !== null ) {
351 return $this->firstLetterData;
352 }
353
354 $cache = wfGetCache( CACHE_ANYTHING );
355 $cacheKey = wfMemcKey( 'first-letters', $this->locale, $this->digitTransformLanguage->getCode() );
356 $cacheEntry = $cache->get( $cacheKey );
357
358 if ( $cacheEntry && isset( $cacheEntry['version'] )
359 && $cacheEntry['version'] == self::FIRST_LETTER_VERSION
360 ) {
361 $this->firstLetterData = $cacheEntry;
362 return $this->firstLetterData;
363 }
364
365 // Generate data from serialized data file
366
367 if ( isset( self::$tailoringFirstLetters[$this->locale] ) ) {
368 $letters = wfGetPrecompiledData( "first-letters-root.ser" );
369 // Append additional characters
370 $letters = array_merge( $letters, self::$tailoringFirstLetters[$this->locale] );
371 // Remove unnecessary ones, if any
372 if ( isset( self::$tailoringFirstLetters['-' . $this->locale] ) ) {
373 $letters = array_diff( $letters, self::$tailoringFirstLetters['-' . $this->locale] );
374 }
375 // Apply digit transforms
376 $digits = array( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' );
377 $letters = array_diff( $letters, $digits );
378 foreach ( $digits as $digit ) {
379 $letters[] = $this->digitTransformLanguage->formatNum( $digit, true );
380 }
381 } else {
382 $letters = wfGetPrecompiledData( "first-letters-{$this->locale}.ser" );
383 if ( $letters === false ) {
384 throw new MWException( "MediaWiki does not support ICU locale " .
385 "\"{$this->locale}\"" );
386 }
387 }
388
389 // Sort the letters.
390 //
391 // It's impossible to have the precompiled data file properly sorted,
392 // because the sort order changes depending on ICU version. If the
393 // array is not properly sorted, the binary search will return random
394 // results.
395 //
396 // We also take this opportunity to remove primary collisions.
397 $letterMap = array();
398 foreach ( $letters as $letter ) {
399 $key = $this->getPrimarySortKey( $letter );
400 if ( isset( $letterMap[$key] ) ) {
401 // Primary collision
402 // Keep whichever one sorts first in the main collator
403 if ( $this->mainCollator->compare( $letter, $letterMap[$key] ) < 0 ) {
404 $letterMap[$key] = $letter;
405 }
406 } else {
407 $letterMap[$key] = $letter;
408 }
409 }
410 ksort( $letterMap, SORT_STRING );
411 // Remove duplicate prefixes. Basically if something has a sortkey
412 // which is a prefix of some other sortkey, then it is an
413 // expansion and probably should not be considered a section
414 // header.
415 //
416 // For example 'þ' is sometimes sorted as if it is the letters
417 // 'th'. Other times it is its own primary element. Another
418 // example is '₨'. Sometimes its a currency symbol. Sometimes it
419 // is an 'R' followed by an 's'.
420 //
421 // Additionally an expanded element should always sort directly
422 // after its first element due to they way sortkeys work.
423 //
424 // UCA sortkey elements are of variable length but no collation
425 // element should be a prefix of some other element, so I think
426 // this is safe. See:
427 // * https://ssl.icu-project.org/repos/icu/icuhtml/trunk/design/collation/ICU_collation_design.htm
428 // * http://site.icu-project.org/design/collation/uca-weight-allocation
429 //
430 // Additionally, there is something called primary compression to
431 // worry about. Basically, if you have two primary elements that
432 // are more than one byte and both start with the same byte then
433 // the first byte is dropped on the second primary. Additionally
434 // either \x03 or \xFF may be added to mean that the next primary
435 // does not start with the first byte of the first primary.
436 //
437 // This shouldn't matter much, as the first primary is not
438 // changed, and that is what we are comparing against.
439 //
440 // tl;dr: This makes some assumptions about how icu implements
441 // collations. It seems incredibly unlikely these assumptions
442 // will change, but nonetheless they are assumptions.
443
444 $prev = false;
445 $duplicatePrefixes = array();
446 foreach ( $letterMap as $key => $value ) {
447 // Remove terminator byte. Otherwise the prefix
448 // comparison will get hung up on that.
449 $trimmedKey = rtrim( $key, "\0" );
450 if ( $prev === false || $prev === '' ) {
451 $prev = $trimmedKey;
452 // We don't yet have a collation element
453 // to compare against, so continue.
454 continue;
455 }
456
457 // Due to the fact the array is sorted, we only have
458 // to compare with the element directly previous
459 // to the current element (skipping expansions).
460 // An element "X" will always sort directly
461 // before "XZ" (Unless we have "XY", but we
462 // do not update $prev in that case).
463 if ( substr( $trimmedKey, 0, strlen( $prev ) ) === $prev ) {
464 $duplicatePrefixes[] = $key;
465 // If this is an expansion, we don't want to
466 // compare the next element to this element,
467 // but to what is currently $prev
468 continue;
469 }
470 $prev = $trimmedKey;
471 }
472 foreach ( $duplicatePrefixes as $badKey ) {
473 wfDebug( "Removing '{$letterMap[$badKey]}' from first letters." );
474 unset( $letterMap[$badKey] );
475 // This code assumes that unsetting does not change sort order.
476 }
477 $data = array(
478 'chars' => array_values( $letterMap ),
479 'keys' => array_keys( $letterMap ),
480 'version' => self::FIRST_LETTER_VERSION,
481 );
482
483 // Reduce memory usage before caching
484 unset( $letterMap );
485
486 // Save to cache
487 $this->firstLetterData = $data;
488 $cache->set( $cacheKey, $data, 86400 * 7 /* 1 week */ );
489 return $data;
490 }
491
492 function getLetterByIndex( $index ) {
493 if ( $this->firstLetterData === null ) {
494 $this->getFirstLetterData();
495 }
496 return $this->firstLetterData['chars'][$index];
497 }
498
499 function getSortKeyByLetterIndex( $index ) {
500 if ( $this->firstLetterData === null ) {
501 $this->getFirstLetterData();
502 }
503 return $this->firstLetterData['keys'][$index];
504 }
505
506 function getFirstLetterCount() {
507 if ( $this->firstLetterData === null ) {
508 $this->getFirstLetterData();
509 }
510 return count( $this->firstLetterData['chars'] );
511 }
512
513 /**
514 * Do a binary search, and return the index of the largest item that sorts
515 * less than or equal to the target value.
516 *
517 * @deprecated in 1.23; use ArrayUtils::findLowerBound() instead
518 *
519 * @param array $valueCallback A function to call to get the value with
520 * a given array index.
521 * @param int $valueCount The number of items accessible via $valueCallback,
522 * indexed from 0 to $valueCount - 1
523 * @param array $comparisonCallback A callback to compare two values, returning
524 * -1, 0 or 1 in the style of strcmp().
525 * @param string $target The target value to find.
526 *
527 * @return int|bool The item index of the lower bound, or false if the target value
528 * sorts before all items.
529 */
530 function findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target ) {
531 wfDeprecated( __METHOD__, '1.23' );
532 return ArrayUtils::findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target );
533 }
534
535 static function isCjk( $codepoint ) {
536 foreach ( self::$cjkBlocks as $block ) {
537 if ( $codepoint >= $block[0] && $codepoint <= $block[1] ) {
538 return true;
539 }
540 }
541 return false;
542 }
543
544 /**
545 * Return the version of ICU library used by PHP's intl extension,
546 * or false when the extension is not installed of the version
547 * can't be determined.
548 *
549 * The constant INTL_ICU_VERSION this function refers to isn't really
550 * documented. It is available since PHP 5.3.7 (see PHP bug 54561).
551 * This function will return false on older PHPs.
552 *
553 * @since 1.21
554 * @return string|false
555 */
556 static function getICUVersion() {
557 return defined( 'INTL_ICU_VERSION' ) ? INTL_ICU_VERSION : false;
558 }
559
560 /**
561 * Return the version of Unicode appropriate for the version of ICU library
562 * currently in use, or false when it can't be determined.
563 *
564 * @since 1.21
565 * @return string|false
566 */
567 static function getUnicodeVersionForICU() {
568 $icuVersion = IcuCollation::getICUVersion();
569 if ( !$icuVersion ) {
570 return false;
571 }
572
573 $versionPrefix = substr( $icuVersion, 0, 3 );
574 // Source: http://site.icu-project.org/download
575 $map = array(
576 '50.' => '6.2',
577 '49.' => '6.1',
578 '4.8' => '6.0',
579 '4.6' => '6.0',
580 '4.4' => '5.2',
581 '4.2' => '5.1',
582 '4.0' => '5.1',
583 '3.8' => '5.0',
584 '3.6' => '5.0',
585 '3.4' => '4.1',
586 );
587
588 if ( isset( $map[$versionPrefix] ) ) {
589 return $map[$versionPrefix];
590 } else {
591 return false;
592 }
593 }
594 }
595
596 /**
597 * Workaround for the lack of support of Sorani Kurdish / Central Kurdish language ('ckb') in ICU.
598 *
599 * Uses the same collation rules as Persian / Farsi ('fa'), but different characters for digits.
600 */
601 class CollationCkb extends IcuCollation {
602 function __construct() {
603 // This will set $locale and collators, which affect the actual sorting order
604 parent::__construct( 'fa' );
605 // Override the 'fa' language set by parent constructor, which affects #getFirstLetterData()
606 $this->digitTransformLanguage = Language::factory( 'ckb' );
607 }
608 }