mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * Blogware XML Importer
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 * @subpackage Importer
mp-wp_genesis 7 * @author Shayne Sweeney
mp-wp_genesis 8 * @link http://www.theshayne.com/
mp-wp_genesis 9 */
mp-wp_genesis 10
mp-wp_genesis 11 /**
mp-wp_genesis 12 * Blogware XML Importer class
mp-wp_genesis 13 *
mp-wp_genesis 14 * Extract posts from Blogware XML export file into your blog.
mp-wp_genesis 15 *
mp-wp_genesis 16 * @since unknown
mp-wp_genesis 17 */
mp-wp_genesis 18 class BW_Import {
mp-wp_genesis 19
mp-wp_genesis 20 var $file;
mp-wp_genesis 21
mp-wp_genesis 22 function header() {
mp-wp_genesis 23 echo '<div class="wrap">';
mp-wp_genesis 24 screen_icon();
mp-wp_genesis 25 echo '<h2>'.__('Import Blogware').'</h2>';
mp-wp_genesis 26 }
mp-wp_genesis 27
mp-wp_genesis 28 function footer() {
mp-wp_genesis 29 echo '</div>';
mp-wp_genesis 30 }
mp-wp_genesis 31
mp-wp_genesis 32 function unhtmlentities($string) { // From php.net for < 4.3 compat
mp-wp_genesis 33 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
mp-wp_genesis 34 $trans_tbl = array_flip($trans_tbl);
mp-wp_genesis 35 return strtr($string, $trans_tbl);
mp-wp_genesis 36 }
mp-wp_genesis 37
mp-wp_genesis 38 function greet() {
mp-wp_genesis 39 echo '<div class="narrow">';
mp-wp_genesis 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>';
mp-wp_genesis 41 wp_import_upload_form("admin.php?import=blogware&step=1");
mp-wp_genesis 42 echo '</div>';
mp-wp_genesis 43 }
mp-wp_genesis 44
mp-wp_genesis 45 function import_posts() {
mp-wp_genesis 46 global $wpdb, $current_user;
mp-wp_genesis 47
mp-wp_genesis 48 set_magic_quotes_runtime(0);
mp-wp_genesis 49 $importdata = file($this->file); // Read the file into an array
mp-wp_genesis 50 $importdata = implode('', $importdata); // squish it
mp-wp_genesis 51 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
mp-wp_genesis 52
mp-wp_genesis 53 preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
mp-wp_genesis 54 $posts = $posts[1];
mp-wp_genesis 55 unset($importdata);
mp-wp_genesis 56 echo '<ol>';
mp-wp_genesis 57 foreach ($posts as $post) {
mp-wp_genesis 58 flush();
mp-wp_genesis 59 preg_match('|<item type=\"(.*?)\">|is', $post, $post_type);
mp-wp_genesis 60 $post_type = $post_type[1];
mp-wp_genesis 61 if($post_type == "photo") {
mp-wp_genesis 62 preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title);
mp-wp_genesis 63 } else {
mp-wp_genesis 64 preg_match('|<title>(.*?)</title>|is', $post, $post_title);
mp-wp_genesis 65 }
mp-wp_genesis 66 $post_title = $wpdb->escape(trim($post_title[1]));
mp-wp_genesis 67
mp-wp_genesis 68 preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date);
mp-wp_genesis 69 $post_date = strtotime($post_date[1]);
mp-wp_genesis 70 $post_date = gmdate('Y-m-d H:i:s', $post_date);
mp-wp_genesis 71
mp-wp_genesis 72 preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
mp-wp_genesis 73 $categories = $categories[1];
mp-wp_genesis 74
mp-wp_genesis 75 $cat_index = 0;
mp-wp_genesis 76 foreach ($categories as $category) {
mp-wp_genesis 77 $categories[$cat_index] = $wpdb->escape($this->unhtmlentities($category));
mp-wp_genesis 78 $cat_index++;
mp-wp_genesis 79 }
mp-wp_genesis 80
mp-wp_genesis 81 if(strcasecmp($post_type, "photo") === 0) {
mp-wp_genesis 82 preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content);
mp-wp_genesis 83 $post_content = '<img src="'.trim($post_content[1]).'" />';
mp-wp_genesis 84 $post_content = $this->unhtmlentities($post_content);
mp-wp_genesis 85 } else {
mp-wp_genesis 86 preg_match('|<body>(.*?)</body>|is', $post, $post_content);
mp-wp_genesis 87 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
mp-wp_genesis 88 $post_content = $this->unhtmlentities($post_content);
mp-wp_genesis 89 }
mp-wp_genesis 90
mp-wp_genesis 91 // Clean up content
mp-wp_genesis 92 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
mp-wp_genesis 93 $post_content = str_replace('<br>', '<br />', $post_content);
mp-wp_genesis 94 $post_content = str_replace('<hr>', '<hr />', $post_content);
mp-wp_genesis 95 $post_content = $wpdb->escape($post_content);
mp-wp_genesis 96
mp-wp_genesis 97 $post_author = $current_user->ID;
mp-wp_genesis 98 preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
mp-wp_genesis 99 $post_status = trim($post_status[1]);
mp-wp_genesis 100
mp-wp_genesis 101 echo '<li>';
mp-wp_genesis 102 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
mp-wp_genesis 103 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
mp-wp_genesis 104 } else {
mp-wp_genesis 105 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
mp-wp_genesis 106 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
mp-wp_genesis 107 $post_id = wp_insert_post($postdata);
mp-wp_genesis 108 if ( is_wp_error( $post_id ) ) {
mp-wp_genesis 109 return $post_id;
mp-wp_genesis 110 }
mp-wp_genesis 111 if (!$post_id) {
mp-wp_genesis 112 _e("Couldn't get post ID");
mp-wp_genesis 113 echo '</li>';
mp-wp_genesis 114 break;
mp-wp_genesis 115 }
mp-wp_genesis 116 if(0 != count($categories))
mp-wp_genesis 117 wp_create_categories($categories, $post_id);
mp-wp_genesis 118 }
mp-wp_genesis 119
mp-wp_genesis 120 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
mp-wp_genesis 121 $comments = $comments[1];
mp-wp_genesis 122
mp-wp_genesis 123 if ( $comments ) {
mp-wp_genesis 124 $comment_post_ID = (int) $post_id;
mp-wp_genesis 125 $num_comments = 0;
mp-wp_genesis 126 foreach ($comments as $comment) {
mp-wp_genesis 127 preg_match('|<body>(.*?)</body>|is', $comment, $comment_content);
mp-wp_genesis 128 $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
mp-wp_genesis 129 $comment_content = $this->unhtmlentities($comment_content);
mp-wp_genesis 130
mp-wp_genesis 131 // Clean up content
mp-wp_genesis 132 $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
mp-wp_genesis 133 $comment_content = str_replace('<br>', '<br />', $comment_content);
mp-wp_genesis 134 $comment_content = str_replace('<hr>', '<hr />', $comment_content);
mp-wp_genesis 135 $comment_content = $wpdb->escape($comment_content);
mp-wp_genesis 136
mp-wp_genesis 137 preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date);
mp-wp_genesis 138 $comment_date = trim($comment_date[1]);
mp-wp_genesis 139 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
mp-wp_genesis 140
mp-wp_genesis 141 preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
mp-wp_genesis 142 $comment_author = $wpdb->escape(trim($comment_author[1]));
mp-wp_genesis 143
mp-wp_genesis 144 $comment_author_email = NULL;
mp-wp_genesis 145
mp-wp_genesis 146 $comment_approved = 1;
mp-wp_genesis 147 // Check if it's already there
mp-wp_genesis 148 if (!comment_exists($comment_author, $comment_date)) {
mp-wp_genesis 149 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
mp-wp_genesis 150 $commentdata = wp_filter_comment($commentdata);
mp-wp_genesis 151 wp_insert_comment($commentdata);
mp-wp_genesis 152 $num_comments++;
mp-wp_genesis 153 }
mp-wp_genesis 154 }
mp-wp_genesis 155 }
mp-wp_genesis 156 if ( $num_comments ) {
mp-wp_genesis 157 echo ' ';
mp-wp_genesis 158 printf( __ngettext('%s comment', '%s comments', $num_comments), $num_comments );
mp-wp_genesis 159 }
mp-wp_genesis 160 echo '</li>';
mp-wp_genesis 161 flush();
mp-wp_genesis 162 ob_flush();
mp-wp_genesis 163 }
mp-wp_genesis 164 echo '</ol>';
mp-wp_genesis 165 }
mp-wp_genesis 166
mp-wp_genesis 167 function import() {
mp-wp_genesis 168 $file = wp_import_handle_upload();
mp-wp_genesis 169 if ( isset($file['error']) ) {
mp-wp_genesis 170 echo $file['error'];
mp-wp_genesis 171 return;
mp-wp_genesis 172 }
mp-wp_genesis 173
mp-wp_genesis 174 $this->file = $file['file'];
mp-wp_genesis 175 $result = $this->import_posts();
mp-wp_genesis 176 if ( is_wp_error( $result ) )
mp-wp_genesis 177 return $result;
mp-wp_genesis 178 wp_import_cleanup($file['id']);
mp-wp_genesis 179 do_action('import_done', 'blogware');
mp-wp_genesis 180 echo '<h3>';
mp-wp_genesis 181 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
mp-wp_genesis 182 echo '</h3>';
mp-wp_genesis 183 }
mp-wp_genesis 184
mp-wp_genesis 185 function dispatch() {
mp-wp_genesis 186 if (empty ($_GET['step']))
mp-wp_genesis 187 $step = 0;
mp-wp_genesis 188 else
mp-wp_genesis 189 $step = (int) $_GET['step'];
mp-wp_genesis 190
mp-wp_genesis 191 $this->header();
mp-wp_genesis 192
mp-wp_genesis 193 switch ($step) {
mp-wp_genesis 194 case 0 :
mp-wp_genesis 195 $this->greet();
mp-wp_genesis 196 break;
mp-wp_genesis 197 case 1 :
mp-wp_genesis 198 $result = $this->import();
mp-wp_genesis 199 if ( is_wp_error( $result ) )
mp-wp_genesis 200 $result->get_error_message();
mp-wp_genesis 201 break;
mp-wp_genesis 202 }
mp-wp_genesis 203
mp-wp_genesis 204 $this->footer();
mp-wp_genesis 205 }
mp-wp_genesis 206
mp-wp_genesis 207 function BW_Import() {
mp-wp_genesis 208 // Nothing.
mp-wp_genesis 209 }
mp-wp_genesis 210 }
mp-wp_genesis 211
mp-wp_genesis 212 $blogware_import = new BW_Import();
mp-wp_genesis 213
mp-wp_genesis 214 register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));
mp-wp_genesis 215 ?>