MediaWiki master
CustomUppercaseCollation.php
Go to the documentation of this file.
1<?php
25
44
46 private $alphabet;
47
49 private $puaSubset;
50
52 private $firstLetters;
53
61 public function __construct(
62 LanguageFactory $languageFactory,
63 array $alphabet,
64 $digitTransformLang
65 ) {
66 if ( count( $alphabet ) < 1 || count( $alphabet ) >= 4096 ) {
67 throw new UnexpectedValueException( "Alphabet must be < 4096 items" );
68 }
69 $this->firstLetters = $alphabet;
70 $digitTransformLang = $digitTransformLang instanceof Language
71 ? $digitTransformLang
72 : $languageFactory->getLanguage( $digitTransformLang );
73 // For digraphs, only the first letter is capitalized in input
74 $this->alphabet = array_map( [ $digitTransformLang, 'uc' ], $alphabet );
75
76 $this->puaSubset = [];
77 $len = count( $alphabet );
78 for ( $i = 0; $i < $len; $i++ ) {
79 $this->puaSubset[] = "\xF3\xB3" . chr( (int)floor( $i / 64 ) + 128 ) . chr( ( $i % 64 ) + 128 );
80 }
81
82 // Sort these arrays so that any trigraphs, digraphs etc. are first
83 // (and they get replaced first in convertToPua()).
84 $lengths = array_map( 'mb_strlen', $this->alphabet );
85 array_multisort( $lengths, SORT_DESC, $this->firstLetters, $this->alphabet, $this->puaSubset );
86
87 parent::__construct( $languageFactory, $digitTransformLang );
88 }
89
90 private function convertToPua( $string ) {
91 return str_replace( $this->alphabet, $this->puaSubset, $string );
92 }
93
94 public function getSortKey( $string ) {
95 return $this->convertToPua( parent::getSortKey( $string ) );
96 }
97
98 public function getFirstLetter( $string ) {
99 $sortkey = $this->getSortKey( $string );
100
101 // In case a title begins with a character from our alphabet, return the corresponding
102 // first-letter. (This also happens if the title has a corresponding PUA code in it, to avoid
103 // inconsistent behaviour. This class mostly assumes that people will not use PUA codes.)
104 $index = array_search( substr( $sortkey, 0, 4 ), $this->puaSubset );
105 if ( $index !== false ) {
106 return $this->firstLetters[ $index ];
107 }
108
109 // String begins with a character outside of our alphabet, fall back
110 return parent::getFirstLetter( $string );
111 }
112}
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:82
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'.