Completed
Push — add/min-max-id-endpoints ( d1f121...403842 )
by Marin
06:21
created

Module::get_check_sum()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
nop 1
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\Listener;
11
use Automattic\Jetpack\Sync\Replicastore;
12
13
/**
14
 * Basic methods implemented by Jetpack Sync extensions.
15
 *
16
 * @abstract
17
 */
18
abstract class Module {
19
	/**
20
	 * Number of items per chunk when grouping objects for performance reasons.
21
	 *
22
	 * @access public
23
	 *
24
	 * @var int
25
	 */
26
	const ARRAY_CHUNK_SIZE = 10;
27
28
	/**
29
	 * Sync module name.
30
	 *
31
	 * @access public
32
	 *
33
	 * @return string
34
	 */
35
	abstract public function name();
36
37
	/**
38
	 * The id field in the database.
39
	 *
40
	 * @access public
41
	 *
42
	 * @return string
43
	 */
44
	public function id_field() {
45
		return 'ID';
46
	}
47
48
	/**
49
	 * The table in the database.
50
	 *
51
	 * @access public
52
	 *
53
	 * @return string|bool
54
	 */
55
	public function table_name() {
56
		return false;
57
	}
58
59
	// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
60
61
	/**
62
	 * Retrieve a sync object by its ID.
63
	 *
64
	 * @access public
65
	 *
66
	 * @param string $object_type Type of the sync object.
67
	 * @param int    $id          ID of the sync object.
68
	 * @return mixed Object, or false if the object is invalid.
69
	 */
70
	public function get_object_by_id( $object_type, $id ) {
71
		return false;
72
	}
73
74
	/**
75
	 * Initialize callables action listeners.
76
	 * Override these to set up listeners and set/reset data/defaults.
77
	 *
78
	 * @access public
79
	 *
80
	 * @param callable $callable Action handler callable.
81
	 */
82
	public function init_listeners( $callable ) {
83
	}
84
85
	/**
86
	 * Initialize module action listeners for full sync.
87
	 *
88
	 * @access public
89
	 *
90
	 * @param callable $callable Action handler callable.
91
	 */
92
	public function init_full_sync_listeners( $callable ) {
93
	}
94
95
	/**
96
	 * Initialize the module in the sender.
97
	 *
98
	 * @access public
99
	 */
100
	public function init_before_send() {
101
	}
102
103
	/**
104
	 * Set module defaults.
105
	 *
106
	 * @access public
107
	 */
108
	public function set_defaults() {
109
	}
110
111
	/**
112
	 * Perform module cleanup.
113
	 * Usually triggered when uninstalling the plugin.
114
	 *
115
	 * @access public
116
	 */
117
	public function reset_data() {
118
	}
119
120
	/**
121
	 * Enqueue the module actions for full sync.
122
	 *
123
	 * @access public
124
	 *
125
	 * @param array   $config               Full sync configuration for this sync module.
126
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue.
127
	 * @param boolean $state                True if full sync has finished enqueueing this module, false otherwise.
128
	 * @return array Number of actions enqueued, and next module state.
129
	 */
130
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
131
		// In subclasses, return the number of actions enqueued, and next module state (true == done).
132
		return array( null, true );
133
	}
134
135
	/**
136
	 * Retrieve an estimated number of actions that will be enqueued.
137
	 *
138
	 * @access public
139
	 *
140
	 * @param array $config Full sync configuration for this sync module.
141
	 * @return array Number of items yet to be enqueued.
142
	 */
143
	public function estimate_full_sync_actions( $config ) {
144
		// In subclasses, return the number of items yet to be enqueued.
145
		return null;
146
	}
147
148
	// phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
149
150
	/**
151
	 * Retrieve the actions that will be sent for this module during a full sync.
152
	 *
153
	 * @access public
154
	 *
155
	 * @return array Full sync actions of this module.
156
	 */
157
	public function get_full_sync_actions() {
158
		return array();
159
	}
160
161
	/**
162
	 * Get the number of actions that we care about.
163
	 *
164
	 * @access protected
165
	 *
166
	 * @param array $action_names     Action names we're interested in.
167
	 * @param array $actions_to_count Unfiltered list of actions we want to count.
168
	 * @return array Number of actions that we're interested in.
169
	 */
170
	protected function count_actions( $action_names, $actions_to_count ) {
171
		return count( array_intersect( $action_names, $actions_to_count ) );
172
	}
173
174
	/**
175
	 * Calculate the checksum of one or more values.
176
	 *
177
	 * @access protected
178
	 *
179
	 * @param mixed $values Values to calculate checksum for.
180
	 * @return int The checksum.
181
	 */
182
	protected function get_check_sum( $values ) {
183
		return crc32( wp_json_encode( jetpack_json_wrap( $values ) ) );
184
	}
185
186
	/**
187
	 * Whether a particular checksum in a set of checksums is valid.
188
	 *
189
	 * @access protected
190
	 *
191
	 * @param array  $sums_to_check Array of checksums.
192
	 * @param string $name          Name of the checksum.
193
	 * @param int    $new_sum       Checksum to compare against.
194
	 * @return boolean Whether the checksum is valid.
195
	 */
196
	protected function still_valid_checksum( $sums_to_check, $name, $new_sum ) {
197
		if ( isset( $sums_to_check[ $name ] ) && $sums_to_check[ $name ] === $new_sum ) {
198
			return true;
199
		}
200
201
		return false;
202
	}
203
204
	/**
205
	 * Enqueue all items of a sync type as an action.
206
	 *
207
	 * @access protected
208
	 *
209
	 * @param string  $action_name          Name of the action.
210
	 * @param string  $table_name           Name of the database table.
211
	 * @param string  $id_field             Name of the ID field in the database.
212
	 * @param string  $where_sql            The SQL WHERE clause to filter to the desired items.
213
	 * @param int     $max_items_to_enqueue Maximum number of items to enqueue in the same time.
214
	 * @param boolean $state                Whether enqueueing has finished.
215
	 * @return array Array, containing the number of chunks and TRUE, indicating enqueueing has finished.
216
	 */
217
	protected function enqueue_all_ids_as_action( $action_name, $table_name, $id_field, $where_sql, $max_items_to_enqueue, $state ) {
218
		global $wpdb;
219
220
		if ( ! $where_sql ) {
221
			$where_sql = '1 = 1';
222
		}
223
224
		$items_per_page        = 1000;
225
		$page                  = 1;
226
		$chunk_count           = 0;
227
		$previous_interval_end = $state ? $state : '~0';
228
		$listener              = Listener::get_instance();
229
230
		// Count down from max_id to min_id so we get newest posts/comments/etc first.
231
		// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
232
		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}" ) ) {
233
			// Request posts in groups of N for efficiency.
234
			$chunked_ids = array_chunk( $ids, self::ARRAY_CHUNK_SIZE );
235
236
			// If we hit our row limit, process and return.
237 View Code Duplication
			if ( $chunk_count + count( $chunked_ids ) >= $max_items_to_enqueue ) {
238
				$remaining_items_count                      = $max_items_to_enqueue - $chunk_count;
239
				$remaining_items                            = array_slice( $chunked_ids, 0, $remaining_items_count );
240
				$remaining_items_with_previous_interval_end = $this->get_chunks_with_preceding_end( $remaining_items, $previous_interval_end );
241
				$listener->bulk_enqueue_full_sync_actions( $action_name, $remaining_items_with_previous_interval_end );
242
243
				$last_chunk = end( $remaining_items );
244
				return array( $remaining_items_count + $chunk_count, end( $last_chunk ) );
245
			}
246
			$chunked_ids_with_previous_end = $this->get_chunks_with_preceding_end( $chunked_ids, $previous_interval_end );
247
248
			$listener->bulk_enqueue_full_sync_actions( $action_name, $chunked_ids_with_previous_end );
249
250
			$chunk_count += count( $chunked_ids );
251
			$page++;
252
			// The $ids are ordered in descending order.
253
			$previous_interval_end = end( $ids );
254
		}
255
256
		return array( $chunk_count, true );
257
	}
258
259
	/**
260
	 * Retrieve chunk IDs with previous interval end.
261
	 *
262
	 * @access protected
263
	 *
264
	 * @param array $chunks                All remaining items.
265
	 * @param int   $previous_interval_end The last item from the previous interval.
266
	 * @return array Chunk IDs with the previous interval end.
267
	 */
268
	protected function get_chunks_with_preceding_end( $chunks, $previous_interval_end ) {
269
		$chunks_with_ends = array();
270
		foreach ( $chunks as $chunk ) {
271
			$chunks_with_ends[] = array(
272
				'ids'          => $chunk,
273
				'previous_end' => $previous_interval_end,
274
			);
275
			// Chunks are ordered in descending order.
276
			$previous_interval_end = end( $chunk );
277
		}
278
		return $chunks_with_ends;
279
	}
280
281
	/**
282
	 * Get metadata of a particular object type within the designated meta key whitelist.
283
	 *
284
	 * @access protected
285
	 *
286
	 * @todo Refactor to use $wpdb->prepare() on the SQL query.
287
	 *
288
	 * @param array  $ids                Object IDs.
289
	 * @param string $meta_type          Meta type.
290
	 * @param array  $meta_key_whitelist Meta key whitelist.
291
	 * @return array Unserialized meta values.
292
	 */
293
	protected function get_metadata( $ids, $meta_type, $meta_key_whitelist ) {
294
		global $wpdb;
295
		$table = _get_meta_table( $meta_type );
296
		$id    = $meta_type . '_id';
297
		if ( ! $table ) {
298
			return array();
299
		}
300
301
		$private_meta_whitelist_sql = "'" . implode( "','", array_map( 'esc_sql', $meta_key_whitelist ) ) . "'";
302
303
		return array_map(
304
			array( $this, 'unserialize_meta' ),
305
			$wpdb->get_results(
306
				// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
307
				"SELECT $id, meta_key, meta_value, meta_id FROM $table WHERE $id IN ( " . implode( ',', wp_parse_id_list( $ids ) ) . ' )' .
308
				" AND meta_key IN ( $private_meta_whitelist_sql ) ",
309
				// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
310
				OBJECT
311
			)
312
		);
313
	}
314
315
	/**
316
	 * Initialize listeners for the particular meta type.
317
	 *
318
	 * @access public
319
	 *
320
	 * @param string   $meta_type Meta type.
321
	 * @param callable $callable  Action handler callable.
322
	 */
323
	public function init_listeners_for_meta_type( $meta_type, $callable ) {
324
		add_action( "added_{$meta_type}_meta", $callable, 10, 4 );
325
		add_action( "updated_{$meta_type}_meta", $callable, 10, 4 );
326
		add_action( "deleted_{$meta_type}_meta", $callable, 10, 4 );
327
	}
328
329
	/**
330
	 * Initialize meta whitelist handler for the particular meta type.
331
	 *
332
	 * @access public
333
	 *
334
	 * @param string   $meta_type         Meta type.
335
	 * @param callable $whitelist_handler Action handler callable.
336
	 */
337
	public function init_meta_whitelist_handler( $meta_type, $whitelist_handler ) {
338
		add_filter( "jetpack_sync_before_enqueue_added_{$meta_type}_meta", $whitelist_handler );
339
		add_filter( "jetpack_sync_before_enqueue_updated_{$meta_type}_meta", $whitelist_handler );
340
		add_filter( "jetpack_sync_before_enqueue_deleted_{$meta_type}_meta", $whitelist_handler );
341
	}
342
343
	/**
344
	 * Retrieve the term relationships for the specified object IDs.
345
	 *
346
	 * @access protected
347
	 *
348
	 * @todo This feels too specific to be in the abstract sync Module class. Move it?
349
	 *
350
	 * @param array $ids Object IDs.
351
	 * @return array Term relationships - object ID and term taxonomy ID pairs.
352
	 */
353
	protected function get_term_relationships( $ids ) {
354
		global $wpdb;
355
356
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
357
		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 );
358
	}
359
360
	/**
361
	 * Unserialize the value of a meta object, if necessary.
362
	 *
363
	 * @access public
364
	 *
365
	 * @param object $meta Meta object.
366
	 * @return object Meta object with possibly unserialized value.
367
	 */
368
	public function unserialize_meta( $meta ) {
369
		$meta->meta_value = maybe_unserialize( $meta->meta_value );
370
		return $meta;
371
	}
372
373
	/**
374
	 * Retrieve a set of objects by their IDs.
375
	 *
376
	 * @access public
377
	 *
378
	 * @param string $object_type Object type.
379
	 * @param array  $ids         Object IDs.
380
	 * @return array Array of objects.
381
	 */
382
	public function get_objects_by_id( $object_type, $ids ) {
383
		if ( empty( $ids ) || empty( $object_type ) ) {
384
			return array();
385
		}
386
387
		$objects = array();
388
		foreach ( (array) $ids as $id ) {
389
			$object = $this->get_object_by_id( $object_type, $id );
390
391
			// Only add object if we have the object.
392
			if ( $object ) {
393
				$objects[ $id ] = $object;
394
			}
395
		}
396
397
		return $objects;
398
	}
399
400
	/**
401
	 * Gets a list of minimum and maximum object ids for each batch based on the given batch size.
402
	 *
403
	 * @access public
404
	 *
405
	 * @param int         $batch_size The batch size for objects.
406
	 * @param string|bool $where_sql  The sql where clause minus 'WHERE', or false if no where clause is needed.
407
	 *
408
	 * @return array|bool An array of min and max ids for each batch. FALSE if no table can be found.
409
	 */
410
	public function get_min_max_object_ids_for_batches( $batch_size, $where_sql = false ) {
411
		global $wpdb;
412
413
		if ( ! $this->table_name() ) {
414
			return false;
415
		}
416
417
		$results      = array();
418
		$table        = $wpdb->{$this->table_name()};
419
		$current_max  = 0;
420
		$current_min  = 1;
421
		$id_field     = $this->id_field();
422
		$replicastore = new Replicastore();
423
424
		$total = $replicastore->get_min_max_object_id(
425
			$id_field,
426
			$table,
427
			$where_sql,
0 ignored issues
show
Bug introduced by
It seems like $where_sql defined by parameter $where_sql on line 410 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...
428
			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...
429
		);
430
431
		while ( $total->max > $current_max ) {
432
			$where  = $where_sql ?
433
				$where_sql . " AND $id_field > $current_max" :
434
				"$id_field > $current_max";
435
			$result = $replicastore->get_min_max_object_id(
436
				$id_field,
437
				$table,
438
				$where,
439
				$batch_size
440
			);
441
			if ( empty( $result->min ) && empty( $result->max ) ) {
442
				// Our query produced no min and max. We can assume the min from the previous query,
443
				// and the total max we found in the initial query.
444
				$current_max = (int) $total->max;
445
				$result      = (object) array(
446
					'min' => $current_min,
447
					'max' => $current_max,
448
				);
449
			} else {
450
				$current_min = (int) $result->min;
451
				$current_max = (int) $result->max;
452
			}
453
			$results[] = $result;
454
		}
455
456
		return $results;
457
	}
458
}
459