[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

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

   1  <?php # $Id: wordpress.inc.php,v 1.16 2005/05/17 11:34:48 garvinhicking Exp $
   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 PostgreSQL Importer, by Devrim GUNDUZ          *

   7   *****************************************************************/
   8  
   9  class Serendipity_Import_WordPress_PG extends Serendipity_Import {
  10      var $info        = array('software' => 'WordPress PostgreSQL');
  11      var $data        = array();
  12      var $inputFields = array();
  13  
  14  
  15      function Serendipity_Import_WordPress_PG($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' => 'password'),
  28  
  29                                     array('text' => INSTALL_DBPORT,
  30                                           'type' => 'input',
  31                                           'name' => 'port'),
  32  
  33                                     array('text' => INSTALL_DBNAME,
  34                                           'type' => 'input',
  35                                           'name' => 'name'),
  36  
  37                                     array('text' => INSTALL_DBPREFIX,
  38                                           'type' => 'input',
  39                                           'name' => 'prefix'),
  40  
  41                                     array('text'    => CHARSET,
  42                                           'type'    => 'list',
  43                                           'name'    => 'charset',
  44                                           'value'   => 'UTF-8',
  45                                           'default' => $this->getCharsets(true)),
  46  
  47                                     array('text'    => CONVERT_HTMLENTITIES,
  48                                           'type'    => 'bool',
  49                                           'name'    => 'use_strtr',
  50                                           'default' => 'true'),
  51  
  52                                     array('text'    => ACTIVATE_AUTODISCOVERY,
  53                                           'type'    => 'bool',
  54                                           'name'    => 'autodiscovery',
  55                                           'default' => 'false')
  56                              );
  57      }
  58  
  59      function validateData() {
  60          return sizeof($this->data);
  61      }
  62  
  63      function getInputFields() {
  64          return $this->inputFields;
  65      }
  66  
  67      function import() {
  68          global $serendipity;
  69  
  70          // Save this so we can return it to its original value at the end of this method.

  71          $noautodiscovery = isset($serendipity['noautodiscovery']) ? $serendipity['noautodiscovery'] : false;
  72  
  73          if ($this->data['autodiscovery'] == 'false') {
  74              $serendipity['noautodiscovery'] = 1;
  75          }
  76  
  77          $this->getTransTable();
  78  
  79          $this->data['prefix'] = serendipity_db_escape_string($this->data['prefix']);
  80          $users = array();
  81          $categories = array();
  82          $entries = array();
  83  
  84          if ( !extension_loaded('pgsql') ) {
  85              return PGSQL_REQUIRED;;
  86          }
  87  
  88          $wpdb = pg_connect("$this->data['host'], $this->data['port'], $this->data['user'], $this->data['pass'], $this->data['name']");
  89          if ( !$wpdb ) {
  90              return sprintf(PGSQL_COULDNT_CONNECT, $this->data['pass']);
  91          }
  92  
  93          /* Users */

  94          $res = pg_query($wpdb, "SELECT ID, user_login, user_pass, user_email, user_level FROM {$this->data['prefix']}users;");
  95          if ( !$res ) {
  96              return sprintf(COULDNT_SELECT_USER_INFO, pg_last_error($wpdb));
  97          }
  98  
  99          for ( $x=0 ; $x<pg_num_rows($res) ; $x++ ) {
 100              $users[$x] = pg_fetch_assoc($res);
 101  
 102              $data = array('right_publish' => ($users[$x]['user_level'] >= 1) ? 1 : 0,
 103                            'realname'      => $users[$x]['user_login'],
 104                            'username'      => $users[$x]['user_login'],
 105                            'password'      => $users[$x]['user_pass']); // WP uses md5, too.

 106  
 107              if ( $users[$x]['user_level'] <= 1 ) {
 108                  $data['userlevel'] = USERLEVEL_EDITOR;
 109              } elseif ( $users[$x]['user_level'] < 5 ) {
 110                  $data['userlevel'] = USERLEVEL_CHIEF;
 111              } else {
 112                  $data['userlevel'] = USERLEVEL_ADMIN;
 113              }
 114  
 115              if ($serendipity['serendipityUserlevel'] < $data['userlevel']) {
 116                  $data['userlevel'] = $serendipity['serendipityUserlevel'];
 117              }
 118  
 119              serendipity_db_insert('authors', $this->strtrRecursive($data));
 120              $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid');
 121          }
 122  
 123          /* Categories */

 124          $res = @pg_query($wpdb, "SELECT cat_ID, cat_name, category_description, category_parent FROM {$this->data['prefix']}categories ORDER BY category_parent, cat_ID;");
 125          if ( !$res ) {
 126              return sprintf(COULDNT_SELECT_CATEGORY_INFO, pg_last_error($wpdb));
 127          }
 128  
 129          // Get all the info we need

 130          for ( $x=0 ; $x<pg_num_rows($res) ; $x++ )
 131              $categories[] = pg_fetch_assoc($res);
 132  
 133          // Insert all categories as top level (we need to know everyone's ID before we can represent the hierarchy).

 134          for ( $x=0 ; $x<sizeof($categories) ; $x++ ) {
 135              $cat = array('category_name'        => $categories[$x]['cat_name'],
 136                           'category_description' => $categories[$x]['category_description'],
 137                           'parentid'             => 0, // <---
 138                           'category_left'        => 0,
 139                           'category_right'       => 0);
 140  
 141              serendipity_db_insert('category', $this->strtrRecursive($cat));
 142              $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid');
 143          }
 144  
 145          // There has to be a more efficient way of doing this...

 146          foreach ( $categories as $cat ) {
 147              if ( $cat['category_parent'] != 0 ) {
 148                  // Find the parent

 149                  $par_id = 0;
 150                  foreach ( $categories as $possible_par ) {
 151                      if ( $possible_par['cat_ID'] == $cat['category_parent'] ) {
 152                          $par_id = $possible_par['categoryid'];
 153                          break;
 154                      }
 155                  }
 156  
 157                  if ( $par_id != 0 ) {
 158                    serendipity_db_query("UPDATE {$serendipity['dbPrefix']}category SET parentid={$par_id} WHERE categoryid={$cat['categoryid']};");
 159                  } // else { echo "D'oh! " . random_string_of_profanity(); }

 160              }
 161          }
 162  
 163          serendipity_rebuildCategoryTree();
 164  
 165          /* Entries */

 166          $res = @pg_query($wpdb, "SELECT * FROM {$this->data['prefix']}posts ORDER BY post_date;");
 167          if ( !$res ) {
 168              return sprintf(COULDNT_SELECT_ENTRY_INFO, pg_last_error($wpdb));
 169          }
 170  
 171          for ( $x=0 ; $x<pg_num_rows($res) ; $x++ ) {
 172              $entries[$x] = pg_fetch_assoc($res);
 173  
 174              $entry = array('title'          => $this->decode($entries[$x]['post_title']), // htmlentities() is called later, so we can leave this.
 175                             'isdraft'        => ($entries[$x]['post_status'] == 'publish') ? 'false' : 'true',
 176                             'allow_comments' => ($entries[$x]['comment_status'] == 'open' ) ? 'true' : 'false',
 177                             'timestamp'      => strtotime($entries[$x]['post_date']),
 178                             'body'           => $this->strtr($entries[$x]['post_content']));
 179  
 180              foreach ( $users as $user ) {
 181                  if ( $user['ID'] == $entries[$x]['post_author'] ) {
 182                      $entry['authorid'] = $user['authorid'];
 183                      break;
 184                  }
 185              }
 186  
 187              if ( !is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry)) ) {
 188                  return $entries[$x]['entryid'];
 189              }
 190          }
 191  
 192          /* Entry/category */

 193          $res = @pg_query($wpdb, "SELECT * FROM {$this->data['prefix']}post2cat;");
 194          if ( !$res ) {
 195              return sprintf(COULDNT_SELECT_ENTRY_INFO, pg_last_error($wpdb));
 196          }
 197  
 198          while ( $a = pg_fetch_assoc($res) ) {
 199              foreach ( $categories as $category ) {
 200                  if ( $category['cat_ID'] == $a['category_id'] ) {
 201                      foreach ( $entries as $entry ) {
 202                          if ( $a['post_id'] == $entry['ID'] ) {
 203                              $data = array('entryid' => $entry['entryid'],
 204                                            'categoryid' => $category['categoryid']);
 205                              serendipity_db_insert('entrycat', $this->strtrRecursive($data));
 206                              break;
 207                          }
 208                      }
 209                      break;
 210                  }
 211              }
 212          }
 213  
 214          /* Comments */

 215          $res = @pg_query($wpdb, "SELECT * FROM {$this->data['prefix']}comments;");
 216          if ( !$res ) {
 217              return sprintf(COULDNT_SELECT_COMMENT_INFO, pg_last_error($wpdb));
 218          }
 219  
 220          while ( $a = pg_fetch_assoc($res) ) {
 221              foreach ( $entries as $entry ) {
 222                  if ( $entry['ID'] == $a['comment_post_ID'] ) {
 223                      $comment = array('entry_id ' => $entry['entryid'],
 224                                       'parent_id' => 0,
 225                                       'timestamp' => strtotime($a['comment_date']),
 226                                       'author'    => $a['comment_author'],
 227                                       'email'     => $a['comment_author_email'],
 228                                       'url'       => $a['comment_author_url'],
 229                                       'ip'        => $a['comment_author_IP'],
 230                                       'status'    => (empty($a['comment_approved']) || $a['comment_approved'] == '1') ? 'approved' : 'pending',
 231                                       'subscribed'=> 'false',
 232                                       'body'      => $a['comment_content'],
 233                                       'type'      => 'NORMAL');
 234  
 235                      serendipity_db_insert('comments', $this->strtrRecursive($comment));
 236                      if ($comment['status'] == 'approved') {
 237                          $cid = serendipity_db_insert_id('comments', 'id');
 238                          serendipity_approveComment($cid, $entry['entryid'], true);
 239                      }
 240                  }
 241              }
 242          }
 243  
 244          $serendipity['noautodiscovery'] = $noautodiscovery;
 245  
 246          // That was fun.

 247          return true;
 248      }
 249  }
 250  
 251  return 'Serendipity_Import_WordPress_PG';
 252  
 253  /* vim: set sts=4 ts=4 expandtab : */

 254  ?>


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