Completed
Push — add/private-site-mode ( 81d3a8...c37ff2 )
by
unknown
25:46 queued 11:45
created

Module::still_valid_checksum()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 2
dl 0
loc 7
c 0
b 0
f 0
cc 3
nop 3
rs 10
1
<?php
2
/**
3
 * A base abstraction of a sync module.
4
 *
5
 * @package automattic/jetpack-sync
6
 */
7
8
namespace Automattic\Jetpack\Sync\Modules;
9
10
use Automattic\Jetpack\Sync\Functions;
11
use Automattic\Jetpack\Sync\Listener;
12
use Automattic\Jetpack\Sync\Replicastore;
13
use Automattic\Jetpack\Sync\Sender;
14
use Automattic\Jetpack\Sync\Settings;
15
16
/**
17
 * Basic methods implemented by Jetpack Sync extensions.
18
 *
19
 * @abstract
20
 */
21
abstract class Module {
22
	/**
23
	 * Number of items per chunk when grouping objects for performance reasons.
24
	 *
25
	 * @access public
26
	 *
27
	 * @var int
28
	 */
29
	const ARRAY_CHUNK_SIZE = 10;
30
31
	/**
32
	 * Sync module name.
33
	 *
34
	 * @access public
35
	 *
36
	 * @return string
37
	 */
38
	abstract public function name();
39
40
	/**
41
	 * The id field in the database.
42
	 *
43
	 * @access public
44
	 *
45
	 * @return string
46
	 */
47
	public function id_field() {
48
		return 'ID';
49
	}
50
51
	/**
52
	 * The table in the database.
53
	 *
54
	 * @access public
55
	 *
56
	 * @return string|bool
57
	 */
58
	public function table_name() {
59
		return false;
60
	}
61
62
	// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
63
64
	/**
65
	 * Retrieve a sync object by its ID.
66
	 *
67
	 * @access public
68
	 *
69
	 * @param string $object_type Type of the sync object.
70
	 * @param int    $id          ID of the sync object.
71
	 * @return mixed Object, or false if the object is invalid.
72
	 */
73
	public function get_object_by_id( $object_type, $id ) {
74
		return false;
75
	}
76
77
	/**
78
	 * Initialize callables action listeners.
79
	 * Override these to set up listeners and set/reset data/defaults.
80
	 *
81
	 * @access public
82
	 *
83
	 * @param callable $callable Action handler callable.
84
	 */
85
	public function init_listeners( $callable ) {
86
	}
87
88
	/**
89
	 * Initialize module action listeners for full sync.
90
	 *
91
	 * @access public
92
	 *
93
	 * @param callable $callable Action handler callable.
94
	 */
95
	public function init_full_sync_listeners( $callable ) {
96
	}
97
98
	/**
99
	 * Initialize the module in the sender.
100
	 *
101
	 * @access public
102
	 */
103
	public function init_before_send() {
104
	}
105
106
	/**
107
	 * Set module defaults.
108
	 *
109
	 * @access public
110
	 */
111
	public function set_defaults() {
112
	}
113
114
	/**
115
	 * Perform module cleanup.
116
	 * Usually triggered when uninstalling the plugin.
117
	 *
118
	 * @access public
119
	 */
120
	public function reset_data() {
121
	}
122
123
	/**
124
	 * Enqueue the module actions for full sync.
125
	 *
126
	 * @access public
127
	 *
128
	 * @param array   $config               Full sync configuration for this sync module.
129
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
130
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
131
	 * @return array  Number of actions enqueued, and next module state.
132
	 */
133
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
134
		// In subclasses, return the number of actions enqueued, and next module state (true == done).
135
		return array( null, true );
136
	}
137
138
	/**
139
	 * Retrieve an estimated number of actions that will be enqueued.
140
	 *
141
	 * @access public
142
	 *
143
	 * @param array $config Full sync configuration for this sync module.
144
	 * @return array Number of items yet to be enqueued.
145
	 */
146
	public function estimate_full_sync_actions( $config ) {
147
		// In subclasses, return the number of items yet to be enqueued.
148
		return null;
149
	}
150
151
	// phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
152
153
	/**
154
	 * Retrieve the actions that will be sent for this module during a full sync.
155
	 *
156
	 * @access public
157
	 *
158
	 * @return array Full sync actions of this module.
159
	 */
160
	public function get_full_sync_actions() {
161
		return array();
162
	}
163
164
	/**
165
	 * Get the number of actions that we care about.
166
	 *
167
	 * @access protected
168
	 *
169
	 * @param array $action_names     Action names we're interested in.
170
	 * @param array $actions_to_count Unfiltered list of actions we want to count.
171
	 * @return array Number of actions that we're interested in.
172
	 */
173
	protected function count_actions( $action_names, $actions_to_count ) {
174
		return count( array_intersect( $action_names, $actions_to_count ) );
175
	}
176
177
	/**
178
	 * Calculate the checksum of one or more values.
179
	 *
180
	 * @access protected
181
	 *
182
	 * @param mixed $values Values to calculate checksum for.
183
	 * @param bool  $sort If $values should have ksort called on it.
184
	 * @return int The checksum.
185
	 */
186
	protected function get_check_sum( $values, $sort = true ) {
187
		// Associative array order changes the generated checksum value.
188
		if ( $sort && is_array( $values ) ) {
189
			$this->recursive_ksort( $values );
190
		}
191
		return crc32( wp_json_encode( Functions::json_wrap( $values ) ) );
192
	}
193
194
	/**
195
	 * Recursively call ksort on an Array
196
	 *
197
	 * @param array $values Array.
198
	 */
199
	private function recursive_ksort( &$values ) {
200
		ksort( $values );
201
		foreach ( $values as &$value ) {
202
			if ( is_array( $value ) ) {
203
				$this->recursive_ksort( $value );
204
			}
205
		}
206
	}
207
208
	/**
209
	 * Whether a particular checksum in a set of checksums is valid.
210
	 *
211
	 * @access protected
212
	 *
213
	 * @param array  $sums_to_check Array of checksums.
214
	 * @param string $name          Name of the checksum.
215
	 * @param int    $new_sum       Checksum to compare against.
216
	 * @return boolean Whether the checksum is valid.
217
	 */
218
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
219
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
220
			return true;
221
		}
222
223
		return false;
224
	}
225
226
	/**
227
	 * Enqueue all items of a sync type as an action.
228
	 *
229
	 * @access protected
230
	 *
231
	 * @param string  $action_name          Name of the action.
232
	 * @param string  $table_name           Name of the database table.
233
	 * @param string  $id_field             Name of the ID field in the database.
234
	 * @param string  $where_sql            The SQL WHERE clause to filter to the desired items.
235
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue in the same time.
236
	 * @param boolean $state                Whether enqueueing has finished.
237
	 * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished.
238
	 */
239
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) {
240
		global $wpdb;
241
242
		if ( ! $where_sql ) {
243
			$where_sql = '1 = 1';
244
		}
245
246
		$items_per_page        = 1000;
247
		$page                  = 1;
248
		$chunk_count           = 0;
249
		$previous_interval_end = $state ? $state : '~0';
250
		$listener              = Listener::get_instance();
251
252
		// Count down from max_id to min_id so we get newest posts/comments/etc first.
253
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
254
		while ( $ids = $wpdb->get_col( "SELECT {$id_field} FROM {$table_name} WHERE {$where_sql} AND {$id_field} < {$previous_interval_end} ORDER BY {$id_field} DESC LIMIT {$items_per_page}" ) ) {
255
			// Request posts in groups of N for efficiency.
256
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
257
258
			// If we hit our row limit, process and return.
259
			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
260
				$remaining_items_count                      = $max_items_to_enqueue - $chunk_count;
261
				$remaining_items                            = array_slice( $chunked_ids, 0, $remaining_items_count );
262
				$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end );
263
				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end );
264
265
				$last_chunk = end( $remaining_items );
266
				return array( $remaining_items_count + $chunk_count, end( $last_chunk ) );
267
			}
268
			$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end );
269
270
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end );
271
272
			$chunk_count += count( $chunked_ids );
273
			$page++;
274
			// The $ids are ordered in descending order.
275
			$previous_interval_end = end( $ids );
276
		}
277
278
		if ( $wpdb->last_error ) {
279
			// return the values that were passed in so all these chunks get retried.
280
			return array( $max_items_to_enqueue, $state );
281
		}
282
283
		return array( $chunk_count, true );
284
	}
285
286
	/**
287
	 * Given the Module Full Sync Configuration and Status return the next chunk of items to send.
288
	 *
289
	 * @param array $config This module Full Sync configuration.
290
	 * @param array $status This module Full Sync status.
291
	 * @param int   $chunk_size Chunk size.
292
	 *
293
	 * @return array|object|null
294
	 */
295
	public function get_next_chunk( $config, $status, $chunk_size ) {
296
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
297
		global $wpdb;
298
		return $wpdb->get_col(
299
			<<<SQL
300
SELECT {$this->id_field()}
301
FROM {$wpdb->{$this->table_name()}}
302
WHERE {$this->get_where_sql( $config )}
303
AND {$this->id_field()} < {$status['last_sent']}
304
ORDER BY {$this->id_field()}
305
DESC LIMIT {$chunk_size}
306
SQL
307
		);
308
		// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
309
	}
310
311
	/**
312
	 * Return the initial last sent object.
313
	 *
314
	 * @return string|array initial status.
315
	 */
316
	public function get_initial_last_sent() {
317
		return '~0';
318
	}
319
320
	/**
321
	 * Immediately send all items of a sync type as an action.
322
	 *
323
	 * @access protected
324
	 *
325
	 * @param string $config Full sync configuration for this module.
326
	 * @param array  $status the current module full sync status.
327
	 * @param float  $send_until timestamp until we want this request to send full sync events.
328
	 *
329
	 * @return array Status, the module full sync status updated.
330
	 */
331
	public function send_full_sync_actions( $config, $status, $send_until ) {
332
		global $wpdb;
333
334
		if ( empty( $status['last_sent'] ) ) {
335
			$status['last_sent'] = $this->get_initial_last_sent();
336
		}
337
338
		$limits = Settings::get_setting( 'full_sync_limits' )[ $this->name() ];
339
340
		$chunks_sent = 0;
341
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
342
		while ( $objects = $this->get_next_chunk( $config, $status, $limits['chunk_size'] ) ) {
0 ignored issues
show
Documentation introduced by
$config is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
343
			if ( $chunks_sent++ === $limits['max_chunks'] || microtime( true ) >= $send_until ) {
344
				return $status;
345
			}
346
347
			$result = $this->send_action( 'jetpack_full_sync_' . $this->name(), array( $objects, $status['last_sent'] ) );
348
349
			if ( is_wp_error( $result ) || $wpdb->last_error ) {
350
				$status['error'] = true;
351
				return $status;
352
			}
353
			// The $ids are ordered in descending order.
354
			$status['last_sent'] = end( $objects );
355
			$status['sent']     += count( $objects );
356
		}
357
358
		if ( ! $wpdb->last_error ) {
359
			$status['finished'] = true;
360
		}
361
362
		return $status;
363
	}
364
365
	/**
366
	 * Immediately sends a single item without firing or enqueuing it
367
	 *
368
	 * @param string $action_name The action.
369
	 * @param array  $data The data associated with the action.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $data not be array|null?

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.

Loading history...
370
	 */
371
	public function send_action( $action_name, $data = null ) {
372
		$sender = Sender::get_instance();
373
		return $sender->send_action( $action_name, $data );
374
	}
375
376
	/**
377
	 * Retrieve chunk IDs with previous interval end.
378
	 *
379
	 * @access protected
380
	 *
381
	 * @param array $chunks                All remaining items.
382
	 * @param int   $previous_interval_end The last item from the previous interval.
383
	 * @return array Chunk IDs with the previous interval end.
384
	 */
385
	protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) {
386
		$chunks_with_ends = array();
387
		foreach ( $chunks as $chunk ) {
388
			$chunks_with_ends[] = array(
389
				'ids'          => $chunk,
390
				'previous_end' => $previous_interval_end,
391
			);
392
			// Chunks are ordered in descending order.
393
			$previous_interval_end = end( $chunk );
394
		}
395
		return $chunks_with_ends;
396
	}
397
398
	/**
399
	 * Get metadata of a particular object type within the designated meta key whitelist.
400
	 *
401
	 * @access protected
402
	 *
403
	 * @todo Refactor to use $wpdb->prepare() on the SQL query.
404
	 *
405
	 * @param array  $ids                Object IDs.
406
	 * @param string $meta_type          Meta type.
407
	 * @param array  $meta_key_whitelist Meta key whitelist.
408
	 * @return array Unserialized meta values.
409
	 */
410
	protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) {
411
		global $wpdb;
412
		$table = _get_meta_table( $meta_type );
413
		$id    = $meta_type . '_id';
414
		if ( ! $table ) {
415
			return array();
416
		}
417
418
		$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'";
419
420
		return array_map(
421
			array( $this, 'unserialize_meta' ),
422
			$wpdb->get_results(
423
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
424
				"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )' .
425
				" AND meta_key IN ( $private_meta_whitelist_sql ) ",
426
				// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
427
				OBJECT
428
			)
429
		);
430
	}
431
432
	/**
433
	 * Initialize listeners for the particular meta type.
434
	 *
435
	 * @access public
436
	 *
437
	 * @param string   $meta_type Meta type.
438
	 * @param callable $callable  Action handler callable.
439
	 */
440
	public function init_listeners_for_meta_type( $meta_type, $callable ) {
441
		add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
442
		add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
443
		add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
444
	}
445
446
	/**
447
	 * Initialize meta whitelist handler for the particular meta type.
448
	 *
449
	 * @access public
450
	 *
451
	 * @param string   $meta_type         Meta type.
452
	 * @param callable $whitelist_handler Action handler callable.
453
	 */
454
	public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) {
455
		add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
456
		add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
457
		add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
458
	}
459
460
	/**
461
	 * Retrieve the term relationships for the specified object IDs.
462
	 *
463
	 * @access protected
464
	 *
465
	 * @todo This feels too specific to be in the abstract sync Module class. Move it?
466
	 *
467
	 * @param array $ids Object IDs.
468
	 * @return array Term relationships - object ID and term taxonomy ID pairs.
469
	 */
470
	protected function get_term_relationships( $ids ) {
471
		global $wpdb;
472
473
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
474
		return $wpdb->get_results( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )', OBJECT );
475
	}
476
477
	/**
478
	 * Unserialize the value of a meta object, if necessary.
479
	 *
480
	 * @access public
481
	 *
482
	 * @param object $meta Meta object.
483
	 * @return object Meta object with possibly unserialized value.
484
	 */
485
	public function unserialize_meta( $meta ) {
486
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
487
		return $meta;
488
	}
489
490
	/**
491
	 * Retrieve a set of objects by their IDs.
492
	 *
493
	 * @access public
494
	 *
495
	 * @param string $object_type Object type.
496
	 * @param array  $ids         Object IDs.
497
	 * @return array Array of objects.
498
	 */
499
	public function get_objects_by_id( $object_type, $ids ) {
500
		if ( empty( $ids ) || empty( $object_type ) ) {
501
			return array();
502
		}
503
504
		$objects = array();
505
		foreach ( (array) $ids as $id ) {
506
			$object = $this->get_object_by_id( $object_type, $id );
507
508
			// Only add object if we have the object.
509
			if ( $object ) {
510
				$objects[ $id ] = $object;
511
			}
512
		}
513
514
		return $objects;
515
	}
516
517
	/**
518
	 * Gets a list of minimum and maximum object ids for each batch based on the given batch size.
519
	 *
520
	 * @access public
521
	 *
522
	 * @param int         $batch_size The batch size for objects.
523
	 * @param string|bool $where_sql  The sql where clause minus 'WHERE', or false if no where clause is needed.
524
	 *
525
	 * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
526
	 */
527
	public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
528
		global $wpdb;
529
530
		if ( ! $this->table_name() ) {
531
			return false;
532
		}
533
534
		$results      = array();
535
		$table        = $wpdb->{$this->table_name()};
536
		$current_max  = 0;
537
		$current_min  = 1;
538
		$id_field     = $this->id_field();
539
		$replicastore = new Replicastore();
540
541
		$total = $replicastore->get_min_max_object_id(
542
			$id_field,
543
			$table,
544
			$where_sql,
0 ignored issues
show
Bug introduced by
It seems like $where_sql defined by parameter $where_sql on line 527 can also be of type boolean; however, Automattic\Jetpack\Sync\...get_min_max_object_id() does only seem to accept string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
545
			false
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
546
		);
547
548
		while ( $total->max > $current_max ) {
549
			$where  = $where_sql ?
550
				$where_sql . " AND $id_field > $current_max" :
551
				"$id_field > $current_max";
552
			$result = $replicastore->get_min_max_object_id(
553
				$id_field,
554
				$table,
555
				$where,
556
				$batch_size
557
			);
558
			if ( empty( $result->min ) && empty( $result->max ) ) {
559
				// Our query produced no min and max. We can assume the min from the previous query,
560
				// and the total max we found in the initial query.
561
				$current_max = (int) $total->max;
562
				$result      = (object) array(
563
					'min' => $current_min,
564
					'max' => $current_max,
565
				);
566
			} else {
567
				$current_min = (int) $result->min;
568
				$current_max = (int) $result->max;
569
			}
570
			$results[] = $result;
571
		}
572
573
		return $results;
574
	}
575
576
	/**
577
	 * Return Total number of objects.
578
	 *
579
	 * @param array $config Full Sync config.
580
	 *
581
	 * @return int total
582
	 */
583
	public function total( $config ) {
584
		global $wpdb;
585
		$table = $wpdb->{$this->table_name()};
586
		$where = $this->get_where_sql( $config );
587
588
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
589
		return $wpdb->get_var( "SELECT COUNT(*) FROM $table WHERE $where" );
590
	}
591
592
	/**
593
	 * Retrieve the WHERE SQL clause based on the module config.
594
	 *
595
	 * @access public
596
	 *
597
	 * @param array $config Full sync configuration for this sync module.
598
	 * @return string WHERE SQL clause, or `null` if no comments are specified in the module config.
599
	 */
600
	public function get_where_sql( $config ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
601
		return '1=1';
602
	}
603
604
}
605