[ Index ]

WordPress Source Cross Reference

title

Body

[close]

/wp-admin/import/ -> livejournal.php (source)

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

Your comment here...

Name: Location:
Comments:


List: Classes | Functions | Variables | Constants | Tables

Generated: Sat Jul 15 11:57:04 2006 Courtesy of Taragana