parsers.php 23 KB

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