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