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

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