Completed
Push — add/sync-rest-2 ( 59fd01...a434af )
by
unknown
66:44 queued 57:55
created

Jetpack_Sync_Utils::get_item_value()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 8
Ratio 57.14 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 8
loc 14
rs 8.8571
c 2
b 1
f 0
cc 5
eloc 6
nc 4
nop 1
1
<?php
2
3
/**
4
 * A buffer of items from the queue that can be checked out
5
 */
6
class Jetpack_Sync_Queue_Buffer {
7
	public $id;
8
	public $items_with_ids;
9
10
	public function __construct( $items_with_ids ) {
11
		$this->id             = uniqid();
12
		$this->items_with_ids = $items_with_ids;
13
	}
14
15
	public function get_items() {
16
		return Jetpack_Sync_Utils::get_item_values( $this->items_with_ids );
17
	}
18
19
	public function get_item_ids() {
20
		return Jetpack_Sync_Utils::get_item_ids( $this->items_with_ids );
21
	}
22
}
23
24
/**
25
 * A persistent queue that can be flushed in increments of N items,
26
 * and which blocks reads until checked-out buffers are checked in or
27
 * closed. This uses raw SQL for two reasons: speed, and not triggering
28
 * tons of added_option callbacks.
29
 */
30
class Jetpack_Sync_Queue {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
31
	public $id;
32
	private $checkout_size;
33
	private $row_iterator;
34
35
	function __construct( $id, $checkout_size = 10 ) {
36
		$this->id            = str_replace( '-', '_', $id ); // necessary to ensure we don't have ID collisions in the SQL
37
		$this->checkout_size = $checkout_size;
38
		$this->row_iterator  = 0;
39
	}
40
41
	function add( $item ) {
42
		global $wpdb;
43
		$added = false;
44
		// this basically tries to add the option until enough time has elapsed that
45
		// it has a unique (microtime-based) option key
46
		while ( ! $added ) {
47
			$rows_added = $wpdb->query( $wpdb->prepare(
48
				"INSERT INTO $wpdb->options (option_name, option_value) VALUES (%s, %s)",
49
				$this->get_next_data_row_option_name(),
50
				serialize( $item )
51
			) );
52
			$added      = ( $rows_added !== 0 );
53
		}
54
	}
55
56
	// Attempts to insert all the items in a single SQL query. May be subject to query size limits!
57
	function add_all( $items ) {
58
		global $wpdb;
59
		$base_option_name = $this->get_next_data_row_option_name();
60
61
		$query = "INSERT INTO $wpdb->options (option_name, option_value) VALUES ";
62
63
		$rows = array();
64
65
		for ( $i = 0; $i < count( $items ); $i += 1 ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
66
			$option_name  = esc_sql( $base_option_name . '-' . $i );
67
			$option_value = esc_sql( serialize( $items[ $i ] ) );
68
			$rows[]       = "('$option_name', '$option_value')";
69
		}
70
71
		$rows_added = $wpdb->query( $query . join( ',', $rows ) );
72
73
		if ( $rows_added !== count( $items ) ) {
74
			return new WP_Error( 'row_count_mismatch', "The number of rows inserted didn't match the size of the input array" );
75
		}
76
	}
77
78
	// Peek at the front-most item on the queue without checking it out
79
	function peek( $count = 1 ) {
80
		$items = $this->fetch_items( $count );
81
		if ( $items ) {
82
			return Jetpack_Sync_Utils::get_item_values( $items );
83
		}
84
85
		return array();
86
	}
87
88
	// lag is the difference in time between the age of the oldest item and the current time
89
	function lag() {
90
		global $wpdb;
91
92
		$last_item_name = $wpdb->get_var( $wpdb->prepare( 
93
			"SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT 1",
94
			"jpsq_{$this->id}-%"
95
		) );
96
97
		if ( ! $last_item_name ) {
98
			return null;
99
		}
100
101
		// break apart the item name to get the timestamp
102
		$matches = null;
103
		if ( preg_match( '/^jpsq_'.$this->id.'-(\d+\.\d+)-/', $last_item_name, $matches ) ) {
104
			return microtime(true) - floatval($matches[1]);	
105
		} else {
106
			return null;
107
		}		
108
	}
109
110
	function reset() {
111
		global $wpdb;
112
		$this->delete_checkout_id();
113
		$wpdb->query( $wpdb->prepare(
114
			"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%"
115
		) );
116
	}
117
118
	function size() {
119
		global $wpdb;
120
121
		return $wpdb->get_var( $wpdb->prepare(
122
			"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%"
123
		) );
124
	}
125
126
	// we use this peculiar implementation because it's much faster than count(*)
127
	function has_any_items() {
128
		global $wpdb;
129
		$value = $wpdb->get_var( $wpdb->prepare(
130
			"SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jpsq_{$this->id}-%"
131
		) );
132
133
		return ( $value === "1" );
134
	}
135
136
	function checkout() {
137
		if ( $this->get_checkout_id() ) {
138
			return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' );
139
		}
140
141
		$items = $this->fetch_items( $this->checkout_size );
142
143
		if ( count( $items ) === 0 ) {
144
			return false;
145
		}
146
147
		$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $this->checkout_size ) );
148
149
		$result = $this->set_checkout_id( $buffer->id );
150
151
		if ( ! $result || is_wp_error( $result ) ) {
152
			error_log( "badness setting checkout ID (this should not happen)" );
153
154
			return $result;
155
		}
156
157
		return $buffer;
158
	}
159
160
	function checkin( $buffer ) {
161
		$is_valid = $this->validate_checkout( $buffer );
162
163
		if ( is_wp_error( $is_valid ) ) {
164
			return $is_valid;
165
		}
166
167
		$this->delete_checkout_id();
168
169
		return true;
170
	}
171
172
	function close( $buffer ) {
173
		$is_valid = $this->validate_checkout( $buffer );
174
175
		if ( is_wp_error( $is_valid ) ) {
176
			return $is_valid;
177
		}
178
179
		$this->delete_checkout_id();
180
181
		global $wpdb;
182
183
		// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause
184
		$ids_to_remove = $buffer->get_item_ids();
185
		if ( count( $ids_to_remove ) > 0 ) {
186
			$sql   = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')';
187
			$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) );
188
			$wpdb->query( $query );
189
		}
190
191
		return true;
192
	}
193
194
	function flush_all() {
195
		$items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() );
196
		$this->reset();
197
198
		return $items;
199
	}
200
201
	function get_all() {
202
		return $this->fetch_items();
203
	}
204
205
	function set_checkout_size( $new_size ) {
206
		$this->checkout_size = $new_size;
207
	}
208
209
	private function get_checkout_id() {
210
		return get_option( $this->get_checkout_option_name(), false );
211
	}
212
213
	private function set_checkout_id( $checkout_id ) {
214
		$added = add_option( $this->get_checkout_option_name(), $checkout_id, null, true ); // this one we should autoload
215
		if ( ! $added ) {
216
			return new WP_Error( 'buffer_mismatch', 'Another buffer is already checked out: ' . $this->get_checkout_id() );
217
		} else {
218
			return true;
219
		}
220
	}
221
222
	private function delete_checkout_id() {
223
		delete_option( $this->get_checkout_option_name() );
224
	}
225
226
	private function get_checkout_option_name() {
227
		return "jpsq_{$this->id}_checkout";
228
	}
229
230
	private function get_next_data_row_option_name() {
231
		// this option is specifically chosen to, as much as possible, preserve time order
232
		// and minimise the possibility of collisions between multiple processes working 
233
		// at the same time
234
		// TODO: confirm we only need to support PHP 5.05+ (otherwise we'll need to emulate microtime as float, and avoid PHP_INT_MAX)
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
235
		// @see: http://php.net/manual/en/function.microtime.php
236
		$timestamp = sprintf( '%.6f', microtime( true ) );
237
238
		// row iterator is used to avoid collisions where we're writing data waaay fast in a single process
239
		if ( $this->row_iterator === PHP_INT_MAX ) {
240
			$this->row_iterator = 0;
241
		} else {
242
			$this->row_iterator += 1;
243
		}
244
245
		return 'jpsq_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator;
246
	}
247
248
	private function fetch_items( $limit = null ) {
249
		global $wpdb;
250
251
		if ( $limit ) {
252
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC LIMIT %d", "jpsq_{$this->id}-%", $limit );
253
		} else {
254
			$query_sql = $wpdb->prepare( "SELECT option_name AS id, option_value AS value FROM $wpdb->options WHERE option_name LIKE %s ORDER BY option_name ASC", "jpsq_{$this->id}-%" );
255
		}
256
257
		$items = $wpdb->get_results( $query_sql );
258
		foreach ( $items as $item ) {
259
			$item->value = maybe_unserialize( $item->value );
260
		}
261
262
		return $items;
263
	}
264
265
	private function validate_checkout( $buffer ) {
266
		if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) {
267
			return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' );
268
		}
269
270
		$checkout_id = $this->get_checkout_id();
271
272
		if ( ! $checkout_id ) {
273
			return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' );
274
		}
275
276
		if ( $checkout_id != $buffer->id ) {
277
			return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' );
278
		}
279
280
		return true;
281
	}
282
}
283
284
class Jetpack_Sync_Utils {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
285
286
	static function get_item_values( $items ) {
287
		return array_map( array( __CLASS__, 'get_item_value' ), $items );
288
	}
289
290
	static function get_item_ids( $items ) {
291
		return array_map( array( __CLASS__, 'get_item_id' ), $items );
292
	}
293
294
	static private function get_item_value( $item ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
295 View Code Duplication
		if ( 'jetpack_full_sync_posts' === $item->value[0] && isset( $item->value[1] ) ) {
296
			// turn post ids to post data
297
			$item->value[1] = apply_filters( 'jetpack_full_sync_posts_data', $item->value[1] ); 
298
		}
299
300 View Code Duplication
		if ( 'jetpack_full_sync_comments' === $item->value[0] && isset( $item->value[1] ) ) {
301
			// turn comment ids to comment data
302
			$item->value[1] = apply_filters( 'jetpack_full_sync_comments_data', $item->value[1] );
303
		}
304
305
		
306
		return $item->value;
307
	}
308
309
	static private function get_item_id( $item ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
310
		return $item->id;
311
	}
312
}
313