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
23 use MediaWiki\MediaWikiServices
;
27 * @author Tim Starling
29 abstract class Collation
{
30 private static $instance;
36 public static function singleton() {
37 if ( !self
::$instance ) {
38 global $wgCategoryCollation;
39 self
::$instance = self
::factory( $wgCategoryCollation );
41 return self
::$instance;
47 * @param string $collationName
50 public static function factory( $collationName ) {
51 switch ( $collationName ) {
53 return new UppercaseCollation
;
55 return new NumericUppercaseCollation(
56 MediaWikiServices
::getInstance()->getContentLanguage() );
58 return new IdentityCollation
;
60 return new IcuCollation( 'root' );
61 case 'uca-default-u-kn':
62 return new IcuCollation( 'root-u-kn' );
64 return new CollationCkb
;
66 return new AbkhazUppercaseCollation
;
68 return new BashkirUppercaseCollation
;
71 if ( preg_match( '/^uca-([A-Za-z@=-]+)$/', $collationName, $match ) ) {
72 return new IcuCollation( $match[1] );
75 # Provide a mechanism for extensions to hook in.
76 $collationObject = null;
77 Hooks
::run( 'Collation::factory', [ $collationName, &$collationObject ] );
79 if ( $collationObject instanceof self
) {
80 return $collationObject;
83 // If all else fails...
84 throw new MWException( __METHOD__
. ": unknown collation type \"$collationName\"" );
89 * Given a string, convert it to a (hopefully short) key that can be used
90 * for efficient sorting. A binary sort according to the sortkeys
91 * corresponds to a logical sort of the corresponding strings. Current
92 * code expects that a line feed character should sort before all others, but
93 * has no other particular expectations (and that one can be changed if
98 * @param string $string UTF-8 string
99 * @return string Binary sortkey
101 abstract function getSortKey( $string );
104 * Given a string, return the logical "first letter" to be used for
105 * grouping on category pages and so on. This has to be coordinated
106 * carefully with convertToSortkey(), or else the sorted list might jump
107 * back and forth between the same "initial letters" or other pathological
108 * behavior. For instance, if you just return the first character, but "a"
109 * sorts the same as "A" based on getSortKey(), then you might get a
121 * etc., assuming for the sake of argument that $wgCapitalLinks is false.
125 * @param string $string UTF-8 string
126 * @return string UTF-8 string corresponding to the first letter of input
128 abstract function getFirstLetter( $string );