MediaWiki master
RemoteIcuCollation.php
Go to the documentation of this file.
1<?php
2
4use Shellbox\RPC\RpcClient;
5
12 private RpcClient $rpcClient;
13 private string $locale;
14
15 public function __construct( ShellboxClientFactory $shellboxClientFactory, string $locale ) {
16 $this->rpcClient = $shellboxClientFactory->getRpcClient(
17 [ 'service' => 'icu-collation' ] );
18 $this->locale = $locale;
19 }
20
22 public function getSortKey( $string ) {
23 return $this->getSortKeys( [ $string ] )[0];
24 }
25
32 private static function encode( $strings ) {
33 $ret = '';
34 foreach ( $strings as $s ) {
35 $ret .= sprintf( "%08x", strlen( $s ) ) . $s;
36 }
37 return $ret;
38 }
39
46 private static function decode( $blob ) {
47 $p = 0;
48 $ret = [];
49 while ( $p < strlen( $blob ) ) {
50 $len = intval( substr( $blob, $p, 8 ), 16 );
51 $p += 8;
52 $ret[] = substr( $blob, $p, $len );
53 $p += $len;
54 }
55 return $ret;
56 }
57
59 public function getSortKeys( $strings ) {
60 if ( !count( $strings ) ) {
61 return [];
62 }
63 $blob = $this->rpcClient->call(
64 'icu-collation',
65 [ self::class, 'doGetSortKeys' ],
66 [
67 $this->locale,
68 self::encode( array_values( $strings ) )
69 ],
70 [
71 'classes' => [ parent::class, self::class ],
72 'binary' => true
73 ]
74 );
75 return array_combine(
76 array_keys( $strings ),
77 self::decode( $blob )
78 );
79 }
80
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 ( str_ends_with( $locale, '-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.
static doGetSortKeys( $locale, $blob)
The remote entry point.
__construct(ShellboxClientFactory $shellboxClientFactory, string $locale)
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.string[]
getFirstLetter( $string)
Given a string, return the logical "first letter" to be used for grouping on category pages and so on...