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 _normalize_tag( $matches ) {
46 return '<' . strtolower( $matches[1] );
49 function import_posts() {
50 global $wpdb, $current_user;
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);
57 preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
61 foreach ($posts as $post) {
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);
68 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
70 $post_title = $wpdb->escape(trim($post_title[1]));
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);
76 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
77 $categories = $categories[1];
80 foreach ($categories as $category) {
81 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
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);
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);
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);
101 $post_author = $current_user->ID;
102 preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
103 $post_status = trim($post_status[1]);
106 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
107 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
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 ) ) {
116 _e('Couldn’t get post ID');
120 if(0 != count($categories))
121 wp_create_categories($categories, $post_id);
124 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
125 $comments = $comments[1];
128 $comment_post_ID = (int) $post_id;
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);
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);
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));
145 preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
146 $comment_author = $wpdb->escape(trim($comment_author[1]));
148 $comment_author_email = NULL;
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);
160 if ( $num_comments ) {
162 printf( _n('%s comment', '%s comments', $num_comments), $num_comments );
172 $file = wp_import_handle_upload();
173 if ( isset($file['error']) ) {
178 $this->file = $file['file'];
179 $result = $this->import_posts();
180 if ( is_wp_error( $result ) )
182 wp_import_cleanup($file['id']);
183 do_action('import_done', 'blogware');
185 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
189 function dispatch() {
190 if (empty ($_GET['step']))
193 $step = (int) $_GET['step'];
202 $result = $this->import();
203 if ( is_wp_error( $result ) )
204 $result->get_error_message();
211 function BW_Import() {
216 $blogware_import = new BW_Import();
218 register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));