Merge "Revert "Use display name in category page subheadings if provided""
[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 switch ( $collationName ) {
50 case 'uppercase':
51 return new UppercaseCollation;
52 case 'numeric':
53 return new NumericUppercaseCollation;
54 case 'identity':
55 return new IdentityCollation;
56 case 'uca-default':
57 return new IcuCollation( 'root' );
58 case 'uca-default-u-kn':
59 return new IcuCollation( 'root-u-kn' );
60 case 'xx-uca-ckb':
61 return new CollationCkb;
62 case 'xx-uca-et':
63 return new CollationEt;
64 default:
65 $match = [];
66 if ( preg_match( '/^uca-([a-z@=-]+)$/', $collationName, $match ) ) {
67 return new IcuCollation( $match[1] );
68 }
69
70 # Provide a mechanism for extensions to hook in.
71 $collationObject = null;
72 Hooks::run( 'Collation::factory', [ $collationName, &$collationObject ] );
73
74 if ( $collationObject instanceof Collation ) {
75 return $collationObject;
76 }
77
78 // If all else fails...
79 throw new MWException( __METHOD__ . ": unknown collation type \"$collationName\"" );
80 }
81 }
82
83 /**
84 * Given a string, convert it to a (hopefully short) key that can be used
85 * for efficient sorting. A binary sort according to the sortkeys
86 * corresponds to a logical sort of the corresponding strings. Current
87 * code expects that a line feed character should sort before all others, but
88 * has no other particular expectations (and that one can be changed if
89 * necessary).
90 *
91 * @since 1.16.3
92 *
93 * @param string $string UTF-8 string
94 * @return string Binary sortkey
95 */
96 abstract function getSortKey( $string );
97
98 /**
99 * Given a string, return the logical "first letter" to be used for
100 * grouping on category pages and so on. This has to be coordinated
101 * carefully with convertToSortkey(), or else the sorted list might jump
102 * back and forth between the same "initial letters" or other pathological
103 * behavior. For instance, if you just return the first character, but "a"
104 * sorts the same as "A" based on getSortKey(), then you might get a
105 * list like
106 *
107 * == A ==
108 * * [[Aardvark]]
109 *
110 * == a ==
111 * * [[antelope]]
112 *
113 * == A ==
114 * * [[Ape]]
115 *
116 * etc., assuming for the sake of argument that $wgCapitalLinks is false.
117 *
118 * @since 1.16.3
119 *
120 * @param string $string UTF-8 string
121 * @return string UTF-8 string corresponding to the first letter of input
122 */
123 abstract function getFirstLetter( $string );
124
125 }