[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/include/admin/importers/ -> wordpress.inc.php (source)

   1  <?php # $Id: wordpress.inc.php 1629 2007-02-22 11:56:26Z garvinhicking $
   2  # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
   3  # All rights reserved.  See LICENSE file for licensing details
   4  
   5  /*****************************************************************
   6   *                WordPress Importer, by Evan Nemerson           *
   7   *****************************************************************/
   8  
   9  class Serendipity_Import_WordPress extends Serendipity_Import {
  10      var $info        = array('software' => 'WordPress');
  11      var $data        = array();
  12      var $inputFields = array();
  13  
  14  
  15      function Serendipity_Import_WordPress($data) {
  16          $this->data = $data;
  17          $this->inputFields = array(array('text' => INSTALL_DBHOST,
  18                                           'type' => 'input',
  19                                           'name' => 'host'),
  20  
  21                                     array('text' => INSTALL_DBUSER,
  22                                           'type' => 'input',
  23                                           'name' => 'user'),
  24  
  25                                     array('text' => INSTALL_DBPASS,
  26                                           'type' => 'protected',
  27                                           'name' => 'pass'),
  28  
  29                                     array('text' => INSTALL_DBNAME,
  30                                           'type' => 'input',
  31                                           'name' => 'name'),
  32  
  33                                     array('text' => INSTALL_DBPREFIX,
  34                                           'type' => 'input',
  35                                           'name' => 'prefix'),
  36  
  37                                     array('text'    => CHARSET,
  38                                           'type'    => 'list',
  39                                           'name'    => 'charset',
  40                                           'value'   => 'UTF-8',
  41                                           'default' => $this->getCharsets(true)),
  42  
  43                                     array('text'    => CONVERT_HTMLENTITIES,
  44                                           'type'    => 'bool',
  45                                           'name'    => 'use_strtr',
  46                                           'default' => 'true'),
  47  
  48                                     array('text'    => ACTIVATE_AUTODISCOVERY,
  49                                           'type'    => 'bool',
  50                                           'name'    => 'autodiscovery',
  51                                           'default' => 'false'),
  52                                     
  53                                     array('text'    => IMPORT_WP_PAGES,
  54                                           'type'    => 'bool',
  55                                           'name'    => 'import_all',
  56                                           'default' => 'false'      
  57                                     )
  58                              );
  59      }
  60  
  61      function validateData() {
  62          return sizeof($this->data);
  63      }
  64  
  65      function getInputFields() {
  66          return $this->inputFields;
  67      }
  68  
  69      function import() {
  70          global $serendipity;
  71          static $debug = true;
  72  
  73          // Save this so we can return it to its original value at the end of this method.
  74          $noautodiscovery = isset($serendipity['noautodiscovery']) ? $serendipity['noautodiscovery'] : false;
  75  
  76          if ($this->data['autodiscovery'] == 'false') {
  77              $serendipity['noautodiscovery'] = 1;
  78          }
  79  
  80          $this->getTransTable();
  81  
  82          $this->data['prefix'] = serendipity_db_escape_string($this->data['prefix']);
  83          $users = array();
  84          $categories = array();
  85          $entries = array();
  86  
  87          if ( !extension_loaded('mysql') ) {
  88              return MYSQL_REQUIRED;
  89          }
  90  
  91          if (function_exists('set_time_limit')) {
  92              @set_time_limit(300);
  93          }
  94  
  95          $wpdb = @mysql_connect($this->data['host'], $this->data['user'], $this->data['pass']);
  96          if (!$wpdb) {
  97              return sprintf(COULDNT_CONNECT, $this->data['host']);
  98          }
  99  
 100          if (!@mysql_select_db($this->data['name'])) {
 101              return sprintf(COULDNT_SELECT_DB, mysql_error($wpdb));
 102          }
 103          
 104          // This will hold the s9y <-> WP ID associations.
 105          $assoc = array();
 106  
 107          /* Users */
 108          // Fields: ID, user_login, user_pass, user_email, user_level
 109          $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}users;", $wpdb);
 110          if (!$res) {
 111              printf(COULDNT_SELECT_USER_INFO, mysql_error($wpdb));
 112          } else {
 113              if ($debug) echo "Importing users...<br />\n";
 114              for ($x=0, $c = mysql_num_rows($res) ; $x < $c ; $x++) {
 115                  $users[$x] = mysql_fetch_assoc($res);
 116      
 117                  $data = array('right_publish' => (!isset($users[$x]['user_level']) || $users[$x]['user_level'] >= 1) ? 1 : 0,
 118                                'realname'      => $users[$x]['user_login'],
 119                                'username'      => $users[$x]['user_login'],
 120                                'password'      => $users[$x]['user_pass']); // WP uses md5, too.
 121      
 122                  if (isset($users[$x]['user_level']) && $users[$x]['user_level'] <= 1) {
 123                      $data['userlevel'] = USERLEVEL_EDITOR;
 124                  } elseif (isset($users[$x]['user_level']) && $users[$x]['user_level'] < 5) {
 125                      $data['userlevel'] = USERLEVEL_CHIEF;
 126                  } else {
 127                      $data['userlevel'] = USERLEVEL_ADMIN;
 128                  }
 129      
 130                  if ($serendipity['serendipityUserlevel'] < $data['userlevel']) {
 131                      $data['userlevel'] = $serendipity['serendipityUserlevel'];
 132                  }
 133      
 134                  serendipity_db_insert('authors', $this->strtrRecursive($data));
 135                  $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid');
 136                  
 137                  // Set association.
 138                  $assoc['users'][$users[$x]['ID']] = $users[$x]['authorid'];
 139              }
 140              if ($debug) echo "Imported users.<br />\n";
 141              
 142              // Clean memory
 143              unset($users);
 144          }
 145  
 146          /* Categories */
 147          $res = @$this->nativeQuery("SELECT cat_ID, cat_name, category_description, category_parent 
 148                                        FROM {$this->data['prefix']}categories 
 149                                    ORDER BY category_parent, cat_ID;", $wpdb);
 150          if (!$res) {
 151              printf(COULDNT_SELECT_CATEGORY_INFO, mysql_error($wpdb));
 152          } else {
 153              if ($debug) echo "Importing categories...<br />\n";
 154  
 155              // Get all the info we need
 156              for ($x=0 ; $x<mysql_num_rows($res) ; $x++) {
 157                  $categories[] = mysql_fetch_assoc($res);
 158              }
 159      
 160              // Insert all categories as top level (we need to know everyone's ID before we can represent the hierarchy).
 161              for ($x=0, $c = sizeof($categories) ; $x < $c ; $x++) {
 162                  $cat = array('category_name'        => $categories[$x]['cat_name'],
 163                               'category_description' => $categories[$x]['category_description'],
 164                               'parentid'             => 0,
 165                               'category_left'        => 0,
 166                               'category_right'       => 0);
 167      
 168                  serendipity_db_insert('category', $this->strtrRecursive($cat));
 169                  $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid');
 170                  
 171                  // Set association.
 172                  $assoc['categories'][$categories[$x]['cat_ID']] = $categories[$x]['categoryid'];
 173              }
 174      
 175              foreach ($categories as $cat) {
 176                  if ($cat['category_parent'] != 0) {
 177                      // Find the parent
 178                      $par_id = 0;
 179                      foreach ($categories as $possible_par) {
 180                          if ($possible_par['cat_ID'] == $cat['category_parent']) {
 181                              $par_id = $possible_par['categoryid'];
 182                              break;
 183                          }
 184                      }
 185      
 186                      if ($par_id != 0) {
 187                          serendipity_db_query("UPDATE {$serendipity['dbPrefix']}category 
 188                                                   SET parentid={$par_id} 
 189                                                 WHERE categoryid={$cat['categoryid']};");
 190                      }
 191                  }
 192              }
 193  
 194              // Clean memory
 195              unset($categories);
 196  
 197              if ($debug) echo "Imported categories.<br />\n";
 198              if ($debug) echo "Rebuilding category tree...<br />\n";
 199              serendipity_rebuildCategoryTree();
 200              if ($debug) echo "Rebuilt category tree.<br />\n";
 201          }
 202  
 203  
 204          /* Entries */
 205          if (serendipity_db_bool($this->data['import_all'])) {
 206              $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}posts WHERE post_status IN ('publish', 'draft') ORDER BY post_date;", $wpdb);
 207          } else {
 208              $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}posts ORDER BY post_date;", $wpdb);
 209          }
 210  
 211          if (!$res) {
 212              printf(COULDNT_SELECT_ENTRY_INFO, mysql_error($wpdb));
 213          } else {
 214              if ($debug) echo "Importing entries...<br />\n";
 215              for ($x=0, $c = mysql_num_rows($res) ; $x < $c ; $x++ ) {
 216                  $entries[$x] = mysql_fetch_assoc($res);
 217      
 218                  $content  = explode('<!--more-->', $entries[$x]['post_content'], 2);
 219                  $body     = $content[0];
 220                  $extended = $content[1];
 221      
 222                  $entry = array('title'          => $this->decode($entries[$x]['post_title']), // htmlentities() is called later, so we can leave this.
 223                                 'isdraft'        => ($entries[$x]['post_status'] == 'publish') ? 'false' : 'true',
 224                                 'allow_comments' => ($entries[$x]['comment_status'] == 'open' ) ? 'true' : 'false',
 225                                 'timestamp'      => strtotime($entries[$x]['post_date']),
 226                                 'body'           => $this->strtr($body),
 227                                 'extended'       => $this->strtr($extended),
 228                                 'authorid'       => $assoc['users'][$entries[$x]['post_author']]);
 229      
 230                  if (!is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry))) {
 231                      printf(COULDNT_SELECT_ENTRY_INFO, mysql_error($wpdb));
 232                      echo "ID: {$entries[$x]['ID']} - {$entry['title']}<br />\n";
 233                      return $entries[$x]['entryid'];
 234                  }
 235                  
 236                  $assoc['entries'][$entries[$x]['ID']] = $entries[$x]['entryid'];
 237              }
 238              if ($debug) echo "Imported entries...<br />\n";
 239  
 240              // Clean memory
 241              unset($entries);
 242          }
 243      
 244          /* Entry/category */
 245          $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}post2cat;", $wpdb);
 246          if (!$res) {
 247              printf(COULDNT_SELECT_ENTRY_INFO, mysql_error($wpdb));
 248          } else {
 249              if ($debug) echo "Importing category associations...<br />\n";
 250              while ($a = mysql_fetch_assoc($res)) {
 251                  $data = array('entryid'    => $assoc['entries'][$a['post_id']],
 252                                'categoryid' => $assoc['categories'][$a['category_id']]);
 253                  serendipity_db_insert('entrycat', $this->strtrRecursive($data));
 254              }
 255              if ($debug) echo "Imported category associations.<br />\n";
 256          }
 257  
 258          /* Comments */
 259          $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}comments;", $wpdb);
 260          if (!$res) {
 261              printf(COULDNT_SELECT_COMMENT_INFO, mysql_error($wpdb));
 262          } else {
 263              $serendipity['allowSubscriptions'] = false;
 264              if ($debug) echo "Importing comments...<br />\n";
 265              while ($a = mysql_fetch_assoc($res)) {
 266                  $comment = array('entry_id ' => $assoc['entries'][$a['comment_post_ID']],
 267                                   'parent_id' => 0,
 268                                   'timestamp' => strtotime($a['comment_date']),
 269                                   'author'    => $a['comment_author'],
 270                                   'email'     => $a['comment_author_email'],
 271                                   'url'       => $a['comment_author_url'],
 272                                   'ip'        => $a['comment_author_IP'],
 273                                   'status'    => (empty($a['comment_approved']) || $a['comment_approved'] == '1') ? 'approved' : 'pending',
 274                                   'subscribed'=> 'false',
 275                                   'body'      => $a['comment_content'],
 276                                   'type'      => 'NORMAL');
 277      
 278                  serendipity_db_insert('comments', $this->strtrRecursive($comment));
 279                  if ($comment['status'] == 'approved') {
 280                      $cid = serendipity_db_insert_id('comments', 'id');
 281                      serendipity_approveComment($cid, $comment['entry_id'], true);
 282                  }
 283              }
 284              if ($debug) echo "Imported comments.<br />\n";
 285          }
 286  
 287          $serendipity['noautodiscovery'] = $noautodiscovery;
 288  
 289          // That was fun.
 290          return true;
 291      }
 292  }
 293  
 294  return 'Serendipity_Import_WordPress';
 295  
 296  /* vim: set sts=4 ts=4 expandtab : */


Généré le : Sat Nov 24 09:00:37 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics