Add custom collation for Northern Sami
[lhc/web/wiklou.git] / includes / collation / Collation.php
1 <?php
2 /**
3 * Database row sorting.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * @since 1.16.3
25 * @author Tim Starling
26 */
27 abstract class Collation {
28 private static $instance;
29
30 /**
31 * @since 1.16.3
32 * @return Collation
33 */
34 public static function singleton() {
35 if ( !self::$instance ) {
36 global $wgCategoryCollation;
37 self::$instance = self::factory( $wgCategoryCollation );
38 }
39 return self::$instance;
40 }
41
42 /**
43 * @since 1.16.3
44 * @throws MWException
45 * @param string $collationName
46 * @return Collation
47 */
48 public static function factory( $collationName ) {
49 global $wgContLang;
50
51 switch ( $collationName ) {
52 case 'uppercase':
53 return new UppercaseCollation;
54 case 'numeric':
55 return new NumericUppercaseCollation( $wgContLang );
56 case 'identity':
57 return new IdentityCollation;
58 case 'uca-default':
59 return new IcuCollation( 'root' );
60 case 'uca-default-u-kn':
61 return new IcuCollation( 'root-u-kn' );
62 case 'xx-uca-ckb':
63 return new CollationCkb;
64 case 'xx-uca-et':
65 return new CollationEt;
66 case 'xx-uca-fa':
67 return new CollationFa;
68 case 'uppercase-ba':
69 return new BashkirUppercaseCollation;
70 case 'uppercase-se':
71 return new NorthernSamiUppercaseCollation;
72 default:
73 $match = [];
74 if ( preg_match( '/^uca-([A-Za-z@=-]+)$/', $collationName, $match ) ) {
75 return new IcuCollation( $match[1] );
76 }
77
78 # Provide a mechanism for extensions to hook in.
79 $collationObject = null;
80 Hooks::run( 'Collation::factory', [ $collationName, &$collationObject ] );
81
82 if ( $collationObject instanceof Collation ) {
83 return $collationObject;
84 }
85
86 // If all else fails...
87 throw new MWException( __METHOD__ . ": unknown collation type \"$collationName\"" );
88 }
89 }
90
91 /**
92 * Given a string, convert it to a (hopefully short) key that can be used
93 * for efficient sorting. A binary sort according to the sortkeys
94 * corresponds to a logical sort of the corresponding strings. Current
95 * code expects that a line feed character should sort before all others, but
96 * has no other particular expectations (and that one can be changed if
97 * necessary).
98 *
99 * @since 1.16.3
100 *
101 * @param string $string UTF-8 string
102 * @return string Binary sortkey
103 */
104 abstract function getSortKey( $string );
105
106 /**
107 * Given a string, return the logical "first letter" to be used for
108 * grouping on category pages and so on. This has to be coordinated
109 * carefully with convertToSortkey(), or else the sorted list might jump
110 * back and forth between the same "initial letters" or other pathological
111 * behavior. For instance, if you just return the first character, but "a"
112 * sorts the same as "A" based on getSortKey(), then you might get a
113 * list like
114 *
115 * == A ==
116 * * [[Aardvark]]
117 *
118 * == a ==
119 * * [[antelope]]
120 *
121 * == A ==
122 * * [[Ape]]
123 *
124 * etc., assuming for the sake of argument that $wgCapitalLinks is false.
125 *
126 * @since 1.16.3
127 *
128 * @param string $string UTF-8 string
129 * @return string UTF-8 string corresponding to the first letter of input
130 */
131 abstract function getFirstLetter( $string );
132
133 }