[ Index ]

PHP Cross Reference of Wordpress 2.7.1

title

Body

[close]

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

   1  <?php
   2  /**
   3   * LiveJournal Importer
   4   *
   5   * @package WordPress
   6   * @subpackage Importer
   7   */
   8  
   9  /**
  10   * LiveJournal Importer class
  11   *
  12   * Imports your LiveJournal XML exported file into WordPress.
  13   *
  14   * @since unknown
  15   */
  16  class LJ_Import {
  17  
  18      var $file;
  19  
  20  	function header() {
  21          echo '<div class="wrap">';
  22          screen_icon();
  23          echo '<h2>'.__('Import LiveJournal').'</h2>';
  24      }
  25  
  26  	function footer() {
  27          echo '</div>';
  28      }
  29  
  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);
  34      }
  35  
  36  	function greet() {
  37          echo '<div class="narrow">';
  38          echo '<p>'.__('Howdy! Upload your LiveJournal XML export file and we&#8217;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&amp;step=1");
  41          echo '</div>';
  42      }
  43  
  44  	function import_posts() {
  45          global $wpdb, $current_user;
  46  
  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);
  51  
  52          preg_match_all('|<entry>(.*?)</entry>|is', $importdata, $posts);
  53          $posts = $posts[1];
  54          unset($importdata);
  55          echo '<ol>';
  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]));
  62              }
  63  
  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);
  67  
  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);
  71  
  72              // Clean up 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);
  77  
  78              $post_author = $current_user->ID;
  79              $post_status = 'publish';
  80  
  81              echo '<li>';
  82              if ($post_id = post_exists($post_title, $post_content, $post_date)) {
  83                  printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
  84              } else {
  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 ) )
  89                      return $post_id;
  90                  if (!$post_id) {
  91                      _e("Couldn't get post ID");
  92                      echo '</li>';
  93                      break;
  94                  }
  95              }
  96  
  97              preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
  98              $comments = $comments[1];
  99  
 100              if ( $comments ) {
 101                  $comment_post_ID = (int) $post_id;
 102                  $num_comments = 0;
 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);
 107  
 108                      // Clean up 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);
 113  
 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));
 117  
 118                      preg_match('|<name>(.*?)</name>|is', $comment, $comment_author);
 119                      $comment_author = $wpdb->escape(trim($comment_author[1]));
 120  
 121                      preg_match('|<email>(.*?)</email>|is', $comment, $comment_author_email);
 122                      $comment_author_email = $wpdb->escape(trim($comment_author_email[1]));
 123  
 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);
 130                          $num_comments++;
 131                      }
 132                  }
 133              }
 134              if ( $num_comments ) {
 135                  echo ' ';
 136                  printf(__ngettext('(%s comment)', '(%s comments)', $num_comments), $num_comments);
 137              }
 138              echo '</li>';
 139          }
 140          echo '</ol>';
 141      }
 142  
 143  	function import() {
 144          $file = wp_import_handle_upload();
 145          if ( isset($file['error']) ) {
 146              echo $file['error'];
 147              return;
 148          }
 149  
 150          $this->file = $file['file'];
 151          $result = $this->import_posts();
 152          if ( is_wp_error( $result ) )
 153              return $result;
 154          wp_import_cleanup($file['id']);
 155          do_action('import_done', 'livejournal');
 156  
 157          echo '<h3>';
 158          printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
 159          echo '</h3>';
 160      }
 161  
 162  	function dispatch() {
 163          if (empty ($_GET['step']))
 164              $step = 0;
 165          else
 166              $step = (int) $_GET['step'];
 167  
 168          $this->header();
 169  
 170          switch ($step) {
 171              case 0 :
 172                  $this->greet();
 173                  break;
 174              case 1 :
 175                  check_admin_referer('import-upload');
 176                  $result = $this->import();
 177                  if ( is_wp_error( $result ) )
 178                      echo $result->get_error_message();
 179                  break;
 180          }
 181  
 182          $this->footer();
 183      }
 184  
 185  	function LJ_Import() {
 186          // Nothing.
 187      }
 188  }
 189  
 190  $livejournal_import = new LJ_Import();
 191  
 192  register_importer('livejournal', __('LiveJournal'), __('Import posts from a LiveJournal XML export file.'), array ($livejournal_import, 'dispatch'));
 193  ?>


Generated: Mon Mar 23 16:23:02 2009 Cross-referenced by PHPXref 0.7