Records.php
2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace WPML\ST\Batch\Translation;
use WPML\FP\Curryable;
use WPML\FP\Fns;
use WPML\FP\Lst;
use function WPML\Container\make;
use function WPML\FP\curryN;
/**
* @phpstan-type curried '__CURRIED_PLACEHOLDER__'
*
* @method static callable|void installSchema( ...$wpdb ) :: wpdb → void
* @method static callable|void set( ...$wpdb, ...$batchId, ...$stringId ) :: wpdb → int → int → void
* @method static callable|int[] findBatches( ...$wpdb, ...$stringId ) :: wpdb → int → int[]
*/
class Records {
use Curryable;
/** @var string */
public static $string_batch_sql_prototype = '
CREATE TABLE IF NOT EXISTS `%sicl_string_batches` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`string_id` bigint(20) unsigned NOT NULL,
`batch_id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`)
)
';
/**
* @param \wpdb|null $wpdb
* @param int|curried $batchId
* @return int|callable
*
* @phpstan-return ($batchId is not null ? int : callable)
*/
public static function get( \wpdb $wpdb = null, $batchId = null ) {
return call_user_func_array(
curryN(
2,
function ( \wpdb $wpdb, $batchId ) {
/** @var string $sql */
$sql = $wpdb->prepare( "SELECT string_id FROM {$wpdb->prefix}icl_string_batches WHERE batch_id = %d", $batchId );
return $wpdb->get_col( $sql );
}
),
func_get_args()
);
}
}
Records::curryN(
'installSchema',
1,
function ( \wpdb $wpdb ) {
$option = make( 'WPML\WP\OptionManager' );
if ( ! $option->get( 'ST', Records::class . '_schema_installed' ) ) {
$wpdb->query( sprintf( Records::$string_batch_sql_prototype, $wpdb->prefix ) );
$option->set( 'ST', Records::class . '_schema_installed', true );
}
}
);
Records::curryN(
'set',
3,
function ( \wpdb $wpdb, $batchId, $stringId ) {
// TODO: ignore duplicates
$wpdb->insert(
"{$wpdb->prefix}icl_string_batches",
[
'batch_id' => $batchId,
'string_id' => $stringId,
],
[ '%d', '%d' ]
);
}
);
Records::curryN(
'findBatch',
2,
function ( \wpdb $wpdb, $stringId ) {
/** @var string $sql */
$sql = $wpdb->prepare( "SELECT batch_id FROM {$wpdb->prefix}icl_string_batches WHERE string_id = %d", $stringId );
return $wpdb->get_var( $sql );
}
);
Records::curryN(
'findBatches',
2,
function ( \wpdb $wpdb, $stringIds ) {
$in = wpml_prepare_in( $stringIds, '%d' );
$data = $wpdb->get_results(
"SELECT batch_id, string_id FROM {$wpdb->prefix}icl_string_batches WHERE string_id IN ({$in})"
);
$keyByStringId = Fns::converge( Lst::zipObj(), [ Lst::pluck( 'string_id' ), Lst::pluck( 'batch_id' ) ] );
return $keyByStringId( $data );
}
);