MediaWiki REL1_39
RemoteIcuCollation.php
Go to the documentation of this file.
1<?php
2
4
11 private $rpcClient;
12 private $locale;
13
18 public function __construct( ShellboxClientFactory $shellboxClientFactory, $locale ) {
19 $this->rpcClient = $shellboxClientFactory->getRpcClient(
20 [ 'service' => 'icu-collation' ] );
21 $this->locale = $locale;
22 }
23
24 public function getSortKey( $string ) {
25 return $this->getSortKeys( [ $string ] )[0];
26 }
27
34 private static function encode( $strings ) {
35 $ret = '';
36 foreach ( $strings as $s ) {
37 $ret .= sprintf( "%08x", strlen( $s ) ) . $s;
38 }
39 return $ret;
40 }
41
48 private static function decode( $blob ) {
49 $p = 0;
50 $ret = [];
51 while ( $p < strlen( $blob ) ) {
52 $len = intval( substr( $blob, $p, 8 ), 16 );
53 $p += 8;
54 $ret[] = substr( $blob, $p, $len );
55 $p += $len;
56 }
57 return $ret;
58 }
59
60 public function getSortKeys( $strings ) {
61 if ( !count( $strings ) ) {
62 return [];
63 }
64 $blob = $this->rpcClient->call(
65 'icu-collation',
66 self::class . '::' . 'doGetSortKeys',
67 [
68 $this->locale,
69 self::encode( array_values( $strings ) )
70 ],
71 [
72 'classes' => [ parent::class, self::class ],
73 'binary' => true
74 ]
75 );
76 return array_combine(
77 array_keys( $strings ),
78 self::decode( $blob )
79 );
80 }
81
82 public function getFirstLetter( $string ) {
83 // @phan-suppress-previous-line PhanPluginNeverReturnMethod
84 throw new RuntimeException( __METHOD__ . ': not implemented' );
85 }
86
94 public static function doGetSortKeys( $locale, $blob ) {
95 $mainCollator = Collator::create( $locale );
96 if ( !$mainCollator ) {
97 throw new RuntimeException( "Invalid ICU locale specified for collation: $locale" );
98 }
99
100 // If the special suffix for numeric collation is present, turn on numeric collation.
101 if ( substr( $locale, -5, 5 ) === '-u-kn' ) {
102 $mainCollator->setAttribute( Collator::NUMERIC_COLLATION, Collator::ON );
103 }
104 $ret = [];
105 foreach ( self::decode( $blob ) as $string ) {
106 $ret[] = $mainCollator->getSortKey( $string );
107 }
108 return self::encode( $ret );
109 }
110}
This is a service which provides a configured client to access a remote Shellbox installation.
getRpcClient(array $options=[])
Get a Shellbox RPC client with specified options.
An ICU collation that uses a remote server to compute sort keys.
__construct(ShellboxClientFactory $shellboxClientFactory, $locale)
static doGetSortKeys( $locale, $blob)
The remote entry point.
getSortKey( $string)
Given a string, convert it to a (hopefully short) key that can be used for efficient sorting.
getSortKeys( $strings)
Get multiple sort keys.
getFirstLetter( $string)
Given a string, return the logical "first letter" to be used for grouping on category pages and so on...
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s