|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Services: Rebuild Service. |
|
4
|
|
|
* |
|
5
|
|
|
* The Wordlift_Rebuild_Service allows to rebuild the Linked Data dataset from |
|
6
|
|
|
* scratch by clearing out data on the remote dataset, parsing all data in WordPress |
|
7
|
|
|
* and resending data to the remote dataset. |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.6.0 |
|
10
|
|
|
* @package Wordlift |
|
11
|
|
|
* @subpackage Wordlift/includes |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Define the {@link Wordlift_Rebuild_Service} class. |
|
16
|
|
|
* |
|
17
|
|
|
* @since 3.6.0 |
|
18
|
|
|
* @package Wordlift |
|
19
|
|
|
* @subpackage Wordlift/includes |
|
20
|
|
|
*/ |
|
21
|
|
|
class Wordlift_Rebuild_Service extends Wordlift_Listable { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A {@link Wordlift_Log_Service} instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @since 3.6.0 |
|
27
|
|
|
* @access private |
|
28
|
|
|
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
29
|
|
|
*/ |
|
30
|
|
|
private $log; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* A {@link Wordlift_Sparql_Service} instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @since 3.6.0 |
|
36
|
|
|
* @access private |
|
37
|
|
|
* @var \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance. |
|
38
|
|
|
*/ |
|
39
|
|
|
private $sparql_service; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The {@link Wordlift_Uri_Service} instance. |
|
43
|
|
|
* |
|
44
|
|
|
* @since 3.15.0 |
|
45
|
|
|
* @access private |
|
46
|
|
|
* @var \Wordlift_Uri_Service $uri_service The {@link Wordlift_Uri_Service} instance. |
|
47
|
|
|
*/ |
|
48
|
|
|
private $uri_service; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create an instance of Wordlift_Rebuild_Service. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 3.6.0 |
|
54
|
|
|
* |
|
55
|
|
|
* @param \Wordlift_Sparql_Service $sparql_service A {@link Wordlift_Sparql_Service} instance used to query the remote dataset. |
|
56
|
|
|
* @param \Wordlift_Uri_Service $uri_service |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct( $sparql_service, $uri_service ) { |
|
59
|
|
|
|
|
60
|
|
|
$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Rebuild_Service' ); |
|
61
|
|
|
|
|
62
|
|
|
$this->sparql_service = $sparql_service; |
|
63
|
|
|
$this->uri_service = $uri_service; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Rebuild the Linked Data remote dataset by clearing it out and repopulating |
|
68
|
|
|
* it with local data. |
|
69
|
|
|
* |
|
70
|
|
|
* @since 3.6.0 |
|
71
|
|
|
*/ |
|
72
|
|
|
public function rebuild() { |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
ob_clean(); |
|
75
|
|
|
|
|
76
|
|
|
// Give ourselves some time to process the data. |
|
77
|
|
|
set_time_limit( 21600 ); // 6 hours |
|
78
|
|
|
|
|
79
|
|
|
// Send textual output. |
|
80
|
|
|
header( 'Content-type: text/plain; charset=utf-8' ); |
|
81
|
|
|
|
|
82
|
|
|
// We start at 0 by default and get to max. |
|
83
|
|
|
$offset = $_GET['offset'] ?: 0; |
|
84
|
|
|
$limit = $_GET['limit'] ?: 1; |
|
85
|
|
|
$max = $offset + $limit; |
|
86
|
|
|
|
|
87
|
|
|
// Whether we should run queries asynchronously, this is handled in `wordlift_constants.php`. |
|
88
|
|
|
$asynchronous = isset( $_GET['wl-async'] ) && 'true' === $_GET['wl-async']; |
|
89
|
|
|
|
|
90
|
|
|
// If we're starting at offset 0, then delete existing URIs and data from |
|
91
|
|
|
// the remote dataset. |
|
92
|
|
|
if ( 0 === $offset ) { |
|
93
|
|
|
|
|
94
|
|
|
// Clear out all generated URIs, since the dataset URI might have changed |
|
95
|
|
|
// in the process. |
|
96
|
|
|
$this->uri_service->delete_all(); |
|
97
|
|
|
|
|
98
|
|
|
// Delete all the triples in the remote dataset. |
|
99
|
|
|
$this->sparql_service->execute( 'DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };' ); |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Go through the list of published entities and posts and call the (legacy) |
|
104
|
|
|
// `wl_linked_data_save_post` function for each one. We're using the `process` |
|
105
|
|
|
// function which is provided by the parent `Wordlift_Listable` abstract class |
|
106
|
|
|
// and will cycle through all the posts w/ a very small memory footprint |
|
107
|
|
|
// in order to avoid memory errors. |
|
108
|
|
|
|
|
109
|
|
|
$count = 0; |
|
110
|
|
|
$log = $this->log; |
|
111
|
|
|
$this->process( function ( $post ) use ( &$count, $log ) { |
|
112
|
|
|
$count ++; |
|
113
|
|
|
$log->trace( "Going to save post $count, ID $post->ID..." ); |
|
114
|
|
|
wl_linked_data_save_post( $post->ID ); |
|
115
|
|
|
}, array( |
|
116
|
|
|
'post_status' => 'publish', |
|
117
|
|
|
), $offset, $max ); |
|
118
|
|
|
|
|
119
|
|
|
// Redirect to the next chunk. |
|
120
|
|
|
if ( $count == $limit ) { |
|
121
|
|
|
$log->trace( 'Redirecting to post #' . ( $offset + 1 ) . '...' ); |
|
122
|
|
|
$this->redirect( admin_url( 'admin-ajax.php?action=wl_rebuild&offset=' . ( $offset + $limit ) . '&limit=' . $limit . '&wl-async=' . ( $asynchronous ? 'true' : 'false' ) ) ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$this->log->info( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
126
|
|
|
echo( "Rebuild complete [ count :: $count ][ limit :: $limit ]" ); |
|
127
|
|
|
|
|
128
|
|
|
// If we're being called as AJAX, die here. |
|
129
|
|
|
if ( DOING_AJAX ) { |
|
130
|
|
|
wp_die(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Redirect using a client-side meta to avoid browsers' redirect restrictions. |
|
137
|
|
|
* |
|
138
|
|
|
* @since 3.9.8 |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $url The URL to redirect to. |
|
141
|
|
|
*/ |
|
142
|
|
|
private function redirect( $url ) { |
|
143
|
|
|
|
|
144
|
|
|
ob_clean(); |
|
145
|
|
|
|
|
146
|
|
|
@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); |
|
|
|
|
|
|
147
|
|
|
?> |
|
148
|
|
|
<html> |
|
149
|
|
|
<head> |
|
150
|
|
|
<meta http-equiv="refresh" |
|
151
|
|
|
content="0; <?php echo esc_attr( $url ); ?>"> |
|
152
|
|
|
</head> |
|
153
|
|
|
<body> |
|
154
|
|
|
Rebuilding, please wait... |
|
155
|
|
|
</body> |
|
156
|
|
|
</html> |
|
157
|
|
|
<?php |
|
158
|
|
|
|
|
159
|
|
|
exit; |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* List the items starting at the specified offset and up to the specified limit. |
|
165
|
|
|
* |
|
166
|
|
|
* @since 3.6.0 |
|
167
|
|
|
* |
|
168
|
|
|
* @param int $offset The start offset. |
|
169
|
|
|
* @param int $limit The maximum number of items to return. |
|
170
|
|
|
* @param array $args Additional arguments. |
|
171
|
|
|
* |
|
172
|
|
|
* @return array A array of items (or an empty array if no items are found). |
|
173
|
|
|
*/ |
|
174
|
|
|
function find( $offset = 0, $limit = 10, $args = array() ) { |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
return get_posts( wp_parse_args( $args, Wordlift_Entity_Service::add_criterias( array( |
|
177
|
|
|
'offset' => $offset, |
|
178
|
|
|
'numberposts' => $limit, |
|
179
|
|
|
'fields' => 'all', |
|
180
|
|
|
'orderby' => 'ID', |
|
181
|
|
|
'order' => 'ASC', |
|
182
|
|
|
'post_status' => 'any', |
|
183
|
|
|
'cache_results' => false, |
|
184
|
|
|
) ) ) ); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: