10 * LiveJournal Importer class
12 * Imports your LiveJournal XML exported file into WordPress.
21 echo '<div class="wrap">';
23 echo '<h2>'.__('Import LiveJournal').'</h2>';
30 function unhtmlentities($string) { // From php.net for < 4.3 compat
31 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
32 $trans_tbl = array_flip($trans_tbl);
33 return strtr($string, $trans_tbl);
37 echo '<div class="narrow">';
38 echo '<p>'.__('Howdy! Upload your LiveJournal XML export file and we’ll import the posts into this blog.').'</p>';
39 echo '<p>'.__('Choose a LiveJournal XML file to upload, then click Upload file and import.').'</p>';
40 wp_import_upload_form("admin.php?import=livejournal&step=1");
44 function import_posts() {
45 global $wpdb, $current_user;
47 set_magic_quotes_runtime(0);
48 $importdata = file($this->file); // Read the file into an array
49 $importdata = implode('', $importdata); // squish it
50 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
52 preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
56 foreach ($posts as $post) {
57 preg_match('|<subject>(.*?)</subject>|is', $post, $post_title);
58 $post_title = $wpdb->escape(trim($post_title[1]));
59 if ( empty($post_title) ) {
60 preg_match('|<itemid>(.*?)</itemid>|is', $post, $post_title);
61 $post_title = $wpdb->escape(trim($post_title[1]));
64 preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date);
65 $post_date = strtotime($post_date[1]);
66 $post_date = date('Y-m-d H:i:s', $post_date);
68 preg_match('|<event>(.*?)</event>|is', $post, $post_content);
69 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
70 $post_content = $this->unhtmlentities($post_content);
73 $post_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $post_content);
74 $post_content = str_replace('<br>', '<br />', $post_content);
75 $post_content = str_replace('<hr>', '<hr />', $post_content);
76 $post_content = $wpdb->escape($post_content);
78 $post_author = $current_user->ID;
79 $post_status = 'publish';
82 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
83 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
85 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
86 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
87 $post_id = wp_insert_post($postdata);
88 if ( is_wp_error( $post_id ) )
91 _e("Couldn't get post ID");
97 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
98 $comments = $comments[1];
101 $comment_post_ID = (int) $post_id;
103 foreach ($comments as $comment) {
104 preg_match('|<event>(.*?)</event>|is', $comment, $comment_content);
105 $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
106 $comment_content = $this->unhtmlentities($comment_content);
109 $comment_content = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $comment_content);
110 $comment_content = str_replace('<br>', '<br />', $comment_content);
111 $comment_content = str_replace('<hr>', '<hr />', $comment_content);
112 $comment_content = $wpdb->escape($comment_content);
114 preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date);
115 $comment_date = trim($comment_date[1]);
116 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
118 preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
119 $comment_author = $wpdb->escape(trim($comment_author[1]));
121 preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
122 $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
124 $comment_approved = 1;
125 // Check if it's already there
126 if (!comment_exists($comment_author, $comment_date)) {
127 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
128 $commentdata = wp_filter_comment($commentdata);
129 wp_insert_comment($commentdata);
134 if ( $num_comments ) {
136 printf(__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
144 $file = wp_import_handle_upload();
145 if ( isset($file['error']) ) {
150 $this->file = $file['file'];
151 $result = $this->import_posts();
152 if ( is_wp_error( $result ) )
154 wp_import_cleanup($file['id']);
155 do_action('import_done', 'livejournal');
158 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
162 function dispatch() {
163 if (empty ($_GET['step']))
166 $step = (int) $_GET['step'];
175 check_admin_referer('import-upload');
176 $result = $this->import();
177 if ( is_wp_error( $result ) )
178 echo $result->get_error_message();
185 function LJ_Import() {
190 $livejournal_import = new LJ_Import();
192 register_importer('livejournal', __('LiveJournal'), __('Import posts from a LiveJournal XML export file.'), array ($livejournal_import, 'dispatch'));