MediaWiki  master
NumericUppercaseCollation.php
Go to the documentation of this file.
1 <?php
22 
38 
43 
50  public function __construct(
51  LanguageFactory $languageFactory,
53  ) {
54  $this->digitTransformLang = $digitTransformLang instanceof Language
56  : $languageFactory->getLanguage( $digitTransformLang );
57  parent::__construct( $languageFactory );
58  }
59 
60  public function getSortKey( $string ) {
61  $sortkey = parent::getSortKey( $string );
62  $sortkey = $this->convertDigits( $sortkey );
63  // For each sequence of digits, insert the digit '0' and then the length of the sequence
64  // (encoded in two bytes) before it. That's all folks, it sorts correctly now! The '0' ensures
65  // correct position (where digits would normally sort), then the length will be compared putting
66  // shorter numbers before longer ones; if identical, then the characters will be compared, which
67  // generates the correct results for numbers of equal length.
68  $sortkey = preg_replace_callback( '/\d+/', static function ( $matches ) {
69  // Strip any leading zeros
70  $number = ltrim( $matches[0], '0' );
71  $len = strlen( $number );
72  // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
73  // would allow only for 256, which doesn't feel future-proof.
74  $prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 );
75  return '0' . $prefix . $number;
76  }, $sortkey );
77 
78  return $sortkey;
79  }
80 
89  private function convertDigits( $string ) {
90  $table = $this->digitTransformLang->digitTransformTable();
91  if ( $table ) {
92  $table = array_filter( $table );
93  $flipped = array_flip( $table );
94  // Some languages seem to also have commas in this table.
95  $flipped = array_filter( $flipped, 'is_numeric' );
96  $string = strtr( $string, $flipped );
97  }
98  return $string;
99  }
100 
101  public function getFirstLetter( $string ) {
102  $convertedString = $this->convertDigits( $string );
103 
104  if ( preg_match( '/^\d/', $convertedString ) ) {
105  return wfMessage( 'category-header-numerals' )
106  ->numParams( 0, 9 )
107  ->text();
108  } else {
109  return parent::getFirstLetter( $string );
110  }
111  }
112 }
NumericUppercaseCollation\getSortKey
getSortKey( $string)
Given a string, convert it to a (hopefully short) key that can be used for efficient sorting.
Definition: NumericUppercaseCollation.php:60
NumericUppercaseCollation\getFirstLetter
getFirstLetter( $string)
Given a string, return the logical "first letter" to be used for grouping on category pages and so on...
Definition: NumericUppercaseCollation.php:101
NumericUppercaseCollation\__construct
__construct(LanguageFactory $languageFactory, $digitTransformLang)
Definition: NumericUppercaseCollation.php:50
MediaWiki\Languages\LanguageFactory
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: LanguageFactory.php:42
UppercaseCollation
Definition: UppercaseCollation.php:25
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1186
NumericUppercaseCollation\$digitTransformLang
Language $digitTransformLang
How to convert digits (usually the content language)
Definition: NumericUppercaseCollation.php:42
$matches
$matches
Definition: NoLocalSettings.php:24
NumericUppercaseCollation
Collation that orders text with numbers "naturally", so that 'Foo 1' < 'Foo 2' < 'Foo 12'.
Definition: NumericUppercaseCollation.php:37
MediaWiki\Languages\LanguageFactory\getLanguage
getLanguage( $code)
Get a cached or new language object for a given language code.
Definition: LanguageFactory.php:111
NumericUppercaseCollation\convertDigits
convertDigits( $string)
Convert localized digits to english digits.
Definition: NumericUppercaseCollation.php:89
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42