<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[2133] 2013/frederickding/importer/trunk: Rename parsers file to `class-wxr-parser.php` based on standard practices.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://gsoc.trac.wordpress.org/changeset/2133">2133</a></dd>
<dt>Author</dt> <dd>frederick.ding</dd>
<dt>Date</dt> <dd>2013-07-18 21:51:59 +0000 (Thu, 18 Jul 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Rename parsers file to `class-wxr-parser.php` based on standard practices.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#2013frederickdingimportertrunkwordpressimporterphp">2013/frederickding/importer/trunk/wordpress-importer.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#2013frederickdingimportertrunkclasswxrparserphp">2013/frederickding/importer/trunk/class-wxr-parser.php</a></li>
</ul>
<h3>Removed Paths</h3>
<ul>
<li><a href="#2013frederickdingimportertrunkparsersphp">2013/frederickding/importer/trunk/parsers.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="2013frederickdingimportertrunkclasswxrparserphpfromrev21322013frederickdingimportertrunkparsersphp"></a>
<div class="copfile"><h4>Copied: 2013/frederickding/importer/trunk/class-wxr-parser.php (from rev 2132, 2013/frederickding/importer/trunk/parsers.php) (0 => 2133)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/frederickding/importer/trunk/class-wxr-parser.php (rev 0)
+++ 2013/frederickding/importer/trunk/class-wxr-parser.php 2013-07-18 21:51:59 UTC (rev 2133)
</span><span class="lines">@@ -0,0 +1,664 @@
</span><ins>+<?php
+/**
+ * WordPress eXtended RSS file parser implementations
+ *
+ * @package WordPress
+ * @subpackage Importer
+ */
+
+/**
+ * WordPress Importer class for managing parsing of WXR files.
+ */
+class WXR_Parser {
+ function parse( $file ) {
+ // Attempt to use proper XML parsers first
+ if ( extension_loaded( 'simplexml' ) ) {
+ $parser = new WXR_Parser_SimpleXML;
+ $result = $parser->parse( $file );
+
+ // If SimpleXML succeeds or this is an invalid WXR file then return the results
+ if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() )
+ return $result;
+ } else if ( extension_loaded( 'xml' ) ) {
+ $parser = new WXR_Parser_XML;
+ $result = $parser->parse( $file );
+
+ // If XMLParser succeeds or this is an invalid WXR file then return the results
+ if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() )
+ return $result;
+ }
+
+ // We have a malformed XML file, so display the error and fallthrough to regex
+ if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) {
+ echo '<pre>';
+ if ( 'SimpleXML_parse_error' == $result->get_error_code() ) {
+ foreach ( $result->get_error_data() as $error )
+ echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n";
+ } else if ( 'XML_parse_error' == $result->get_error_code() ) {
+ $error = $result->get_error_data();
+ echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] );
+ }
+ echo '</pre>';
+ echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />';
+ echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>';
+ }
+
+ // use regular expressions if nothing else available or this is bad XML
+ $parser = new WXR_Parser_Regex;
+ return $parser->parse( $file );
+ }
+}
+
+/**
+ * WXR Parser that makes use of the SimpleXML PHP extension.
+ */
+class WXR_Parser_SimpleXML {
+ function parse( $file ) {
+ $authors = $posts = $categories = $tags = $terms = array();
+
+ $internal_errors = libxml_use_internal_errors(true);
+
+ $dom = new DOMDocument;
+ $old_value = null;
+ if ( function_exists( 'libxml_disable_entity_loader' ) ) {
+ $old_value = libxml_disable_entity_loader( true );
+ }
+ $success = $dom->loadXML( file_get_contents( $file ) );
+ if ( ! is_null( $old_value ) ) {
+ libxml_disable_entity_loader( $old_value );
+ }
+
+ if ( ! $success || isset( $dom->doctype ) ) {
+ return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
+ }
+
+ $xml = simplexml_import_dom( $dom );
+ unset( $dom );
+
+ // halt if loading produces an error
+ if ( ! $xml )
+ return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
+
+ $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
+ if ( ! $wxr_version )
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
+
+ $wxr_version = (string) trim( $wxr_version[0] );
+ // confirm that we are dealing with the correct file format
+ if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
+
+ $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
+ $base_url = (string) trim( $base_url[0] );
+
+ $namespaces = $xml->getDocNamespaces();
+ if ( ! isset( $namespaces['wp'] ) )
+ $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
+ if ( ! isset( $namespaces['excerpt'] ) )
+ $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
+
+ // grab authors
+ foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
+ $a = $author_arr->children( $namespaces['wp'] );
+ $login = (string) $a->author_login;
+ $authors[$login] = array(
+ 'author_id' => (int) $a->author_id,
+ 'author_login' => $login,
+ 'author_email' => (string) $a->author_email,
+ 'author_display_name' => (string) $a->author_display_name,
+ 'author_first_name' => (string) $a->author_first_name,
+ 'author_last_name' => (string) $a->author_last_name
+ );
+ }
+
+ // grab cats, tags and terms
+ foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
+ $t = $term_arr->children( $namespaces['wp'] );
+ $categories[] = array(
+ 'term_id' => (int) $t->term_id,
+ 'category_nicename' => (string) $t->category_nicename,
+ 'category_parent' => (string) $t->category_parent,
+ 'cat_name' => (string) $t->cat_name,
+ 'category_description' => (string) $t->category_description
+ );
+ }
+
+ foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
+ $t = $term_arr->children( $namespaces['wp'] );
+ $tags[] = array(
+ 'term_id' => (int) $t->term_id,
+ 'tag_slug' => (string) $t->tag_slug,
+ 'tag_name' => (string) $t->tag_name,
+ 'tag_description' => (string) $t->tag_description
+ );
+ }
+
+ foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
+ $t = $term_arr->children( $namespaces['wp'] );
+ $terms[] = array(
+ 'term_id' => (int) $t->term_id,
+ 'term_taxonomy' => (string) $t->term_taxonomy,
+ 'slug' => (string) $t->term_slug,
+ 'term_parent' => (string) $t->term_parent,
+ 'term_name' => (string) $t->term_name,
+ 'term_description' => (string) $t->term_description
+ );
+ }
+
+ // grab posts
+ foreach ( $xml->channel->item as $item ) {
+ $post = array(
+ 'post_title' => (string) $item->title,
+ 'guid' => (string) $item->guid,
+ );
+
+ $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
+ $post['post_author'] = (string) $dc->creator;
+
+ $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
+ $excerpt = $item->children( $namespaces['excerpt'] );
+ $post['post_content'] = (string) $content->encoded;
+ $post['post_excerpt'] = (string) $excerpt->encoded;
+
+ $wp = $item->children( $namespaces['wp'] );
+ $post['post_id'] = (int) $wp->post_id;
+ $post['post_date'] = (string) $wp->post_date;
+ $post['post_date_gmt'] = (string) $wp->post_date_gmt;
+ $post['comment_status'] = (string) $wp->comment_status;
+ $post['ping_status'] = (string) $wp->ping_status;
+ $post['post_name'] = (string) $wp->post_name;
+ $post['status'] = (string) $wp->status;
+ $post['post_parent'] = (int) $wp->post_parent;
+ $post['menu_order'] = (int) $wp->menu_order;
+ $post['post_type'] = (string) $wp->post_type;
+ $post['post_password'] = (string) $wp->post_password;
+ $post['is_sticky'] = (int) $wp->is_sticky;
+
+ if ( isset($wp->attachment_url) )
+ $post['attachment_url'] = (string) $wp->attachment_url;
+
+ foreach ( $item->category as $c ) {
+ $att = $c->attributes();
+ if ( isset( $att['nicename'] ) )
+ $post['terms'][] = array(
+ 'name' => (string) $c,
+ 'slug' => (string) $att['nicename'],
+ 'domain' => (string) $att['domain']
+ );
+ }
+
+ foreach ( $wp->postmeta as $meta ) {
+ $post['postmeta'][] = array(
+ 'key' => (string) $meta->meta_key,
+ 'value' => (string) $meta->meta_value
+ );
+ }
+
+ foreach ( $wp->comment as $comment ) {
+ $meta = array();
+ if ( isset( $comment->commentmeta ) ) {
+ foreach ( $comment->commentmeta as $m ) {
+ $meta[] = array(
+ 'key' => (string) $m->meta_key,
+ 'value' => (string) $m->meta_value
+ );
+ }
+ }
+
+ $post['comments'][] = array(
+ 'comment_id' => (int) $comment->comment_id,
+ 'comment_author' => (string) $comment->comment_author,
+ 'comment_author_email' => (string) $comment->comment_author_email,
+ 'comment_author_IP' => (string) $comment->comment_author_IP,
+ 'comment_author_url' => (string) $comment->comment_author_url,
+ 'comment_date' => (string) $comment->comment_date,
+ 'comment_date_gmt' => (string) $comment->comment_date_gmt,
+ 'comment_content' => (string) $comment->comment_content,
+ 'comment_approved' => (string) $comment->comment_approved,
+ 'comment_type' => (string) $comment->comment_type,
+ 'comment_parent' => (string) $comment->comment_parent,
+ 'comment_user_id' => (int) $comment->comment_user_id,
+ 'commentmeta' => $meta,
+ );
+ }
+
+ $posts[] = $post;
+ }
+
+ return array(
+ 'authors' => $authors,
+ 'posts' => $posts,
+ 'categories' => $categories,
+ 'tags' => $tags,
+ 'terms' => $terms,
+ 'base_url' => $base_url,
+ 'version' => $wxr_version
+ );
+ }
+}
+
+/**
+ * WXR Parser that makes use of the XML Parser PHP extension.
+ */
+class WXR_Parser_XML {
+ var $wp_tags = array(
+ 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url',
+ 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password',
+ 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description',
+ 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent',
+ 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name',
+ 'wp:author_first_name', 'wp:author_last_name',
+ );
+ var $wp_sub_tags = array(
+ 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url',
+ 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content',
+ 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id',
+ );
+
+ function parse( $file ) {
+ $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false;
+ $this->authors = $this->posts = $this->term = $this->category = $this->tag = array();
+
+ $xml = xml_parser_create( 'UTF-8' );
+ xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 );
+ xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 );
+ xml_set_object( $xml, $this );
+ xml_set_character_data_handler( $xml, 'cdata' );
+ xml_set_element_handler( $xml, 'tag_open', 'tag_close' );
+
+ if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) {
+ $current_line = xml_get_current_line_number( $xml );
+ $current_column = xml_get_current_column_number( $xml );
+ $error_code = xml_get_error_code( $xml );
+ $error_string = xml_error_string( $error_code );
+ return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) );
+ }
+ xml_parser_free( $xml );
+
+ if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) )
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
+
+ return array(
+ 'authors' => $this->authors,
+ 'posts' => $this->posts,
+ 'categories' => $this->category,
+ 'tags' => $this->tag,
+ 'terms' => $this->term,
+ 'base_url' => $this->base_url,
+ 'version' => $this->wxr_version
+ );
+ }
+
+ function tag_open( $parse, $tag, $attr ) {
+ if ( in_array( $tag, $this->wp_tags ) ) {
+ $this->in_tag = substr( $tag, 3 );
+ return;
+ }
+
+ if ( in_array( $tag, $this->wp_sub_tags ) ) {
+ $this->in_sub_tag = substr( $tag, 3 );
+ return;
+ }
+
+ switch ( $tag ) {
+ case 'category':
+ if ( isset($attr['domain'], $attr['nicename']) ) {
+ $this->sub_data['domain'] = $attr['domain'];
+ $this->sub_data['slug'] = $attr['nicename'];
+ }
+ break;
+ case 'item': $this->in_post = true;
+ case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break;
+ case 'guid': $this->in_tag = 'guid'; break;
+ case 'dc:creator': $this->in_tag = 'post_author'; break;
+ case 'content:encoded': $this->in_tag = 'post_content'; break;
+ case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break;
+
+ case 'wp:term_slug': $this->in_tag = 'slug'; break;
+ case 'wp:meta_key': $this->in_sub_tag = 'key'; break;
+ case 'wp:meta_value': $this->in_sub_tag = 'value'; break;
+ }
+ }
+
+ function cdata( $parser, $cdata ) {
+ if ( ! trim( $cdata ) )
+ return;
+
+ $this->cdata .= trim( $cdata );
+ }
+
+ function tag_close( $parser, $tag ) {
+ switch ( $tag ) {
+ case 'wp:comment':
+ unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data
+ if ( ! empty( $this->sub_data ) )
+ $this->data['comments'][] = $this->sub_data;
+ $this->sub_data = false;
+ break;
+ case 'wp:commentmeta':
+ $this->sub_data['commentmeta'][] = array(
+ 'key' => $this->sub_data['key'],
+ 'value' => $this->sub_data['value']
+ );
+ break;
+ case 'category':
+ if ( ! empty( $this->sub_data ) ) {
+ $this->sub_data['name'] = $this->cdata;
+ $this->data['terms'][] = $this->sub_data;
+ }
+ $this->sub_data = false;
+ break;
+ case 'wp:postmeta':
+ if ( ! empty( $this->sub_data ) )
+ $this->data['postmeta'][] = $this->sub_data;
+ $this->sub_data = false;
+ break;
+ case 'item':
+ $this->posts[] = $this->data;
+ $this->data = false;
+ break;
+ case 'wp:category':
+ case 'wp:tag':
+ case 'wp:term':
+ $n = substr( $tag, 3 );
+ array_push( $this->$n, $this->data );
+ $this->data = false;
+ break;
+ case 'wp:author':
+ if ( ! empty($this->data['author_login']) )
+ $this->authors[$this->data['author_login']] = $this->data;
+ $this->data = false;
+ break;
+ case 'wp:base_site_url':
+ $this->base_url = $this->cdata;
+ break;
+ case 'wp:wxr_version':
+ $this->wxr_version = $this->cdata;
+ break;
+
+ default:
+ if ( $this->in_sub_tag ) {
+ $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
+ $this->in_sub_tag = false;
+ } else if ( $this->in_tag ) {
+ $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
+ $this->in_tag = false;
+ }
+ }
+
+ $this->cdata = false;
+ }
+}
+
+/**
+ * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
+ */
+class WXR_Parser_Regex {
+ var $authors = array();
+ var $posts = array();
+ var $categories = array();
+ var $tags = array();
+ var $terms = array();
+ var $base_url = '';
+
+ function __construct() {
+ $this->has_gzip = is_callable( 'gzopen' );
+ }
+
+ function WXR_Parser_Regex() {
+ $this->__construct();
+ }
+
+ function parse( $file ) {
+ $wxr_version = $in_post = false;
+
+ $fp = $this->fopen( $file, 'r' );
+ if ( $fp ) {
+ while ( ! $this->feof( $fp ) ) {
+ $importline = rtrim( $this->fgets( $fp ) );
+
+ if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
+ $wxr_version = $version[1];
+
+ if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
+ preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
+ $this->base_url = $url[1];
+ continue;
+ }
+ if ( false !== strpos( $importline, '<wp:category>' ) ) {
+ preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category );
+ $this->categories[] = $this->process_category( $category[1] );
+ continue;
+ }
+ if ( false !== strpos( $importline, '<wp:tag>' ) ) {
+ preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag );
+ $this->tags[] = $this->process_tag( $tag[1] );
+ continue;
+ }
+ if ( false !== strpos( $importline, '<wp:term>' ) ) {
+ preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term );
+ $this->terms[] = $this->process_term( $term[1] );
+ continue;
+ }
+ if ( false !== strpos( $importline, '<wp:author>' ) ) {
+ preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
+ $a = $this->process_author( $author[1] );
+ $this->authors[$a['author_login']] = $a;
+ continue;
+ }
+ if ( false !== strpos( $importline, '<item>' ) ) {
+ $post = '';
+ $in_post = true;
+ continue;
+ }
+ if ( false !== strpos( $importline, '</item>' ) ) {
+ $in_post = false;
+ $this->posts[] = $this->process_post( $post );
+ continue;
+ }
+ if ( $in_post ) {
+ $post .= $importline . "\n";
+ }
+ }
+
+ $this->fclose($fp);
+ }
+
+ if ( ! $wxr_version )
+ return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
+
+ return array(
+ 'authors' => $this->authors,
+ 'posts' => $this->posts,
+ 'categories' => $this->categories,
+ 'tags' => $this->tags,
+ 'terms' => $this->terms,
+ 'base_url' => $this->base_url,
+ 'version' => $wxr_version
+ );
+ }
+
+ function get_tag( $string, $tag ) {
+ preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
+ if ( isset( $return[1] ) ) {
+ if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
+ if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
+ preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
+ $return = '';
+ foreach( $matches[1] as $match )
+ $return .= $match;
+ } else {
+ $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
+ }
+ } else {
+ $return = $return[1];
+ }
+ } else {
+ $return = '';
+ }
+ return $return;
+ }
+
+ function process_category( $c ) {
+ return array(
+ 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
+ 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
+ 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
+ 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
+ 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
+ );
+ }
+
+ function process_tag( $t ) {
+ return array(
+ 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
+ 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
+ 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
+ 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
+ );
+ }
+
+ function process_term( $t ) {
+ return array(
+ 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
+ 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
+ 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
+ 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
+ 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
+ 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
+ );
+ }
+
+ function process_author( $a ) {
+ return array(
+ 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
+ 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
+ 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
+ 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
+ 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
+ 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
+ );
+ }
+
+ function process_post( $post ) {
+ $post_id = $this->get_tag( $post, 'wp:post_id' );
+ $post_title = $this->get_tag( $post, 'title' );
+ $post_date = $this->get_tag( $post, 'wp:post_date' );
+ $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
+ $comment_status = $this->get_tag( $post, 'wp:comment_status' );
+ $ping_status = $this->get_tag( $post, 'wp:ping_status' );
+ $status = $this->get_tag( $post, 'wp:status' );
+ $post_name = $this->get_tag( $post, 'wp:post_name' );
+ $post_parent = $this->get_tag( $post, 'wp:post_parent' );
+ $menu_order = $this->get_tag( $post, 'wp:menu_order' );
+ $post_type = $this->get_tag( $post, 'wp:post_type' );
+ $post_password = $this->get_tag( $post, 'wp:post_password' );
+ $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
+ $guid = $this->get_tag( $post, 'guid' );
+ $post_author = $this->get_tag( $post, 'dc:creator' );
+
+ $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
+ $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
+ $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
+ $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
+
+ $post_content = $this->get_tag( $post, 'content:encoded' );
+ $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
+ $post_content = str_replace( '<br>', '<br />', $post_content );
+ $post_content = str_replace( '<hr>', '<hr />', $post_content );
+
+ $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
+ 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
+ 'menu_order', 'post_type', 'post_password', 'is_sticky'
+ );
+
+ $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
+ if ( $attachment_url )
+ $postdata['attachment_url'] = $attachment_url;
+
+ preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
+ foreach ( $terms as $t ) {
+ $post_terms[] = array(
+ 'slug' => $t[2],
+ 'domain' => $t[1],
+ 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
+ );
+ }
+ if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
+
+ preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
+ $comments = $comments[1];
+ if ( $comments ) {
+ foreach ( $comments as $comment ) {
+ preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta );
+ $commentmeta = $commentmeta[1];
+ $c_meta = array();
+ foreach ( $commentmeta as $m ) {
+ $c_meta[] = array(
+ 'key' => $this->get_tag( $m, 'wp:meta_key' ),
+ 'value' => $this->get_tag( $m, 'wp:meta_value' ),
+ );
+ }
+
+ $post_comments[] = array(
+ 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
+ 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
+ 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
+ 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
+ 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
+ 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
+ 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
+ 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
+ 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
+ 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
+ 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
+ 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
+ 'commentmeta' => $c_meta,
+ );
+ }
+ }
+ if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments;
+
+ preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta );
+ $postmeta = $postmeta[1];
+ if ( $postmeta ) {
+ foreach ( $postmeta as $p ) {
+ $post_postmeta[] = array(
+ 'key' => $this->get_tag( $p, 'wp:meta_key' ),
+ 'value' => $this->get_tag( $p, 'wp:meta_value' ),
+ );
+ }
+ }
+ if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta;
+
+ return $postdata;
+ }
+
+ function _normalize_tag( $matches ) {
+ return '<' . strtolower( $matches[1] );
+ }
+
+ function fopen( $filename, $mode = 'r' ) {
+ if ( $this->has_gzip )
+ return gzopen( $filename, $mode );
+ return fopen( $filename, $mode );
+ }
+
+ function feof( $fp ) {
+ if ( $this->has_gzip )
+ return gzeof( $fp );
+ return feof( $fp );
+ }
+
+ function fgets( $fp, $len = 8192 ) {
+ if ( $this->has_gzip )
+ return gzgets( $fp, $len );
+ return fgets( $fp, $len );
+ }
+
+ function fclose( $fp ) {
+ if ( $this->has_gzip )
+ return gzclose( $fp );
+ return fclose( $fp );
+ }
+}
</ins></span></pre></div>
<a id="2013frederickdingimportertrunkparsersphp"></a>
<div class="delfile"><h4>Deleted: 2013/frederickding/importer/trunk/parsers.php (2132 => 2133)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/frederickding/importer/trunk/parsers.php 2013-07-17 21:58:02 UTC (rev 2132)
+++ 2013/frederickding/importer/trunk/parsers.php 2013-07-18 21:51:59 UTC (rev 2133)
</span><span class="lines">@@ -1,664 +0,0 @@
</span><del>-<?php
-/**
- * WordPress eXtended RSS file parser implementations
- *
- * @package WordPress
- * @subpackage Importer
- */
-
-/**
- * WordPress Importer class for managing parsing of WXR files.
- */
-class WXR_Parser {
- function parse( $file ) {
- // Attempt to use proper XML parsers first
- if ( extension_loaded( 'simplexml' ) ) {
- $parser = new WXR_Parser_SimpleXML;
- $result = $parser->parse( $file );
-
- // If SimpleXML succeeds or this is an invalid WXR file then return the results
- if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() )
- return $result;
- } else if ( extension_loaded( 'xml' ) ) {
- $parser = new WXR_Parser_XML;
- $result = $parser->parse( $file );
-
- // If XMLParser succeeds or this is an invalid WXR file then return the results
- if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() )
- return $result;
- }
-
- // We have a malformed XML file, so display the error and fallthrough to regex
- if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) {
- echo '<pre>';
- if ( 'SimpleXML_parse_error' == $result->get_error_code() ) {
- foreach ( $result->get_error_data() as $error )
- echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n";
- } else if ( 'XML_parse_error' == $result->get_error_code() ) {
- $error = $result->get_error_data();
- echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] );
- }
- echo '</pre>';
- echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />';
- echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>';
- }
-
- // use regular expressions if nothing else available or this is bad XML
- $parser = new WXR_Parser_Regex;
- return $parser->parse( $file );
- }
-}
-
-/**
- * WXR Parser that makes use of the SimpleXML PHP extension.
- */
-class WXR_Parser_SimpleXML {
- function parse( $file ) {
- $authors = $posts = $categories = $tags = $terms = array();
-
- $internal_errors = libxml_use_internal_errors(true);
-
- $dom = new DOMDocument;
- $old_value = null;
- if ( function_exists( 'libxml_disable_entity_loader' ) ) {
- $old_value = libxml_disable_entity_loader( true );
- }
- $success = $dom->loadXML( file_get_contents( $file ) );
- if ( ! is_null( $old_value ) ) {
- libxml_disable_entity_loader( $old_value );
- }
-
- if ( ! $success || isset( $dom->doctype ) ) {
- return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
- }
-
- $xml = simplexml_import_dom( $dom );
- unset( $dom );
-
- // halt if loading produces an error
- if ( ! $xml )
- return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
-
- $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version');
- if ( ! $wxr_version )
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
-
- $wxr_version = (string) trim( $wxr_version[0] );
- // confirm that we are dealing with the correct file format
- if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) )
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
-
- $base_url = $xml->xpath('/rss/channel/wp:base_site_url');
- $base_url = (string) trim( $base_url[0] );
-
- $namespaces = $xml->getDocNamespaces();
- if ( ! isset( $namespaces['wp'] ) )
- $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
- if ( ! isset( $namespaces['excerpt'] ) )
- $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
-
- // grab authors
- foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) {
- $a = $author_arr->children( $namespaces['wp'] );
- $login = (string) $a->author_login;
- $authors[$login] = array(
- 'author_id' => (int) $a->author_id,
- 'author_login' => $login,
- 'author_email' => (string) $a->author_email,
- 'author_display_name' => (string) $a->author_display_name,
- 'author_first_name' => (string) $a->author_first_name,
- 'author_last_name' => (string) $a->author_last_name
- );
- }
-
- // grab cats, tags and terms
- foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) {
- $t = $term_arr->children( $namespaces['wp'] );
- $categories[] = array(
- 'term_id' => (int) $t->term_id,
- 'category_nicename' => (string) $t->category_nicename,
- 'category_parent' => (string) $t->category_parent,
- 'cat_name' => (string) $t->cat_name,
- 'category_description' => (string) $t->category_description
- );
- }
-
- foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) {
- $t = $term_arr->children( $namespaces['wp'] );
- $tags[] = array(
- 'term_id' => (int) $t->term_id,
- 'tag_slug' => (string) $t->tag_slug,
- 'tag_name' => (string) $t->tag_name,
- 'tag_description' => (string) $t->tag_description
- );
- }
-
- foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) {
- $t = $term_arr->children( $namespaces['wp'] );
- $terms[] = array(
- 'term_id' => (int) $t->term_id,
- 'term_taxonomy' => (string) $t->term_taxonomy,
- 'slug' => (string) $t->term_slug,
- 'term_parent' => (string) $t->term_parent,
- 'term_name' => (string) $t->term_name,
- 'term_description' => (string) $t->term_description
- );
- }
-
- // grab posts
- foreach ( $xml->channel->item as $item ) {
- $post = array(
- 'post_title' => (string) $item->title,
- 'guid' => (string) $item->guid,
- );
-
- $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
- $post['post_author'] = (string) $dc->creator;
-
- $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
- $excerpt = $item->children( $namespaces['excerpt'] );
- $post['post_content'] = (string) $content->encoded;
- $post['post_excerpt'] = (string) $excerpt->encoded;
-
- $wp = $item->children( $namespaces['wp'] );
- $post['post_id'] = (int) $wp->post_id;
- $post['post_date'] = (string) $wp->post_date;
- $post['post_date_gmt'] = (string) $wp->post_date_gmt;
- $post['comment_status'] = (string) $wp->comment_status;
- $post['ping_status'] = (string) $wp->ping_status;
- $post['post_name'] = (string) $wp->post_name;
- $post['status'] = (string) $wp->status;
- $post['post_parent'] = (int) $wp->post_parent;
- $post['menu_order'] = (int) $wp->menu_order;
- $post['post_type'] = (string) $wp->post_type;
- $post['post_password'] = (string) $wp->post_password;
- $post['is_sticky'] = (int) $wp->is_sticky;
-
- if ( isset($wp->attachment_url) )
- $post['attachment_url'] = (string) $wp->attachment_url;
-
- foreach ( $item->category as $c ) {
- $att = $c->attributes();
- if ( isset( $att['nicename'] ) )
- $post['terms'][] = array(
- 'name' => (string) $c,
- 'slug' => (string) $att['nicename'],
- 'domain' => (string) $att['domain']
- );
- }
-
- foreach ( $wp->postmeta as $meta ) {
- $post['postmeta'][] = array(
- 'key' => (string) $meta->meta_key,
- 'value' => (string) $meta->meta_value
- );
- }
-
- foreach ( $wp->comment as $comment ) {
- $meta = array();
- if ( isset( $comment->commentmeta ) ) {
- foreach ( $comment->commentmeta as $m ) {
- $meta[] = array(
- 'key' => (string) $m->meta_key,
- 'value' => (string) $m->meta_value
- );
- }
- }
-
- $post['comments'][] = array(
- 'comment_id' => (int) $comment->comment_id,
- 'comment_author' => (string) $comment->comment_author,
- 'comment_author_email' => (string) $comment->comment_author_email,
- 'comment_author_IP' => (string) $comment->comment_author_IP,
- 'comment_author_url' => (string) $comment->comment_author_url,
- 'comment_date' => (string) $comment->comment_date,
- 'comment_date_gmt' => (string) $comment->comment_date_gmt,
- 'comment_content' => (string) $comment->comment_content,
- 'comment_approved' => (string) $comment->comment_approved,
- 'comment_type' => (string) $comment->comment_type,
- 'comment_parent' => (string) $comment->comment_parent,
- 'comment_user_id' => (int) $comment->comment_user_id,
- 'commentmeta' => $meta,
- );
- }
-
- $posts[] = $post;
- }
-
- return array(
- 'authors' => $authors,
- 'posts' => $posts,
- 'categories' => $categories,
- 'tags' => $tags,
- 'terms' => $terms,
- 'base_url' => $base_url,
- 'version' => $wxr_version
- );
- }
-}
-
-/**
- * WXR Parser that makes use of the XML Parser PHP extension.
- */
-class WXR_Parser_XML {
- var $wp_tags = array(
- 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url',
- 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password',
- 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description',
- 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent',
- 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name',
- 'wp:author_first_name', 'wp:author_last_name',
- );
- var $wp_sub_tags = array(
- 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url',
- 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content',
- 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id',
- );
-
- function parse( $file ) {
- $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false;
- $this->authors = $this->posts = $this->term = $this->category = $this->tag = array();
-
- $xml = xml_parser_create( 'UTF-8' );
- xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 );
- xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 );
- xml_set_object( $xml, $this );
- xml_set_character_data_handler( $xml, 'cdata' );
- xml_set_element_handler( $xml, 'tag_open', 'tag_close' );
-
- if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) {
- $current_line = xml_get_current_line_number( $xml );
- $current_column = xml_get_current_column_number( $xml );
- $error_code = xml_get_error_code( $xml );
- $error_string = xml_error_string( $error_code );
- return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) );
- }
- xml_parser_free( $xml );
-
- if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) )
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
-
- return array(
- 'authors' => $this->authors,
- 'posts' => $this->posts,
- 'categories' => $this->category,
- 'tags' => $this->tag,
- 'terms' => $this->term,
- 'base_url' => $this->base_url,
- 'version' => $this->wxr_version
- );
- }
-
- function tag_open( $parse, $tag, $attr ) {
- if ( in_array( $tag, $this->wp_tags ) ) {
- $this->in_tag = substr( $tag, 3 );
- return;
- }
-
- if ( in_array( $tag, $this->wp_sub_tags ) ) {
- $this->in_sub_tag = substr( $tag, 3 );
- return;
- }
-
- switch ( $tag ) {
- case 'category':
- if ( isset($attr['domain'], $attr['nicename']) ) {
- $this->sub_data['domain'] = $attr['domain'];
- $this->sub_data['slug'] = $attr['nicename'];
- }
- break;
- case 'item': $this->in_post = true;
- case 'title': if ( $this->in_post ) $this->in_tag = 'post_title'; break;
- case 'guid': $this->in_tag = 'guid'; break;
- case 'dc:creator': $this->in_tag = 'post_author'; break;
- case 'content:encoded': $this->in_tag = 'post_content'; break;
- case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break;
-
- case 'wp:term_slug': $this->in_tag = 'slug'; break;
- case 'wp:meta_key': $this->in_sub_tag = 'key'; break;
- case 'wp:meta_value': $this->in_sub_tag = 'value'; break;
- }
- }
-
- function cdata( $parser, $cdata ) {
- if ( ! trim( $cdata ) )
- return;
-
- $this->cdata .= trim( $cdata );
- }
-
- function tag_close( $parser, $tag ) {
- switch ( $tag ) {
- case 'wp:comment':
- unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data
- if ( ! empty( $this->sub_data ) )
- $this->data['comments'][] = $this->sub_data;
- $this->sub_data = false;
- break;
- case 'wp:commentmeta':
- $this->sub_data['commentmeta'][] = array(
- 'key' => $this->sub_data['key'],
- 'value' => $this->sub_data['value']
- );
- break;
- case 'category':
- if ( ! empty( $this->sub_data ) ) {
- $this->sub_data['name'] = $this->cdata;
- $this->data['terms'][] = $this->sub_data;
- }
- $this->sub_data = false;
- break;
- case 'wp:postmeta':
- if ( ! empty( $this->sub_data ) )
- $this->data['postmeta'][] = $this->sub_data;
- $this->sub_data = false;
- break;
- case 'item':
- $this->posts[] = $this->data;
- $this->data = false;
- break;
- case 'wp:category':
- case 'wp:tag':
- case 'wp:term':
- $n = substr( $tag, 3 );
- array_push( $this->$n, $this->data );
- $this->data = false;
- break;
- case 'wp:author':
- if ( ! empty($this->data['author_login']) )
- $this->authors[$this->data['author_login']] = $this->data;
- $this->data = false;
- break;
- case 'wp:base_site_url':
- $this->base_url = $this->cdata;
- break;
- case 'wp:wxr_version':
- $this->wxr_version = $this->cdata;
- break;
-
- default:
- if ( $this->in_sub_tag ) {
- $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
- $this->in_sub_tag = false;
- } else if ( $this->in_tag ) {
- $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : '';
- $this->in_tag = false;
- }
- }
-
- $this->cdata = false;
- }
-}
-
-/**
- * WXR Parser that uses regular expressions. Fallback for installs without an XML parser.
- */
-class WXR_Parser_Regex {
- var $authors = array();
- var $posts = array();
- var $categories = array();
- var $tags = array();
- var $terms = array();
- var $base_url = '';
-
- function __construct() {
- $this->has_gzip = is_callable( 'gzopen' );
- }
-
- function WXR_Parser_Regex() {
- $this->__construct();
- }
-
- function parse( $file ) {
- $wxr_version = $in_post = false;
-
- $fp = $this->fopen( $file, 'r' );
- if ( $fp ) {
- while ( ! $this->feof( $fp ) ) {
- $importline = rtrim( $this->fgets( $fp ) );
-
- if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) )
- $wxr_version = $version[1];
-
- if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) {
- preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url );
- $this->base_url = $url[1];
- continue;
- }
- if ( false !== strpos( $importline, '<wp:category>' ) ) {
- preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category );
- $this->categories[] = $this->process_category( $category[1] );
- continue;
- }
- if ( false !== strpos( $importline, '<wp:tag>' ) ) {
- preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag );
- $this->tags[] = $this->process_tag( $tag[1] );
- continue;
- }
- if ( false !== strpos( $importline, '<wp:term>' ) ) {
- preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term );
- $this->terms[] = $this->process_term( $term[1] );
- continue;
- }
- if ( false !== strpos( $importline, '<wp:author>' ) ) {
- preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author );
- $a = $this->process_author( $author[1] );
- $this->authors[$a['author_login']] = $a;
- continue;
- }
- if ( false !== strpos( $importline, '<item>' ) ) {
- $post = '';
- $in_post = true;
- continue;
- }
- if ( false !== strpos( $importline, '</item>' ) ) {
- $in_post = false;
- $this->posts[] = $this->process_post( $post );
- continue;
- }
- if ( $in_post ) {
- $post .= $importline . "\n";
- }
- }
-
- $this->fclose($fp);
- }
-
- if ( ! $wxr_version )
- return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) );
-
- return array(
- 'authors' => $this->authors,
- 'posts' => $this->posts,
- 'categories' => $this->categories,
- 'tags' => $this->tags,
- 'terms' => $this->terms,
- 'base_url' => $this->base_url,
- 'version' => $wxr_version
- );
- }
-
- function get_tag( $string, $tag ) {
- preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return );
- if ( isset( $return[1] ) ) {
- if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) {
- if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) {
- preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches );
- $return = '';
- foreach( $matches[1] as $match )
- $return .= $match;
- } else {
- $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] );
- }
- } else {
- $return = $return[1];
- }
- } else {
- $return = '';
- }
- return $return;
- }
-
- function process_category( $c ) {
- return array(
- 'term_id' => $this->get_tag( $c, 'wp:term_id' ),
- 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ),
- 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ),
- 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ),
- 'category_description' => $this->get_tag( $c, 'wp:category_description' ),
- );
- }
-
- function process_tag( $t ) {
- return array(
- 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
- 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ),
- 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ),
- 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ),
- );
- }
-
- function process_term( $t ) {
- return array(
- 'term_id' => $this->get_tag( $t, 'wp:term_id' ),
- 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ),
- 'slug' => $this->get_tag( $t, 'wp:term_slug' ),
- 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ),
- 'term_name' => $this->get_tag( $t, 'wp:term_name' ),
- 'term_description' => $this->get_tag( $t, 'wp:term_description' ),
- );
- }
-
- function process_author( $a ) {
- return array(
- 'author_id' => $this->get_tag( $a, 'wp:author_id' ),
- 'author_login' => $this->get_tag( $a, 'wp:author_login' ),
- 'author_email' => $this->get_tag( $a, 'wp:author_email' ),
- 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ),
- 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ),
- 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ),
- );
- }
-
- function process_post( $post ) {
- $post_id = $this->get_tag( $post, 'wp:post_id' );
- $post_title = $this->get_tag( $post, 'title' );
- $post_date = $this->get_tag( $post, 'wp:post_date' );
- $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' );
- $comment_status = $this->get_tag( $post, 'wp:comment_status' );
- $ping_status = $this->get_tag( $post, 'wp:ping_status' );
- $status = $this->get_tag( $post, 'wp:status' );
- $post_name = $this->get_tag( $post, 'wp:post_name' );
- $post_parent = $this->get_tag( $post, 'wp:post_parent' );
- $menu_order = $this->get_tag( $post, 'wp:menu_order' );
- $post_type = $this->get_tag( $post, 'wp:post_type' );
- $post_password = $this->get_tag( $post, 'wp:post_password' );
- $is_sticky = $this->get_tag( $post, 'wp:is_sticky' );
- $guid = $this->get_tag( $post, 'guid' );
- $post_author = $this->get_tag( $post, 'dc:creator' );
-
- $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' );
- $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt );
- $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt );
- $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt );
-
- $post_content = $this->get_tag( $post, 'content:encoded' );
- $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content );
- $post_content = str_replace( '<br>', '<br />', $post_content );
- $post_content = str_replace( '<hr>', '<hr />', $post_content );
-
- $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt',
- 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent',
- 'menu_order', 'post_type', 'post_password', 'is_sticky'
- );
-
- $attachment_url = $this->get_tag( $post, 'wp:attachment_url' );
- if ( $attachment_url )
- $postdata['attachment_url'] = $attachment_url;
-
- preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER );
- foreach ( $terms as $t ) {
- $post_terms[] = array(
- 'slug' => $t[2],
- 'domain' => $t[1],
- 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ),
- );
- }
- if ( ! empty( $post_terms ) ) $postdata['terms'] = $post_terms;
-
- preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments );
- $comments = $comments[1];
- if ( $comments ) {
- foreach ( $comments as $comment ) {
- preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta );
- $commentmeta = $commentmeta[1];
- $c_meta = array();
- foreach ( $commentmeta as $m ) {
- $c_meta[] = array(
- 'key' => $this->get_tag( $m, 'wp:meta_key' ),
- 'value' => $this->get_tag( $m, 'wp:meta_value' ),
- );
- }
-
- $post_comments[] = array(
- 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ),
- 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ),
- 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ),
- 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ),
- 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ),
- 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ),
- 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ),
- 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ),
- 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ),
- 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ),
- 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ),
- 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ),
- 'commentmeta' => $c_meta,
- );
- }
- }
- if ( ! empty( $post_comments ) ) $postdata['comments'] = $post_comments;
-
- preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta );
- $postmeta = $postmeta[1];
- if ( $postmeta ) {
- foreach ( $postmeta as $p ) {
- $post_postmeta[] = array(
- 'key' => $this->get_tag( $p, 'wp:meta_key' ),
- 'value' => $this->get_tag( $p, 'wp:meta_value' ),
- );
- }
- }
- if ( ! empty( $post_postmeta ) ) $postdata['postmeta'] = $post_postmeta;
-
- return $postdata;
- }
-
- function _normalize_tag( $matches ) {
- return '<' . strtolower( $matches[1] );
- }
-
- function fopen( $filename, $mode = 'r' ) {
- if ( $this->has_gzip )
- return gzopen( $filename, $mode );
- return fopen( $filename, $mode );
- }
-
- function feof( $fp ) {
- if ( $this->has_gzip )
- return gzeof( $fp );
- return feof( $fp );
- }
-
- function fgets( $fp, $len = 8192 ) {
- if ( $this->has_gzip )
- return gzgets( $fp, $len );
- return fgets( $fp, $len );
- }
-
- function fclose( $fp ) {
- if ( $this->has_gzip )
- return gzclose( $fp );
- return fclose( $fp );
- }
-}
</del></span></pre></div>
<a id="2013frederickdingimportertrunkwordpressimporterphp"></a>
<div class="modfile"><h4>Modified: 2013/frederickding/importer/trunk/wordpress-importer.php (2132 => 2133)</h4>
<pre class="diff"><span>
<span class="info">--- 2013/frederickding/importer/trunk/wordpress-importer.php 2013-07-17 21:58:02 UTC (rev 2132)
+++ 2013/frederickding/importer/trunk/wordpress-importer.php 2013-07-18 21:51:59 UTC (rev 2133)
</span><span class="lines">@@ -30,7 +30,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> // include WXR file parsers
</span><del>-require dirname( __FILE__ ) . '/parsers.php';
</del><ins>+require dirname( __FILE__ ) . '/class-wxr-parser.php';
</ins><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * WordPress Importer class for managing the import process of a WXR file
</span></span></pre>
</div>
</div>
</body>
</html>