1 <?php |
|
2 /** |
|
3 * WordPress eXtended RSS file parser implementations |
|
4 * |
|
5 * @package WordPress |
|
6 * @subpackage Importer |
|
7 */ |
|
8 |
|
9 /** |
|
10 * WordPress Importer class for managing parsing of WXR files. |
|
11 */ |
|
12 class WXR_Parser { |
|
13 function parse( $file ) { |
|
14 // Attempt to use proper XML parsers first |
|
15 if ( extension_loaded( 'simplexml' ) ) { |
|
16 $parser = new WXR_Parser_SimpleXML; |
|
17 $result = $parser->parse( $file ); |
|
18 |
|
19 // If SimpleXML succeeds or this is an invalid WXR file then return the results |
|
20 if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() ) |
|
21 return $result; |
|
22 } else if ( extension_loaded( 'xml' ) ) { |
|
23 $parser = new WXR_Parser_XML; |
|
24 $result = $parser->parse( $file ); |
|
25 |
|
26 // If XMLParser succeeds or this is an invalid WXR file then return the results |
|
27 if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() ) |
|
28 return $result; |
|
29 } |
|
30 |
|
31 // We have a malformed XML file, so display the error and fallthrough to regex |
|
32 if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
33 echo '<pre>'; |
|
34 if ( 'SimpleXML_parse_error' == $result->get_error_code() ) { |
|
35 foreach ( $result->get_error_data() as $error ) |
|
36 echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n"; |
|
37 } else if ( 'XML_parse_error' == $result->get_error_code() ) { |
|
38 $error = $result->get_error_data(); |
|
39 echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] ); |
|
40 } |
|
41 echo '</pre>'; |
|
42 echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />'; |
|
43 echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>'; |
|
44 } |
|
45 |
|
46 // use regular expressions if nothing else available or this is bad XML |
|
47 $parser = new WXR_Parser_Regex; |
|
48 return $parser->parse( $file ); |
|
49 } |
|
50 } |
|
51 |
|
52 /** |
|
53 * WXR Parser that makes use of the SimpleXML PHP extension. |
|
54 */ |
|
55 class WXR_Parser_SimpleXML { |
|
56 function parse( $file ) { |
|
57 $authors = $posts = $categories = $tags = $terms = array(); |
|
58 |
|
59 $internal_errors = libxml_use_internal_errors(true); |
|
60 |
|
61 $dom = new DOMDocument; |
|
62 $old_value = null; |
|
63 if ( function_exists( 'libxml_disable_entity_loader' ) ) { |
|
64 $old_value = libxml_disable_entity_loader( true ); |
|
65 } |
|
66 $success = $dom->loadXML( file_get_contents( $file ) ); |
|
67 if ( ! is_null( $old_value ) ) { |
|
68 libxml_disable_entity_loader( $old_value ); |
|
69 } |
|
70 |
|
71 if ( ! $success || isset( $dom->doctype ) ) { |
|
72 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
73 } |
|
74 |
|
75 $xml = simplexml_import_dom( $dom ); |
|
76 unset( $dom ); |
|
77 |
|
78 // halt if loading produces an error |
|
79 if ( ! $xml ) |
|
80 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
81 |
|
82 $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version'); |
|
83 if ( ! $wxr_version ) |
|
84 return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
85 |
|
86 $wxr_version = (string) trim( $wxr_version[0] ); |
|
87 // confirm that we are dealing with the correct file format |
|
88 if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) |
|
89 return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
90 |
|
91 $base_url = $xml->xpath('/rss/channel/wp:base_site_url'); |
|
92 $base_url = (string) trim( $base_url[0] ); |
|
93 |
|
94 $namespaces = $xml->getDocNamespaces(); |
|
95 if ( ! isset( $namespaces['wp'] ) ) |
|
96 $namespaces['wp'] = 'http://wordpress.org/export/1.1/'; |
|
97 if ( ! isset( $namespaces['excerpt'] ) ) |
|
98 $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/'; |
|
99 |
|
100 // grab authors |
|
101 foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) { |
|
102 $a = $author_arr->children( $namespaces['wp'] ); |
|
103 $login = (string) $a->author_login; |
|
104 $authors[$login] = array( |
|
105 'author_id' => (int) $a->author_id, |
|
106 'author_login' => $login, |
|
107 'author_email' => (string) $a->author_email, |
|
108 'author_display_name' => (string) $a->author_display_name, |
|
109 'author_first_name' => (string) $a->author_first_name, |
|
110 'author_last_name' => (string) $a->author_last_name |
|
111 ); |
|
112 } |
|
113 |
|
114 // grab cats, tags and terms |
|
115 foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) { |
|
116 $t = $term_arr->children( $namespaces['wp'] ); |
|
117 $category = array( |
|
118 'term_id' => (int) $t->term_id, |
|
119 'category_nicename' => (string) $t->category_nicename, |
|
120 'category_parent' => (string) $t->category_parent, |
|
121 'cat_name' => (string) $t->cat_name, |
|
122 'category_description' => (string) $t->category_description |
|
123 ); |
|
124 |
|
125 foreach ( $t->termmeta as $meta ) { |
|
126 $category['termmeta'][] = array( |
|
127 'key' => (string) $meta->meta_key, |
|
128 'value' => (string) $meta->meta_value |
|
129 ); |
|
130 } |
|
131 |
|
132 $categories[] = $category; |
|
133 } |
|
134 |
|
135 foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) { |
|
136 $t = $term_arr->children( $namespaces['wp'] ); |
|
137 $tag = array( |
|
138 'term_id' => (int) $t->term_id, |
|
139 'tag_slug' => (string) $t->tag_slug, |
|
140 'tag_name' => (string) $t->tag_name, |
|
141 'tag_description' => (string) $t->tag_description |
|
142 ); |
|
143 |
|
144 foreach ( $t->termmeta as $meta ) { |
|
145 $tag['termmeta'][] = array( |
|
146 'key' => (string) $meta->meta_key, |
|
147 'value' => (string) $meta->meta_value |
|
148 ); |
|
149 } |
|
150 |
|
151 $tags[] = $tag; |
|
152 } |
|
153 |
|
154 foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) { |
|
155 $t = $term_arr->children( $namespaces['wp'] ); |
|
156 $term = array( |
|
157 'term_id' => (int) $t->term_id, |
|
158 'term_taxonomy' => (string) $t->term_taxonomy, |
|
159 'slug' => (string) $t->term_slug, |
|
160 'term_parent' => (string) $t->term_parent, |
|
161 'term_name' => (string) $t->term_name, |
|
162 'term_description' => (string) $t->term_description |
|
163 ); |
|
164 |
|
165 foreach ( $t->termmeta as $meta ) { |
|
166 $term['termmeta'][] = array( |
|
167 'key' => (string) $meta->meta_key, |
|
168 'value' => (string) $meta->meta_value |
|
169 ); |
|
170 } |
|
171 |
|
172 $terms[] = $term; |
|
173 } |
|
174 |
|
175 // grab posts |
|
176 foreach ( $xml->channel->item as $item ) { |
|
177 $post = array( |
|
178 'post_title' => (string) $item->title, |
|
179 'guid' => (string) $item->guid, |
|
180 ); |
|
181 |
|
182 $dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); |
|
183 $post['post_author'] = (string) $dc->creator; |
|
184 |
|
185 $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); |
|
186 $excerpt = $item->children( $namespaces['excerpt'] ); |
|
187 $post['post_content'] = (string) $content->encoded; |
|
188 $post['post_excerpt'] = (string) $excerpt->encoded; |
|
189 |
|
190 $wp = $item->children( $namespaces['wp'] ); |
|
191 $post['post_id'] = (int) $wp->post_id; |
|
192 $post['post_date'] = (string) $wp->post_date; |
|
193 $post['post_date_gmt'] = (string) $wp->post_date_gmt; |
|
194 $post['comment_status'] = (string) $wp->comment_status; |
|
195 $post['ping_status'] = (string) $wp->ping_status; |
|
196 $post['post_name'] = (string) $wp->post_name; |
|
197 $post['status'] = (string) $wp->status; |
|
198 $post['post_parent'] = (int) $wp->post_parent; |
|
199 $post['menu_order'] = (int) $wp->menu_order; |
|
200 $post['post_type'] = (string) $wp->post_type; |
|
201 $post['post_password'] = (string) $wp->post_password; |
|
202 $post['is_sticky'] = (int) $wp->is_sticky; |
|
203 |
|
204 if ( isset($wp->attachment_url) ) |
|
205 $post['attachment_url'] = (string) $wp->attachment_url; |
|
206 |
|
207 foreach ( $item->category as $c ) { |
|
208 $att = $c->attributes(); |
|
209 if ( isset( $att['nicename'] ) ) |
|
210 $post['terms'][] = array( |
|
211 'name' => (string) $c, |
|
212 'slug' => (string) $att['nicename'], |
|
213 'domain' => (string) $att['domain'] |
|
214 ); |
|
215 } |
|
216 |
|
217 foreach ( $wp->postmeta as $meta ) { |
|
218 $post['postmeta'][] = array( |
|
219 'key' => (string) $meta->meta_key, |
|
220 'value' => (string) $meta->meta_value |
|
221 ); |
|
222 } |
|
223 |
|
224 foreach ( $wp->comment as $comment ) { |
|
225 $meta = array(); |
|
226 if ( isset( $comment->commentmeta ) ) { |
|
227 foreach ( $comment->commentmeta as $m ) { |
|
228 $meta[] = array( |
|
229 'key' => (string) $m->meta_key, |
|
230 'value' => (string) $m->meta_value |
|
231 ); |
|
232 } |
|
233 } |
|
234 |
|
235 $post['comments'][] = array( |
|
236 'comment_id' => (int) $comment->comment_id, |
|
237 'comment_author' => (string) $comment->comment_author, |
|
238 'comment_author_email' => (string) $comment->comment_author_email, |
|
239 'comment_author_IP' => (string) $comment->comment_author_IP, |
|
240 'comment_author_url' => (string) $comment->comment_author_url, |
|
241 'comment_date' => (string) $comment->comment_date, |
|
242 'comment_date_gmt' => (string) $comment->comment_date_gmt, |
|
243 'comment_content' => (string) $comment->comment_content, |
|
244 'comment_approved' => (string) $comment->comment_approved, |
|
245 'comment_type' => (string) $comment->comment_type, |
|
246 'comment_parent' => (string) $comment->comment_parent, |
|
247 'comment_user_id' => (int) $comment->comment_user_id, |
|
248 'commentmeta' => $meta, |
|
249 ); |
|
250 } |
|
251 |
|
252 $posts[] = $post; |
|
253 } |
|
254 |
|
255 return array( |
|
256 'authors' => $authors, |
|
257 'posts' => $posts, |
|
258 'categories' => $categories, |
|
259 'tags' => $tags, |
|
260 'terms' => $terms, |
|
261 'base_url' => $base_url, |
|
262 'version' => $wxr_version |
|
263 ); |
|
264 } |
|
265 } |
|
266 |
|
267 /** |
|
268 * WXR Parser that makes use of the XML Parser PHP extension. |
|
269 */ |
|
270 class WXR_Parser_XML { |
|
271 var $wp_tags = array( |
|
272 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url', |
|
273 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password', |
|
274 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description', |
|
275 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent', |
|
276 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name', |
|
277 'wp:author_first_name', 'wp:author_last_name', |
|
278 ); |
|
279 var $wp_sub_tags = array( |
|
280 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url', |
|
281 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content', |
|
282 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id', |
|
283 ); |
|
284 |
|
285 function parse( $file ) { |
|
286 $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false; |
|
287 $this->authors = $this->posts = $this->term = $this->category = $this->tag = array(); |
|
288 |
|
289 $xml = xml_parser_create( 'UTF-8' ); |
|
290 xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 ); |
|
291 xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 ); |
|
292 xml_set_object( $xml, $this ); |
|
293 xml_set_character_data_handler( $xml, 'cdata' ); |
|
294 xml_set_element_handler( $xml, 'tag_open', 'tag_close' ); |
|
295 |
|
296 if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) { |
|
297 $current_line = xml_get_current_line_number( $xml ); |
|
298 $current_column = xml_get_current_column_number( $xml ); |
|
299 $error_code = xml_get_error_code( $xml ); |
|
300 $error_string = xml_error_string( $error_code ); |
|
301 return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) ); |
|
302 } |
|
303 xml_parser_free( $xml ); |
|
304 |
|
305 if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) ) |
|
306 return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
307 |
|
308 return array( |
|
309 'authors' => $this->authors, |
|
310 'posts' => $this->posts, |
|
311 'categories' => $this->category, |
|
312 'tags' => $this->tag, |
|
313 'terms' => $this->term, |
|
314 'base_url' => $this->base_url, |
|
315 'version' => $this->wxr_version |
|
316 ); |
|
317 } |
|
318 |
|
319 function tag_open( $parse, $tag, $attr ) { |
|
320 if ( in_array( $tag, $this->wp_tags ) ) { |
|
321 $this->in_tag = substr( $tag, 3 ); |
|
322 return; |
|
323 } |
|
324 |
|
325 if ( in_array( $tag, $this->wp_sub_tags ) ) { |
|
326 $this->in_sub_tag = substr( $tag, 3 ); |
|
327 return; |
|
328 } |
|
329 |
|
330 switch ( $tag ) { |
|
331 case 'category': |
|
332 if ( isset($attr['domain'], $attr['nicename']) ) { |
|
333 $this->sub_data['domain'] = $attr['domain']; |
|
334 $this->sub_data['slug'] = $attr['nicename']; |
|
335 } |
|
336 break; |
|
337 case 'item': $this->in_post = true; |
|
338 case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break; |
|
339 case 'guid': $this->in_tag = 'guid'; break; |
|
340 case 'dc:creator': $this->in_tag = 'post_author'; break; |
|
341 case 'content:encoded': $this->in_tag = 'post_content'; break; |
|
342 case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break; |
|
343 |
|
344 case 'wp:term_slug': $this->in_tag = 'slug'; break; |
|
345 case 'wp:meta_key': $this->in_sub_tag = 'key'; break; |
|
346 case 'wp:meta_value': $this->in_sub_tag = 'value'; break; |
|
347 } |
|
348 } |
|
349 |
|
350 function cdata( $parser, $cdata ) { |
|
351 if ( ! trim( $cdata ) ) |
|
352 return; |
|
353 |
|
354 if ( false !== $this->in_tag || false !== $this->in_sub_tag ) { |
|
355 $this->cdata .= $cdata; |
|
356 } else { |
|
357 $this->cdata .= trim( $cdata ); |
|
358 } |
|
359 } |
|
360 |
|
361 function tag_close( $parser, $tag ) { |
|
362 switch ( $tag ) { |
|
363 case 'wp:comment': |
|
364 unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data |
|
365 if ( ! empty( $this->sub_data ) ) |
|
366 $this->data['comments'][] = $this->sub_data; |
|
367 $this->sub_data = false; |
|
368 break; |
|
369 case 'wp:commentmeta': |
|
370 $this->sub_data['commentmeta'][] = array( |
|
371 'key' => $this->sub_data['key'], |
|
372 'value' => $this->sub_data['value'] |
|
373 ); |
|
374 break; |
|
375 case 'category': |
|
376 if ( ! empty( $this->sub_data ) ) { |
|
377 $this->sub_data['name'] = $this->cdata; |
|
378 $this->data['terms'][] = $this->sub_data; |
|
379 } |
|
380 $this->sub_data = false; |
|
381 break; |
|
382 case 'wp:postmeta': |
|
383 if ( ! empty( $this->sub_data ) ) |
|
384 $this->data['postmeta'][] = $this->sub_data; |
|
385 $this->sub_data = false; |
|
386 break; |
|
387 case 'item': |
|
388 $this->posts[] = $this->data; |
|
389 $this->data = false; |
|
390 break; |
|
391 case 'wp:category': |
|
392 case 'wp:tag': |
|
393 case 'wp:term': |
|
394 $n = substr( $tag, 3 ); |
|
395 array_push( $this->$n, $this->data ); |
|
396 $this->data = false; |
|
397 break; |
|
398 case 'wp:author': |
|
399 if ( ! empty($this->data['author_login']) ) |
|
400 $this->authors[$this->data['author_login']] = $this->data; |
|
401 $this->data = false; |
|
402 break; |
|
403 case 'wp:base_site_url': |
|
404 $this->base_url = $this->cdata; |
|
405 break; |
|
406 case 'wp:wxr_version': |
|
407 $this->wxr_version = $this->cdata; |
|
408 break; |
|
409 |
|
410 default: |
|
411 if ( $this->in_sub_tag ) { |
|
412 $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
413 $this->in_sub_tag = false; |
|
414 } else if ( $this->in_tag ) { |
|
415 $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
416 $this->in_tag = false; |
|
417 } |
|
418 } |
|
419 |
|
420 $this->cdata = false; |
|
421 } |
|
422 } |
|
423 |
|
424 /** |
|
425 * WXR Parser that uses regular expressions. Fallback for installs without an XML parser. |
|
426 */ |
|
427 class WXR_Parser_Regex { |
|
428 var $authors = array(); |
|
429 var $posts = array(); |
|
430 var $categories = array(); |
|
431 var $tags = array(); |
|
432 var $terms = array(); |
|
433 var $base_url = ''; |
|
434 |
|
435 function __construct() { |
|
436 $this->has_gzip = is_callable( 'gzopen' ); |
|
437 } |
|
438 |
|
439 function parse( $file ) { |
|
440 $wxr_version = $in_multiline = false; |
|
441 |
|
442 $multiline_content = ''; |
|
443 |
|
444 $multiline_tags = array( |
|
445 'item' => array( 'posts', array( $this, 'process_post' ) ), |
|
446 'wp:category' => array( 'categories', array( $this, 'process_category' ) ), |
|
447 'wp:tag' => array( 'tags', array( $this, 'process_tag' ) ), |
|
448 'wp:term' => array( 'terms', array( $this, 'process_term' ) ), |
|
449 ); |
|
450 |
|
451 $fp = $this->fopen( $file, 'r' ); |
|
452 if ( $fp ) { |
|
453 while ( ! $this->feof( $fp ) ) { |
|
454 $importline = rtrim( $this->fgets( $fp ) ); |
|
455 |
|
456 if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) |
|
457 $wxr_version = $version[1]; |
|
458 |
|
459 if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) { |
|
460 preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url ); |
|
461 $this->base_url = $url[1]; |
|
462 continue; |
|
463 } |
|
464 |
|
465 if ( false !== strpos( $importline, '<wp:author>' ) ) { |
|
466 preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
|
467 $a = $this->process_author( $author[1] ); |
|
468 $this->authors[$a['author_login']] = $a; |
|
469 continue; |
|
470 } |
|
471 |
|
472 foreach ( $multiline_tags as $tag => $handler ) { |
|
473 // Handle multi-line tags on a singular line |
|
474 if ( preg_match( '|<' . $tag . '>(.*?)</' . $tag . '>|is', $importline, $matches ) ) { |
|
475 $this->{$handler[0]}[] = call_user_func( $handler[1], $matches[1] ); |
|
476 |
|
477 } elseif ( false !== ( $pos = strpos( $importline, "<$tag>" ) ) ) { |
|
478 // Take note of any content after the opening tag |
|
479 $multiline_content = trim( substr( $importline, $pos + strlen( $tag ) + 2 ) ); |
|
480 |
|
481 // We don't want to have this line added to `$is_multiline` below. |
|
482 $importline = ''; |
|
483 $in_multiline = $tag; |
|
484 |
|
485 } elseif ( false !== ( $pos = strpos( $importline, "</$tag>" ) ) ) { |
|
486 $in_multiline = false; |
|
487 $multiline_content .= trim( substr( $importline, 0, $pos ) ); |
|
488 |
|
489 $this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content ); |
|
490 } |
|
491 } |
|
492 |
|
493 if ( $in_multiline && $importline ) { |
|
494 $multiline_content .= $importline . "\n"; |
|
495 } |
|
496 } |
|
497 |
|
498 $this->fclose($fp); |
|
499 } |
|
500 |
|
501 if ( ! $wxr_version ) |
|
502 return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
503 |
|
504 return array( |
|
505 'authors' => $this->authors, |
|
506 'posts' => $this->posts, |
|
507 'categories' => $this->categories, |
|
508 'tags' => $this->tags, |
|
509 'terms' => $this->terms, |
|
510 'base_url' => $this->base_url, |
|
511 'version' => $wxr_version |
|
512 ); |
|
513 } |
|
514 |
|
515 function get_tag( $string, $tag ) { |
|
516 preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
|
517 if ( isset( $return[1] ) ) { |
|
518 if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) { |
|
519 if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) { |
|
520 preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches ); |
|
521 $return = ''; |
|
522 foreach( $matches[1] as $match ) |
|
523 $return .= $match; |
|
524 } else { |
|
525 $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] ); |
|
526 } |
|
527 } else { |
|
528 $return = $return[1]; |
|
529 } |
|
530 } else { |
|
531 $return = ''; |
|
532 } |
|
533 return $return; |
|
534 } |
|
535 |
|
536 function process_category( $c ) { |
|
537 return array( |
|
538 'term_id' => $this->get_tag( $c, 'wp:term_id' ), |
|
539 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ), |
|
540 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ), |
|
541 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ), |
|
542 'category_description' => $this->get_tag( $c, 'wp:category_description' ), |
|
543 ); |
|
544 } |
|
545 |
|
546 function process_tag( $t ) { |
|
547 return array( |
|
548 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
549 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ), |
|
550 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ), |
|
551 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ), |
|
552 ); |
|
553 } |
|
554 |
|
555 function process_term( $t ) { |
|
556 return array( |
|
557 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
558 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ), |
|
559 'slug' => $this->get_tag( $t, 'wp:term_slug' ), |
|
560 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ), |
|
561 'term_name' => $this->get_tag( $t, 'wp:term_name' ), |
|
562 'term_description' => $this->get_tag( $t, 'wp:term_description' ), |
|
563 ); |
|
564 } |
|
565 |
|
566 function process_author( $a ) { |
|
567 return array( |
|
568 'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
|
569 'author_login' => $this->get_tag( $a, 'wp:author_login' ), |
|
570 'author_email' => $this->get_tag( $a, 'wp:author_email' ), |
|
571 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ), |
|
572 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), |
|
573 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), |
|
574 ); |
|
575 } |
|
576 |
|
577 function process_post( $post ) { |
|
578 $post_id = $this->get_tag( $post, 'wp:post_id' ); |
|
579 $post_title = $this->get_tag( $post, 'title' ); |
|
580 $post_date = $this->get_tag( $post, 'wp:post_date' ); |
|
581 $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); |
|
582 $comment_status = $this->get_tag( $post, 'wp:comment_status' ); |
|
583 $ping_status = $this->get_tag( $post, 'wp:ping_status' ); |
|
584 $status = $this->get_tag( $post, 'wp:status' ); |
|
585 $post_name = $this->get_tag( $post, 'wp:post_name' ); |
|
586 $post_parent = $this->get_tag( $post, 'wp:post_parent' ); |
|
587 $menu_order = $this->get_tag( $post, 'wp:menu_order' ); |
|
588 $post_type = $this->get_tag( $post, 'wp:post_type' ); |
|
589 $post_password = $this->get_tag( $post, 'wp:post_password' ); |
|
590 $is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); |
|
591 $guid = $this->get_tag( $post, 'guid' ); |
|
592 $post_author = $this->get_tag( $post, 'dc:creator' ); |
|
593 |
|
594 $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
|
595 $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt ); |
|
596 $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
|
597 $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt ); |
|
598 |
|
599 $post_content = $this->get_tag( $post, 'content:encoded' ); |
|
600 $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content ); |
|
601 $post_content = str_replace( '<br>', '<br />', $post_content ); |
|
602 $post_content = str_replace( '<hr>', '<hr />', $post_content ); |
|
603 |
|
604 $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', |
|
605 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', |
|
606 'menu_order', 'post_type', 'post_password', 'is_sticky' |
|
607 ); |
|
608 |
|
609 $attachment_url = $this->get_tag( $post, 'wp:attachment_url' ); |
|
610 if ( $attachment_url ) |
|
611 $postdata['attachment_url'] = $attachment_url; |
|
612 |
|
613 preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
|
614 foreach ( $terms as $t ) { |
|
615 $post_terms[] = array( |
|
616 'slug' => $t[2], |
|
617 'domain' => $t[1], |
|
618 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ), |
|
619 ); |
|
620 } |
|
621 if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms; |
|
622 |
|
623 preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments ); |
|
624 $comments = $comments[1]; |
|
625 if ( $comments ) { |
|
626 foreach ( $comments as $comment ) { |
|
627 preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta ); |
|
628 $commentmeta = $commentmeta[1]; |
|
629 $c_meta = array(); |
|
630 foreach ( $commentmeta as $m ) { |
|
631 $c_meta[] = array( |
|
632 'key' => $this->get_tag( $m, 'wp:meta_key' ), |
|
633 'value' => $this->get_tag( $m, 'wp:meta_value' ), |
|
634 ); |
|
635 } |
|
636 |
|
637 $post_comments[] = array( |
|
638 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), |
|
639 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), |
|
640 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
|
641 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), |
|
642 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), |
|
643 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), |
|
644 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), |
|
645 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), |
|
646 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), |
|
647 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), |
|
648 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), |
|
649 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), |
|
650 'commentmeta' => $c_meta, |
|
651 ); |
|
652 } |
|
653 } |
|
654 if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments; |
|
655 |
|
656 preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta ); |
|
657 $postmeta = $postmeta[1]; |
|
658 if ( $postmeta ) { |
|
659 foreach ( $postmeta as $p ) { |
|
660 $post_postmeta[] = array( |
|
661 'key' => $this->get_tag( $p, 'wp:meta_key' ), |
|
662 'value' => $this->get_tag( $p, 'wp:meta_value' ), |
|
663 ); |
|
664 } |
|
665 } |
|
666 if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta; |
|
667 |
|
668 return $postdata; |
|
669 } |
|
670 |
|
671 function _normalize_tag( $matches ) { |
|
672 return '<' . strtolower( $matches[1] ); |
|
673 } |
|
674 |
|
675 function fopen( $filename, $mode = 'r' ) { |
|
676 if ( $this->has_gzip ) |
|
677 return gzopen( $filename, $mode ); |
|
678 return fopen( $filename, $mode ); |
|
679 } |
|
680 |
|
681 function feof( $fp ) { |
|
682 if ( $this->has_gzip ) |
|
683 return gzeof( $fp ); |
|
684 return feof( $fp ); |
|
685 } |
|
686 |
|
687 function fgets( $fp, $len = 8192 ) { |
|
688 if ( $this->has_gzip ) |
|
689 return gzgets( $fp, $len ); |
|
690 return fgets( $fp, $len ); |
|
691 } |
|
692 |
|
693 function fclose( $fp ) { |
|
694 if ( $this->has_gzip ) |
|
695 return gzclose( $fp ); |
|
696 return fclose( $fp ); |
|
697 } |
|
698 } |
|