-
+ AEBAE525E0A662A64F5DE26803D85D29D63B7A1864F5ECB3D16C5BDF560AEFB0C927C051D0916E8FE288F3DC23E27F955790F8FEE9B4CC441D3ABEDB3F751D6C
mp-wp/wp-admin/import/livejournal.php
(0 . 0)(1 . 193)
19106 <?php
19107 /**
19108 * LiveJournal Importer
19109 *
19110 * @package WordPress
19111 * @subpackage Importer
19112 */
19113
19114 /**
19115 * LiveJournal Importer class
19116 *
19117 * Imports your LiveJournal XML exported file into WordPress.
19118 *
19119 * @since unknown
19120 */
19121 class LJ_Import {
19122
19123 var $file;
19124
19125 function header() {
19126 echo '<div class="wrap">';
19127 screen_icon();
19128 echo '<h2>'.__('Import LiveJournal').'</h2>';
19129 }
19130
19131 function footer() {
19132 echo '</div>';
19133 }
19134
19135 function unhtmlentities($string) { // From php.net for < 4.3 compat
19136 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
19137 $trans_tbl = array_flip($trans_tbl);
19138 return strtr($string, $trans_tbl);
19139 }
19140
19141 function greet() {
19142 echo '<div class="narrow">';
19143 echo '<p>'.__('Howdy! Upload your LiveJournal XML export file and we’ll import the posts into this blog.').'</p>';
19144 echo '<p>'.__('Choose a LiveJournal XML file to upload, then click Upload file and import.').'</p>';
19145 wp_import_upload_form("admin.php?import=livejournal&step=1");
19146 echo '</div>';
19147 }
19148
19149 function import_posts() {
19150 global $wpdb, $current_user;
19151
19152 set_magic_quotes_runtime(0);
19153 $importdata = file($this->file); // Read the file into an array
19154 $importdata = implode('', $importdata); // squish it
19155 $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
19156
19157 preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
19158 $posts = $posts[1];
19159 unset($importdata);
19160 echo '<ol>';
19161 foreach ($posts as $post) {
19162 preg_match('|<subject>(.*?)</subject>|is', $post, $post_title);
19163 $post_title = $wpdb->escape(trim($post_title[1]));
19164 if ( empty($post_title) ) {
19165 preg_match('|<itemid>(.*?)</itemid>|is', $post, $post_title);
19166 $post_title = $wpdb->escape(trim($post_title[1]));
19167 }
19168
19169 preg_match('|<eventtime>(.*?)</eventtime>|is', $post, $post_date);
19170 $post_date = strtotime($post_date[1]);
19171 $post_date = date('Y-m-d H:i:s', $post_date);
19172
19173 preg_match('|<event>(.*?)</event>|is', $post, $post_content);
19174 $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
19175 $post_content = $this->unhtmlentities($post_content);
19176
19177 // Clean up content
19178 $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
19179 $post_content = str_replace('<br>', '<br />', $post_content);
19180 $post_content = str_replace('<hr>', '<hr />', $post_content);
19181 $post_content = $wpdb->escape($post_content);
19182
19183 $post_author = $current_user->ID;
19184 $post_status = 'publish';
19185
19186 echo '<li>';
19187 if ($post_id = post_exists($post_title, $post_content, $post_date)) {
19188 printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
19189 } else {
19190 printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
19191 $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
19192 $post_id = wp_insert_post($postdata);
19193 if ( is_wp_error( $post_id ) )
19194 return $post_id;
19195 if (!$post_id) {
19196 _e("Couldn't get post ID");
19197 echo '</li>';
19198 break;
19199 }
19200 }
19201
19202 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
19203 $comments = $comments[1];
19204
19205 if ( $comments ) {
19206 $comment_post_ID = (int) $post_id;
19207 $num_comments = 0;
19208 foreach ($comments as $comment) {
19209 preg_match('|<event>(.*?)</event>|is', $comment, $comment_content);
19210 $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
19211 $comment_content = $this->unhtmlentities($comment_content);
19212
19213 // Clean up content
19214 $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content);
19215 $comment_content = str_replace('<br>', '<br />', $comment_content);
19216 $comment_content = str_replace('<hr>', '<hr />', $comment_content);
19217 $comment_content = $wpdb->escape($comment_content);
19218
19219 preg_match('|<eventtime>(.*?)</eventtime>|is', $comment, $comment_date);
19220 $comment_date = trim($comment_date[1]);
19221 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
19222
19223 preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
19224 $comment_author = $wpdb->escape(trim($comment_author[1]));
19225
19226 preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
19227 $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
19228
19229 $comment_approved = 1;
19230 // Check if it's already there
19231 if (!comment_exists($comment_author, $comment_date)) {
19232 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
19233 $commentdata = wp_filter_comment($commentdata);
19234 wp_insert_comment($commentdata);
19235 $num_comments++;
19236 }
19237 }
19238 }
19239 if ( $num_comments ) {
19240 echo ' ';
19241 printf(__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
19242 }
19243 echo '</li>';
19244 }
19245 echo '</ol>';
19246 }
19247
19248 function import() {
19249 $file = wp_import_handle_upload();
19250 if ( isset($file['error']) ) {
19251 echo $file['error'];
19252 return;
19253 }
19254
19255 $this->file = $file['file'];
19256 $result = $this->import_posts();
19257 if ( is_wp_error( $result ) )
19258 return $result;
19259 wp_import_cleanup($file['id']);
19260 do_action('import_done', 'livejournal');
19261
19262 echo '<h3>';
19263 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
19264 echo '</h3>';
19265 }
19266
19267 function dispatch() {
19268 if (empty ($_GET['step']))
19269 $step = 0;
19270 else
19271 $step = (int) $_GET['step'];
19272
19273 $this->header();
19274
19275 switch ($step) {
19276 case 0 :
19277 $this->greet();
19278 break;
19279 case 1 :
19280 check_admin_referer('import-upload');
19281 $result = $this->import();
19282 if ( is_wp_error( $result ) )
19283 echo $result->get_error_message();
19284 break;
19285 }
19286
19287 $this->footer();
19288 }
19289
19290 function LJ_Import() {
19291 // Nothing.
19292 }
19293 }
19294
19295 $livejournal_import = new LJ_Import();
19296
19297 register_importer('livejournal', __('LiveJournal'), __('Import posts from a LiveJournal XML export file.'), array ($livejournal_import, 'dispatch'));
19298 ?>