1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Table Checksums Class. |
4
|
|
|
* |
5
|
|
|
* @package automattic/jetpack-sync |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Sync\Replicastore; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Sync; |
11
|
|
|
use Exception; |
12
|
|
|
use WP_Error; |
13
|
|
|
|
14
|
|
|
// TODO add rest endpoints to work with this, hopefully in the same folder. |
15
|
|
|
/** |
16
|
|
|
* Class to handle Table Checksums. |
17
|
|
|
*/ |
18
|
|
|
class Table_Checksum { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Table to be checksummed. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public $table = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Table Checksum Configuration. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
public $table_configuration = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Field to be used for range queries. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $range_field = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ID Field(s) to be used. |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
public $key_fields = array(); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Field(s) to be used in generating the checksum value. |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
public $checksum_fields = array(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Default filter values for the table |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
public $filter_values = array(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* SQL Query to be used to filter results (allow/disallow). |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
public $additional_filter_sql = ''; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Default Checksum Table Configurations. |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
public $default_tables = array(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Salt to be used when generating checksum. |
78
|
|
|
* |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
public $salt = ''; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tables which are allowed to be checksummed. |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
public $allowed_tables = array(); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* If the table has a "parent" table that it's related to. |
92
|
|
|
* |
93
|
|
|
* @var mixed|null |
94
|
|
|
*/ |
95
|
|
|
private $parent_table = null; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* What field to use for the parent table join, if it has a "parent" table. |
99
|
|
|
* |
100
|
|
|
* @var mixed|null |
101
|
|
|
*/ |
102
|
|
|
private $parent_join_field = null; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* What field to use for the table join, if it has a "parent" table. |
106
|
|
|
* |
107
|
|
|
* @var mixed|null |
108
|
|
|
*/ |
109
|
|
|
private $table_join_field = null; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Table_Checksum constructor. |
113
|
|
|
* |
114
|
|
|
* @param string $table The table to calculate checksums for. |
115
|
|
|
* @param string $salt Optional salt to add to the checksum. |
|
|
|
|
116
|
|
|
* |
117
|
|
|
* @throws Exception Throws exception from inner functions. |
118
|
|
|
*/ |
119
|
|
|
public function __construct( $table, $salt = null ) { |
120
|
|
|
|
121
|
|
|
if ( ! Sync\Settings::is_checksum_enabled() ) { |
122
|
|
|
throw new Exception( 'Checksums are currently disabled.' ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$this->salt = $salt; |
126
|
|
|
|
127
|
|
|
$this->default_tables = $this->get_default_tables(); |
128
|
|
|
|
129
|
|
|
// TODO change filters to allow the array format. |
130
|
|
|
// TODO add get_fields or similar method to get things out of the table. |
131
|
|
|
// TODO extract this configuration in a better way, still make it work with `$wpdb` names. |
132
|
|
|
// TODO take over the replicastore functions and move them over to this class. |
133
|
|
|
// TODO make the API work. |
134
|
|
|
|
135
|
|
|
$this->allowed_tables = apply_filters( 'jetpack_sync_checksum_allowed_tables', $this->default_tables ); |
136
|
|
|
|
137
|
|
|
$this->table = $this->validate_table_name( $table ); |
138
|
|
|
$this->table_configuration = $this->allowed_tables[ $table ]; |
139
|
|
|
|
140
|
|
|
$this->prepare_fields( $this->table_configuration ); |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get Default Table configurations. |
146
|
|
|
* |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
private function get_default_tables() { |
150
|
|
|
global $wpdb; |
151
|
|
|
|
152
|
|
|
return array( |
153
|
|
|
'posts' => array( |
154
|
|
|
'table' => $wpdb->posts, |
155
|
|
|
'range_field' => 'ID', |
156
|
|
|
'key_fields' => array( 'ID' ), |
157
|
|
|
'checksum_fields' => array( 'post_modified_gmt' ), |
158
|
|
|
'filter_values' => Sync\Settings::get_disallowed_post_types_structured(), |
159
|
|
|
), |
160
|
|
|
'postmeta' => array( |
161
|
|
|
'table' => $wpdb->postmeta, |
162
|
|
|
'range_field' => 'post_id', |
163
|
|
|
'key_fields' => array( 'post_id', 'meta_key' ), |
164
|
|
|
'checksum_fields' => array( 'meta_key', 'meta_value' ), |
165
|
|
|
'filter_values' => Sync\Settings::get_allowed_post_meta_structured(), |
166
|
|
|
'parent_table' => 'posts', |
167
|
|
|
'parent_join_field' => 'ID', |
168
|
|
|
'table_join_field' => 'post_id', |
169
|
|
|
), |
170
|
|
|
'comments' => array( |
171
|
|
|
'table' => $wpdb->comments, |
172
|
|
|
'range_field' => 'comment_ID', |
173
|
|
|
'key_fields' => array( 'comment_ID' ), |
174
|
|
|
'checksum_fields' => array( 'comment_date_gmt' ), |
175
|
|
|
'filter_values' => array( |
176
|
|
|
'comment_type' => array( |
177
|
|
|
'operator' => 'IN', |
178
|
|
|
'values' => apply_filters( |
179
|
|
|
'jetpack_sync_whitelisted_comment_types', |
180
|
|
|
array( '', 'comment', 'trackback', 'pingback', 'review' ) |
181
|
|
|
), |
182
|
|
|
), |
183
|
|
|
'comment_approved' => array( |
184
|
|
|
'operator' => 'NOT IN', |
185
|
|
|
'values' => array( 'spam' ), |
186
|
|
|
), |
187
|
|
|
), |
188
|
|
|
), |
189
|
|
|
'commentmeta' => array( |
190
|
|
|
'table' => $wpdb->commentmeta, |
191
|
|
|
'range_field' => 'comment_id', |
192
|
|
|
'key_fields' => array( 'comment_id', 'meta_key' ), |
193
|
|
|
'checksum_fields' => array( 'meta_key', 'meta_value' ), |
194
|
|
|
'filter_values' => Sync\Settings::get_allowed_comment_meta_structured(), |
195
|
|
|
'parent_table' => 'comments', |
196
|
|
|
'parent_join_field' => 'comment_ID', |
197
|
|
|
'table_join_field' => 'comment_id', |
198
|
|
|
), |
199
|
|
|
'terms' => array( |
200
|
|
|
'table' => $wpdb->terms, |
201
|
|
|
'range_field' => 'term_id', |
202
|
|
|
'key_fields' => array( 'term_id' ), |
203
|
|
|
'checksum_fields' => array( 'term_id', 'name', 'slug' ), |
204
|
|
|
'parent_table' => 'term_taxonomy', |
205
|
|
|
), |
206
|
|
|
'termmeta' => array( |
207
|
|
|
'table' => $wpdb->termmeta, |
208
|
|
|
'range_field' => 'term_id', |
209
|
|
|
'key_fields' => array( 'term_id', 'meta_key' ), |
210
|
|
|
'checksum_fields' => array( 'meta_key', 'meta_value' ), |
211
|
|
|
'parent_table' => 'term_taxonomy', |
212
|
|
|
), |
213
|
|
|
'term_relationships' => array( |
214
|
|
|
'table' => $wpdb->term_relationships, |
215
|
|
|
'range_field' => 'object_id', |
216
|
|
|
'key_fields' => array( 'object_id' ), |
217
|
|
|
'checksum_fields' => array( 'object_id', 'term_taxonomy_id' ), |
218
|
|
|
'parent_table' => 'term_taxonomy', |
219
|
|
|
'parent_join_field' => 'term_taxonomy_id', |
220
|
|
|
'table_join_field' => 'term_taxonomy_id', |
221
|
|
|
), |
222
|
|
|
'term_taxonomy' => array( |
223
|
|
|
'table' => $wpdb->term_taxonomy, |
224
|
|
|
'range_field' => 'term_taxonomy_id', |
225
|
|
|
'key_fields' => array( 'term_taxonomy_id' ), |
226
|
|
|
'checksum_fields' => array( 'term_taxonomy_id', 'term_id', 'taxonomy', 'description', 'parent' ), |
227
|
|
|
'filter_values' => Sync\Settings::get_blacklisted_taxonomies_structured(), |
228
|
|
|
), |
229
|
|
|
'links' => $wpdb->links, // TODO describe in the array format or add exceptions. |
230
|
|
|
'options' => $wpdb->options, // TODO describe in the array format or add exceptions. |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Prepare field params based off provided configuration. |
236
|
|
|
* |
237
|
|
|
* @param array $table_configuration The table configuration array. |
238
|
|
|
*/ |
239
|
|
|
private function prepare_fields( $table_configuration ) { |
240
|
|
|
$this->key_fields = $table_configuration['key_fields']; |
241
|
|
|
$this->range_field = $table_configuration['range_field']; |
242
|
|
|
$this->checksum_fields = $table_configuration['checksum_fields']; |
243
|
|
|
$this->filter_values = isset( $table_configuration['filter_values'] ) ? $table_configuration['filter_values'] : null; |
244
|
|
|
$this->additional_filter_sql = ! empty( $table_configuration['filter_sql'] ) ? $table_configuration['filter_sql'] : ''; |
245
|
|
|
$this->parent_table = isset( $table_configuration['parent_table'] ) ? $table_configuration['parent_table'] : null; |
246
|
|
|
$this->parent_join_field = isset( $table_configuration['parent_join_field'] ) ? $table_configuration['parent_join_field'] : $table_configuration['range_field']; |
247
|
|
|
$this->table_join_field = isset( $table_configuration['table_join_field'] ) ? $table_configuration['table_join_field'] : $table_configuration['range_field']; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Verify provided table name is valid for checksum processing. |
252
|
|
|
* |
253
|
|
|
* @param string $table Table name to validate. |
254
|
|
|
* |
255
|
|
|
* @return mixed|string |
256
|
|
|
* @throws Exception Throw an exception on validation failure. |
257
|
|
|
*/ |
258
|
|
|
private function validate_table_name( $table ) { |
259
|
|
|
if ( empty( $table ) ) { |
260
|
|
|
throw new Exception( 'Invalid table name: empty' ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if ( ! array_key_exists( $table, $this->allowed_tables ) ) { |
264
|
|
|
throw new Exception( "Invalid table name: $table not allowed" ); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// TODO other checks if such are needed. |
268
|
|
|
|
269
|
|
|
return $this->allowed_tables[ $table ]['table']; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Verify provided fields are proper names. |
274
|
|
|
* |
275
|
|
|
* @param array $fields Array of field names to validate. |
276
|
|
|
* |
277
|
|
|
* @throws Exception Throw an exception on failure to validate. |
278
|
|
|
*/ |
279
|
|
|
private function validate_fields( $fields ) { |
280
|
|
|
foreach ( $fields as $field ) { |
281
|
|
|
if ( ! preg_match( '/^[0-9,a-z,A-Z$_]+$/i', $field ) ) { |
282
|
|
|
throw new Exception( "Invalid field name: $field is not allowed" ); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// TODO other verifications of the field names. |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Verify the fields exist in the table. |
291
|
|
|
* |
292
|
|
|
* @param array $fields Array of fields to validate. |
293
|
|
|
* |
294
|
|
|
* @return bool |
295
|
|
|
* @throws Exception Throw an exception on failure to validate. |
296
|
|
|
*/ |
297
|
|
|
private function validate_fields_against_table( $fields ) { |
298
|
|
|
global $wpdb; |
299
|
|
|
|
300
|
|
|
$valid_fields = array(); |
301
|
|
|
|
302
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
303
|
|
|
$result = $wpdb->get_results( "SHOW COLUMNS FROM {$this->table}", ARRAY_A ); |
304
|
|
|
|
305
|
|
|
foreach ( $result as $result_row ) { |
306
|
|
|
$valid_fields[] = $result_row['Field']; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
// Check if the fields are actually contained in the table. |
310
|
|
|
foreach ( $fields as $field_to_check ) { |
311
|
|
|
if ( ! in_array( $field_to_check, $valid_fields, true ) ) { |
312
|
|
|
throw new Exception( "Invalid field name: field '{$field_to_check}' doesn't exist in table {$this->table}" ); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return true; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Verify the configured fields. |
321
|
|
|
* |
322
|
|
|
* @throws Exception Throw an exception on failure to validate in the internal functions. |
323
|
|
|
*/ |
324
|
|
|
private function validate_input() { |
325
|
|
|
$fields = array_merge( array( $this->range_field ), $this->key_fields, $this->checksum_fields ); |
326
|
|
|
|
327
|
|
|
$this->validate_fields( $fields ); |
328
|
|
|
$this->validate_fields_against_table( $fields ); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Prepare filter values as SQL statements to be added to the other filters. |
333
|
|
|
* |
334
|
|
|
* @param array $filter_values The filter values array. |
335
|
|
|
* @param string $table_prefix If the values are going to be used in a sub-query, add a prefix with the table alias. |
336
|
|
|
* |
337
|
|
|
* @return array|null |
338
|
|
|
*/ |
339
|
|
|
private function prepare_filter_values_as_sql( $filter_values = array(), $table_prefix = '' ) { |
340
|
|
|
global $wpdb; |
341
|
|
|
|
342
|
|
|
if ( ! is_array( $filter_values ) ) { |
343
|
|
|
return null; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
$result = array(); |
347
|
|
|
|
348
|
|
|
foreach ( $filter_values as $field => $filter ) { |
349
|
|
|
$key = ( ! empty( $table_prefix ) ? $table_prefix : $this->table ) . '.' . $field; |
350
|
|
|
|
351
|
|
|
switch ( $filter['operator'] ) { |
352
|
|
|
case 'IN': |
353
|
|
|
case 'NOT IN': |
354
|
|
|
$values_placeholders = implode( ',', array_fill( 0, count( $filter['values'] ), '%s' ) ); |
355
|
|
|
$statement = "{$key} {$filter['operator']} ( $values_placeholders )"; |
356
|
|
|
|
357
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
358
|
|
|
$prepared_statement = $wpdb->prepare( $statement, $filter['values'] ); |
359
|
|
|
|
360
|
|
|
$result[] = $prepared_statement; |
361
|
|
|
break; |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
return $result; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Build the filter query baased off range fields and values and the additional sql. |
370
|
|
|
* |
371
|
|
|
* @param int|null $range_from Start of the range. |
372
|
|
|
* @param int|null $range_to End of the range. |
373
|
|
|
* @param array|null $filter_values Additional filter values. Not used at the moment. |
374
|
|
|
* @param string $table_prefix Table name to be prefixed to the columns. Used in sub-queries where columns can clash. |
375
|
|
|
* |
376
|
|
|
* @return string |
377
|
|
|
*/ |
378
|
|
|
public function build_filter_statement( $range_from = null, $range_to = null, $filter_values = null, $table_prefix = '' ) { |
379
|
|
|
global $wpdb; |
380
|
|
|
|
381
|
|
|
// If there is a field prefix that we want to use with table aliases. |
382
|
|
|
$parent_prefix = ( ! empty( $table_prefix ) ? $table_prefix : $this->table ) . '.'; |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Prepare the ranges. |
386
|
|
|
*/ |
387
|
|
|
|
388
|
|
|
$filter_array = array( '1 = 1' ); |
389
|
|
View Code Duplication |
if ( null !== $range_from ) { |
390
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
391
|
|
|
$filter_array[] = $wpdb->prepare( "{$parent_prefix}{$this->range_field} >= %d", array( intval( $range_from ) ) ); |
392
|
|
|
} |
393
|
|
View Code Duplication |
if ( null !== $range_to ) { |
394
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
395
|
|
|
$filter_array[] = $wpdb->prepare( "{$parent_prefix}{$this->range_field} <= %d", array( intval( $range_to ) ) ); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* End prepare the ranges. |
400
|
|
|
*/ |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Prepare data filters. |
404
|
|
|
*/ |
405
|
|
|
|
406
|
|
|
// Default filters. |
407
|
|
|
if ( $this->filter_values ) { |
|
|
|
|
408
|
|
|
$prepared_values_statements = $this->prepare_filter_values_as_sql( $this->filter_values, $table_prefix ); |
409
|
|
|
if ( $prepared_values_statements ) { |
410
|
|
|
$filter_array = array_merge( $filter_array, $prepared_values_statements ); |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
// Additional filters. |
415
|
|
|
if ( ! empty( $filter_values ) ) { |
416
|
|
|
// Prepare filtering. |
417
|
|
|
$prepared_values_statements = $this->prepare_filter_values_as_sql( $filter_values, $table_prefix ); |
418
|
|
|
if ( $prepared_values_statements ) { |
419
|
|
|
$filter_array = array_merge( $filter_array, $prepared_values_statements ); |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
// Add any additional filters via direct SQL statement. |
424
|
|
|
// Currently used only because we haven't converted all filtering to happen via `filter_values`. |
425
|
|
|
// This SQL is NOT prefixed and column clashes can occur when used in sub-queries. |
426
|
|
|
if ( $this->additional_filter_sql ) { |
427
|
|
|
$filter_array[] = $this->additional_filter_sql; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* End prepare data filters. |
432
|
|
|
*/ |
433
|
|
|
return implode( ' AND ', $filter_array ); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Returns the checksum query. All validation of fields and configurations are expected to occur prior to usage. |
438
|
|
|
* |
439
|
|
|
* @param int|null $range_from The start of the range. |
440
|
|
|
* @param int|null $range_to The end of the range. |
441
|
|
|
* @param array|null $filter_values Additional filter values. Not used at the moment. |
442
|
|
|
* @param bool $granular_result If the function should return a granular result. |
443
|
|
|
* |
444
|
|
|
* @return string |
445
|
|
|
* |
446
|
|
|
* @throws Exception Throws and exception if validation fails in the internal function calls. |
447
|
|
|
*/ |
448
|
|
|
private function build_checksum_query( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false ) { |
449
|
|
|
global $wpdb; |
450
|
|
|
|
451
|
|
|
// Escape the salt. |
452
|
|
|
$salt = $wpdb->prepare( '%s', $this->salt ); // TODO escape or prepare statement. |
453
|
|
|
|
454
|
|
|
// Prepare the compound key. |
455
|
|
|
$key_fields = array(); |
456
|
|
|
|
457
|
|
|
// Prefix the fields with the table name, to avoid clashes in queries with sub-queries (e.g. meta tables). |
458
|
|
|
foreach ( $this->key_fields as $field ) { |
459
|
|
|
$key_fields[] = $this->table . '.' . $field; |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
$key_fields = implode( ',', $key_fields ); |
463
|
|
|
|
464
|
|
|
// Prepare the checksum fields. |
465
|
|
|
$checksum_fields = array(); |
466
|
|
|
// Prefix the fields with the table name, to avoid clashes in queries with sub-queries (e.g. meta tables). |
467
|
|
|
foreach ( $this->checksum_fields as $field ) { |
468
|
|
|
$checksum_fields[] = $this->table . '.' . $field; |
469
|
|
|
} |
470
|
|
|
$checksum_fields_string = implode( ',', array_merge( $checksum_fields, array( $salt ) ) ); |
471
|
|
|
|
472
|
|
|
$additional_fields = ''; |
473
|
|
|
if ( $granular_result ) { |
474
|
|
|
// TODO uniq the fields as sometimes(most) range_index is the key and there's no need to select the same field twice. |
475
|
|
|
$additional_fields = " |
476
|
|
|
{$this->table}.{$this->range_field} as range_index, |
477
|
|
|
{$key_fields}, |
478
|
|
|
"; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
$filter_stamenet = $this->build_filter_statement( $range_from, $range_to, $filter_values ); |
482
|
|
|
|
483
|
|
|
$join_statement = ''; |
484
|
|
|
if ( $this->parent_table ) { |
485
|
|
|
$parent_table_obj = new Table_Checksum( $this->parent_table ); |
486
|
|
|
$parent_filter_query = $parent_table_obj->build_filter_statement( null, null, null, 'parent_table' ); |
487
|
|
|
|
488
|
|
|
$join_statement = " |
489
|
|
|
INNER JOIN {$parent_table_obj->table} as parent_table ON ({$this->table}.{$this->table_join_field} = parent_table.{$this->parent_join_field} AND {$parent_filter_query}) |
490
|
|
|
"; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
$query = " |
494
|
|
|
SELECT |
495
|
|
|
{$additional_fields} |
496
|
|
|
SUM( |
497
|
|
|
CRC32( |
498
|
|
|
CONCAT_WS( '#', {$salt}, {$checksum_fields_string} ) |
499
|
|
|
) |
500
|
|
|
) AS checksum |
501
|
|
|
FROM |
502
|
|
|
{$this->table} |
503
|
|
|
{$join_statement} |
504
|
|
|
WHERE |
505
|
|
|
{$filter_stamenet} |
506
|
|
|
"; |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* We need the GROUP BY only for compound keys. |
510
|
|
|
*/ |
511
|
|
|
if ( $granular_result ) { |
512
|
|
|
$query .= " |
513
|
|
|
GROUP BY {$key_fields} |
514
|
|
|
LIMIT 9999999 |
515
|
|
|
"; |
516
|
|
|
} |
517
|
|
|
|
518
|
|
|
return $query; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Obtain the min-max values (edges) of the range. |
523
|
|
|
* |
524
|
|
|
* @param int|null $range_from The start of the range. |
525
|
|
|
* @param int|null $range_to The end of the range. |
526
|
|
|
* @param int|null $limit How many values to return. |
527
|
|
|
* |
528
|
|
|
* @return array|object|void |
529
|
|
|
* @throws Exception Throws an exception if validation fails on the internal function calls. |
530
|
|
|
*/ |
531
|
|
|
public function get_range_edges( $range_from = null, $range_to = null, $limit = null ) { |
532
|
|
|
global $wpdb; |
533
|
|
|
|
534
|
|
|
$this->validate_fields( array( $this->range_field ) ); |
535
|
|
|
|
536
|
|
|
// Performance :: When getting the postmeta range we do not want to filter by the whitelist. |
537
|
|
|
// The reason for this is that it leads to a non-performant query that can timeout. |
538
|
|
|
// Instead lets get the range based on posts regardless of meta. |
539
|
|
|
$filter_values = $this->filter_values; |
540
|
|
|
if ( 'postmeta' === $this->table ) { |
541
|
|
|
$this->filter_values = null; |
|
|
|
|
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
// `trim()` to make sure we don't add the statement if it's empty. |
545
|
|
|
$filters = trim( $this->build_filter_statement( $range_from, $range_to ) ); |
546
|
|
|
|
547
|
|
|
// Reset Post meta filter. |
548
|
|
|
if ( 'postmeta' === $this->table ) { |
549
|
|
|
$this->filter_values = $filter_values; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
$filter_statement = ''; |
553
|
|
|
if ( ! empty( $filters ) ) { |
554
|
|
|
$filter_statement = " |
555
|
|
|
WHERE |
556
|
|
|
{$filters} |
557
|
|
|
"; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
// Only make the distinct count when we know there can be multiple entries for the range column. |
561
|
|
|
$distinct_count = count( $this->key_fields ) > 1 ? 'DISTINCT' : ''; |
562
|
|
|
|
563
|
|
|
$query = " |
564
|
|
|
SELECT |
565
|
|
|
MIN({$this->range_field}) as min_range, |
566
|
|
|
MAX({$this->range_field}) as max_range, |
567
|
|
|
COUNT( {$distinct_count} {$this->range_field}) as item_count |
568
|
|
|
FROM |
569
|
|
|
"; |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* If `$limit` is not specified, we can directly use the table. |
573
|
|
|
*/ |
574
|
|
|
if ( ! $limit ) { |
|
|
|
|
575
|
|
|
$query .= " |
576
|
|
|
{$this->table} |
577
|
|
|
{$filter_statement} |
578
|
|
|
"; |
579
|
|
|
} else { |
580
|
|
|
/** |
581
|
|
|
* If there is `$limit` specified, we can't directly use `MIN/MAX()` as they don't work with `LIMIT`. |
582
|
|
|
* That's why we will alter the query for this case. |
583
|
|
|
*/ |
584
|
|
|
$limit = intval( $limit ); |
585
|
|
|
|
586
|
|
|
$query .= " |
587
|
|
|
( |
588
|
|
|
SELECT |
589
|
|
|
{$distinct_count} {$this->range_field} |
590
|
|
|
FROM |
591
|
|
|
{$this->table} |
592
|
|
|
{$filter_statement} |
593
|
|
|
ORDER BY |
594
|
|
|
{$this->range_field} ASC |
595
|
|
|
LIMIT {$limit} |
596
|
|
|
) as ids_query |
597
|
|
|
"; |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
601
|
|
|
$result = $wpdb->get_row( $query, ARRAY_A ); |
602
|
|
|
|
603
|
|
|
if ( ! $result || ! is_array( $result ) ) { |
604
|
|
|
throw new Exception( 'Unable to get range edges' ); |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
return $result; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Update the results to have key/checksum format. |
612
|
|
|
* |
613
|
|
|
* @param array $results Prepare the results for output of granular results. |
614
|
|
|
*/ |
615
|
|
|
protected function prepare_results_for_output( &$results ) { |
616
|
|
|
// get the compound key. |
617
|
|
|
// only return range and compound key for granular results. |
618
|
|
|
|
619
|
|
|
$return_value = array(); |
620
|
|
|
|
621
|
|
|
foreach ( $results as &$result ) { |
622
|
|
|
// Working on reference to save memory here. |
623
|
|
|
|
624
|
|
|
$key = array(); |
625
|
|
|
foreach ( $this->key_fields as $field ) { |
626
|
|
|
$key[] = $result[ $field ]; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
$return_value[ implode( '-', $key ) ] = $result['checksum']; |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
return $return_value; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
/** |
636
|
|
|
* Calculate the checksum based on provided range and filters. |
637
|
|
|
* |
638
|
|
|
* @param int|null $range_from The start of the range. |
639
|
|
|
* @param int|null $range_to The end of the range. |
640
|
|
|
* @param array|null $filter_values Additional filter values. Not used at the moment. |
641
|
|
|
* @param bool $granular_result If the returned result should be granular or only the checksum. |
642
|
|
|
* @param bool $simple_return_value If we want to use a simple return value for non-granular results (return only the checksum, without wrappers). |
643
|
|
|
* |
644
|
|
|
* @return array|mixed|object|WP_Error|null |
645
|
|
|
*/ |
646
|
|
|
public function calculate_checksum( $range_from = null, $range_to = null, $filter_values = null, $granular_result = false, $simple_return_value = true ) { |
647
|
|
|
|
648
|
|
|
if ( ! Sync\Settings::is_checksum_enabled() ) { |
649
|
|
|
return new WP_Error( 'checksum_disabled', 'Checksums are currently disabled.' ); |
|
|
|
|
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
try { |
653
|
|
|
$this->validate_input(); |
654
|
|
|
} catch ( Exception $ex ) { |
655
|
|
|
return new WP_Error( 'invalid_input', $ex->getMessage() ); |
|
|
|
|
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
$query = $this->build_checksum_query( $range_from, $range_to, $filter_values, $granular_result ); |
659
|
|
|
|
660
|
|
|
global $wpdb; |
661
|
|
|
|
662
|
|
|
if ( ! $granular_result ) { |
663
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
664
|
|
|
$result = $wpdb->get_row( $query, ARRAY_A ); |
665
|
|
|
|
666
|
|
|
if ( ! is_array( $result ) ) { |
667
|
|
|
return new WP_Error( 'invalid_query', "Result wasn't an array" ); |
|
|
|
|
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
if ( $simple_return_value ) { |
671
|
|
|
return $result['checksum']; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
return array( |
675
|
|
|
'range' => $range_from . '-' . $range_to, |
676
|
|
|
'checksum' => $result['checksum'], |
677
|
|
|
); |
678
|
|
|
} else { |
679
|
|
|
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
680
|
|
|
$result = $wpdb->get_results( $query, ARRAY_A ); |
681
|
|
|
return $this->prepare_results_for_output( $result ); |
682
|
|
|
} |
683
|
|
|
} |
684
|
|
|
} |
685
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.