0
|
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 |
$categories[] = 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 |
|
|
126 |
foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) { |
|
127 |
$t = $term_arr->children( $namespaces['wp'] ); |
|
128 |
$tags[] = array( |
|
129 |
'term_id' => (int) $t->term_id, |
|
130 |
'tag_slug' => (string) $t->tag_slug, |
|
131 |
'tag_name' => (string) $t->tag_name, |
|
132 |
'tag_description' => (string) $t->tag_description |
|
133 |
); |
|
134 |
} |
|
135 |
|
|
136 |
foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) { |
|
137 |
$t = $term_arr->children( $namespaces['wp'] ); |
|
138 |
$terms[] = array( |
|
139 |
'term_id' => (int) $t->term_id, |
|
140 |
'term_taxonomy' => (string) $t->term_taxonomy, |
|
141 |
'slug' => (string) $t->term_slug, |
|
142 |
'term_parent' => (string) $t->term_parent, |
|
143 |
'term_name' => (string) $t->term_name, |
|
144 |
'term_description' => (string) $t->term_description |
|
145 |
); |
|
146 |
} |
|
147 |
|
|
148 |
// grab posts |
|
149 |
foreach ( $xml->channel->item as $item ) { |
|
150 |
$post = array( |
|
151 |
'post_title' => (string) $item->title, |
|
152 |
'guid' => (string) $item->guid, |
|
153 |
); |
|
154 |
|
|
155 |
$dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); |
|
156 |
$post['post_author'] = (string) $dc->creator; |
|
157 |
|
|
158 |
$content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); |
|
159 |
$excerpt = $item->children( $namespaces['excerpt'] ); |
|
160 |
$post['post_content'] = (string) $content->encoded; |
|
161 |
$post['post_excerpt'] = (string) $excerpt->encoded; |
|
162 |
|
|
163 |
$wp = $item->children( $namespaces['wp'] ); |
|
164 |
$post['post_id'] = (int) $wp->post_id; |
|
165 |
$post['post_date'] = (string) $wp->post_date; |
|
166 |
$post['post_date_gmt'] = (string) $wp->post_date_gmt; |
|
167 |
$post['comment_status'] = (string) $wp->comment_status; |
|
168 |
$post['ping_status'] = (string) $wp->ping_status; |
|
169 |
$post['post_name'] = (string) $wp->post_name; |
|
170 |
$post['status'] = (string) $wp->status; |
|
171 |
$post['post_parent'] = (int) $wp->post_parent; |
|
172 |
$post['menu_order'] = (int) $wp->menu_order; |
|
173 |
$post['post_type'] = (string) $wp->post_type; |
|
174 |
$post['post_password'] = (string) $wp->post_password; |
|
175 |
$post['is_sticky'] = (int) $wp->is_sticky; |
|
176 |
|
|
177 |
if ( isset($wp->attachment_url) ) |
|
178 |
$post['attachment_url'] = (string) $wp->attachment_url; |
|
179 |
|
|
180 |
foreach ( $item->category as $c ) { |
|
181 |
$att = $c->attributes(); |
|
182 |
if ( isset( $att['nicename'] ) ) |
|
183 |
$post['terms'][] = array( |
|
184 |
'name' => (string) $c, |
|
185 |
'slug' => (string) $att['nicename'], |
|
186 |
'domain' => (string) $att['domain'] |
|
187 |
); |
|
188 |
} |
|
189 |
|
|
190 |
foreach ( $wp->postmeta as $meta ) { |
|
191 |
$post['postmeta'][] = array( |
|
192 |
'key' => (string) $meta->meta_key, |
|
193 |
'value' => (string) $meta->meta_value |
|
194 |
); |
|
195 |
} |
|
196 |
|
|
197 |
foreach ( $wp->comment as $comment ) { |
|
198 |
$meta = array(); |
|
199 |
if ( isset( $comment->commentmeta ) ) { |
|
200 |
foreach ( $comment->commentmeta as $m ) { |
|
201 |
$meta[] = array( |
|
202 |
'key' => (string) $m->meta_key, |
|
203 |
'value' => (string) $m->meta_value |
|
204 |
); |
|
205 |
} |
|
206 |
} |
|
207 |
|
|
208 |
$post['comments'][] = array( |
|
209 |
'comment_id' => (int) $comment->comment_id, |
|
210 |
'comment_author' => (string) $comment->comment_author, |
|
211 |
'comment_author_email' => (string) $comment->comment_author_email, |
|
212 |
'comment_author_IP' => (string) $comment->comment_author_IP, |
|
213 |
'comment_author_url' => (string) $comment->comment_author_url, |
|
214 |
'comment_date' => (string) $comment->comment_date, |
|
215 |
'comment_date_gmt' => (string) $comment->comment_date_gmt, |
|
216 |
'comment_content' => (string) $comment->comment_content, |
|
217 |
'comment_approved' => (string) $comment->comment_approved, |
|
218 |
'comment_type' => (string) $comment->comment_type, |
|
219 |
'comment_parent' => (string) $comment->comment_parent, |
|
220 |
'comment_user_id' => (int) $comment->comment_user_id, |
|
221 |
'commentmeta' => $meta, |
|
222 |
); |
|
223 |
} |
|
224 |
|
|
225 |
$posts[] = $post; |
|
226 |
} |
|
227 |
|
|
228 |
return array( |
|
229 |
'authors' => $authors, |
|
230 |
'posts' => $posts, |
|
231 |
'categories' => $categories, |
|
232 |
'tags' => $tags, |
|
233 |
'terms' => $terms, |
|
234 |
'base_url' => $base_url, |
|
235 |
'version' => $wxr_version |
|
236 |
); |
|
237 |
} |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* WXR Parser that makes use of the XML Parser PHP extension. |
|
242 |
*/ |
|
243 |
class WXR_Parser_XML { |
|
244 |
var $wp_tags = array( |
|
245 |
'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url', |
|
246 |
'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password', |
|
247 |
'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description', |
|
248 |
'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent', |
|
249 |
'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name', |
|
250 |
'wp:author_first_name', 'wp:author_last_name', |
|
251 |
); |
|
252 |
var $wp_sub_tags = array( |
|
253 |
'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url', |
|
254 |
'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content', |
|
255 |
'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id', |
|
256 |
); |
|
257 |
|
|
258 |
function parse( $file ) { |
|
259 |
$this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false; |
|
260 |
$this->authors = $this->posts = $this->term = $this->category = $this->tag = array(); |
|
261 |
|
|
262 |
$xml = xml_parser_create( 'UTF-8' ); |
|
263 |
xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 ); |
|
264 |
xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 ); |
|
265 |
xml_set_object( $xml, $this ); |
|
266 |
xml_set_character_data_handler( $xml, 'cdata' ); |
|
267 |
xml_set_element_handler( $xml, 'tag_open', 'tag_close' ); |
|
268 |
|
|
269 |
if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) { |
|
270 |
$current_line = xml_get_current_line_number( $xml ); |
|
271 |
$current_column = xml_get_current_column_number( $xml ); |
|
272 |
$error_code = xml_get_error_code( $xml ); |
|
273 |
$error_string = xml_error_string( $error_code ); |
|
274 |
return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) ); |
|
275 |
} |
|
276 |
xml_parser_free( $xml ); |
|
277 |
|
|
278 |
if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) ) |
|
279 |
return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
280 |
|
|
281 |
return array( |
|
282 |
'authors' => $this->authors, |
|
283 |
'posts' => $this->posts, |
|
284 |
'categories' => $this->category, |
|
285 |
'tags' => $this->tag, |
|
286 |
'terms' => $this->term, |
|
287 |
'base_url' => $this->base_url, |
|
288 |
'version' => $this->wxr_version |
|
289 |
); |
|
290 |
} |
|
291 |
|
|
292 |
function tag_open( $parse, $tag, $attr ) { |
|
293 |
if ( in_array( $tag, $this->wp_tags ) ) { |
|
294 |
$this->in_tag = substr( $tag, 3 ); |
|
295 |
return; |
|
296 |
} |
|
297 |
|
|
298 |
if ( in_array( $tag, $this->wp_sub_tags ) ) { |
|
299 |
$this->in_sub_tag = substr( $tag, 3 ); |
|
300 |
return; |
|
301 |
} |
|
302 |
|
|
303 |
switch ( $tag ) { |
|
304 |
case 'category': |
|
305 |
if ( isset($attr['domain'], $attr['nicename']) ) { |
|
306 |
$this->sub_data['domain'] = $attr['domain']; |
|
307 |
$this->sub_data['slug'] = $attr['nicename']; |
|
308 |
} |
|
309 |
break; |
|
310 |
case 'item': $this->in_post = true; |
|
311 |
case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break; |
|
312 |
case 'guid': $this->in_tag = 'guid'; break; |
|
313 |
case 'dc:creator': $this->in_tag = 'post_author'; break; |
|
314 |
case 'content:encoded': $this->in_tag = 'post_content'; break; |
|
315 |
case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break; |
|
316 |
|
|
317 |
case 'wp:term_slug': $this->in_tag = 'slug'; break; |
|
318 |
case 'wp:meta_key': $this->in_sub_tag = 'key'; break; |
|
319 |
case 'wp:meta_value': $this->in_sub_tag = 'value'; break; |
|
320 |
} |
|
321 |
} |
|
322 |
|
|
323 |
function cdata( $parser, $cdata ) { |
|
324 |
if ( ! trim( $cdata ) ) |
|
325 |
return; |
|
326 |
|
|
327 |
$this->cdata .= trim( $cdata ); |
|
328 |
} |
|
329 |
|
|
330 |
function tag_close( $parser, $tag ) { |
|
331 |
switch ( $tag ) { |
|
332 |
case 'wp:comment': |
|
333 |
unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data |
|
334 |
if ( ! empty( $this->sub_data ) ) |
|
335 |
$this->data['comments'][] = $this->sub_data; |
|
336 |
$this->sub_data = false; |
|
337 |
break; |
|
338 |
case 'wp:commentmeta': |
|
339 |
$this->sub_data['commentmeta'][] = array( |
|
340 |
'key' => $this->sub_data['key'], |
|
341 |
'value' => $this->sub_data['value'] |
|
342 |
); |
|
343 |
break; |
|
344 |
case 'category': |
|
345 |
if ( ! empty( $this->sub_data ) ) { |
|
346 |
$this->sub_data['name'] = $this->cdata; |
|
347 |
$this->data['terms'][] = $this->sub_data; |
|
348 |
} |
|
349 |
$this->sub_data = false; |
|
350 |
break; |
|
351 |
case 'wp:postmeta': |
|
352 |
if ( ! empty( $this->sub_data ) ) |
|
353 |
$this->data['postmeta'][] = $this->sub_data; |
|
354 |
$this->sub_data = false; |
|
355 |
break; |
|
356 |
case 'item': |
|
357 |
$this->posts[] = $this->data; |
|
358 |
$this->data = false; |
|
359 |
break; |
|
360 |
case 'wp:category': |
|
361 |
case 'wp:tag': |
|
362 |
case 'wp:term': |
|
363 |
$n = substr( $tag, 3 ); |
|
364 |
array_push( $this->$n, $this->data ); |
|
365 |
$this->data = false; |
|
366 |
break; |
|
367 |
case 'wp:author': |
|
368 |
if ( ! empty($this->data['author_login']) ) |
|
369 |
$this->authors[$this->data['author_login']] = $this->data; |
|
370 |
$this->data = false; |
|
371 |
break; |
|
372 |
case 'wp:base_site_url': |
|
373 |
$this->base_url = $this->cdata; |
|
374 |
break; |
|
375 |
case 'wp:wxr_version': |
|
376 |
$this->wxr_version = $this->cdata; |
|
377 |
break; |
|
378 |
|
|
379 |
default: |
|
380 |
if ( $this->in_sub_tag ) { |
|
381 |
$this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
382 |
$this->in_sub_tag = false; |
|
383 |
} else if ( $this->in_tag ) { |
|
384 |
$this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
385 |
$this->in_tag = false; |
|
386 |
} |
|
387 |
} |
|
388 |
|
|
389 |
$this->cdata = false; |
|
390 |
} |
|
391 |
} |
|
392 |
|
|
393 |
/** |
|
394 |
* WXR Parser that uses regular expressions. Fallback for installs without an XML parser. |
|
395 |
*/ |
|
396 |
class WXR_Parser_Regex { |
|
397 |
var $authors = array(); |
|
398 |
var $posts = array(); |
|
399 |
var $categories = array(); |
|
400 |
var $tags = array(); |
|
401 |
var $terms = array(); |
|
402 |
var $base_url = ''; |
|
403 |
|
|
404 |
function WXR_Parser_Regex() { |
|
405 |
$this->__construct(); |
|
406 |
} |
|
407 |
|
|
408 |
function __construct() { |
|
409 |
$this->has_gzip = is_callable( 'gzopen' ); |
|
410 |
} |
|
411 |
|
|
412 |
function parse( $file ) { |
|
413 |
$wxr_version = $in_post = false; |
|
414 |
|
|
415 |
$fp = $this->fopen( $file, 'r' ); |
|
416 |
if ( $fp ) { |
|
417 |
while ( ! $this->feof( $fp ) ) { |
|
418 |
$importline = rtrim( $this->fgets( $fp ) ); |
|
419 |
|
|
420 |
if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) |
|
421 |
$wxr_version = $version[1]; |
|
422 |
|
|
423 |
if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) { |
|
424 |
preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url ); |
|
425 |
$this->base_url = $url[1]; |
|
426 |
continue; |
|
427 |
} |
|
428 |
if ( false !== strpos( $importline, '<wp:category>' ) ) { |
|
429 |
preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category ); |
|
430 |
$this->categories[] = $this->process_category( $category[1] ); |
|
431 |
continue; |
|
432 |
} |
|
433 |
if ( false !== strpos( $importline, '<wp:tag>' ) ) { |
|
434 |
preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag ); |
|
435 |
$this->tags[] = $this->process_tag( $tag[1] ); |
|
436 |
continue; |
|
437 |
} |
|
438 |
if ( false !== strpos( $importline, '<wp:term>' ) ) { |
|
439 |
preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term ); |
|
440 |
$this->terms[] = $this->process_term( $term[1] ); |
|
441 |
continue; |
|
442 |
} |
|
443 |
if ( false !== strpos( $importline, '<wp:author>' ) ) { |
|
444 |
preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
|
445 |
$a = $this->process_author( $author[1] ); |
|
446 |
$this->authors[$a['author_login']] = $a; |
|
447 |
continue; |
|
448 |
} |
|
449 |
if ( false !== strpos( $importline, '<item>' ) ) { |
|
450 |
$post = ''; |
|
451 |
$in_post = true; |
|
452 |
continue; |
|
453 |
} |
|
454 |
if ( false !== strpos( $importline, '</item>' ) ) { |
|
455 |
$in_post = false; |
|
456 |
$this->posts[] = $this->process_post( $post ); |
|
457 |
continue; |
|
458 |
} |
|
459 |
if ( $in_post ) { |
|
460 |
$post .= $importline . "\n"; |
|
461 |
} |
|
462 |
} |
|
463 |
|
|
464 |
$this->fclose($fp); |
|
465 |
} |
|
466 |
|
|
467 |
if ( ! $wxr_version ) |
|
468 |
return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
469 |
|
|
470 |
return array( |
|
471 |
'authors' => $this->authors, |
|
472 |
'posts' => $this->posts, |
|
473 |
'categories' => $this->categories, |
|
474 |
'tags' => $this->tags, |
|
475 |
'terms' => $this->terms, |
|
476 |
'base_url' => $this->base_url, |
|
477 |
'version' => $wxr_version |
|
478 |
); |
|
479 |
} |
|
480 |
|
|
481 |
function get_tag( $string, $tag ) { |
|
482 |
preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
|
483 |
if ( isset( $return[1] ) ) { |
|
484 |
if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) { |
|
485 |
if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) { |
|
486 |
preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches ); |
|
487 |
$return = ''; |
|
488 |
foreach( $matches[1] as $match ) |
|
489 |
$return .= $match; |
|
490 |
} else { |
|
491 |
$return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] ); |
|
492 |
} |
|
493 |
} else { |
|
494 |
$return = $return[1]; |
|
495 |
} |
|
496 |
} else { |
|
497 |
$return = ''; |
|
498 |
} |
|
499 |
return $return; |
|
500 |
} |
|
501 |
|
|
502 |
function process_category( $c ) { |
|
503 |
return array( |
|
504 |
'term_id' => $this->get_tag( $c, 'wp:term_id' ), |
|
505 |
'cat_name' => $this->get_tag( $c, 'wp:cat_name' ), |
|
506 |
'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ), |
|
507 |
'category_parent' => $this->get_tag( $c, 'wp:category_parent' ), |
|
508 |
'category_description' => $this->get_tag( $c, 'wp:category_description' ), |
|
509 |
); |
|
510 |
} |
|
511 |
|
|
512 |
function process_tag( $t ) { |
|
513 |
return array( |
|
514 |
'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
515 |
'tag_name' => $this->get_tag( $t, 'wp:tag_name' ), |
|
516 |
'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ), |
|
517 |
'tag_description' => $this->get_tag( $t, 'wp:tag_description' ), |
|
518 |
); |
|
519 |
} |
|
520 |
|
|
521 |
function process_term( $t ) { |
|
522 |
return array( |
|
523 |
'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
524 |
'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ), |
|
525 |
'slug' => $this->get_tag( $t, 'wp:term_slug' ), |
|
526 |
'term_parent' => $this->get_tag( $t, 'wp:term_parent' ), |
|
527 |
'term_name' => $this->get_tag( $t, 'wp:term_name' ), |
|
528 |
'term_description' => $this->get_tag( $t, 'wp:term_description' ), |
|
529 |
); |
|
530 |
} |
|
531 |
|
|
532 |
function process_author( $a ) { |
|
533 |
return array( |
|
534 |
'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
|
535 |
'author_login' => $this->get_tag( $a, 'wp:author_login' ), |
|
536 |
'author_email' => $this->get_tag( $a, 'wp:author_email' ), |
|
537 |
'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ), |
|
538 |
'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), |
|
539 |
'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), |
|
540 |
); |
|
541 |
} |
|
542 |
|
|
543 |
function process_post( $post ) { |
|
544 |
$post_id = $this->get_tag( $post, 'wp:post_id' ); |
|
545 |
$post_title = $this->get_tag( $post, 'title' ); |
|
546 |
$post_date = $this->get_tag( $post, 'wp:post_date' ); |
|
547 |
$post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); |
|
548 |
$comment_status = $this->get_tag( $post, 'wp:comment_status' ); |
|
549 |
$ping_status = $this->get_tag( $post, 'wp:ping_status' ); |
|
550 |
$status = $this->get_tag( $post, 'wp:status' ); |
|
551 |
$post_name = $this->get_tag( $post, 'wp:post_name' ); |
|
552 |
$post_parent = $this->get_tag( $post, 'wp:post_parent' ); |
|
553 |
$menu_order = $this->get_tag( $post, 'wp:menu_order' ); |
|
554 |
$post_type = $this->get_tag( $post, 'wp:post_type' ); |
|
555 |
$post_password = $this->get_tag( $post, 'wp:post_password' ); |
|
556 |
$is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); |
|
557 |
$guid = $this->get_tag( $post, 'guid' ); |
|
558 |
$post_author = $this->get_tag( $post, 'dc:creator' ); |
|
559 |
|
|
560 |
$post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
|
561 |
$post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt ); |
|
562 |
$post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
|
563 |
$post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt ); |
|
564 |
|
|
565 |
$post_content = $this->get_tag( $post, 'content:encoded' ); |
|
566 |
$post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content ); |
|
567 |
$post_content = str_replace( '<br>', '<br />', $post_content ); |
|
568 |
$post_content = str_replace( '<hr>', '<hr />', $post_content ); |
|
569 |
|
|
570 |
$postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', |
|
571 |
'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', |
|
572 |
'menu_order', 'post_type', 'post_password', 'is_sticky' |
|
573 |
); |
|
574 |
|
|
575 |
$attachment_url = $this->get_tag( $post, 'wp:attachment_url' ); |
|
576 |
if ( $attachment_url ) |
|
577 |
$postdata['attachment_url'] = $attachment_url; |
|
578 |
|
|
579 |
preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
|
580 |
foreach ( $terms as $t ) { |
|
581 |
$post_terms[] = array( |
|
582 |
'slug' => $t[2], |
|
583 |
'domain' => $t[1], |
|
584 |
'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ), |
|
585 |
); |
|
586 |
} |
|
587 |
if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms; |
|
588 |
|
|
589 |
preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments ); |
|
590 |
$comments = $comments[1]; |
|
591 |
if ( $comments ) { |
|
592 |
foreach ( $comments as $comment ) { |
|
593 |
preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta ); |
|
594 |
$commentmeta = $commentmeta[1]; |
|
595 |
$c_meta = array(); |
|
596 |
foreach ( $commentmeta as $m ) { |
|
597 |
$c_meta[] = array( |
|
598 |
'key' => $this->get_tag( $m, 'wp:meta_key' ), |
|
599 |
'value' => $this->get_tag( $m, 'wp:meta_value' ), |
|
600 |
); |
|
601 |
} |
|
602 |
|
|
603 |
$post_comments[] = array( |
|
604 |
'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), |
|
605 |
'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), |
|
606 |
'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
|
607 |
'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), |
|
608 |
'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), |
|
609 |
'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), |
|
610 |
'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), |
|
611 |
'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), |
|
612 |
'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), |
|
613 |
'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), |
|
614 |
'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), |
|
615 |
'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), |
|
616 |
'commentmeta' => $c_meta, |
|
617 |
); |
|
618 |
} |
|
619 |
} |
|
620 |
if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments; |
|
621 |
|
|
622 |
preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta ); |
|
623 |
$postmeta = $postmeta[1]; |
|
624 |
if ( $postmeta ) { |
|
625 |
foreach ( $postmeta as $p ) { |
|
626 |
$post_postmeta[] = array( |
|
627 |
'key' => $this->get_tag( $p, 'wp:meta_key' ), |
|
628 |
'value' => $this->get_tag( $p, 'wp:meta_value' ), |
|
629 |
); |
|
630 |
} |
|
631 |
} |
|
632 |
if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta; |
|
633 |
|
|
634 |
return $postdata; |
|
635 |
} |
|
636 |
|
|
637 |
function _normalize_tag( $matches ) { |
|
638 |
return '<' . strtolower( $matches[1] ); |
|
639 |
} |
|
640 |
|
|
641 |
function fopen( $filename, $mode = 'r' ) { |
|
642 |
if ( $this->has_gzip ) |
|
643 |
return gzopen( $filename, $mode ); |
|
644 |
return fopen( $filename, $mode ); |
|
645 |
} |
|
646 |
|
|
647 |
function feof( $fp ) { |
|
648 |
if ( $this->has_gzip ) |
|
649 |
return gzeof( $fp ); |
|
650 |
return feof( $fp ); |
|
651 |
} |
|
652 |
|
|
653 |
function fgets( $fp, $len = 8192 ) { |
|
654 |
if ( $this->has_gzip ) |
|
655 |
return gzgets( $fp, $len ); |
|
656 |
return fgets( $fp, $len ); |
|
657 |
} |
|
658 |
|
|
659 |
function fclose( $fp ) { |
|
660 |
if ( $this->has_gzip ) |
|
661 |
return gzclose( $fp ); |
|
662 |
return fclose( $fp ); |
|
663 |
} |
|
664 |
} |