3 * Blogware XML Importer
7 * @author Shayne Sweeney
8 * @link http://www.theshayne.com/
12 * Blogware XML Importer class
14 * Extract posts from Blogware XML export file into your blog.
23 echo '<div class="wrap">';
25 echo '<h2>'.__('Import Blogware').'</h2>';
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);
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&step=1");
45 function import_posts() {
46 global $wpdb, $current_user;
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);
53 preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
57 foreach ($posts as $post) {
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);
64 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
66 $post_title = $wpdb->escape(trim($post_title[1]));
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);
72 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
73 $categories = $categories[1];
76 foreach ($categories as $category) {
77 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
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);
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);
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);
97 $post_author = $current_user->ID;
98 preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
99 $post_status = trim($post_status[1]);
102 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
103 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
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 ) ) {
112 _e('Couldn’t get post ID');
116 if(0 != count($categories))
117 wp_create_categories($categories, $post_id);
120 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
121 $comments = $comments[1];
124 $comment_post_ID = (int) $post_id;
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);
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);
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));
141 preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
142 $comment_author = $wpdb->escape(trim($comment_author[1]));
144 $comment_author_email = NULL;
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);
156 if ( $num_comments ) {
158 printf( _n('%s comment', '%s comments', $num_comments), $num_comments );
168 $file = wp_import_handle_upload();
169 if ( isset($file['error']) ) {
174 $this->file = $file['file'];
175 $result = $this->import_posts();
176 if ( is_wp_error( $result ) )
178 wp_import_cleanup($file['id']);
179 do_action('import_done', 'blogware');
181 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
185 function dispatch() {
186 if (empty ($_GET['step']))
189 $step = (int) $_GET['step'];
198 $result = $this->import();
199 if ( is_wp_error( $result ) )
200 $result->get_error_message();
207 function BW_Import() {
212 $blogware_import = new BW_Import();
214 register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));