parsers.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * WordPress eXtended RSS file parser implementations
  4. *
  5. * @package Importer
  6. */
  7. function vamtam_get_export_data( $file ) {
  8. $replaced_contents = file_get_contents( $file );
  9. return str_replace( '{WPV:SAMPLES_URI}', VAMTAM_SAMPLES_URI , $replaced_contents );
  10. }
  11. /**
  12. * WordPress Importer class for managing parsing of WXR files.
  13. */
  14. class VAMTAM_WXR_Parser {
  15. function parse( $file ) {
  16. // use regular expressions if nothing else available or this is bad XML
  17. $parser = new VAMTAM_WXR_Parser_Regex;
  18. return $parser->parse( $file );
  19. }
  20. }
  21. /**
  22. * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
  23. */
  24. class VAMTAM_WXR_Parser_Regex {
  25. var $authors = array();
  26. var $posts = array();
  27. var $categories = array();
  28. var $tags = array();
  29. var $terms = array();
  30. var $base_url = '';
  31. function __construct() {
  32. $this->has_gzip = is_callable( 'gzopen' );
  33. }
  34. function parse( $file ) {
  35. $wxr_version = $frontpage = $woocommerce_pages = false;
  36. $in_multiline = false;
  37. $multiline_content = '';
  38. $multiline_tags = array(
  39. 'wp:term' => array( 'terms', array( $this, 'process_term' ) ),
  40. 'wp:category' => array( 'categories', array( $this, 'process_category' ) ),
  41. 'wp:tag' => array( 'tags', array( $this, 'process_tag' ) ),
  42. 'item' => array( 'posts', array( $this, 'process_post' ) ),
  43. );
  44. $fp = $this->fopen( $file, 'r' );
  45. if ( $fp ) {
  46. while ( ! $this->feof( $fp ) ) {
  47. $importline = $this->fgets( $fp );
  48. $importline = str_replace( '{WPV:SAMPLES_URI}', VAMTAM_SAMPLES_URI , $importline );
  49. if ( ! $frontpage ) {
  50. preg_match( '|<wpv:frontpage.*?>(\d*?)</wpv:frontpage>|is', $importline, $frontpage );
  51. if ( isset( $frontpage[1] ) ) {
  52. $frontpage = $frontpage[1];
  53. }
  54. }
  55. if ( ! $woocommerce_pages ) {
  56. preg_match( '|<wpv:woocommerce_pages.*?><!\[CDATA\[(.*?)\]\]></wpv:woocommerce_pages>|is', $importline, $woocommerce_pages );
  57. if ( isset( $woocommerce_pages[1] ) ) {
  58. $woocommerce_pages = $woocommerce_pages[1];
  59. }
  60. }
  61. if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
  62. $wxr_version = $version[1];
  63. if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
  64. preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
  65. $this->base_url = $url[1];
  66. continue;
  67. }
  68. if ( false !== strpos( $importline, '<wp:author>' ) ) {
  69. preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
  70. $a = $this->process_author( $author[1] );
  71. $this->authors[$a['author_login']] = $a;
  72. continue;
  73. }
  74. foreach ( $multiline_tags as $tag => $handler ) {
  75. if ( false !== strpos( $importline, "<$tag>" ) ) {
  76. $multiline_content = '';
  77. $in_multiline = true;
  78. break;
  79. }
  80. if ( false !== strpos( $importline, "</$tag>" ) ) {
  81. $in_multiline = false;
  82. $this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content );
  83. break;
  84. }
  85. }
  86. if ( $in_multiline ) {
  87. $multiline_content .= $importline;
  88. }
  89. }
  90. $this->fclose( $fp );
  91. if ( class_exists( 'FLBuilderImporterDataFix' ) ) {
  92. // Try to fix any broken builder data.
  93. foreach ( $this->posts as $post_index => $post ) {
  94. if ( ! isset( $post['postmeta'] ) || ! is_array( $post['postmeta'] ) ) {
  95. continue;
  96. }
  97. foreach ( $post['postmeta'] as $postmeta_index => $postmeta ) {
  98. if ( stristr( $postmeta['key'], '_fl_builder_' ) ) {
  99. $this->posts[ $post_index ]['postmeta'][ $postmeta_index ]['value'] = Vamtam_Importers::fix_serialized( $postmeta['value'] );
  100. }
  101. }
  102. }
  103. }
  104. }
  105. if ( ! $wxr_version )
  106. return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
  107. return array(
  108. 'authors' => $this->authors,
  109. 'posts' => $this->posts,
  110. 'categories' => $this->categories,
  111. 'tags' => $this->tags,
  112. 'terms' => $this->terms,
  113. 'base_url' => $this->base_url,
  114. 'version' => $wxr_version,
  115. 'frontpage' => $frontpage,
  116. 'woocommerce_pages' => $woocommerce_pages,
  117. );
  118. }
  119. function get_tag( $string, $tag ) {
  120. preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
  121. if ( isset( $return[1] ) ) {
  122. if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
  123. if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
  124. preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
  125. $return = '';
  126. foreach( $matches[1] as $match )
  127. $return .= $match;
  128. } else {
  129. $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
  130. }
  131. } else {
  132. $return = $return[1];
  133. }
  134. } else {
  135. $return = '';
  136. }
  137. return $return;
  138. }
  139. function process_category( $c ) {
  140. return array(
  141. 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
  142. 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
  143. 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
  144. 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
  145. 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
  146. );
  147. }
  148. function process_tag( $t ) {
  149. return array(
  150. 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
  151. 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
  152. 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
  153. 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
  154. );
  155. }
  156. function process_term( $t ) {
  157. return array(
  158. 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
  159. 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
  160. 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
  161. 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
  162. 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
  163. 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
  164. );
  165. }
  166. function process_author( $a ) {
  167. return array(
  168. 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
  169. 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
  170. 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
  171. 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
  172. 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
  173. 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
  174. );
  175. }
  176. function process_post( $post ) {
  177. $post_id = $this->get_tag( $post, 'wp:post_id' );
  178. $post_title = $this->get_tag( $post, 'title' );
  179. $post_date = $this->get_tag( $post, 'wp:post_date' );
  180. $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
  181. $comment_status = $this->get_tag( $post, 'wp:comment_status' );
  182. $ping_status = $this->get_tag( $post, 'wp:ping_status' );
  183. $status = $this->get_tag( $post, 'wp:status' );
  184. $post_name = $this->get_tag( $post, 'wp:post_name' );
  185. $post_parent = $this->get_tag( $post, 'wp:post_parent' );
  186. $menu_order = $this->get_tag( $post, 'wp:menu_order' );
  187. $post_type = $this->get_tag( $post, 'wp:post_type' );
  188. $post_password = $this->get_tag( $post, 'wp:post_password' );
  189. $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
  190. $guid = $this->get_tag( $post, 'guid' );
  191. $post_author = $this->get_tag( $post, 'dc:creator' );
  192. $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
  193. $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
  194. $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
  195. $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
  196. $post_content = $this->get_tag( $post, 'content:encoded' );
  197. $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
  198. $post_content = str_replace( '<br>', '<br />', $post_content );
  199. $post_content = str_replace( '<hr>', '<hr />', $post_content );
  200. $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
  201. 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
  202. 'menu_order', 'post_type', 'post_password', 'is_sticky'
  203. );
  204. $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
  205. if ( $attachment_url )
  206. $postdata['attachment_url'] = $attachment_url;
  207. preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
  208. foreach ( $terms as $t ) {
  209. $post_terms[] = array(
  210. 'slug' => $t[2],
  211. 'domain' => $t[1],
  212. 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
  213. );
  214. }
  215. if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
  216. preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
  217. $comments = $comments[1];
  218. if ( $comments ) {
  219. foreach ( $comments as $comment ) {
  220. preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta );
  221. $commentmeta = $commentmeta[1];
  222. $c_meta = array();
  223. foreach ( $commentmeta as $m ) {
  224. $c_meta[] = array(
  225. 'key' => $this->get_tag( $m, 'wp:meta_key' ),
  226. 'value' => $this->get_tag( $m, 'wp:meta_value' ),
  227. );
  228. }
  229. $post_comments[] = array(
  230. 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
  231. 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
  232. 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
  233. 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
  234. 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
  235. 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
  236. 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
  237. 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
  238. 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
  239. 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
  240. 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
  241. 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
  242. 'commentmeta' => $c_meta,
  243. );
  244. }
  245. }
  246. if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments;
  247. preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta );
  248. $postmeta = $postmeta[1];
  249. if ( $postmeta ) {
  250. foreach ( $postmeta as $p ) {
  251. $post_postmeta[] = array(
  252. 'key' => $this->get_tag( $p, 'wp:meta_key' ),
  253. 'value' => $this->get_tag( $p, 'wp:meta_value' ),
  254. );
  255. }
  256. }
  257. if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta;
  258. return $postdata;
  259. }
  260. function _normalize_tag( $matches ) {
  261. return '<' . strtolower( $matches[1] );
  262. }
  263. function fopen( $filename, $mode = 'r' ) {
  264. if ( $this->has_gzip )
  265. return gzopen( $filename, $mode );
  266. return fopen( $filename, $mode );
  267. }
  268. function feof( $fp ) {
  269. if ( $this->has_gzip )
  270. return gzeof( $fp );
  271. return feof( $fp );
  272. }
  273. function fgets( $fp ) {
  274. if ( $this->has_gzip )
  275. return gzgets( $fp );
  276. return fgets( $fp );
  277. }
  278. function fclose( $fp ) {
  279. if ( $this->has_gzip )
  280. return gzclose( $fp );
  281. return fclose( $fp );
  282. }
  283. }