[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Blogware XML Importer
   4   *
   5   * @package WordPress
   6   * @subpackage Importer
   7   */
   8  
   9  /**
  10   * Blogware XML Importer class
  11   *
  12   * Extract posts from Blogware XML export file into your blog.
  13   *
  14   * @since unknown
  15   */
  16  class BW_Import {
  17  
  18      var $file;
  19  
  20  	function header() {
  21          echo '<div class="wrap">';
  22          screen_icon();
  23          echo '<h2>'.__('Import Blogware').'</h2>';
  24      }
  25  
  26  	function footer() {
  27          echo '</div>';
  28      }
  29  
  30  	function greet() {
  31          echo '<div class="narrow">';
  32          echo '<p>'.__('Howdy! This importer allows you to extract posts from Blogware XML export file into your site. Pick a Blogware file to upload and click Import.').'</p>';
  33          wp_import_upload_form("admin.php?import=blogware&amp;step=1");
  34          echo '</div>';
  35      }
  36  
  37  	function _normalize_tag( $matches ) {
  38          return '<' . strtolower( $matches[1] );
  39      }
  40  
  41  	function import_posts() {
  42          global $wpdb, $current_user;
  43  
  44          set_magic_quotes_runtime(0);
  45          $importdata = file($this->file); // Read the file into an array
  46          $importdata = implode('', $importdata); // squish it
  47          $importdata = str_replace(array ("\r\n", "\r"), "\n", $importdata);
  48  
  49          preg_match_all('|(<item[^>]+>(.*?)</item>)|is', $importdata, $posts);
  50          $posts = $posts[1];
  51          unset($importdata);
  52          echo '<ol>';
  53          foreach ($posts as $post) {
  54              flush();
  55              preg_match('|<item type=\"(.*?)\">|is', $post, $post_type);
  56              $post_type = $post_type[1];
  57              if ($post_type == "photo") {
  58                  preg_match('|<photoFilename>(.*?)</photoFilename>|is', $post, $post_title);
  59              } else {
  60                  preg_match('|<title>(.*?)</title>|is', $post, $post_title);
  61              }
  62              $post_title = $wpdb->escape(trim($post_title[1]));
  63  
  64              preg_match('|<pubDate>(.*?)</pubDate>|is', $post, $post_date);
  65              $post_date = strtotime($post_date[1]);
  66              $post_date = gmdate('Y-m-d H:i:s', $post_date);
  67  
  68              preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
  69              $categories = $categories[1];
  70  
  71              $cat_index = 0;
  72              foreach ($categories as $category) {
  73                  $categories[$cat_index] = $wpdb->escape( html_entity_decode($category) );
  74                  $cat_index++;
  75              }
  76  
  77              if ( strcasecmp($post_type, "photo") === 0 ) {
  78                  preg_match('|<sizedPhotoUrl>(.*?)</sizedPhotoUrl>|is', $post, $post_content);
  79                  $post_content = '<img src="'.trim($post_content[1]).'" />';
  80                  $post_content = html_entity_decode( $post_content );
  81              } else {
  82                  preg_match('|<body>(.*?)</body>|is', $post, $post_content);
  83                  $post_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($post_content[1]));
  84                  $post_content = html_entity_decode( $post_content );
  85              }
  86  
  87              // Clean up content
  88              $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content);
  89              $post_content = str_replace('<br>', '<br />', $post_content);
  90              $post_content = str_replace('<hr>', '<hr />', $post_content);
  91              $post_content = $wpdb->escape($post_content);
  92  
  93              $post_author = $current_user->ID;
  94              preg_match('|<postStatus>(.*?)</postStatus>|is', $post, $post_status);
  95              $post_status = trim($post_status[1]);
  96  
  97              echo '<li>';
  98              if ($post_id = post_exists($post_title, $post_content, $post_date)) {
  99                  printf(__('Post <em>%s</em> already exists.'), stripslashes($post_title));
 100              } else {
 101                  printf(__('Importing post <em>%s</em>...'), stripslashes($post_title));
 102                  $postdata = compact('post_author', 'post_date', 'post_content', 'post_title', 'post_status');
 103                  $post_id = wp_insert_post($postdata);
 104                  if ( is_wp_error( $post_id ) ) {
 105                      return $post_id;
 106                  }
 107                  if (!$post_id) {
 108                      _e('Couldn&#8217;t get post ID');
 109                      echo '</li>';
 110                      break;
 111                  }
 112                  if ( 0 != count($categories) )
 113                      wp_create_categories($categories, $post_id);
 114              }
 115  
 116              preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments);
 117              $comments = $comments[1];
 118  
 119              if ( $comments ) {
 120                  $comment_post_ID = (int) $post_id;
 121                  $num_comments = 0;
 122                  foreach ($comments as $comment) {
 123                      preg_match('|<body>(.*?)</body>|is', $comment, $comment_content);
 124                      $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1]));
 125                      $comment_content = html_entity_decode( $comment_content );
 126  
 127                      // Clean up content
 128                      $comment_content = preg_replace_callback('|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $comment_content);
 129                      $comment_content = str_replace('<br>', '<br />', $comment_content);
 130                      $comment_content = str_replace('<hr>', '<hr />', $comment_content);
 131                      $comment_content = $wpdb->escape($comment_content);
 132  
 133                      preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date);
 134                      $comment_date = trim($comment_date[1]);
 135                      $comment_date = date('Y-m-d H:i:s', strtotime($comment_date));
 136  
 137                      preg_match('|<author>(.*?)</author>|is', $comment, $comment_author);
 138                      $comment_author = $wpdb->escape(trim($comment_author[1]));
 139  
 140                      $comment_author_email = NULL;
 141  
 142                      $comment_approved = 1;
 143                      // Check if it's already there
 144                      if (!comment_exists($comment_author, $comment_date)) {
 145                          $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved');
 146                          $commentdata = wp_filter_comment($commentdata);
 147                          wp_insert_comment($commentdata);
 148                          $num_comments++;
 149                      }
 150                  }
 151              }
 152              if ( $num_comments ) {
 153                  echo ' ';
 154                  printf( _n('%s comment', '%s comments', $num_comments), $num_comments );
 155              }
 156              echo '</li>';
 157              flush();
 158              ob_flush();
 159          }
 160          echo '</ol>';
 161      }
 162  
 163  	function import() {
 164          $file = wp_import_handle_upload();
 165          if ( isset($file['error']) ) {
 166              echo $file['error'];
 167              return;
 168          }
 169  
 170          $this->file = $file['file'];
 171          $result = $this->import_posts();
 172          if ( is_wp_error( $result ) )
 173              return $result;
 174          wp_import_cleanup($file['id']);
 175          do_action('import_done', 'blogware');
 176          echo '<h3>';
 177          printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home'));
 178          echo '</h3>';
 179      }
 180  
 181  	function dispatch() {
 182          if (empty ($_GET['step']))
 183              $step = 0;
 184          else
 185              $step = (int) $_GET['step'];
 186  
 187          $this->header();
 188  
 189          switch ($step) {
 190              case 0 :
 191                  $this->greet();
 192                  break;
 193              case 1 :
 194                  $result = $this->import();
 195                  if ( is_wp_error( $result ) )
 196                      $result->get_error_message();
 197                  break;
 198          }
 199  
 200          $this->footer();
 201      }
 202  
 203  	function BW_Import() {
 204          // Nothing.
 205      }
 206  }
 207  
 208  $blogware_import = new BW_Import();
 209  
 210  register_importer('blogware', __('Blogware'), __('Import posts from Blogware.'), array ($blogware_import, 'dispatch'));
 211  ?>


Generated: Thu May 20 03:55:44 2010 Hosted by follow the white rabbit.