]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/collation/NumericUppercaseCollation.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / collation / NumericUppercaseCollation.php
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.
7  *
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.
12  *
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
17  *
18  * @file
19  */
20
21 /**
22  * Collation that orders text with numbers "naturally", so that 'Foo 1' < 'Foo 2' < 'Foo 12'.
23  *
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.
26  *
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.
32  *
33  * @since 1.28
34  */
35 class NumericUppercaseCollation extends UppercaseCollation {
36
37         /**
38          * @var $digitTransformLang Language How to convert digits (usually $wgContLang)
39          */
40         private $digitTransformLang;
41
42         /**
43          * @param Language $lang How to convert digits.
44          *  For example, if given language "my" than ၇ is treated like 7.
45          *
46          * It is expected that usually this is given $wgContLang.
47          */
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
74         /**
75          * Convert localized digits to english digits.
76          *
77          * based on Language::parseFormattedNumber but without commas.
78          *
79          * @param string $string sortkey to unlocalize digits of
80          * @return string Sortkey with all localized digits replaced with ASCII digits.
81          */
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 }