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