Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
NumericUppercaseCollation | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getSortKey | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
convertDigits | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getFirstLetter | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
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 | use MediaWiki\Language\Language; |
22 | use MediaWiki\Languages\LanguageFactory; |
23 | |
24 | /** |
25 | * Collation that orders text with numbers "naturally", so that 'Foo 1' < 'Foo 2' < 'Foo 12'. |
26 | * |
27 | * Note that this only works in terms of sequences of digits, and the behavior for decimal fractions |
28 | * or pretty-formatted numbers may be unexpected. |
29 | * |
30 | * Digits will be based on the wiki's content language settings. If |
31 | * you change the content language of a wiki you will need to run |
32 | * updateCollation.php --force. Only English (ASCII 0-9) and the |
33 | * localized version will be counted. Localized digits from other languages |
34 | * or weird unicode digit equivalents (e.g. 4, 𝟜, ⓸ , ⁴, etc) will not count. |
35 | * |
36 | * @since 1.28 |
37 | */ |
38 | class NumericUppercaseCollation extends UppercaseCollation { |
39 | |
40 | /** |
41 | * @var Language How to convert digits (usually the content language) |
42 | */ |
43 | private $digitTransformLang; |
44 | |
45 | /** |
46 | * @param LanguageFactory $languageFactory |
47 | * @param string|Language $digitTransformLang How to convert digits. |
48 | * For example, if given language "my" than ၇ is treated like 7. |
49 | * It is expected that usually this is given the content language. |
50 | */ |
51 | public function __construct( |
52 | LanguageFactory $languageFactory, |
53 | $digitTransformLang |
54 | ) { |
55 | $this->digitTransformLang = $digitTransformLang instanceof Language |
56 | ? $digitTransformLang |
57 | : $languageFactory->getLanguage( $digitTransformLang ); |
58 | parent::__construct( $languageFactory ); |
59 | } |
60 | |
61 | public function getSortKey( $string ) { |
62 | $sortkey = parent::getSortKey( $string ); |
63 | $sortkey = $this->convertDigits( $sortkey ); |
64 | // For each sequence of digits, insert the digit '0' and then the length of the sequence |
65 | // (encoded in two bytes) before it. That's all folks, it sorts correctly now! The '0' ensures |
66 | // correct position (where digits would normally sort), then the length will be compared putting |
67 | // shorter numbers before longer ones; if identical, then the characters will be compared, which |
68 | // generates the correct results for numbers of equal length. |
69 | $sortkey = preg_replace_callback( '/\d+/', static function ( $matches ) { |
70 | // Strip any leading zeros |
71 | $number = ltrim( $matches[0], '0' ); |
72 | $len = strlen( $number ); |
73 | // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte |
74 | // would allow only for 256, which doesn't feel future-proof. |
75 | $prefix = chr( (int)floor( $len / 256 ) ) . chr( $len % 256 ); |
76 | return '0' . $prefix . $number; |
77 | }, $sortkey ); |
78 | |
79 | return $sortkey; |
80 | } |
81 | |
82 | /** |
83 | * Convert localized digits to english digits. |
84 | * |
85 | * based on Language::parseFormattedNumber but without commas. |
86 | * |
87 | * @param string $string sortkey to unlocalize digits of |
88 | * @return string Sortkey with all localized digits replaced with ASCII digits. |
89 | */ |
90 | private function convertDigits( $string ) { |
91 | $table = $this->digitTransformLang->digitTransformTable(); |
92 | if ( $table ) { |
93 | $table = array_filter( $table ); |
94 | $flipped = array_flip( $table ); |
95 | // Some languages seem to also have commas in this table. |
96 | $flipped = array_filter( $flipped, 'is_numeric' ); |
97 | $string = strtr( $string, $flipped ); |
98 | } |
99 | return $string; |
100 | } |
101 | |
102 | public function getFirstLetter( $string ) { |
103 | $convertedString = $this->convertDigits( $string ); |
104 | |
105 | if ( preg_match( '/^\d/', $convertedString ) ) { |
106 | return wfMessage( 'category-header-numerals' ) |
107 | ->numParams( 0, 9 ) |
108 | ->text(); |
109 | } else { |
110 | return parent::getFirstLetter( $string ); |
111 | } |
112 | } |
113 | } |