]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/import/blogware.php
Wordpress 2.7.1-scripts
[autoinstalls/wordpress.git] / wp-admin / import / blogware.php
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 import_posts() {
46                 global $wpdb, $current_user;
47
48                 set_magic_quotes_runtime(0);
49                 $importdata = file($this->file); // Read the file into an array
50                 $importdata = implode('', $importdata); // squish it
51                 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
52
53                 preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
54                 $posts = $posts[1];
55                 unset($importdata);
56                 echo '<ol>';
57                 foreach ($posts as $post) {
58                         flush();
59                         preg_match('|<item type=\"(.*?)\">|is', $post, $post_type);
60                         $post_type = $post_type[1];
61                         if($post_type == "photo") {
62                                 preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title);
63                         } else {
64                                 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
65                         }
66                         $post_title = $wpdb->escape(trim($post_title[1]));
67
68                         preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date);
69                         $post_date = strtotime($post_date[1]);
70                         $post_date = gmdate('Y-m-d H:i:s', $post_date);
71
72                         preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
73                         $categories = $categories[1];
74
75                         $cat_index = 0;
76                         foreach ($categories as $category) {
77                                 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
78                                 $cat_index++;
79                         }
80
81                         if(strcasecmp($post_type, "photo") === 0) {
82                                 preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content);
83                                 $post_content = '<img src="'.trim($post_content[1]).'" />';
84                                 $post_content = $this->unhtmlentities($post_content);
85                         } else {
86                                 preg_match('|<body>(.*?)</body>|is', $post, $post_content);
87                                 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
88                                 $post_content = $this->unhtmlentities($post_content);
89                         }
90
91                         // Clean up content
92                         $post_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $post_content);
93                         $post_content = str_replace('<br>', '<br />', $post_content);
94                         $post_content = str_replace('<hr>', '<hr />', $post_content);
95                         $post_content = $wpdb->escape($post_content);
96
97                         $post_author = $current_user->ID;
98                         preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
99                         $post_status = trim($post_status[1]);
100
101                         echo '<li>';
102                         if ($post_id = post_exists($post_title, $post_content, $post_date)) {
103                                 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
104                         } else {
105                                 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
106                                 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
107                                 $post_id = wp_insert_post($postdata);
108                                 if ( is_wp_error( $post_id ) ) {
109                                         return $post_id;
110                                 }
111                                 if (!$post_id) {
112                                         _e("Couldn't get post ID");
113                                         echo '</li>';
114                                         break;
115                                 }
116                                 if(0 != count($categories))
117                                         wp_create_categories($categories, $post_id);
118                         }
119
120                         preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
121                         $comments = $comments[1];
122
123                         if ( $comments ) {
124                                 $comment_post_ID = (int) $post_id;
125                                 $num_comments = 0;
126                                 foreach ($comments as $comment) {
127                                         preg_match('|<body>(.*?)</body>|is', $comment, $comment_content);
128                                         $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
129                                         $comment_content = $this->unhtmlentities($comment_content);
130
131                                         // Clean up content
132                                         $comment_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $comment_content);
133                                         $comment_content = str_replace('<br>', '<br />', $comment_content);
134                                         $comment_content = str_replace('<hr>', '<hr />', $comment_content);
135                                         $comment_content = $wpdb->escape($comment_content);
136
137                                         preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date);
138                                         $comment_date = trim($comment_date[1]);
139                                         $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
140
141                                         preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
142                                         $comment_author = $wpdb->escape(trim($comment_author[1]));
143
144                                         $comment_author_email = NULL;
145
146                                         $comment_approved = 1;
147                                         // Check if it's already there
148                                         if (!comment_exists($comment_author, $comment_date)) {
149                                                 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
150                                                 $commentdata = wp_filter_comment($commentdata);
151                                                 wp_insert_comment($commentdata);
152                                                 $num_comments++;
153                                         }
154                                 }
155                         }
156                         if ( $num_comments ) {
157                                 echo ' ';
158                                 printf( __ngettext('%s comment', '%s comments', $num_comments), $num_comments );
159                         }
160                         echo '</li>';
161                         flush();
162                         ob_flush();
163                 }
164                 echo '</ol>';
165         }
166
167         function import() {
168                 $file = wp_import_handle_upload();
169                 if ( isset($file['error']) ) {
170                         echo $file['error'];
171                         return;
172                 }
173
174                 $this->file = $file['file'];
175                 $result = $this->import_posts();
176                 if ( is_wp_error( $result ) )
177                         return $result;
178                 wp_import_cleanup($file['id']);
179                 do_action('import_done', 'blogware');
180                 echo '<h3>';
181                 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
182                 echo '</h3>';
183         }
184
185         function dispatch() {
186                 if (empty ($_GET['step']))
187                         $step = 0;
188                 else
189                         $step = (int) $_GET['step'];
190
191                 $this->header();
192
193                 switch ($step) {
194                         case 0 :
195                                 $this->greet();
196                                 break;
197                         case 1 :
198                                 $result = $this->import();
199                                 if ( is_wp_error( $result ) )
200                                         $result->get_error_message();
201                                 break;
202                 }
203
204                 $this->footer();
205         }
206
207         function BW_Import() {
208                 // Nothing.
209         }
210 }
211
212 $blogware_import = new BW_Import();
213
214 register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));
215 ?>