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 { |
|
|
|
|
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 ) { |
|
|
|
|
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 DESC 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
|
|
|
// return 'jpsq_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator; |
103
|
|
|
$matches = null; |
104
|
|
|
preg_match( '/^jpsq_'.$this->id.'-(\d+).(\d+)/', $last_item_name, $matches ); |
105
|
|
|
|
106
|
|
|
return time() - $matches[1]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
function reset() { |
110
|
|
|
global $wpdb; |
111
|
|
|
$this->delete_checkout_id(); |
112
|
|
|
$wpdb->query( $wpdb->prepare( |
113
|
|
|
"DELETE FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
114
|
|
|
) ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function size() { |
118
|
|
|
global $wpdb; |
119
|
|
|
|
120
|
|
|
return $wpdb->get_var( $wpdb->prepare( |
121
|
|
|
"SELECT count(*) FROM $wpdb->options WHERE option_name LIKE %s", "jpsq_{$this->id}-%" |
122
|
|
|
) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// we use this peculiar implementation because it's much faster than count(*) |
126
|
|
|
function has_any_items() { |
127
|
|
|
global $wpdb; |
128
|
|
|
$value = $wpdb->get_var( $wpdb->prepare( |
129
|
|
|
"SELECT exists( SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s )", "jpsq_{$this->id}-%" |
130
|
|
|
) ); |
131
|
|
|
|
132
|
|
|
return ( $value === "1" ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
function checkout() { |
136
|
|
|
if ( $this->get_checkout_id() ) { |
137
|
|
|
return new WP_Error( 'unclosed_buffer', 'There is an unclosed buffer' ); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$items = $this->fetch_items( $this->checkout_size ); |
141
|
|
|
|
142
|
|
|
if ( count( $items ) === 0 ) { |
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$buffer = new Jetpack_Sync_Queue_Buffer( array_slice( $items, 0, $this->checkout_size ) ); |
147
|
|
|
|
148
|
|
|
$result = $this->set_checkout_id( $buffer->id ); |
149
|
|
|
|
150
|
|
|
if ( ! $result || is_wp_error( $result ) ) { |
151
|
|
|
error_log( "badness setting checkout ID (this should not happen)" ); |
152
|
|
|
|
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $buffer; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
function checkin( $buffer ) { |
160
|
|
|
$is_valid = $this->validate_checkout( $buffer ); |
161
|
|
|
|
162
|
|
|
if ( is_wp_error( $is_valid ) ) { |
163
|
|
|
return $is_valid; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$this->delete_checkout_id(); |
167
|
|
|
|
168
|
|
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
function close( $buffer ) { |
172
|
|
|
$is_valid = $this->validate_checkout( $buffer ); |
173
|
|
|
|
174
|
|
|
if ( is_wp_error( $is_valid ) ) { |
175
|
|
|
return $is_valid; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->delete_checkout_id(); |
179
|
|
|
|
180
|
|
|
global $wpdb; |
181
|
|
|
|
182
|
|
|
// all this fanciness is basically so we can prepare a statement with an IN(id1, id2, id3) clause |
183
|
|
|
$ids_to_remove = $buffer->get_item_ids(); |
184
|
|
|
if ( count( $ids_to_remove ) > 0 ) { |
185
|
|
|
$sql = "DELETE FROM $wpdb->options WHERE option_name IN (" . implode( ', ', array_fill( 0, count( $ids_to_remove ), '%s' ) ) . ')'; |
186
|
|
|
$query = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( array( $sql ), $ids_to_remove ) ); |
187
|
|
|
$wpdb->query( $query ); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function flush_all() { |
194
|
|
|
$items = Jetpack_Sync_Utils::get_item_values( $this->fetch_items() ); |
195
|
|
|
$this->reset(); |
196
|
|
|
|
197
|
|
|
return $items; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
function get_all() { |
201
|
|
|
return $this->fetch_items(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
function set_checkout_size( $new_size ) { |
205
|
|
|
$this->checkout_size = $new_size; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
private function get_checkout_id() { |
209
|
|
|
return get_option( $this->get_checkout_option_name(), false ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
private function set_checkout_id( $checkout_id ) { |
213
|
|
|
$added = add_option( $this->get_checkout_option_name(), $checkout_id, null, true ); // this one we should autoload |
214
|
|
|
if ( ! $added ) { |
215
|
|
|
return new WP_Error( 'buffer_mismatch', 'Another buffer is already checked out: ' . $this->get_checkout_id() ); |
216
|
|
|
} else { |
217
|
|
|
return true; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
private function delete_checkout_id() { |
222
|
|
|
delete_option( $this->get_checkout_option_name() ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
private function get_checkout_option_name() { |
226
|
|
|
return "jpsq_{$this->id}-checkout"; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
private function get_next_data_row_option_name() { |
230
|
|
|
// this option is specifically chosen to, as much as possible, preserve time order |
231
|
|
|
// and minimise the possibility of collisions between multiple processes working |
232
|
|
|
// at the same time |
233
|
|
|
// TODO: confirm we only need to support PHP 5.05+ (otherwise we'll need to emulate microtime as float, and avoid PHP_INT_MAX) |
|
|
|
|
234
|
|
|
// @see: http://php.net/manual/en/function.microtime.php |
235
|
|
|
$timestamp = sprintf( '%.6f', microtime( true ) ); |
236
|
|
|
|
237
|
|
|
// row iterator is used to avoid collisions where we're writing data waaay fast in a single process |
238
|
|
|
if ( $this->row_iterator === PHP_INT_MAX ) { |
239
|
|
|
$this->row_iterator = 0; |
240
|
|
|
} else { |
241
|
|
|
$this->row_iterator += 1; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return 'jpsq_' . $this->id . '-' . $timestamp . '-' . getmypid() . '-' . $this->row_iterator; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
private function fetch_items( $limit = null ) { |
248
|
|
|
global $wpdb; |
249
|
|
|
|
250
|
|
|
if ( $limit ) { |
251
|
|
|
$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 ); |
252
|
|
|
} else { |
253
|
|
|
$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}-%" ); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
$items = $wpdb->get_results( $query_sql ); |
257
|
|
|
foreach ( $items as $item ) { |
258
|
|
|
$item->value = maybe_unserialize( $item->value ); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $items; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
private function validate_checkout( $buffer ) { |
265
|
|
|
if ( ! $buffer instanceof Jetpack_Sync_Queue_Buffer ) { |
266
|
|
|
return new WP_Error( 'not_a_buffer', 'You must checkin an instance of Jetpack_Sync_Queue_Buffer' ); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$checkout_id = $this->get_checkout_id(); |
270
|
|
|
|
271
|
|
|
if ( ! $checkout_id ) { |
272
|
|
|
return new WP_Error( 'buffer_not_checked_out', 'There are no checked out buffers' ); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
if ( $checkout_id != $buffer->id ) { |
276
|
|
|
return new WP_Error( 'buffer_mismatch', 'The buffer you checked in was not checked out' ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return true; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
class Jetpack_Sync_Utils { |
|
|
|
|
284
|
|
|
|
285
|
|
|
static function get_item_values( $items ) { |
286
|
|
|
return array_map( array( __CLASS__, 'get_item_value' ), $items ); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
static function get_item_ids( $items ) { |
290
|
|
|
return array_map( array( __CLASS__, 'get_item_id' ), $items ); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
static private function get_item_value( $item ) { |
|
|
|
|
294
|
|
|
return $item->value; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
static private function get_item_id( $item ) { |
|
|
|
|
298
|
|
|
return $item->id; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
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.