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