[ Index ]
 

Code source de WordPress 2.1.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

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

   1  <?php
   2  
   3  class WP_Import {
   4  
   5      var $posts = array ();
   6      var $file;
   7      var $id;
   8      var $mtnames = array ();
   9      var $newauthornames = array ();
  10      var $j = -1;
  11  
  12  	function header() {
  13          echo '<div class="wrap">';
  14          echo '<h2>'.__('Import WordPress').'</h2>';
  15      }
  16  
  17  	function footer() {
  18          echo '</div>';
  19      }
  20  
  21  	function unhtmlentities($string) { // From php.net for < 4.3 compat
  22          $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  23          $trans_tbl = array_flip($trans_tbl);
  24          return strtr($string, $trans_tbl);
  25      }
  26  
  27  	function greet() {
  28          echo '<div class="narrow">';
  29          echo '<p>'.__('Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, comments, custom fields, and categories into this blog.').'</p>';
  30          echo '<p>'.__('Choose a WordPress WXR file to upload, then click Upload file and import.').'</p>';
  31          wp_import_upload_form("admin.php?import=wordpress&amp;step=1");
  32          echo '</div>';
  33      }
  34  
  35  	function get_tag( $string, $tag ) {
  36          preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return);
  37          $return = addslashes( trim( $return[1] ) );
  38          return $return;
  39      }
  40  
  41  	function users_form($n) {
  42          global $wpdb, $testing;
  43          $users = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY ID");
  44  ?><select name="userselect[<?php echo $n; ?>]">
  45      <option value="#NONE#">- Select -</option>
  46      <?php
  47          foreach ($users as $user) {
  48              echo '<option value="'.$user->user_login.'">'.$user->user_login.'</option>';
  49          }
  50  ?>
  51      </select>
  52      <?php
  53      }
  54  
  55      //function to check the authorname and do the mapping
  56  	function checkauthor($author) {
  57          global $wpdb;
  58          //mtnames is an array with the names in the mt import file
  59          $pass = 'changeme';
  60          if (!(in_array($author, $this->mtnames))) { //a new mt author name is found
  61              ++ $this->j;
  62              $this->mtnames[$this->j] = $author; //add that new mt author name to an array
  63              $user_id = username_exists($this->newauthornames[$this->j]); //check if the new author name defined by the user is a pre-existing wp user
  64              if (!$user_id) { //banging my head against the desk now.
  65                  if ($newauthornames[$this->j] == 'left_blank') { //check if the user does not want to change the authorname
  66                      $user_id = wp_create_user($author, $pass);
  67                      $this->newauthornames[$this->j] = $author; //now we have a name, in the place of left_blank.
  68                  } else {
  69                      $user_id = wp_create_user($this->newauthornames[$this->j], $pass);
  70                  }
  71              } else {
  72                  return $user_id; // return pre-existing wp username if it exists
  73              }
  74          } else {
  75              $key = array_search($author, $this->mtnames); //find the array key for $author in the $mtnames array
  76              $user_id = username_exists($this->newauthornames[$key]); //use that key to get the value of the author's name from $newauthornames
  77          }
  78  
  79          return $user_id;
  80      }
  81  
  82  	function get_entries() {
  83          set_magic_quotes_runtime(0);
  84          $importdata = file($this->file); // Read the file into an array
  85          $importdata = implode('', $importdata); // squish it
  86          $importdata = preg_replace("/(\r\n|\n|\r)/", "\n", $importdata);
  87          preg_match_all('|<item>(.*?)</item>|is', $importdata, $this->posts);
  88          $this->posts = $this->posts[1];
  89          preg_match_all('|<wp:category>(.*?)</wp:category>|is', $importdata, $this->categories);
  90          $this->categories = $this->categories[1];
  91      }
  92  
  93  	function get_wp_authors() {
  94          $temp = array ();
  95          $i = -1;
  96          foreach ($this->posts as $post) {
  97              if ('' != trim($post)) {
  98                  ++ $i;
  99                  $author = $this->get_tag( $post, 'dc:creator' );
 100                  array_push($temp, "$author"); //store the extracted author names in a temporary array
 101              }
 102          }
 103  
 104          // We need to find unique values of author names, while preserving the order, so this function emulates the unique_value(); php function, without the sorting.
 105          $authors[0] = array_shift($temp);
 106          $y = count($temp) + 1;
 107          for ($x = 1; $x < $y; $x ++) {
 108              $next = array_shift($temp);
 109              if (!(in_array($next, $authors)))
 110                  array_push($authors, "$next");
 111          }
 112  
 113          return $authors;
 114      }
 115  
 116  	function get_authors_from_post() {
 117          $formnames = array ();
 118          $selectnames = array ();
 119  
 120          foreach ($_POST['user'] as $key => $line) {
 121              $newname = trim(stripslashes($line));
 122              if ($newname == '')
 123                  $newname = 'left_blank'; //passing author names from step 1 to step 2 is accomplished by using POST. left_blank denotes an empty entry in the form.
 124              array_push($formnames, "$newname");
 125          } // $formnames is the array with the form entered names
 126  
 127          foreach ($_POST['userselect'] as $user => $key) {
 128              $selected = trim(stripslashes($key));
 129              array_push($selectnames, "$selected");
 130          }
 131  
 132          $count = count($formnames);
 133          for ($i = 0; $i < $count; $i ++) {
 134              if ($selectnames[$i] != '#NONE#') { //if no name was selected from the select menu, use the name entered in the form
 135                  array_push($this->newauthornames, "$selectnames[$i]");
 136              } else {
 137                  array_push($this->newauthornames, "$formnames[$i]");
 138              }
 139          }
 140      }
 141  
 142  	function wp_authors_form() {
 143  ?>
 144  <h2><?php _e('Assign Authors'); ?></h2>
 145  <p><?php _e('To make it easier for you to edit and save the imported posts and drafts, you may want to change the name of the author of the posts. For example, you may want to import all the entries as <code>admin</code>s entries.'); ?></p>
 146  <p><?php _e('If a new user is created by WordPress, the password will be set, by default, to "changeme". Quite suggestive, eh? ;)'); ?></p>
 147      <?php
 148  
 149  
 150          $authors = $this->get_wp_authors();
 151          echo '<ol id="authors">';
 152          echo '<form action="?import=wordpress&amp;step=2&amp;id=' . $this->id . '" method="post">';
 153          $j = -1;
 154          foreach ($authors as $author) {
 155              ++ $j;
 156              echo '<li>'.__('Current author:').' <strong>'.$author.'</strong><br />'.sprintf(__('Create user %1$s or map to existing'), ' <input type="text" value="'.$author.'" name="'.'user[]'.'" maxlength="30"> <br />');
 157              $this->users_form($j);
 158              echo '</li>';
 159          }
 160  
 161          echo '<input type="submit" value="Submit">'.'<br/>';
 162          echo '</form>';
 163          echo '</ol>';
 164  
 165      }
 166  
 167  	function select_authors() {
 168          $file = wp_import_handle_upload();
 169          if ( isset($file['error']) ) {
 170              $this->header();
 171              echo '<p>'.__('Sorry, there has been an error.').'</p>';
 172              echo '<p><strong>' . $file['error'] . '</strong></p>';
 173              $this->footer();
 174              return;
 175          }
 176          $this->file = $file['file'];
 177          $this->id = $file['id'];
 178  
 179          $this->get_entries();
 180          $this->wp_authors_form();
 181      }
 182  
 183  	function process_categories() {
 184          global $wpdb;
 185  
 186          $cat_names = (array) $wpdb->get_col("SELECT cat_name FROM $wpdb->categories");
 187  
 188          while ( $c = array_shift($this->categories) ) {
 189              $cat_name = trim(str_replace(array ('<![CDATA[', ']]>'), '', $this->get_tag( $c, 'wp:cat_name' )));
 190  
 191              // If the category exists we leave it alone
 192              if ( in_array($cat_name, $cat_names) )
 193                  continue;
 194  
 195              $category_nicename    = $this->get_tag( $c, 'wp:category_nicename' );
 196              $posts_private        = (int) $this->get_tag( $c, 'wp:posts_private' );
 197              $links_private        = (int) $this->get_tag( $c, 'wp:links_private' );
 198  
 199              $parent = $this->get_tag( $c, 'wp:category_parent' );
 200  
 201              if ( empty($parent) )
 202                  $category_parent = '0';
 203              else
 204                  $category_parent = (int) category_exists($parent);
 205  
 206              $catarr = compact('category_nicename', 'category_parent', 'posts_private', 'links_private', 'posts_private', 'cat_name');
 207  
 208              $cat_ID = wp_insert_category($catarr);
 209          }
 210      }
 211  
 212  	function process_posts() {
 213          global $wpdb;
 214          $i = -1;
 215          echo '<ol>';
 216          foreach ($this->posts as $post) {
 217  
 218              // There are only ever one of these
 219              $post_title     = $this->get_tag( $post, 'title' );
 220              $post_date      = $this->get_tag( $post, 'wp:post_date' );
 221              $post_date_gmt  = $this->get_tag( $post, 'wp:post_date_gmt' );
 222              $comment_status = $this->get_tag( $post, 'wp:comment_status' );
 223              $ping_status    = $this->get_tag( $post, 'wp:ping_status' );
 224              $post_status    = $this->get_tag( $post, 'wp:status' );
 225              $post_parent    = $this->get_tag( $post, 'wp:post_parent' );
 226              $post_type      = $this->get_tag( $post, 'wp:post_type' );
 227              $guid           = $this->get_tag( $post, 'guid' );
 228              $post_author    = $this->get_tag( $post, 'dc:creator' );
 229  
 230              $post_content = $this->get_tag( $post, 'content:encoded' );
 231              $post_content = str_replace(array ('<![CDATA[', ']]>'), '', $post_content);
 232              $post_content = preg_replace('|<(/?[A-Z]+)|e', "'<' . strtolower('$1')", $post_content);
 233              $post_content = str_replace('<br>', '<br />', $post_content);
 234              $post_content = str_replace('<hr>', '<hr />', $post_content);
 235  
 236              preg_match_all('|<category>(.*?)</category>|is', $post, $categories);
 237              $categories = $categories[1];
 238  
 239              $cat_index = 0;
 240              foreach ($categories as $category) {
 241                  $categories[$cat_index] = $wpdb->escape($this->unhtmlentities(str_replace(array ('<![CDATA[', ']]>'), '', $category)));
 242                  $cat_index++;
 243              }
 244  
 245              if ($post_id = post_exists($post_title, '', $post_date)) {
 246                  echo '<li>';
 247                  printf(__('Post <i>%s</i> already exists.'), stripslashes($post_title));
 248              } else {
 249                  echo '<li>';
 250                  printf(__('Importing post <i>%s</i>...'), stripslashes($post_title));
 251  
 252                  $post_author = $this->checkauthor($post_author); //just so that if a post already exists, new users are not created by checkauthor
 253  
 254                  $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_excerpt', 'post_status', 'comment_status', 'ping_status', 'post_modified', 'post_modified_gmt', 'guid', 'post_parent', 'post_type');
 255                  $comment_post_ID = $post_id = wp_insert_post($postdata);
 256                  // Add categories.
 257                  if (0 != count($categories)) {
 258                      wp_create_categories($categories, $post_id);
 259                  }
 260              }
 261  
 262                  // Now for comments
 263                  preg_match_all('|<wp:comment>(.*?)</wp:comment>|is', $post, $comments);
 264                  $comments = $comments[1];
 265                  $num_comments = 0;
 266                  if ( $comments) { foreach ($comments as $comment) {
 267                      $comment_author       = $this->get_tag( $comment, 'wp:comment_author');
 268                      $comment_author_email = $this->get_tag( $comment, 'wp:comment_author_email');
 269                      $comment_author_IP    = $this->get_tag( $comment, 'wp:comment_author_IP');
 270                      $comment_author_url   = $this->get_tag( $comment, 'wp:comment_author_url');
 271                      $comment_date         = $this->get_tag( $comment, 'wp:comment_date');
 272                      $comment_date_gmt     = $this->get_tag( $comment, 'wp:comment_date_gmt');
 273                      $comment_content      = $this->get_tag( $comment, 'wp:comment_content');
 274                      $comment_approved     = $this->get_tag( $comment, 'wp:comment_approved');
 275                      $comment_type         = $this->get_tag( $comment, 'wp:comment_type');
 276                      $comment_parent       = $this->get_tag( $comment, 'wp:comment_parent');
 277  
 278                      if ( !comment_exists($comment_author, $comment_date) ) {
 279                          $commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_url', 'comment_author_email', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_approved', 'comment_type', 'comment_parent');
 280                          wp_insert_comment($commentdata);
 281                          $num_comments++;
 282                      }
 283                  } }
 284                  if ( $num_comments )
 285                      printf(' '.__('(%s comments)'), $num_comments);
 286  
 287                  // Now for post meta
 288                  preg_match_all('|<wp:postmeta>(.*?)</wp:postmeta>|is', $post, $postmeta);
 289                  $postmeta = $postmeta[1];
 290                  if ( $postmeta) { foreach ($postmeta as $p) {
 291                      $key   = $this->get_tag( $p, 'wp:meta_key' );
 292                      $value = $this->get_tag( $p, 'wp:meta_value' );
 293                      add_post_meta( $post_id, $key, $value );
 294                  } }
 295  
 296              $index++;
 297          }
 298  
 299          echo '</ol>';
 300  
 301          wp_import_cleanup($this->id);
 302  
 303          echo '<h3>'.sprintf(__('All done.').' <a href="%s">'.__('Have fun!').'</a>', get_option('home')).'</h3>';
 304      }
 305  
 306  	function import() {
 307          $this->id = (int) $_GET['id'];
 308  
 309          $this->file = get_attached_file($this->id);
 310          $this->get_authors_from_post();
 311          $this->get_entries();
 312          $this->process_categories();
 313          $this->process_posts();
 314      }
 315  
 316  	function dispatch() {
 317          if (empty ($_GET['step']))
 318              $step = 0;
 319          else
 320              $step = (int) $_GET['step'];
 321  
 322          $this->header();
 323          switch ($step) {
 324              case 0 :
 325                  $this->greet();
 326                  break;
 327              case 1 :
 328                  $this->select_authors();
 329                  break;
 330              case 2:
 331                  $this->import();
 332                  break;
 333          }
 334          $this->footer();
 335      }
 336  
 337  	function WP_Import() {
 338          // Nothing.
 339      }
 340  }
 341  
 342  $wp_import = new WP_Import();
 343  
 344  register_importer('wordpress', 'WordPress', __('Import <strong>posts, comments, custom fields, pages, and categories</strong> from a WordPress export file'), array ($wp_import, 'dispatch'));
 345  
 346  ?>


Généré le : Fri Mar 30 19:41:27 2007 par Balluche grâce à PHPXref 0.7