Completed
Push — try/sync-package ( 228b13 )
by Marin
07:37
created

Module_Full_Sync::start()   C

Complexity

Conditions 14
Paths 124

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
nc 124
nop 1
dl 0
loc 83
rs 5.2242
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Automattic\Jetpack\Sync;
4
5
/**
6
 * This class does a full resync of the database by
7
 * enqueuing an outbound action for every single object
8
 * that we care about.
9
 *
10
 * This class, and its related class Module, contain a few non-obvious optimisations that should be explained:
11
 * - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database
12
 * - for each object type, we page through the object IDs and enqueue them by firing some monitored actions
13
 * - we load the full objects for those IDs in chunks of Module::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls)
14
 * - we fire a trigger for the entire array which the Listener then serializes and queues.
15
 */
16
17
class Module_Full_Sync extends Module {
18
	const STATUS_OPTION_PREFIX = 'jetpack_sync_full_';
19
	const FULL_SYNC_TIMEOUT    = 3600;
20
21
	public function name() {
22
		return 'full-sync';
23
	}
24
25
	function init_full_sync_listeners( $callable ) {
26
		// synthetic actions for full sync
27
		add_action( 'jetpack_full_sync_start', $callable, 10, 3 );
28
		add_action( 'jetpack_full_sync_end', $callable, 10, 2 );
29
		add_action( 'jetpack_full_sync_cancelled', $callable );
30
	}
31
32
	function init_before_send() {
33
		// this is triggered after actions have been processed on the server
34
		add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) );
35
	}
36
37
	function start( $module_configs = null ) {
38
		$was_already_running = $this->is_started() && ! $this->is_finished();
39
40
		// remove all evidence of previous full sync items and status
41
		$this->reset_data();
42
43
		if ( $was_already_running ) {
44
			/**
45
			 * Fires when a full sync is cancelled.
46
			 *
47
			 * @since 4.2.0
48
			 */
49
			do_action( 'jetpack_full_sync_cancelled' );
50
		}
51
52
		$this->update_status_option( 'started', time() );
53
		$this->update_status_option( 'params', $module_configs );
54
55
		$enqueue_status   = array();
56
		$full_sync_config = array();
57
		$include_empty = false;
58
		$empty = array();
59
		// default value is full sync
60
		if ( ! is_array( $module_configs ) ) {
61
			$module_configs = array();
62
			$include_empty = true;
63
			foreach ( Modules::get_modules() as $module ) {
64
				$module_configs[ $module->name() ] = true;
65
			}
66
		}
67
68
		// set default configuration, calculate totals, and save configuration if totals > 0
69
		foreach ( Modules::get_modules() as $module ) {
70
			$module_name   = $module->name();
71
			$module_config = isset( $module_configs[ $module_name ] ) ? $module_configs[ $module_name ] : false;
72
73
			if ( ! $module_config ) {
74
				continue;
75
			}
76
77
			if ( 'users' === $module_name && 'initial' === $module_config ) {
78
				$module_config = $module->get_initial_sync_user_config();
79
			}
80
81
			$enqueue_status[ $module_name ] = false;
82
83
			$total_items = $module->estimate_full_sync_actions( $module_config );
84
85
			// if there's information to process, configure this module
86
			if ( ! is_null( $total_items ) && $total_items > 0 ) {
87
				$full_sync_config[ $module_name ] = $module_config;
88
				$enqueue_status[ $module_name ]   = array(
89
					$total_items,   // total
90
					0,              // queued
91
					false,          // current state
92
				);
93
			} else if ( $include_empty && $total_items === 0 ) {
94
				$empty[ $module_name ] = true;
95
			}
96
		}
97
98
		$this->set_config( $full_sync_config );
99
		$this->set_enqueue_status( $enqueue_status );
100
101
		$range = $this->get_content_range( $full_sync_config );
102
		/**
103
		 * Fires when a full sync begins. This action is serialized
104
		 * and sent to the server so that it knows a full sync is coming.
105
		 *
106
		 * @since 4.2.0
107
		 * @since 7.3.0 Added $range arg.
108
		 * @since 7.4.0 Added $empty arg.
109
		 *
110
		 * @param array $full_sync_config Sync configuration for all sync modules.
111
		 * @param array $range            Range of the sync items, containing min and max IDs for some item types.
112
		 * @param array $empty            The modules with no items to sync during a full sync.
113
		 */
114
		do_action( 'jetpack_full_sync_start', $full_sync_config, $range, $empty );
115
116
		$this->continue_enqueuing( $full_sync_config, $enqueue_status );
117
118
		return true;
119
	}
120
121
	function continue_enqueuing( $configs = null, $enqueue_status = null ) {
122
		if ( ! $this->is_started() || $this->get_status_option( 'queue_finished' ) ) {
123
			return;
124
		}
125
126
		// if full sync queue is full, don't enqueue more items
127
		$max_queue_size_full_sync = Settings::get_setting( 'max_queue_size_full_sync' );
128
		$full_sync_queue          = new Queue( 'full_sync' );
129
130
		$available_queue_slots = $max_queue_size_full_sync - $full_sync_queue->size();
131
132
		if ( $available_queue_slots <= 0 ) {
133
			return;
134
		} else {
135
			$remaining_items_to_enqueue = min( Settings::get_setting( 'max_enqueue_full_sync' ), $available_queue_slots );
136
		}
137
138
		if ( ! $configs ) {
139
			$configs = $this->get_config();
140
		}
141
142
		if ( ! $enqueue_status ) {
143
			$enqueue_status = $this->get_enqueue_status();
144
		}
145
146
		foreach ( Modules::get_modules() as $module ) {
147
			$module_name = $module->name();
148
149
			// skip module if not configured for this sync or module is done
150
			if ( ! isset( $configs[ $module_name ] )
151
				|| // no module config
152
					! $configs[ $module_name ]
153
				|| // no enqueue status
154
					! $enqueue_status[ $module_name ]
155
				|| // finished enqueuing this module
156
					true === $enqueue_status[ $module_name ][2] ) {
157
				continue;
158
			}
159
160
			list( $items_enqueued, $next_enqueue_state ) = $module->enqueue_full_sync_actions( $configs[ $module_name ], $remaining_items_to_enqueue, $enqueue_status[ $module_name ][2] );
161
162
			$enqueue_status[ $module_name ][2] = $next_enqueue_state;
163
164
			// if items were processed, subtract them from the limit
165
			if ( ! is_null( $items_enqueued ) && $items_enqueued > 0 ) {
166
				$enqueue_status[ $module_name ][1] += $items_enqueued;
167
				$remaining_items_to_enqueue        -= $items_enqueued;
168
			}
169
170
			// stop processing if we've reached our limit of items to enqueue
171
			if ( 0 >= $remaining_items_to_enqueue ) {
172
				$this->set_enqueue_status( $enqueue_status );
173
				return;
174
			}
175
		}
176
177
		$this->set_enqueue_status( $enqueue_status );
178
179
		// setting autoload to true means that it's faster to check whether we should continue enqueuing
180
		$this->update_status_option( 'queue_finished', time(), true );
181
182
		$range = $this->get_content_range( $configs );
183
184
		/**
185
		 * Fires when a full sync ends. This action is serialized
186
		 * and sent to the server.
187
		 *
188
		 * @since 4.2.0
189
		 * @since 7.3.0 Added $range arg.
190
		 *
191
		 * @param string $checksum Deprecated since 7.3.0 - @see https://github.com/Automattic/jetpack/pull/11945/
192
		 * @param array  $range    Range of the sync items, containing min and max IDs for some item types.
193
		 */
194
		do_action( 'jetpack_full_sync_end', '', $range );
195
	}
196
197
	function get_range( $type ) {
198
		global $wpdb;
199
		if ( ! in_array( $type, array( 'comments', 'posts' ) ) ) {
200
			return array();
201
		}
202
203
		switch ( $type ) {
204
			case 'posts':
205
				$table     = $wpdb->posts;
206
				$id        = 'ID';
207
				$where_sql = Settings::get_blacklisted_post_types_sql();
208
209
				break;
210
			case 'comments':
211
				$table     = $wpdb->comments;
212
				$id        = 'comment_ID';
213
				$where_sql = Settings::get_comments_filter_sql();
214
				break;
215
		}
216
		$results = $wpdb->get_results( "SELECT MAX({$id}) as max, MIN({$id}) as min, COUNT({$id}) as count FROM {$table} WHERE {$where_sql}" );
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $table does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $where_sql does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
217
		if ( isset( $results[0] ) ) {
218
			return $results[0];
219
		}
220
221
		return array();
222
	}
223
224
	private function get_content_range( $config ) {
225
		$range = array();
226
		// Only when we are sending the whole range do we want to send also the range
227 View Code Duplication
		if ( isset( $config['posts'] ) && $config['posts'] === true ) {
228
			$range['posts'] = $this->get_range( 'posts' );
229
		}
230
231 View Code Duplication
		if ( isset( $config['comments'] ) && $config['comments'] === true ) {
232
			$range['comments'] = $this->get_range( 'comments' );
233
		}
234
		return $range;
235
	}
236
237
	function update_sent_progress_action( $actions ) {
238
		// quick way to map to first items with an array of arrays
239
		$actions_with_counts = array_count_values( array_filter( array_map( array( $this, 'get_action_name' ), $actions ) ) );
240
241
		// Total item counts for each action.
242
		$actions_with_total_counts = $this->get_actions_totals( $actions );
243
244
		if ( ! $this->is_started() || $this->is_finished() ) {
245
			return;
246
		}
247
248
		if ( isset( $actions_with_counts['jetpack_full_sync_start'] ) ) {
249
			$this->update_status_option( 'send_started', time() );
250
		}
251
252
		foreach ( Modules::get_modules() as $module ) {
253
			$module_actions     = $module->get_full_sync_actions();
254
			$status_option_name = "{$module->name()}_sent";
255
			$total_option_name  = "{$status_option_name}_total";
256
			$items_sent         = $this->get_status_option( $status_option_name, 0 );
257
			$items_sent_total   = $this->get_status_option( $total_option_name, 0 );
258
259
			foreach ( $module_actions as $module_action ) {
260
				if ( isset( $actions_with_counts[ $module_action ] ) ) {
261
					$items_sent += $actions_with_counts[ $module_action ];
262
				}
263
264
				if ( ! empty( $actions_with_total_counts[ $module_action ] ) ) {
265
					$items_sent_total += $actions_with_total_counts[ $module_action ];
266
				}
267
			}
268
269
			if ( $items_sent > 0 ) {
270
				$this->update_status_option( $status_option_name, $items_sent );
271
			}
272
273
			if ( 0 !== $items_sent_total ) {
274
				$this->update_status_option( $total_option_name, $items_sent_total );
275
			}
276
		}
277
278
		if ( isset( $actions_with_counts['jetpack_full_sync_end'] ) ) {
279
			$this->update_status_option( 'finished', time() );
280
		}
281
	}
282
283
	public function get_action_name( $queue_item ) {
284
		if ( is_array( $queue_item ) && isset( $queue_item[0] ) ) {
285
			return $queue_item[0];
286
		}
287
		return false;
288
	}
289
290
	/**
291
	 * Retrieve the total number of items we're syncing in a particular queue item (action).
292
	 * `$queue_item[1]` is expected to contain chunks of items, and `$queue_item[1][0]`
293
	 * represents the first (and only) chunk of items to sync in that action.
294
	 *
295
	 * @param array $queue_item Item of the sync queue that corresponds to a particular action.
296
	 * @return int Total number of items in the action.
297
	 */
298
	public function get_action_totals( $queue_item ) {
299
		if ( is_array( $queue_item ) && isset( $queue_item[1][0] ) ) {
300
			if ( is_array( $queue_item[1][0] ) ) {
301
				// Let's count the items we sync in this action.
302
				return count( $queue_item[1][0] );
303
			}
304
			// -1 indicates that this action syncs all items by design.
305
			return -1;
306
		}
307
		return 0;
308
	}
309
310
	/**
311
	 * Retrieve the total number of items for a set of actions, grouped by action name.
312
	 *
313
	 * @param array $actions An array of actions.
314
	 * @return array An array, representing the total number of items, grouped per action.
315
	 */
316
	public function get_actions_totals( $actions ) {
317
		$totals = array();
318
319
		foreach ( $actions as $action ) {
320
			$name          = $this->get_action_name( $action );
321
			$action_totals = $this->get_action_totals( $action );
322
			if ( ! isset( $totals[ $name ] ) ) {
323
				$totals[ $name ] = 0;
324
			}
325
			$totals[ $name ] += $action_totals;
326
		}
327
328
		return $totals;
329
	}
330
331
	public function is_started() {
332
		return ! ! $this->get_status_option( 'started' );
333
	}
334
335
	public function is_finished() {
336
		return ! ! $this->get_status_option( 'finished' );
337
	}
338
339
	public function get_status() {
340
		$status = array(
341
			'started'        => $this->get_status_option( 'started' ),
342
			'queue_finished' => $this->get_status_option( 'queue_finished' ),
343
			'send_started'   => $this->get_status_option( 'send_started' ),
344
			'finished'       => $this->get_status_option( 'finished' ),
345
			'sent'           => array(),
346
			'sent_total'     => array(),
347
			'queue'          => array(),
348
			'config'         => $this->get_status_option( 'params' ),
349
			'total'          => array(),
350
		);
351
352
		$enqueue_status = $this->get_enqueue_status();
353
354
		foreach ( Modules::get_modules() as $module ) {
355
			$name = $module->name();
356
357
			if ( ! isset( $enqueue_status[ $name ] ) ) {
358
				continue;
359
			}
360
361
			list( $total, $queued, $state ) = $enqueue_status[ $name ];
0 ignored issues
show
Unused Code introduced by
The assignment to $state is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
362
363
			if ( $total ) {
364
				$status['total'][ $name ] = $total;
365
			}
366
367
			if ( $queued ) {
368
				$status['queue'][ $name ] = $queued;
369
			}
370
371
			if ( $sent = $this->get_status_option( "{$name}_sent" ) ) {
372
				$status['sent'][ $name ] = $sent;
373
			}
374
375
			$sent_total = $this->get_status_option( "{$name}_sent_total" );
376
			if ( $sent_total ) {
377
				$status['sent_total'][ $name ] = $sent_total;
378
			}
379
		}
380
381
		return $status;
382
	}
383
384
	public function clear_status() {
385
		$prefix = self::STATUS_OPTION_PREFIX;
386
		Jetpack_Options::delete_raw_option( "{$prefix}_started" );
387
		Jetpack_Options::delete_raw_option( "{$prefix}_params" );
388
		Jetpack_Options::delete_raw_option( "{$prefix}_queue_finished" );
389
		Jetpack_Options::delete_raw_option( "{$prefix}_send_started" );
390
		Jetpack_Options::delete_raw_option( "{$prefix}_finished" );
391
392
		$this->delete_enqueue_status();
393
394
		foreach ( Modules::get_modules() as $module ) {
395
			Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent" );
396
			Jetpack_Options::delete_raw_option( "{$prefix}_{$module->name()}_sent_total" );
397
		}
398
	}
399
400
	public function reset_data() {
401
		$this->clear_status();
402
		$this->delete_config();
403
		$listener = Listener::get_instance();
404
		$listener->get_full_sync_queue()->reset();
405
	}
406
407
	private function get_status_option( $name, $default = null ) {
408
		$value = Jetpack_Options::get_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $default );
409
410
		return is_numeric( $value ) ? intval( $value ) : $value;
411
	}
412
413
	private function update_status_option( $name, $value, $autoload = false ) {
414
		Jetpack_Options::update_raw_option( self::STATUS_OPTION_PREFIX . "_$name", $value, $autoload );
415
	}
416
417
	private function set_enqueue_status( $new_status ) {
418
		Jetpack_Options::update_raw_option( 'jetpack_sync_full_enqueue_status', $new_status );
419
	}
420
421
	private function delete_enqueue_status() {
422
		return Jetpack_Options::delete_raw_option( 'jetpack_sync_full_enqueue_status' );
423
	}
424
425
	private function get_enqueue_status() {
426
		return Jetpack_Options::get_raw_option( 'jetpack_sync_full_enqueue_status' );
427
	}
428
429
	private function set_config( $config ) {
430
		Jetpack_Options::update_raw_option( 'jetpack_sync_full_config', $config );
431
	}
432
433
	private function delete_config() {
434
		return Jetpack_Options::delete_raw_option( 'jetpack_sync_full_config' );
435
	}
436
437
	private function get_config() {
438
		return Jetpack_Options::get_raw_option( 'jetpack_sync_full_config' );
439
	}
440
441
	private function write_option( $name, $value ) {
442
		// we write our own option updating code to bypass filters/caching/etc on set_option/get_option
443
		global $wpdb;
444
		$serialized_value = maybe_serialize( $value );
445
		// try updating, if no update then insert
446
		// TODO: try to deal with the fact that unchanged values can return updated_num = 0
447
		// below we used "insert ignore" to at least suppress the resulting error
448
		$updated_num = $wpdb->query(
449
			$wpdb->prepare(
450
				"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s",
451
				$serialized_value,
452
				$name
453
			)
454
		);
455
456
		if ( ! $updated_num ) {
457
			$updated_num = $wpdb->query(
458
				$wpdb->prepare(
459
					"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )",
460
					$name,
461
					$serialized_value
462
				)
463
			);
464
		}
465
		return $updated_num;
466
	}
467
468
	private function read_option( $name, $default = null ) {
469
		global $wpdb;
470
		$value = $wpdb->get_var(
471
			$wpdb->prepare(
472
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
473
				$name
474
			)
475
		);
476
		$value = maybe_unserialize( $value );
477
478
		if ( $value === null && $default !== null ) {
479
			return $default;
480
		}
481
482
		return $value;
483
	}
484
}
485