MediaWiki REL1_34
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}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Internationalisation code.
Definition Language.php:37
Collation that orders text with numbers "naturally", so that 'Foo 1' < 'Foo 2' < 'Foo 12'.
convertDigits( $string)
Convert localized digits to english digits.
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...