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