Completed
Push — add/eta-to-sync-status ( d17f42 )
by
unknown
09:16 queued 02:41
created

Module::get_sync_speed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Listener;
11
use Automattic\Jetpack\Sync\Replicastore;
12
use Automattic\Jetpack\Sync\Sender;
13
use Automattic\Jetpack\Sync\Settings;
14
use PHP_CodeSniffer\Standards\PSR12\Sniffs\Functions\NullableTypeDeclarationSniff;
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
	 *  An estimate of how many rows per second can be synced during a full sync.
33
	 *
34
	 * @access public
35
	 *
36
	 * @var int|null Null if speed is not important in a full sync.
37
	 */
38
	public $sync_speed = null;
39
40
	/**
41
	 * Sync module name.
42
	 *
43
	 * @access public
44
	 *
45
	 * @return string
46
	 */
47
	abstract public function name();
48
49
	/**
50
	 * The id field in the database.
51
	 *
52
	 * @access public
53
	 *
54
	 * @return string
55
	 */
56
	public function id_field() {
57
		return 'ID';
58
	}
59
60
	/**
61
	 * The table in the database.
62
	 *
63
	 * @access public
64
	 *
65
	 * @return string|bool
66
	 */
67
	public function table_name() {
68
		return false;
69
	}
70
71
	// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
72
73
	/**
74
	 * Retrieve a sync object by its ID.
75
	 *
76
	 * @access public
77
	 *
78
	 * @param string $object_type Type of the sync object.
79
	 * @param int    $id          ID of the sync object.
80
	 * @return mixed Object, or false if the object is invalid.
81
	 */
82
	public function get_object_by_id( $object_type, $id ) {
83
		return false;
84
	}
85
86
	/**
87
	 * Initialize callables action listeners.
88
	 * Override these to set up listeners and set/reset data/defaults.
89
	 *
90
	 * @access public
91
	 *
92
	 * @param callable $callable Action handler callable.
93
	 */
94
	public function init_listeners( $callable ) {
95
	}
96
97
	/**
98
	 * Initialize module action listeners for full sync.
99
	 *
100
	 * @access public
101
	 *
102
	 * @param callable $callable Action handler callable.
103
	 */
104
	public function init_full_sync_listeners( $callable ) {
105
	}
106
107
	/**
108
	 * Initialize the module in the sender.
109
	 *
110
	 * @access public
111
	 */
112
	public function init_before_send() {
113
	}
114
115
	/**
116
	 * Set module defaults.
117
	 *
118
	 * @access public
119
	 */
120
	public function set_defaults() {
121
	}
122
123
	/**
124
	 * Perform module cleanup.
125
	 * Usually triggered when uninstalling the plugin.
126
	 *
127
	 * @access public
128
	 */
129
	public function reset_data() {
130
	}
131
132
	/**
133
	 * Enqueue the module actions for full sync.
134
	 *
135
	 * @access public
136
	 *
137
	 * @param array   $config               Full sync configuration for this sync module.
138
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
139
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
140
	 * @return array  Number of actions enqueued, and next module state.
141
	 */
142
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
143
		// In subclasses, return the number of actions enqueued, and next module state (true == done).
144
		return array( null, true );
145
	}
146
147
	/**
148
	 * Retrieve an estimated number of actions that will be enqueued.
149
	 *
150
	 * @access public
151
	 *
152
	 * @param array $config Full sync configuration for this sync module.
153
	 * @return array Number of items yet to be enqueued.
154
	 */
155
	public function estimate_full_sync_actions( $config ) {
156
		// In subclasses, return the number of items yet to be enqueued.
157
		return null;
158
	}
159
160
	// phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
161
162
	/**
163
	 * Retrieve the actions that will be sent for this module during a full sync.
164
	 *
165
	 * @access public
166
	 *
167
	 * @return array Full sync actions of this module.
168
	 */
169
	public function get_full_sync_actions() {
170
		return array();
171
	}
172
173
	/**
174
	 * Get the number of actions that we care about.
175
	 *
176
	 * @access protected
177
	 *
178
	 * @param array $action_names     Action names we're interested in.
179
	 * @param array $actions_to_count Unfiltered list of actions we want to count.
180
	 * @return array Number of actions that we're interested in.
181
	 */
182
	protected function count_actions( $action_names, $actions_to_count ) {
183
		return count( array_intersect( $action_names, $actions_to_count ) );
184
	}
185
186
	/**
187
	 * Calculate the checksum of one or more values.
188
	 *
189
	 * @access protected
190
	 *
191
	 * @param mixed $values Values to calculate checksum for.
192
	 * @return int The checksum.
193
	 */
194
	protected function get_check_sum( $values ) {
195
		return crc32( wp_json_encode( jetpack_json_wrap( $values ) ) );
196
	}
197
198
	/**
199
	 * Whether a particular checksum in a set of checksums is valid.
200
	 *
201
	 * @access protected
202
	 *
203
	 * @param array  $sums_to_check Array of checksums.
204
	 * @param string $name          Name of the checksum.
205
	 * @param int    $new_sum       Checksum to compare against.
206
	 * @return boolean Whether the checksum is valid.
207
	 */
208
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
209
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
210
			return true;
211
		}
212
213
		return false;
214
	}
215
216
	/**
217
	 * Enqueue all items of a sync type as an action.
218
	 *
219
	 * @access protected
220
	 *
221
	 * @param string  $action_name          Name of the action.
222
	 * @param string  $table_name           Name of the database table.
223
	 * @param string  $id_field             Name of the ID field in the database.
224
	 * @param string  $where_sql            The SQL WHERE clause to filter to the desired items.
225
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue in the same time.
226
	 * @param boolean $state                Whether enqueueing has finished.
227
	 * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished.
228
	 */
229
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) {
230
		global $wpdb;
231
232
		if ( ! $where_sql ) {
233
			$where_sql = '1 = 1';
234
		}
235
236
		$items_per_page        = 1000;
237
		$page                  = 1;
238
		$chunk_count           = 0;
239
		$previous_interval_end = $state ? $state : '~0';
240
		$listener              = Listener::get_instance();
241
242
		// Count down from max_id to min_id so we get newest posts/comments/etc first.
243
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
244
		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}" ) ) {
245
			// Request posts in groups of N for efficiency.
246
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
247
248
			// If we hit our row limit, process and return.
249
			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
250
				$remaining_items_count                      = $max_items_to_enqueue - $chunk_count;
251
				$remaining_items                            = array_slice( $chunked_ids, 0, $remaining_items_count );
252
				$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end );
253
				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end );
254
255
				$last_chunk = end( $remaining_items );
256
				return array( $remaining_items_count + $chunk_count, end( $last_chunk ) );
257
			}
258
			$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end );
259
260
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end );
261
262
			$chunk_count += count( $chunked_ids );
263
			$page++;
264
			// The $ids are ordered in descending order.
265
			$previous_interval_end = end( $ids );
266
		}
267
268
		if ( $wpdb->last_error ) {
269
			// return the values that were passed in so all these chunks get retried.
270
			return array( $max_items_to_enqueue, $state );
271
		}
272
273
		return array( $chunk_count, true );
274
	}
275
276
	/**
277
	 * Given the Module Full Sync Configuration and Status return the next chunk of items to send.
278
	 *
279
	 * @param array $config This module Full Sync configuration.
280
	 * @param array $status This module Full Sync status.
281
	 * @param int   $chunk_size Chunk size.
282
	 *
283
	 * @return array|object|null
284
	 */
285
	public function get_next_chunk( $config, $status, $chunk_size ) {
286
		global $wpdb;
287
		return $wpdb->get_col(
288
			<<<SQL
289
SELECT {$this->id_field()}
290
FROM {$wpdb->{$this->table_name()}}
291
WHERE {$this->get_where_sql( $config )}
292
AND {$this->id_field()} < {$status['last_sent']}
293
ORDER BY {$this->id_field()}
294
DESC LIMIT {$chunk_size}
295
SQL
296
		);
297
	}
298
299
	/**
300
	 * Return the initial last sent object.
301
	 *
302
	 * @return string|array initial status.
303
	 */
304
	public function get_initial_last_sent() {
305
		return '~0';
306
	}
307
308
	/**
309
	 * Immediately send all items of a sync type as an action.
310
	 *
311
	 * @access protected
312
	 *
313
	 * @param string $config Full sync configuration for this module.
314
	 * @param array  $status the current module full sync status.
315
	 * @param float  $send_until timestamp until we want this request to send full sync events.
316
	 *
317
	 * @return array Status, the module full sync status updated.
318
	 */
319
	public function send_full_sync_actions( $config, $status, $send_until ) {
320
		global $wpdb;
321
322
		if ( empty( $status['last_sent'] ) ) {
323
			$status['last_sent'] = $this->get_initial_last_sent();
324
		}
325
326
		$limits = Settings::get_setting( 'full_sync_limits' )[ $this->name() ];
327
328
		$chunks_sent = 0;
329
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
330
		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...
331
			if ( $chunks_sent++ === $limits['max_chunks'] || microtime( true ) >= $send_until ) {
332
				return $status;
333
			}
334
335
			$result = $this->send_action( 'jetpack_full_sync_' . $this->name(), array( $objects, $status['last_sent'] ) );
336
337
			if ( is_wp_error( $result ) || $wpdb->last_error ) {
338
				return $status;
339
			}
340
			// The $ids are ordered in descending order.
341
			$status['last_sent'] = end( $objects );
342
			$status['sent']     += count( $objects );
343
		}
344
345
		if ( ! $wpdb->last_error ) {
346
			$status['finished'] = true;
347
		}
348
349
		return $status;
350
	}
351
352
353
	/**
354
	 * Immediately sends a single item without firing or enqueuing it
355
	 *
356
	 * @param string $action_name The action.
357
	 * @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...
358
	 */
359
	public function send_action( $action_name, $data = null ) {
360
		$sender = Sender::get_instance();
361
		return $sender->send_action( $action_name, $data );
362
	}
363
364
	/**
365
	 * Retrieve chunk IDs with previous interval end.
366
	 *
367
	 * @access protected
368
	 *
369
	 * @param array $chunks                All remaining items.
370
	 * @param int   $previous_interval_end The last item from the previous interval.
371
	 * @return array Chunk IDs with the previous interval end.
372
	 */
373
	protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) {
374
		$chunks_with_ends = array();
375
		foreach ( $chunks as $chunk ) {
376
			$chunks_with_ends[] = array(
377
				'ids'          => $chunk,
378
				'previous_end' => $previous_interval_end,
379
			);
380
			// Chunks are ordered in descending order.
381
			$previous_interval_end = end( $chunk );
382
		}
383
		return $chunks_with_ends;
384
	}
385
386
	/**
387
	 * Get metadata of a particular object type within the designated meta key whitelist.
388
	 *
389
	 * @access protected
390
	 *
391
	 * @todo Refactor to use $wpdb->prepare() on the SQL query.
392
	 *
393
	 * @param array  $ids                Object IDs.
394
	 * @param string $meta_type          Meta type.
395
	 * @param array  $meta_key_whitelist Meta key whitelist.
396
	 * @return array Unserialized meta values.
397
	 */
398
	protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) {
399
		global $wpdb;
400
		$table = _get_meta_table( $meta_type );
401
		$id    = $meta_type . '_id';
402
		if ( ! $table ) {
403
			return array();
404
		}
405
406
		$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'";
407
408
		return array_map(
409
			array( $this, 'unserialize_meta' ),
410
			$wpdb->get_results(
411
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
412
				"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )' .
413
				" AND meta_key IN ( $private_meta_whitelist_sql ) ",
414
				// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
415
				OBJECT
416
			)
417
		);
418
	}
419
420
	/**
421
	 * Initialize listeners for the particular meta type.
422
	 *
423
	 * @access public
424
	 *
425
	 * @param string   $meta_type Meta type.
426
	 * @param callable $callable  Action handler callable.
427
	 */
428
	public function init_listeners_for_meta_type( $meta_type, $callable ) {
429
		add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
430
		add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
431
		add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
432
	}
433
434
	/**
435
	 * Initialize meta whitelist handler for the particular meta type.
436
	 *
437
	 * @access public
438
	 *
439
	 * @param string   $meta_type         Meta type.
440
	 * @param callable $whitelist_handler Action handler callable.
441
	 */
442
	public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) {
443
		add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
444
		add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
445
		add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
446
	}
447
448
	/**
449
	 * Retrieve the term relationships for the specified object IDs.
450
	 *
451
	 * @access protected
452
	 *
453
	 * @todo This feels too specific to be in the abstract sync Module class. Move it?
454
	 *
455
	 * @param array $ids Object IDs.
456
	 * @return array Term relationships - object ID and term taxonomy ID pairs.
457
	 */
458
	protected function get_term_relationships( $ids ) {
459
		global $wpdb;
460
461
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
462
		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 );
463
	}
464
465
	/**
466
	 * Unserialize the value of a meta object, if necessary.
467
	 *
468
	 * @access public
469
	 *
470
	 * @param object $meta Meta object.
471
	 * @return object Meta object with possibly unserialized value.
472
	 */
473
	public function unserialize_meta( $meta ) {
474
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
475
		return $meta;
476
	}
477
478
	/**
479
	 * Retrieve a set of objects by their IDs.
480
	 *
481
	 * @access public
482
	 *
483
	 * @param string $object_type Object type.
484
	 * @param array  $ids         Object IDs.
485
	 * @return array Array of objects.
486
	 */
487
	public function get_objects_by_id( $object_type, $ids ) {
488
		if ( empty( $ids ) || empty( $object_type ) ) {
489
			return array();
490
		}
491
492
		$objects = array();
493
		foreach ( (array) $ids as $id ) {
494
			$object = $this->get_object_by_id( $object_type, $id );
495
496
			// Only add object if we have the object.
497
			if ( $object ) {
498
				$objects[ $id ] = $object;
499
			}
500
		}
501
502
		return $objects;
503
	}
504
505
	/**
506
	 * Gets a list of minimum and maximum object ids for each batch based on the given batch size.
507
	 *
508
	 * @access public
509
	 *
510
	 * @param int         $batch_size The batch size for objects.
511
	 * @param string|bool $where_sql  The sql where clause minus 'WHERE', or false if no where clause is needed.
512
	 *
513
	 * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
514
	 */
515
	public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
516
		global $wpdb;
517
518
		if ( ! $this->table_name() ) {
519
			return false;
520
		}
521
522
		$results      = array();
523
		$table        = $wpdb->{$this->table_name()};
524
		$current_max  = 0;
525
		$current_min  = 1;
526
		$id_field     = $this->id_field();
527
		$replicastore = new Replicastore();
528
529
		$total = $replicastore->get_min_max_object_id(
530
			$id_field,
531
			$table,
532
			$where_sql,
0 ignored issues
show
Bug introduced by
It seems like $where_sql defined by parameter $where_sql on line 515 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...
533
			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...
534
		);
535
536
		while ( $total->max > $current_max ) {
537
			$where  = $where_sql ?
538
				$where_sql . " AND $id_field > $current_max" :
539
				"$id_field > $current_max";
540
			$result = $replicastore->get_min_max_object_id(
541
				$id_field,
542
				$table,
543
				$where,
544
				$batch_size
545
			);
546
			if ( empty( $result->min ) && empty( $result->max ) ) {
547
				// Our query produced no min and max. We can assume the min from the previous query,
548
				// and the total max we found in the initial query.
549
				$current_max = (int) $total->max;
550
				$result      = (object) array(
551
					'min' => $current_min,
552
					'max' => $current_max,
553
				);
554
			} else {
555
				$current_min = (int) $result->min;
556
				$current_max = (int) $result->max;
557
			}
558
			$results[] = $result;
559
		}
560
561
		return $results;
562
	}
563
564
	/**
565
	 * Return Total number of objects.
566
	 *
567
	 * @param array $config Full Sync config.
568
	 *
569
	 * @return int total
570
	 */
571
	public function total( $config ) {
572
		global $wpdb;
573
		$table = $wpdb->{$this->table_name()};
574
		$where = $this->get_where_sql( $config );
575
576
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
577
		return $wpdb->get_var( "SELECT COUNT(*) FROM $table WHERE $where" );
578
	}
579
580
	/**
581
	 * Retrieve the WHERE SQL clause based on the module config.
582
	 *
583
	 * @access public
584
	 *
585
	 * @param array $config Full sync configuration for this sync module.
586
	 * @return string WHERE SQL clause, or `null` if no comments are specified in the module config.
587
	 */
588
	public function get_where_sql( $config ) {
589
		return '1=1';
590
	}
591
592
	/**
593
	 * Gets the sync speed of a module.
594
	 *
595
	 * @access public
596
	 *
597
	 * @return int|null
598
	 */
599
	public function get_sync_speed() {
600
		return $this->sync_speed;
601
	}
602
}
603