web/wp-admin/import/blogware.php
changeset 136 bde1974c263b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /**
       
     3  * Blogware XML Importer
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Importer
       
     7  * @author Shayne Sweeney
       
     8  * @link http://www.theshayne.com/
       
     9  */
       
    10 
       
    11 /**
       
    12  * Blogware XML Importer class
       
    13  *
       
    14  * Extract posts from Blogware XML export file into your blog.
       
    15  *
       
    16  * @since unknown
       
    17  */
       
    18 class BW_Import {
       
    19 
       
    20 	var $file;
       
    21 
       
    22 	function header() {
       
    23 		echo '<div class="wrap">';
       
    24 		screen_icon();
       
    25 		echo '<h2>'.__('Import Blogware').'</h2>';
       
    26 	}
       
    27 
       
    28 	function footer() {
       
    29 		echo '</div>';
       
    30 	}
       
    31 
       
    32 	function unhtmlentities($string) { // From php.net for < 4.3 compat
       
    33 		$trans_tbl = get_html_translation_table(HTML_ENTITIES);
       
    34 		$trans_tbl = array_flip($trans_tbl);
       
    35 		return strtr($string, $trans_tbl);
       
    36 	}
       
    37 
       
    38 	function greet() {
       
    39 		echo '<div class="narrow">';
       
    40 		echo '<p>'.__('Howdy! This importer allows you to extract posts from Blogware XML export file into your blog.  Pick a Blogware file to upload and click Import.').'</p>';
       
    41 		wp_import_upload_form("admin.php?import=blogware&amp;step=1");
       
    42 		echo '</div>';
       
    43 	}
       
    44 
       
    45 	function _normalize_tag( $matches ) {
       
    46 		return '<' . strtolower( $matches[1] );
       
    47 	}
       
    48 
       
    49 	function import_posts() {
       
    50 		global $wpdb, $current_user;
       
    51 
       
    52 		set_magic_quotes_runtime(0);
       
    53 		$importdata = file($this->file); // Read the file into an array
       
    54 		$importdata = implode('', $importdata); // squish it
       
    55 		$importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
       
    56 
       
    57 		preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
       
    58 		$posts = $posts[1];
       
    59 		unset($importdata);
       
    60 		echo '<ol>';
       
    61 		foreach ($posts as $post) {
       
    62 			flush();
       
    63 			preg_match('|<item type=\"(.*?)\">|is', $post, $post_type);
       
    64 			$post_type = $post_type[1];
       
    65 			if($post_type == "photo") {
       
    66 				preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title);
       
    67 			} else {
       
    68 				preg_match('|<title>(.*?)</title>|is', $post, $post_title);
       
    69 			}
       
    70 			$post_title = $wpdb->escape(trim($post_title[1]));
       
    71 
       
    72 			preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date);
       
    73 			$post_date = strtotime($post_date[1]);
       
    74 			$post_date = gmdate('Y-m-d H:i:s', $post_date);
       
    75 
       
    76 			preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
       
    77 			$categories = $categories[1];
       
    78 
       
    79 			$cat_index = 0;
       
    80 			foreach ($categories as $category) {
       
    81 				$categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
       
    82 				$cat_index++;
       
    83 			}
       
    84 
       
    85 			if(strcasecmp($post_type, "photo") === 0) {
       
    86 				preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content);
       
    87 				$post_content = '<img src="'.trim($post_content[1]).'" />';
       
    88 				$post_content = $this->unhtmlentities($post_content);
       
    89 			} else {
       
    90 				preg_match('|<body>(.*?)</body>|is', $post, $post_content);
       
    91 				$post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
       
    92 				$post_content = $this->unhtmlentities($post_content);
       
    93 			}
       
    94 
       
    95 			// Clean up content
       
    96 			$post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
       
    97 			$post_content = str_replace('<br>', '<br />', $post_content);
       
    98 			$post_content = str_replace('<hr>', '<hr />', $post_content);
       
    99 			$post_content = $wpdb->escape($post_content);
       
   100 
       
   101 			$post_author = $current_user->ID;
       
   102 			preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
       
   103 			$post_status = trim($post_status[1]);
       
   104 
       
   105 			echo '<li>';
       
   106 			if ($post_id = post_exists($post_title, $post_content, $post_date)) {
       
   107 				printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
       
   108 			} else {
       
   109 				printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
       
   110 				$postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
       
   111 				$post_id = wp_insert_post($postdata);
       
   112 				if ( is_wp_error( $post_id ) ) {
       
   113 					return $post_id;
       
   114 				}
       
   115 				if (!$post_id) {
       
   116 					_e('Couldn&#8217;t get post ID');
       
   117 					echo '</li>';
       
   118 					break;
       
   119 				}
       
   120 				if(0 != count($categories))
       
   121 					wp_create_categories($categories, $post_id);
       
   122 			}
       
   123 
       
   124 			preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
       
   125 			$comments = $comments[1];
       
   126 
       
   127 			if ( $comments ) {
       
   128 				$comment_post_ID = (int) $post_id;
       
   129 				$num_comments = 0;
       
   130 				foreach ($comments as $comment) {
       
   131 					preg_match('|<body>(.*?)</body>|is', $comment, $comment_content);
       
   132 					$comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
       
   133 					$comment_content = $this->unhtmlentities($comment_content);
       
   134 
       
   135 					// Clean up content
       
   136 					$comment_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $comment_content);
       
   137 					$comment_content = str_replace('<br>', '<br />', $comment_content);
       
   138 					$comment_content = str_replace('<hr>', '<hr />', $comment_content);
       
   139 					$comment_content = $wpdb->escape($comment_content);
       
   140 
       
   141 					preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date);
       
   142 					$comment_date = trim($comment_date[1]);
       
   143 					$comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
       
   144 
       
   145 					preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
       
   146 					$comment_author = $wpdb->escape(trim($comment_author[1]));
       
   147 
       
   148 					$comment_author_email = NULL;
       
   149 
       
   150 					$comment_approved = 1;
       
   151 					// Check if it's already there
       
   152 					if (!comment_exists($comment_author, $comment_date)) {
       
   153 						$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
       
   154 						$commentdata = wp_filter_comment($commentdata);
       
   155 						wp_insert_comment($commentdata);
       
   156 						$num_comments++;
       
   157 					}
       
   158 				}
       
   159 			}
       
   160 			if ( $num_comments ) {
       
   161 				echo ' ';
       
   162 				printf( _n('%s comment', '%s comments', $num_comments), $num_comments );
       
   163 			}
       
   164 			echo '</li>';
       
   165 			flush();
       
   166 			ob_flush();
       
   167 		}
       
   168 		echo '</ol>';
       
   169 	}
       
   170 
       
   171 	function import() {
       
   172 		$file = wp_import_handle_upload();
       
   173 		if ( isset($file['error']) ) {
       
   174 			echo $file['error'];
       
   175 			return;
       
   176 		}
       
   177 
       
   178 		$this->file = $file['file'];
       
   179 		$result = $this->import_posts();
       
   180 		if ( is_wp_error( $result ) )
       
   181 			return $result;
       
   182 		wp_import_cleanup($file['id']);
       
   183 		do_action('import_done', 'blogware');
       
   184 		echo '<h3>';
       
   185 		printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
       
   186 		echo '</h3>';
       
   187 	}
       
   188 
       
   189 	function dispatch() {
       
   190 		if (empty ($_GET['step']))
       
   191 			$step = 0;
       
   192 		else
       
   193 			$step = (int) $_GET['step'];
       
   194 
       
   195 		$this->header();
       
   196 
       
   197 		switch ($step) {
       
   198 			case 0 :
       
   199 				$this->greet();
       
   200 				break;
       
   201 			case 1 :
       
   202 				$result = $this->import();
       
   203 				if ( is_wp_error( $result ) )
       
   204 					$result->get_error_message();
       
   205 				break;
       
   206 		}
       
   207 
       
   208 		$this->footer();
       
   209 	}
       
   210 
       
   211 	function BW_Import() {
       
   212 		// Nothing.
       
   213 	}
       
   214 }
       
   215 
       
   216 $blogware_import = new BW_Import();
       
   217 
       
   218 register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));
       
   219 ?>