parsers.php 24 KB

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