1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This class does a full resync of the database by |
5
|
|
|
* enqueuing an outbound action for every single object |
6
|
|
|
* that we care about. |
7
|
|
|
* |
8
|
|
|
* This class contains a few non-obvious optimisations that should be explained: |
9
|
|
|
* - we fire an action called jetpack_full_sync_start so that WPCOM can erase the contents of the cached database |
10
|
|
|
* - for each object type, we obtain a full list of object IDs to sync via a single API call (hoping that since they're ints, they can all fit in RAM) |
11
|
|
|
* - we load the full objects for those IDs in chunks of Jetpack_Sync_Full::ARRAY_CHUNK_SIZE (to reduce the number of MySQL calls) |
12
|
|
|
* - we fire a trigger for the entire array which the Jetpack_Sync_Sender then serializes and queues. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
require_once 'class.jetpack-sync-wp-replicastore.php'; |
16
|
|
|
|
17
|
|
|
class Jetpack_Sync_Module_Full_Sync extends Jetpack_Sync_Module { |
18
|
|
|
const STATUS_OPTION = 'jetpack_full_sync_status'; |
19
|
|
|
const FULL_SYNC_TIMEOUT = 3600; |
20
|
|
|
|
21
|
|
|
private $sender; |
|
|
|
|
22
|
|
|
|
23
|
|
|
public function name() { |
24
|
|
|
return 'full-sync'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
function init_listeners( $callable ) { |
28
|
|
|
// synthetic actions for full sync |
29
|
|
|
add_action( 'jetpack_full_sync_start', $callable ); |
30
|
|
|
add_action( 'jetpack_full_sync_end', $callable ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function init_before_send() { |
34
|
|
|
// this is triggered after actions have been processed on the server |
35
|
|
|
add_action( 'jetpack_sync_processed_actions', array( $this, 'update_sent_progress_action' ) ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
function start( $modules = null ) { |
39
|
|
|
if( ! $this->should_start_full_sync() ) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// ensure listener is loaded so we can guarantee full sync actions are enqueued |
44
|
|
|
require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
45
|
|
|
Jetpack_Sync_Listener::getInstance(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Fires when a full sync begins. This action is serialized |
49
|
|
|
* and sent to the server so that it knows a full sync is coming. |
50
|
|
|
* |
51
|
|
|
* @since 4.2.0 |
52
|
|
|
*/ |
53
|
|
|
do_action( 'jetpack_full_sync_start' ); |
54
|
|
|
$this->set_status_queuing_started(); |
55
|
|
|
|
56
|
|
|
foreach( Jetpack_Sync_Modules::get_modules() as $module ) { |
57
|
|
|
$module_name = $module->name(); |
58
|
|
|
if ( is_array( $modules ) && ! isset( $modules[ $module_name ] ) ) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$items_enqueued = $module->enqueue_full_sync_actions(); |
63
|
|
|
if ( $items_enqueued !== 0 ) { |
64
|
|
|
$status = $this->get_status(); |
65
|
|
|
|
66
|
|
|
if ( ! isset( $status['queue'][ $module_name ] ) ) { |
67
|
|
|
$status['queue'][ $module_name ] = 0; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$status['queue'][ $module_name ] += $items_enqueued; |
71
|
|
|
} |
72
|
|
|
$this->update_status( $status ); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->set_status_queuing_finished(); |
76
|
|
|
|
77
|
|
|
$store = new Jetpack_Sync_WP_Replicastore(); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Fires when a full sync ends. This action is serialized |
81
|
|
|
* and sent to the server with checksums so that we can confirm the |
82
|
|
|
* sync was successful. |
83
|
|
|
* |
84
|
|
|
* @since 4.2.0 |
85
|
|
|
*/ |
86
|
|
|
do_action( 'jetpack_full_sync_end', $store->checksum_all() ); |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function should_start_full_sync() { |
91
|
|
|
$status = $this->get_status(); |
92
|
|
|
|
93
|
|
|
// We should try sync if we haven't started it yet or if we have finished it. |
94
|
|
|
if( is_null( $status['started'] ) || is_integer( $status['finished'] ) ) { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// allow enqueing if last full sync was started more than FULL_SYNC_TIMEOUT seconds ago |
99
|
|
|
if ( intval( $status['started'] ) + self::FULL_SYNC_TIMEOUT < time() ) { |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function update_sent_progress_action( $actions ) { |
107
|
|
|
// quick way to map to first items with an array of arrays |
108
|
|
|
$actions_with_counts = array_count_values( array_map( 'reset', $actions ) ); |
109
|
|
|
|
110
|
|
|
$status = $this->get_status(); |
111
|
|
|
if ( is_null( $status['started'] ) || $status['finished'] ) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( isset( $actions_with_counts[ 'jetpack_full_sync_start' ] ) ) { |
116
|
|
|
$status['sent_started'] = time(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
foreach( Jetpack_Sync_Modules::get_modules() as $module ) { |
120
|
|
|
$module_name = $module->name(); |
121
|
|
|
$module_actions = $module->get_full_sync_actions(); |
122
|
|
|
foreach( $module_actions as $module_action ) { |
123
|
|
|
if ( isset( $actions_with_counts[ $module_action ] ) ) { |
124
|
|
|
if ( ! isset( $status[ 'sent' ][ $module_name ] ) ) { |
125
|
|
|
$status['sent'][ $module_name ] = 0; |
126
|
|
|
} |
127
|
|
|
$status['sent'][ $module_name ] += $actions_with_counts[ $module_action ]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ( isset( $actions_with_counts[ 'jetpack_full_sync_end' ] ) ) { |
133
|
|
|
$status['finished'] = time(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->update_status( $status ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function set_status_queuing_started() { |
140
|
|
|
$status = $this->initial_status; |
141
|
|
|
$status[ 'started' ] = time(); |
142
|
|
|
$this->update_status( $status ); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function set_status_queuing_finished() { |
146
|
|
|
$this->update_status( array( 'queue_finished' => time() ) ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private $initial_status = array( |
150
|
|
|
'started' => null, |
151
|
|
|
'queue_finished' => null, |
152
|
|
|
'sent_started' => null, |
153
|
|
|
'finished' => null, |
154
|
|
|
'sent' => array(), |
155
|
|
|
'queue' => array(), |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
public function get_status() { |
159
|
|
|
return get_option( self::STATUS_OPTION, $this->initial_status ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function update_status( $status ) { |
163
|
|
|
return update_option( |
164
|
|
|
self::STATUS_OPTION, |
165
|
|
|
array_merge( $this->get_status(), $status ) |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function clear_status() { |
170
|
|
|
delete_option( self::STATUS_OPTION ); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.