Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / collation / NumericUppercaseCollation.php
blob2d2ca47bcf986dcdcfba0516b52fd4e4982cb0a5
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 /**
22 * Collation that orders text with numbers "naturally", so that 'Foo 1' < 'Foo 2' < 'Foo 12'.
24 * Note that this only works in terms of sequences of digits, and the behavior for decimal fractions
25 * or pretty-formatted numbers may be unexpected.
27 * Digits will be based on the wiki's content language settings. If
28 * you change the content langauge of a wiki you will need to run
29 * updateCollation.php --force. Only English (ASCII 0-9) and the
30 * localized version will be counted. Localized digits from other languages
31 * or weird unicode digit equivalents (e.g. 4, 𝟜, ⓸ , ⁴, etc) will not count.
33 * @since 1.28
35 class NumericUppercaseCollation extends UppercaseCollation {
37 /**
38 * @var $digitTransformLang Language How to convert digits (usually $wgContLang)
40 private $digitTransformLang;
42 /**
43 * Constructor
45 * @param $lang Language How to convert digits.
46 * For example, if given language "my" than ၇ is treated like 7.
48 * It is expected that usually this is given $wgContLang.
50 public function __construct( Language $lang ) {
51 $this->digitTransformLang = $lang;
52 parent::__construct();
55 public function getSortKey( $string ) {
56 $sortkey = parent::getSortKey( $string );
57 $sortkey = $this->convertDigits( $sortkey );
58 // For each sequence of digits, insert the digit '0' and then the length of the sequence
59 // (encoded in two bytes) before it. That's all folks, it sorts correctly now! The '0' ensures
60 // correct position (where digits would normally sort), then the length will be compared putting
61 // shorter numbers before longer ones; if identical, then the characters will be compared, which
62 // generates the correct results for numbers of equal length.
63 $sortkey = preg_replace_callback( '/\d+/', function ( $matches ) {
64 // Strip any leading zeros
65 $number = ltrim( $matches[0], '0' );
66 $len = strlen( $number );
67 // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
68 // would allow only for 256, which doesn't feel future-proof.
69 $prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 );
70 return '0' . $prefix . $number;
71 }, $sortkey );
73 return $sortkey;
76 /**
77 * Convert localized digits to english digits.
79 * based on Language::parseFormattedNumber but without commas.
81 * @param $string String sortkey to unlocalize digits of
82 * @return String Sortkey with all localized digits replaced with ASCII digits.
84 private function convertDigits( $string ) {
85 $table = $this->digitTransformLang->digitTransformTable();
86 if ( $table ) {
87 $table = array_filter( $table );
88 $flipped = array_flip( $table );
89 // Some languages seem to also have commas in this table.
90 $flipped = array_filter( $flipped, 'is_numeric' );
91 $string = strtr( $string, $flipped );
93 return $string;
96 public function getFirstLetter( $string ) {
97 $convertedString = $this->convertDigits( $string );
99 if ( preg_match( '/^\d/', $convertedString ) ) {
100 return wfMessage( 'category-header-numerals' )
101 ->numParams( 0, 9 )
102 ->text();
103 } else {
104 return parent::getFirstLetter( $string );