3 * Database row sorting.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * @author Tim Starling
27 abstract class Collation
{
28 private static $instance;
34 public static function singleton() {
35 if ( !self
::$instance ) {
36 global $wgCategoryCollation;
37 self
::$instance = self
::factory( $wgCategoryCollation );
39 return self
::$instance;
45 * @param string $collationName
48 public static function factory( $collationName ) {
49 switch ( $collationName ) {
51 return new UppercaseCollation
;
53 return new IdentityCollation
;
55 return new IcuCollation( 'root' );
56 case 'uca-default-u-kn':
57 return new IcuCollation( 'root-u-kn' );
59 return new CollationCkb
;
61 return new CollationEt
;
64 if ( preg_match( '/^uca-([a-z@=-]+)$/', $collationName, $match ) ) {
65 return new IcuCollation( $match[1] );
68 # Provide a mechanism for extensions to hook in.
69 $collationObject = null;
70 Hooks
::run( 'Collation::factory', [ $collationName, &$collationObject ] );
72 if ( $collationObject instanceof Collation
) {
73 return $collationObject;
76 // If all else fails...
77 throw new MWException( __METHOD__
. ": unknown collation type \"$collationName\"" );
82 * Given a string, convert it to a (hopefully short) key that can be used
83 * for efficient sorting. A binary sort according to the sortkeys
84 * corresponds to a logical sort of the corresponding strings. Current
85 * code expects that a line feed character should sort before all others, but
86 * has no other particular expectations (and that one can be changed if
91 * @param string $string UTF-8 string
92 * @return string Binary sortkey
94 abstract function getSortKey( $string );
97 * Given a string, return the logical "first letter" to be used for
98 * grouping on category pages and so on. This has to be coordinated
99 * carefully with convertToSortkey(), or else the sorted list might jump
100 * back and forth between the same "initial letters" or other pathological
101 * behavior. For instance, if you just return the first character, but "a"
102 * sorts the same as "A" based on getSortKey(), then you might get a
114 * etc., assuming for the sake of argument that $wgCapitalLinks is false.
118 * @param string $string UTF-8 string
119 * @return string UTF-8 string corresponding to the first letter of input
121 abstract function getFirstLetter( $string );