author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WP_Importer base class |
|
4 |
*/ |
|
5 |
class WP_Importer { |
|
6 |
/** |
|
7 |
* Class Constructor |
|
8 |
* |
|
9 |
*/ |
|
5 | 10 |
public function __construct() {} |
0 | 11 |
|
12 |
/** |
|
13 |
* Returns array with imported permalinks from WordPress database |
|
14 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* @param string $importer_name |
0 | 18 |
* @param string $bid |
19 |
* @return array |
|
20 |
*/ |
|
5 | 21 |
public function get_imported_posts( $importer_name, $bid ) { |
0 | 22 |
global $wpdb; |
23 |
||
24 |
$hashtable = array(); |
|
25 |
||
26 |
$limit = 100; |
|
27 |
$offset = 0; |
|
28 |
||
29 |
// Grab all posts in chunks |
|
30 |
do { |
|
31 |
$meta_key = $importer_name . '_' . $bid . '_permalink'; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
$sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit ); |
0 | 33 |
$results = $wpdb->get_results( $sql ); |
34 |
||
35 |
// Increment offset |
|
36 |
$offset = ( $limit + $offset ); |
|
37 |
||
38 |
if ( !empty( $results ) ) { |
|
39 |
foreach ( $results as $r ) { |
|
40 |
// Set permalinks into array |
|
41 |
$hashtable[$r->meta_value] = intval( $r->post_id ); |
|
42 |
} |
|
43 |
} |
|
44 |
} while ( count( $results ) == $limit ); |
|
45 |
||
5 | 46 |
// Unset to save memory. |
0 | 47 |
unset( $results, $r ); |
48 |
||
49 |
return $hashtable; |
|
50 |
} |
|
51 |
||
52 |
/** |
|
53 |
* Return count of imported permalinks from WordPress database |
|
54 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @param string $importer_name |
0 | 58 |
* @param string $bid |
59 |
* @return int |
|
60 |
*/ |
|
5 | 61 |
public function count_imported_posts( $importer_name, $bid ) { |
0 | 62 |
global $wpdb; |
63 |
||
64 |
$count = 0; |
|
65 |
||
66 |
// Get count of permalinks |
|
67 |
$meta_key = $importer_name . '_' . $bid . '_permalink'; |
|
68 |
$sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = '%s'", $meta_key ); |
|
69 |
||
70 |
$result = $wpdb->get_results( $sql ); |
|
71 |
||
72 |
if ( !empty( $result ) ) |
|
73 |
$count = intval( $result[0]->cnt ); |
|
74 |
||
5 | 75 |
// Unset to save memory. |
0 | 76 |
unset( $results ); |
77 |
||
78 |
return $count; |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Set array with imported comments from WordPress database |
|
83 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
* |
0 | 86 |
* @param string $bid |
87 |
* @return array |
|
88 |
*/ |
|
5 | 89 |
public function get_imported_comments( $bid ) { |
0 | 90 |
global $wpdb; |
91 |
||
92 |
$hashtable = array(); |
|
93 |
||
94 |
$limit = 100; |
|
95 |
$offset = 0; |
|
96 |
||
97 |
// Grab all comments in chunks |
|
98 |
do { |
|
99 |
$sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit ); |
|
100 |
$results = $wpdb->get_results( $sql ); |
|
101 |
||
102 |
// Increment offset |
|
103 |
$offset = ( $limit + $offset ); |
|
104 |
||
105 |
if ( !empty( $results ) ) { |
|
106 |
foreach ( $results as $r ) { |
|
107 |
// Explode comment_agent key |
|
108 |
list ( $ca_bid, $source_comment_id ) = explode( '-', $r->comment_agent ); |
|
109 |
$source_comment_id = intval( $source_comment_id ); |
|
110 |
||
111 |
// Check if this comment came from this blog |
|
112 |
if ( $bid == $ca_bid ) { |
|
113 |
$hashtable[$source_comment_id] = intval( $r->comment_ID ); |
|
114 |
} |
|
115 |
} |
|
116 |
} |
|
117 |
} while ( count( $results ) == $limit ); |
|
118 |
||
5 | 119 |
// Unset to save memory. |
0 | 120 |
unset( $results, $r ); |
121 |
||
122 |
return $hashtable; |
|
123 |
} |
|
124 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* @param int $blog_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* @return int|void |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
*/ |
5 | 130 |
public function set_blog( $blog_id ) { |
0 | 131 |
if ( is_numeric( $blog_id ) ) { |
132 |
$blog_id = (int) $blog_id; |
|
133 |
} else { |
|
134 |
$blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id ); |
|
135 |
if ( ( !$parsed = parse_url( $blog ) ) || empty( $parsed['host'] ) ) { |
|
136 |
fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" ); |
|
137 |
exit(); |
|
138 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
if ( empty( $parsed['path'] ) ) { |
0 | 140 |
$parsed['path'] = '/'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
$blogs = get_sites( array( 'domain' => $parsed['host'], 'number' => 1, 'path' => $parsed['path'] ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
if ( ! $blogs ) { |
0 | 144 |
fwrite( STDERR, "Error: Could not find blog\n" ); |
145 |
exit(); |
|
146 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
$blog = array_shift( $blogs ); |
0 | 148 |
$blog_id = (int) $blog->blog_id; |
149 |
} |
|
150 |
||
151 |
if ( function_exists( 'is_multisite' ) ) { |
|
152 |
if ( is_multisite() ) |
|
153 |
switch_to_blog( $blog_id ); |
|
154 |
} |
|
155 |
||
156 |
return $blog_id; |
|
157 |
} |
|
158 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* @param int $user_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
* @return int|void |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
*/ |
5 | 164 |
public function set_user( $user_id ) { |
0 | 165 |
if ( is_numeric( $user_id ) ) { |
166 |
$user_id = (int) $user_id; |
|
167 |
} else { |
|
168 |
$user_id = (int) username_exists( $user_id ); |
|
169 |
} |
|
170 |
||
171 |
if ( !$user_id || !wp_set_current_user( $user_id ) ) { |
|
172 |
fwrite( STDERR, "Error: can not find user\n" ); |
|
173 |
exit(); |
|
174 |
} |
|
175 |
||
176 |
return $user_id; |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Sort by strlen, longest string first |
|
181 |
* |
|
182 |
* @param string $a |
|
183 |
* @param string $b |
|
184 |
* @return int |
|
185 |
*/ |
|
5 | 186 |
public function cmpr_strlen( $a, $b ) { |
0 | 187 |
return strlen( $b ) - strlen( $a ); |
188 |
} |
|
189 |
||
190 |
/** |
|
191 |
* GET URL |
|
192 |
* |
|
193 |
* @param string $url |
|
194 |
* @param string $username |
|
195 |
* @param string $password |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
* @param bool $head |
0 | 197 |
* @return array |
198 |
*/ |
|
5 | 199 |
public function get_page( $url, $username = '', $password = '', $head = false ) { |
0 | 200 |
// Increase the timeout |
201 |
add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); |
|
202 |
||
203 |
$headers = array(); |
|
204 |
$args = array(); |
|
205 |
if ( true === $head ) |
|
206 |
$args['method'] = 'HEAD'; |
|
207 |
if ( !empty( $username ) && !empty( $password ) ) |
|
208 |
$headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" ); |
|
209 |
||
210 |
$args['headers'] = $headers; |
|
211 |
||
212 |
return wp_safe_remote_request( $url, $args ); |
|
213 |
} |
|
214 |
||
215 |
/** |
|
216 |
* Bump up the request timeout for http requests |
|
217 |
* |
|
218 |
* @param int $val |
|
219 |
* @return int |
|
220 |
*/ |
|
5 | 221 |
public function bump_request_timeout( $val ) { |
0 | 222 |
return 60; |
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Check if user has exceeded disk quota |
|
227 |
* |
|
228 |
* @return bool |
|
229 |
*/ |
|
5 | 230 |
public function is_user_over_quota() { |
0 | 231 |
if ( function_exists( 'upload_is_user_over_quota' ) ) { |
5 | 232 |
if ( upload_is_user_over_quota() ) { |
0 | 233 |
return true; |
234 |
} |
|
235 |
} |
|
236 |
||
237 |
return false; |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Replace newlines, tabs, and multiple spaces with a single space |
|
242 |
* |
|
243 |
* @param string $string |
|
244 |
* @return string |
|
245 |
*/ |
|
5 | 246 |
public function min_whitespace( $string ) { |
0 | 247 |
return preg_replace( '|[\r\n\t ]+|', ' ', $string ); |
248 |
} |
|
249 |
||
250 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* Resets global variables that grow out of control during imports. |
0 | 252 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* @since 3.0.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
* @global array $wp_actions |
0 | 257 |
*/ |
5 | 258 |
public function stop_the_insanity() { |
0 | 259 |
global $wpdb, $wp_actions; |
260 |
// Or define( 'WP_IMPORTING', true ); |
|
261 |
$wpdb->queries = array(); |
|
262 |
// Reset $wp_actions to keep it from growing out of control |
|
263 |
$wp_actions = array(); |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Returns value of command line params. |
|
269 |
* Exits when a required param is not set. |
|
270 |
* |
|
271 |
* @param string $param |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
272 |
* @param bool $required |
0 | 273 |
* @return mixed |
274 |
*/ |
|
275 |
function get_cli_args( $param, $required = false ) { |
|
276 |
$args = $_SERVER['argv']; |
|
277 |
||
278 |
$out = array(); |
|
279 |
||
280 |
$last_arg = null; |
|
281 |
$return = null; |
|
282 |
||
283 |
$il = sizeof( $args ); |
|
284 |
||
285 |
for ( $i = 1, $il; $i < $il; $i++ ) { |
|
286 |
if ( (bool) preg_match( "/^--(.+)/", $args[$i], $match ) ) { |
|
287 |
$parts = explode( "=", $match[1] ); |
|
288 |
$key = preg_replace( "/[^a-z0-9]+/", "", $parts[0] ); |
|
289 |
||
290 |
if ( isset( $parts[1] ) ) { |
|
291 |
$out[$key] = $parts[1]; |
|
292 |
} else { |
|
293 |
$out[$key] = true; |
|
294 |
} |
|
295 |
||
296 |
$last_arg = $key; |
|
5 | 297 |
} elseif ( (bool) preg_match( "/^-([a-zA-Z0-9]+)/", $args[$i], $match ) ) { |
0 | 298 |
for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) { |
299 |
$key = $match[1]{$j}; |
|
300 |
$out[$key] = true; |
|
301 |
} |
|
302 |
||
303 |
$last_arg = $key; |
|
5 | 304 |
} elseif ( $last_arg !== null ) { |
0 | 305 |
$out[$last_arg] = $args[$i]; |
306 |
} |
|
307 |
} |
|
308 |
||
309 |
// Check array for specified param |
|
310 |
if ( isset( $out[$param] ) ) { |
|
311 |
// Set return value |
|
312 |
$return = $out[$param]; |
|
313 |
} |
|
314 |
||
315 |
// Check for missing required param |
|
316 |
if ( !isset( $out[$param] ) && $required ) { |
|
317 |
// Display message and exit |
|
318 |
echo "\"$param\" parameter is required but was not specified\n"; |
|
319 |
exit(); |
|
320 |
} |
|
321 |
||
322 |
return $return; |
|
323 |
} |