home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Blogs / wordpress2.6.exe / wordpress2.6 / wp-admin / import / livejournal.php < prev    next >
Encoding:
PHP Script  |  2008-02-29  |  5.5 KB  |  180 lines

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