| [ Index ] |
|
Code source de WordPress 2.1.2 |
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 <i>%s</i> already exists.'), stripslashes($post_title)); 90 } else { 91 printf(__('Importing post <i>%s</i>...'), 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 (!$post_id) { 95 _e("Couldn't get post ID"); 96 echo '</li>'; 97 break; 98 } 99 if(0 != count($categories)) 100 wp_create_categories($categories, $post_id); 101 } 102 103 preg_match_all('|<comment>(.*?)</comment>|is', $post, $comments); 104 $comments = $comments[1]; 105 106 if ( $comments ) { 107 $comment_post_ID = $post_id; 108 $num_comments = 0; 109 foreach ($comments as $comment) { 110 preg_match('|<body>(.*?)</body>|is', $comment, $comment_content); 111 $comment_content = str_replace(array ('<![CDATA[', ']]>'), '', trim($comment_content[1])); 112 $comment_content = $this->unhtmlentities($comment_content); 113 114 // Clean up content 115 $comment_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $comment_content); 116 $comment_content = str_replace('<br>', '<br />', $comment_content); 117 $comment_content = str_replace('<hr>', '<hr />', $comment_content); 118 $comment_content = $wpdb->escape($comment_content); 119 120 preg_match('|<pubDate>(.*?)</pubDate>|is', $comment, $comment_date); 121 $comment_date = trim($comment_date[1]); 122 $comment_date = date('Y-m-d H:i:s', strtotime($comment_date)); 123 124 preg_match('|<author>(.*?)</author>|is', $comment, $comment_author); 125 $comment_author = $wpdb->escape(trim($comment_author[1])); 126 127 $comment_author_email = NULL; 128 129 $comment_approved = 1; 130 // Check if it's already there 131 if (!comment_exists($comment_author, $comment_date)) { 132 $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_date', 'comment_content', 'comment_approved'); 133 $commentdata = wp_filter_comment($commentdata); 134 wp_insert_comment($commentdata); 135 $num_comments++; 136 } 137 } 138 } 139 if ( $num_comments ) { 140 echo ' '; 141 printf(__('(%s comments)'), $num_comments); 142 } 143 echo '</li>'; 144 flush(); 145 ob_flush(); 146 } 147 echo '</ol>'; 148 } 149 150 function import() { 151 $file = wp_import_handle_upload(); 152 if ( isset($file['error']) ) { 153 echo $file['error']; 154 return; 155 } 156 157 $this->file = $file['file']; 158 $this->import_posts(); 159 wp_import_cleanup($file['id']); 160 161 echo '<h3>'; 162 printf(__('All done. <a href="%s">Have fun!</a>'), get_option('home')); 163 echo '</h3>'; 164 } 165 166 function dispatch() { 167 if (empty ($_GET['step'])) 168 $step = 0; 169 else 170 $step = (int) $_GET['step']; 171 172 $this->header(); 173 174 switch ($step) { 175 case 0 : 176 $this->greet(); 177 break; 178 case 1 : 179 $this->import(); 180 break; 181 } 182 183 $this->footer(); 184 } 185 186 function BW_Import() { 187 // Nothing. 188 } 189 } 190 191 $blogware_import = new BW_Import(); 192 193 register_importer('blogware', __('Blogware'), __('Import posts from Blogware'), array ($blogware_import, 'dispatch')); 194 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 30 19:41:27 2007 | par Balluche grâce à PHPXref 0.7 |