MediaWiki  1.34.0
NumericUppercaseCollation.php
Go to the documentation of this file.
1 <?php
36 
41 
48  public function __construct( Language $lang ) {
49  $this->digitTransformLang = $lang;
50  parent::__construct();
51  }
52 
53  public function getSortKey( $string ) {
54  $sortkey = parent::getSortKey( $string );
55  $sortkey = $this->convertDigits( $sortkey );
56  // For each sequence of digits, insert the digit '0' and then the length of the sequence
57  // (encoded in two bytes) before it. That's all folks, it sorts correctly now! The '0' ensures
58  // correct position (where digits would normally sort), then the length will be compared putting
59  // shorter numbers before longer ones; if identical, then the characters will be compared, which
60  // generates the correct results for numbers of equal length.
61  $sortkey = preg_replace_callback( '/\d+/', function ( $matches ) {
62  // Strip any leading zeros
63  $number = ltrim( $matches[0], '0' );
64  $len = strlen( $number );
65  // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
66  // would allow only for 256, which doesn't feel future-proof.
67  $prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 );
68  return '0' . $prefix . $number;
69  }, $sortkey );
70 
71  return $sortkey;
72  }
73 
82  private function convertDigits( $string ) {
83  $table = $this->digitTransformLang->digitTransformTable();
84  if ( $table ) {
85  $table = array_filter( $table );
86  $flipped = array_flip( $table );
87  // Some languages seem to also have commas in this table.
88  $flipped = array_filter( $flipped, 'is_numeric' );
89  $string = strtr( $string, $flipped );
90  }
91  return $string;
92  }
93 
94  public function getFirstLetter( $string ) {
95  $convertedString = $this->convertDigits( $string );
96 
97  if ( preg_match( '/^\d/', $convertedString ) ) {
98  return wfMessage( 'category-header-numerals' )
99  ->numParams( 0, 9 )
100  ->text();
101  } else {
102  return parent::getFirstLetter( $string );
103  }
104  }
105 }
NumericUppercaseCollation\$digitTransformLang
$digitTransformLang
Definition: NumericUppercaseCollation.php:40
NumericUppercaseCollation\getSortKey
getSortKey( $string)
Given a string, convert it to a (hopefully short) key that can be used for efficient sorting.
Definition: NumericUppercaseCollation.php:53
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:94
UppercaseCollation
Definition: UppercaseCollation.php:23
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
NumericUppercaseCollation\__construct
__construct(Language $lang)
Definition: NumericUppercaseCollation.php:48
$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:35
UppercaseCollation\$lang
$lang
Definition: UppercaseCollation.php:25
NumericUppercaseCollation\convertDigits
convertDigits( $string)
Convert localized digits to english digits.
Definition: NumericUppercaseCollation.php:82
Language
Internationalisation code.
Definition: Language.php:37