]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/rss.php
Wordpress 2.0.4
[autoinstalls/wordpress.git] / wp-admin / import / rss.php
1 <?php
2
3 class RSS_Import {
4
5         var $posts = array ();
6         var $file;
7
8         function header() {
9                 echo '<div class="wrap">';
10                 echo '<h2>'.__('Import RSS').'</h2>';
11         }
12
13         function footer() {
14                 echo '</div>';
15         }
16
17         function unhtmlentities($string) { // From php.net for < 4.3 compat
18                 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
19                 $trans_tbl = array_flip($trans_tbl);
20                 return strtr($string, $trans_tbl);
21         }
22         
23         function greet() {
24                 echo '<p>'.__('Howdy! This importer allows you to extract posts from any 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>';
25                 wp_import_upload_form("admin.php?import=rss&amp;step=1");
26         }
27
28         function get_posts() {
29                 global $wpdb;
30                 
31                 set_magic_quotes_runtime(0);
32                 $datalines = file($this->file); // Read the file into an array
33                 $importdata = implode('', $datalines); // squish it
34                 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
35
36                 preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
37                 $this->posts = $this->posts[1];
38                 $index = 0;
39                 foreach ($this->posts as $post) {
40                         preg_match('|<title>(.*?)</title>|is', $post, $post_title);
41                         $post_title = $wpdb->escape(trim($post_title[1]));
42
43                         preg_match('|<pubdate>(.*?)</pubdate>|is', $post, $post_date);
44
45                         if ($post_date) {
46                                 $post_date = strtotime($post_date[1]);
47                         } else {
48                                 // if we don't already have something from pubDate
49                                 preg_match('|<dc:date>(.*?)</dc:date>|is', $post, $post_date);
50                                 $post_date = preg_replace('|([-+])([0-9]+):([0-9]+)$|', '\1\2\3', $post_date[1]);
51                                 $post_date = str_replace('T', ' ', $post_date);
52                                 $post_date = strtotime($post_date);
53                         }
54
55                         $post_date = gmdate('Y-m-d H:i:s', $post_date);
56
57                         preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
58                         $categories = $categories[1];
59
60                         if (!$categories) {
61                                 preg_match_all('|<dc:subject>(.*?)</dc:subject>|is', $post, $categories);
62                                 $categories = $categories[1];
63                         }
64
65                         $cat_index = 0;
66                         foreach ($categories as $category) {
67                                 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
68                                 $cat_index++;
69                         }
70
71                         preg_match('|<guid.+?>(.*?)</guid>|is', $post, $guid);
72                         if ($guid)
73                                 $guid = $wpdb->escape(trim($guid[1]));
74                         else
75                                 $guid = '';
76
77                         preg_match('|<content:encoded>(.*?)</content:encoded>|is', $post, $post_content);
78                         $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $wpdb->escape(trim($post_content[1])));
79
80                         if (!$post_content) {
81                                 // This is for feeds that put content in description
82                                 preg_match('|<description>(.*?)</description>|is', $post, $post_content);
83                                 $post_content = $wpdb->escape($this->unhtmlentities(trim($post_content[1])));
84                         }
85
86                         // Clean up content
87                         $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
88                         $post_content = str_replace('<br>', '<br />', $post_content);
89                         $post_content = str_replace('<hr>', '<hr />', $post_content);
90
91                         $post_author = 1;
92                         $post_status = 'publish';
93                         $this->posts[$index] = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status', 'guid', 'categories');
94                         $index++;
95                 }
96         }
97
98         function import_posts() {
99                 echo '<ol>';
100
101                 foreach ($this->posts as $post) {
102                         echo "<li>".__('Importing post...');
103
104                         extract($post);
105
106                         if ($post_id = post_exists($post_title, $post_content, $post_date)) {
107                                 _e('Post already imported');
108                         } else {
109                                 $post_id = wp_insert_post($post);
110                                 if (!$post_id) {
111                                         _e("Couldn't get post ID");
112                                         return;
113                                 }
114
115                                 if (0 != count($categories))
116                                         wp_create_categories($categories, $post_id);
117                                 _e('Done !');
118                         }
119                         echo '</li>';
120                 }
121
122                 echo '</ol>';
123
124         }
125
126         function import() {
127                 $file = wp_import_handle_upload();
128                 if ( isset($file['error']) ) {
129                         echo $file['error'];
130                         return;
131                 }
132
133                 $this->file = $file['file'];
134                 $this->get_posts();
135                 $this->import_posts();
136                 wp_import_cleanup($file['id']);
137                 
138                 echo '<h3>';
139                 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
140                 echo '</h3>';
141         }
142
143         function dispatch() {
144                 if (empty ($_GET['step']))
145                         $step = 0;
146                 else
147                         $step = (int) $_GET['step'];
148
149                 $this->header();
150                 
151                 switch ($step) {
152                         case 0 :
153                                 $this->greet();
154                                 break;
155                         case 1 :
156                                 $this->import();
157                                 break;
158                 }
159                 
160                 $this->footer();
161         }
162
163         function RSS_Import() {
164                 // Nothing.     
165         }
166 }
167
168 $rss_import = new RSS_Import();
169
170 register_importer('rss', 'RSS', __('Import posts from an RSS feed'), array ($rss_import, 'dispatch'));
171 ?>