web/wp-admin/import/rss.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     1 <?php
       
     2 /**
       
     3  * RSS Importer
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Importer
       
     7  */
       
     8 
       
     9 /**
       
    10  * RSS Importer
       
    11  *
       
    12  * Will process a RSS feed for importing posts into WordPress. This is a very
       
    13  * limited importer and should only be used as the last resort, when no other
       
    14  * importer is available.
       
    15  *
       
    16  * @since unknown
       
    17  */
       
    18 class RSS_Import {
       
    19 
       
    20 	var $posts = array ();
       
    21 	var $file;
       
    22 
       
    23 	function header() {
       
    24 		echo '<div class="wrap">';
       
    25 		screen_icon();
       
    26 		echo '<h2>'.__('Import RSS').'</h2>';
       
    27 	}
       
    28 
       
    29 	function footer() {
       
    30 		echo '</div>';
       
    31 	}
       
    32 
       
    33 	function unhtmlentities($string) { // From php.net for < 4.3 compat
       
    34 		$trans_tbl = get_html_translation_table(HTML_ENTITIES);
       
    35 		$trans_tbl = array_flip($trans_tbl);
       
    36 		return strtr($string, $trans_tbl);
       
    37 	}
       
    38 
       
    39 	function greet() {
       
    40 		echo '<div class="narrow">';
       
    41 		echo '<p>'.__('Howdy! This importer allows you to extract posts from an RSS 2.0 file into your blog. This is useful if you want to import your posts from a system that is not handled by a custom import tool. Pick an RSS file to upload and click Import.').'</p>';
       
    42 		wp_import_upload_form("admin.php?import=rss&amp;step=1");
       
    43 		echo '</div>';
       
    44 	}
       
    45 
       
    46 	function get_posts() {
       
    47 		global $wpdb;
       
    48 
       
    49 		set_magic_quotes_runtime(0);
       
    50 		$datalines = file($this->file); // Read the file into an array
       
    51 		$importdata = implode('', $datalines); // squish it
       
    52 		$importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
       
    53 
       
    54 		preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
       
    55 		$this->posts = $this->posts[1];
       
    56 		$index = 0;
       
    57 		foreach ($this->posts as $post) {
       
    58 			preg_match('|<title>(.*?)</title>|is', $post, $post_title);
       
    59 			$post_title = str_replace(array('<![CDATA[', ']]>'), '', $wpdb->escape( trim($post_title[1]) ));
       
    60 
       
    61 			preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date_gmt);
       
    62 
       
    63 			if ($post_date_gmt) {
       
    64 				$post_date_gmt = strtotime($post_date_gmt[1]);
       
    65 			} else {
       
    66 				// if we don't already have something from pubDate
       
    67 				preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date_gmt);
       
    68 				$post_date_gmt = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date_gmt[1]);
       
    69 				$post_date_gmt = str_replace('T', ' ', $post_date_gmt);
       
    70 				$post_date_gmt = strtotime($post_date_gmt);
       
    71 			}
       
    72 
       
    73 			$post_date_gmt = gmdate('Y-m-d H:i:s', $post_date_gmt);
       
    74 			$post_date = get_date_from_gmt( $post_date_gmt );
       
    75 
       
    76 			preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
       
    77 			$categories = $categories[1];
       
    78 
       
    79 			if (!$categories) {
       
    80 				preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
       
    81 				$categories = $categories[1];
       
    82 			}
       
    83 
       
    84 			$cat_index = 0;
       
    85 			foreach ($categories as $category) {
       
    86 				$categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
       
    87 				$cat_index++;
       
    88 			}
       
    89 
       
    90 			preg_match('|<guid.*?>(.*?)</guid>|is', $post, $guid);
       
    91 			if ($guid)
       
    92 				$guid = $wpdb->escape(trim($guid[1]));
       
    93 			else
       
    94 				$guid = '';
       
    95 
       
    96 			preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
       
    97 			$post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
       
    98 
       
    99 			if (!$post_content) {
       
   100 				// This is for feeds that put content in description
       
   101 				preg_match('|<description>(.*?)</description>|is', $post, $post_content);
       
   102 				$post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
       
   103 			}
       
   104 
       
   105 			// Clean up content
       
   106 			$post_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $post_content);
       
   107 			$post_content = str_replace('<br>', '<br />', $post_content);
       
   108 			$post_content = str_replace('<hr>', '<hr />', $post_content);
       
   109 
       
   110 			$post_author = 1;
       
   111 			$post_status = 'publish';
       
   112 			$this->posts[$index] = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
       
   113 			$index++;
       
   114 		}
       
   115 	}
       
   116 
       
   117 	function import_posts() {
       
   118 		echo '<ol>';
       
   119 
       
   120 		foreach ($this->posts as $post) {
       
   121 			echo "<li>".__('Importing post...');
       
   122 
       
   123 			extract($post);
       
   124 
       
   125 			if ($post_id = post_exists($post_title, $post_content, $post_date)) {
       
   126 				_e('Post already imported');
       
   127 			} else {
       
   128 				$post_id = wp_insert_post($post);
       
   129 				if ( is_wp_error( $post_id ) )
       
   130 					return $post_id;
       
   131 				if (!$post_id) {
       
   132 					_e('Couldn&#8217;t get post ID');
       
   133 					return;
       
   134 				}
       
   135 
       
   136 				if (0 != count($categories))
       
   137 					wp_create_categories($categories, $post_id);
       
   138 				_e('Done !');
       
   139 			}
       
   140 			echo '</li>';
       
   141 		}
       
   142 
       
   143 		echo '</ol>';
       
   144 
       
   145 	}
       
   146 
       
   147 	function import() {
       
   148 		$file = wp_import_handle_upload();
       
   149 		if ( isset($file['error']) ) {
       
   150 			echo $file['error'];
       
   151 			return;
       
   152 		}
       
   153 
       
   154 		$this->file = $file['file'];
       
   155 		$this->get_posts();
       
   156 		$result = $this->import_posts();
       
   157 		if ( is_wp_error( $result ) )
       
   158 			return $result;
       
   159 		wp_import_cleanup($file['id']);
       
   160 		do_action('import_done', 'rss');
       
   161 
       
   162 		echo '<h3>';
       
   163 		printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
       
   164 		echo '</h3>';
       
   165 	}
       
   166 
       
   167 	function dispatch() {
       
   168 		if (empty ($_GET['step']))
       
   169 			$step = 0;
       
   170 		else
       
   171 			$step = (int) $_GET['step'];
       
   172 
       
   173 		$this->header();
       
   174 
       
   175 		switch ($step) {
       
   176 			case 0 :
       
   177 				$this->greet();
       
   178 				break;
       
   179 			case 1 :
       
   180 				check_admin_referer('import-upload');
       
   181 				$result = $this->import();
       
   182 				if ( is_wp_error( $result ) )
       
   183 					echo $result->get_error_message();
       
   184 				break;
       
   185 		}
       
   186 
       
   187 		$this->footer();
       
   188 	}
       
   189 
       
   190 	function RSS_Import() {
       
   191 		// Nothing.
       
   192 	}
       
   193 }
       
   194 
       
   195 $rss_import = new RSS_Import();
       
   196 
       
   197 register_importer('rss', __('RSS'), __('Import posts from an RSS feed.'), array ($rss_import, 'dispatch'));
       
   198 ?>