MediaWiki master
NumericUppercaseCollation.php
Go to the documentation of this file.
1<?php
23
39
43 private $digitTransformLang;
44
51 public function __construct(
52 LanguageFactory $languageFactory,
53 $digitTransformLang
54 ) {
55 $this->digitTransformLang = $digitTransformLang instanceof Language
56 ? $digitTransformLang
57 : $languageFactory->getLanguage( $digitTransformLang );
58 parent::__construct( $languageFactory );
59 }
60
61 public function getSortKey( $string ) {
62 $sortkey = parent::getSortKey( $string );
63 $sortkey = $this->convertDigits( $sortkey );
64 // For each sequence of digits, insert the digit '0' and then the length of the sequence
65 // (encoded in two bytes) before it. That's all folks, it sorts correctly now! The '0' ensures
66 // correct position (where digits would normally sort), then the length will be compared putting
67 // shorter numbers before longer ones; if identical, then the characters will be compared, which
68 // generates the correct results for numbers of equal length.
69 $sortkey = preg_replace_callback( '/\d+/', static function ( $matches ) {
70 // Strip any leading zeros
71 $number = ltrim( $matches[0], '0' );
72 $len = strlen( $number );
73 // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
74 // would allow only for 256, which doesn't feel future-proof.
75 $prefix = chr( (int)floor( $len / 256 ) ) . chr( $len % 256 );
76 return '0' . $prefix . $number;
77 }, $sortkey );
78
79 return $sortkey;
80 }
81
90 private function convertDigits( $string ) {
91 $table = $this->digitTransformLang->digitTransformTable();
92 if ( $table ) {
93 $table = array_filter( $table );
94 $flipped = array_flip( $table );
95 // Some languages seem to also have commas in this table.
96 $flipped = array_filter( $flipped, 'is_numeric' );
97 $string = strtr( $string, $flipped );
98 }
99 return $string;
100 }
101
102 public function getFirstLetter( $string ) {
103 $convertedString = $this->convertDigits( $string );
104
105 if ( preg_match( '/^\d/', $convertedString ) ) {
106 return wfMessage( 'category-header-numerals' )
107 ->numParams( 0, 9 )
108 ->text();
109 } else {
110 return parent::getFirstLetter( $string );
111 }
112 }
113}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Base class for language-specific code.
Definition Language.php:80
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'.
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, $digitTransformLang)