|
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
|
|
|
* @since 1.28 |
|
28
|
|
|
*/ |
|
29
|
|
|
class NumericUppercaseCollation extends UppercaseCollation { |
|
30
|
|
|
public function getSortKey( $string ) { |
|
31
|
|
|
$sortkey = parent::getSortKey( $string ); |
|
32
|
|
|
|
|
33
|
|
|
// For each sequence of digits, insert the digit '0' and then the length of the sequence |
|
34
|
|
|
// (encoded in two bytes) before it. That's all folks, it sorts correctly now! The '0' ensures |
|
35
|
|
|
// correct position (where digits would normally sort), then the length will be compared putting |
|
36
|
|
|
// shorter numbers before longer ones; if identical, then the characters will be compared, which |
|
37
|
|
|
// generates the correct results for numbers of equal length. |
|
38
|
|
|
$sortkey = preg_replace_callback( '/\d+/', function ( $matches ) { |
|
39
|
|
|
$len = strlen( $matches[0] ); |
|
40
|
|
|
// This allows sequences of up to 65536 numeric characters to be handled correctly. One byte |
|
41
|
|
|
// would allow only for 256, which doesn't feel future-proof. |
|
42
|
|
|
$prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 ); |
|
43
|
|
|
return '0' . $prefix . $matches[0]; |
|
44
|
|
|
}, $sortkey ); |
|
45
|
|
|
|
|
46
|
|
|
return $sortkey; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getFirstLetter( $string ) { |
|
50
|
|
|
if ( preg_match( '/^\d/', $string ) ) { |
|
51
|
|
|
// Note that we pass 0 and 9 as normal params, not numParams(). This only works for 0-9 |
|
52
|
|
|
// and not localised digits, so we don't want them to be converted. |
|
53
|
|
|
return wfMessage( 'category-header-numerals' )->params( 0, 9 )->text(); |
|
54
|
|
|
} else { |
|
55
|
|
|
return parent::getFirstLetter( $string ); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|