MediaWiki REL1_39
CustomUppercaseCollation.php
Go to the documentation of this file.
1<?php
24
43
45 private $alphabet;
46
48 private $puaSubset;
49
51 private $firstLetters;
52
60 public function __construct(
61 LanguageFactory $languageFactory,
62 array $alphabet,
63 $digitTransformLang
64 ) {
65 if ( count( $alphabet ) < 1 || count( $alphabet ) >= 4096 ) {
66 throw new UnexpectedValueException( "Alphabet must be < 4096 items" );
67 }
68 $this->firstLetters = $alphabet;
69 $digitTransformLang = $digitTransformLang instanceof Language
70 ? $digitTransformLang
71 : $languageFactory->getLanguage( $digitTransformLang );
72 // For digraphs, only the first letter is capitalized in input
73 $this->alphabet = array_map( [ $digitTransformLang, 'uc' ], $alphabet );
74
75 $this->puaSubset = [];
76 $len = count( $alphabet );
77 for ( $i = 0; $i < $len; $i++ ) {
78 $this->puaSubset[] = "\xF3\xB3" . chr( (int)floor( $i / 64 ) + 128 ) . chr( ( $i % 64 ) + 128 );
79 }
80
81 // Sort these arrays so that any trigraphs, digraphs etc. are first
82 // (and they get replaced first in convertToPua()).
83 $lengths = array_map( 'mb_strlen', $this->alphabet );
84 array_multisort( $lengths, SORT_DESC, $this->firstLetters, $this->alphabet, $this->puaSubset );
85
86 parent::__construct( $languageFactory, $digitTransformLang );
87 }
88
89 private function convertToPua( $string ) {
90 return str_replace( $this->alphabet, $this->puaSubset, $string );
91 }
92
93 public function getSortKey( $string ) {
94 return $this->convertToPua( parent::getSortKey( $string ) );
95 }
96
97 public function getFirstLetter( $string ) {
98 $sortkey = $this->getSortKey( $string );
99
100 // In case a title begins with a character from our alphabet, return the corresponding
101 // first-letter. (This also happens if the title has a corresponding PUA code in it, to avoid
102 // inconsistent behaviour. This class mostly assumes that people will not use PUA codes.)
103 $index = array_search( substr( $sortkey, 0, 4 ), $this->puaSubset );
104 if ( $index !== false ) {
105 return $this->firstLetters[ $index ];
106 }
107
108 // String begins with a character outside of our alphabet, fall back
109 return parent::getFirstLetter( $string );
110 }
111}
Resort normal UTF-8 order by putting a bunch of stuff in PUA.
getSortKey( $string)
Given a string, convert it to a (hopefully short) key that can be used for efficient sorting.
getFirstLetter( $string)
Given a string, return the logical "first letter" to be used for grouping on category pages and so on...
__construct(LanguageFactory $languageFactory, array $alphabet, $digitTransformLang)
Base class for language-specific code.
Definition Language.php:53
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
getLanguage( $code)
Get a cached or new language object for a given language code with normalization of the language code...
Collation that orders text with numbers "naturally", so that 'Foo 1' < 'Foo 2' < 'Foo 12'.