Merge "Revert "Use display name in category page subheadings if provided""
[lhc/web/wiklou.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 * @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 // Strip any leading zeros
40 $number = ltrim( $matches[0], '0' );
41 $len = strlen( $number );
42 // This allows sequences of up to 65536 numeric characters to be handled correctly. One byte
43 // would allow only for 256, which doesn't feel future-proof.
44 $prefix = chr( floor( $len / 256 ) ) . chr( $len % 256 );
45 return '0' . $prefix . $number;
46 }, $sortkey );
47
48 return $sortkey;
49 }
50
51 public function getFirstLetter( $string ) {
52 if ( preg_match( '/^\d/', $string ) ) {
53 // Note that we pass 0 and 9 as normal params, not numParams(). This only works for 0-9
54 // and not localised digits, so we don't want them to be converted.
55 return wfMessage( 'category-header-numerals' )->params( 0, 9 )->text();
56 } else {
57 return parent::getFirstLetter( $string );
58 }
59 }
60 }