/wp-content/plugins/wordpress-importer/parsers/class-wxr-parser-simplexml.php

https://github.com/livinglab/openlab · PHP · 231 lines · 184 code · 33 blank · 14 comment · 14 complexity · b5fe6c9a8239c9c73a57e76e1bb0f40e MD5 · raw file

  1. <?php
  2. /**
  3. * WordPress eXtended RSS file parser implementations
  4. *
  5. * @package WordPress
  6. * @subpackage Importer
  7. */
  8. /**
  9. * WXR Parser that makes use of the SimpleXML PHP extension.
  10. */
  11. class WXR_Parser_SimpleXML {
  12. function parse( $file ) {
  13. $authors = $posts = $categories = $tags = $terms = array();
  14. $internal_errors = libxml_use_internal_errors(true);
  15. $dom = new DOMDocument;
  16. $old_value = null;
  17. if ( function_exists( 'libxml_disable_entity_loader' ) ) {
  18. $old_value = libxml_disable_entity_loader( true );
  19. }
  20. $success = $dom->loadXML( file_get_contents( $file ) );
  21. if ( ! is_null( $old_value ) ) {
  22. libxml_disable_entity_loader( $old_value );
  23. }
  24. if ( ! $success || isset( $dom->doctype ) ) {
  25. return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
  26. }
  27. $xml = simplexml_import_dom( $dom );
  28. unset( $dom );
  29. // halt if loading produces an error
  30. if ( ! $xml )
  31. return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
  32. $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
  33. if ( ! $wxr_version )
  34. return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
  35. $wxr_version = (string) trim( $wxr_version[0] );
  36. // confirm that we are dealing with the correct file format
  37. if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
  38. return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
  39. $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
  40. $base_url = (string) trim( isset( $base_url[0] ) ? $base_url[0] : '' );
  41. $base_blog_url = $xml->xpath('/rss/channel/wp:base_blog_url');
  42. if ( $base_blog_url ) {
  43. $base_blog_url = (string) trim( $base_blog_url[0] );
  44. } else {
  45. $base_blog_url = $base_url;
  46. }
  47. $namespaces = $xml->getDocNamespaces();
  48. if ( ! isset( $namespaces['wp'] ) )
  49. $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
  50. if ( ! isset( $namespaces['excerpt'] ) )
  51. $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
  52. // grab authors
  53. foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
  54. $a = $author_arr->children( $namespaces['wp'] );
  55. $login = (string) $a->author_login;
  56. $authors[$login] = array(
  57. 'author_id' => (int) $a->author_id,
  58. 'author_login' => $login,
  59. 'author_email' => (string) $a->author_email,
  60. 'author_display_name' => (string) $a->author_display_name,
  61. 'author_first_name' => (string) $a->author_first_name,
  62. 'author_last_name' => (string) $a->author_last_name
  63. );
  64. }
  65. // grab cats, tags and terms
  66. foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
  67. $t = $term_arr->children( $namespaces['wp'] );
  68. $category = array(
  69. 'term_id' => (int) $t->term_id,
  70. 'category_nicename' => (string) $t->category_nicename,
  71. 'category_parent' => (string) $t->category_parent,
  72. 'cat_name' => (string) $t->cat_name,
  73. 'category_description' => (string) $t->category_description
  74. );
  75. foreach ( $t->termmeta as $meta ) {
  76. $category['termmeta'][] = array(
  77. 'key' => (string) $meta->meta_key,
  78. 'value' => (string) $meta->meta_value
  79. );
  80. }
  81. $categories[] = $category;
  82. }
  83. foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
  84. $t = $term_arr->children( $namespaces['wp'] );
  85. $tag = array(
  86. 'term_id' => (int) $t->term_id,
  87. 'tag_slug' => (string) $t->tag_slug,
  88. 'tag_name' => (string) $t->tag_name,
  89. 'tag_description' => (string) $t->tag_description
  90. );
  91. foreach ( $t->termmeta as $meta ) {
  92. $tag['termmeta'][] = array(
  93. 'key' => (string) $meta->meta_key,
  94. 'value' => (string) $meta->meta_value
  95. );
  96. }
  97. $tags[] = $tag;
  98. }
  99. foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
  100. $t = $term_arr->children( $namespaces['wp'] );
  101. $term = array(
  102. 'term_id' => (int) $t->term_id,
  103. 'term_taxonomy' => (string) $t->term_taxonomy,
  104. 'slug' => (string) $t->term_slug,
  105. 'term_parent' => (string) $t->term_parent,
  106. 'term_name' => (string) $t->term_name,
  107. 'term_description' => (string) $t->term_description
  108. );
  109. foreach ( $t->termmeta as $meta ) {
  110. $term['termmeta'][] = array(
  111. 'key' => (string) $meta->meta_key,
  112. 'value' => (string) $meta->meta_value
  113. );
  114. }
  115. $terms[] = $term;
  116. }
  117. // grab posts
  118. foreach ( $xml->channel->item as $item ) {
  119. $post = array(
  120. 'post_title' => (string) $item->title,
  121. 'guid' => (string) $item->guid,
  122. );
  123. $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
  124. $post['post_author'] = (string) $dc->creator;
  125. $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
  126. $excerpt = $item->children( $namespaces['excerpt'] );
  127. $post['post_content'] = (string) $content->encoded;
  128. $post['post_excerpt'] = (string) $excerpt->encoded;
  129. $wp = $item->children( $namespaces['wp'] );
  130. $post['post_id'] = (int) $wp->post_id;
  131. $post['post_date'] = (string) $wp->post_date;
  132. $post['post_date_gmt'] = (string) $wp->post_date_gmt;
  133. $post['comment_status'] = (string) $wp->comment_status;
  134. $post['ping_status'] = (string) $wp->ping_status;
  135. $post['post_name'] = (string) $wp->post_name;
  136. $post['status'] = (string) $wp->status;
  137. $post['post_parent'] = (int) $wp->post_parent;
  138. $post['menu_order'] = (int) $wp->menu_order;
  139. $post['post_type'] = (string) $wp->post_type;
  140. $post['post_password'] = (string) $wp->post_password;
  141. $post['is_sticky'] = (int) $wp->is_sticky;
  142. if ( isset($wp->attachment_url) )
  143. $post['attachment_url'] = (string) $wp->attachment_url;
  144. foreach ( $item->category as $c ) {
  145. $att = $c->attributes();
  146. if ( isset( $att['nicename'] ) )
  147. $post['terms'][] = array(
  148. 'name' => (string) $c,
  149. 'slug' => (string) $att['nicename'],
  150. 'domain' => (string) $att['domain']
  151. );
  152. }
  153. foreach ( $wp->postmeta as $meta ) {
  154. $post['postmeta'][] = array(
  155. 'key' => (string) $meta->meta_key,
  156. 'value' => (string) $meta->meta_value
  157. );
  158. }
  159. foreach ( $wp->comment as $comment ) {
  160. $meta = array();
  161. if ( isset( $comment->commentmeta ) ) {
  162. foreach ( $comment->commentmeta as $m ) {
  163. $meta[] = array(
  164. 'key' => (string) $m->meta_key,
  165. 'value' => (string) $m->meta_value
  166. );
  167. }
  168. }
  169. $post['comments'][] = array(
  170. 'comment_id' => (int) $comment->comment_id,
  171. 'comment_author' => (string) $comment->comment_author,
  172. 'comment_author_email' => (string) $comment->comment_author_email,
  173. 'comment_author_IP' => (string) $comment->comment_author_IP,
  174. 'comment_author_url' => (string) $comment->comment_author_url,
  175. 'comment_date' => (string) $comment->comment_date,
  176. 'comment_date_gmt' => (string) $comment->comment_date_gmt,
  177. 'comment_content' => (string) $comment->comment_content,
  178. 'comment_approved' => (string) $comment->comment_approved,
  179. 'comment_type' => (string) $comment->comment_type,
  180. 'comment_parent' => (string) $comment->comment_parent,
  181. 'comment_user_id' => (int) $comment->comment_user_id,
  182. 'commentmeta' => $meta,
  183. );
  184. }
  185. $posts[] = $post;
  186. }
  187. return array(
  188. 'authors' => $authors,
  189. 'posts' => $posts,
  190. 'categories' => $categories,
  191. 'tags' => $tags,
  192. 'terms' => $terms,
  193. 'base_url' => $base_url,
  194. 'base_blog_url' => $base_blog_url,
  195. 'version' => $wxr_version
  196. );
  197. }
  198. }