[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

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

   1  <?php # $Id: generic.inc.php 717 2005-11-21 09:56:25Z 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  # Loosely based on the Importer by Stefan Koopmanschap for TXP:
   6  # http://www.leftontheweb.com/pivot_to_textpattern.phps
   7  
   8  class Serendipity_Import_Pivot extends Serendipity_Import {
   9      var $info        = array('software' => 'Pivot');
  10      var $data        = array();
  11      var $inputFields = array();
  12  
  13      function Serendipity_Import_Pivot($data) {
  14          $this->data = $data;
  15          $this->inputFields = array(array('text'    => PARENT_DIRECTORY,
  16                                           'type'    => 'input',
  17                                           'name'    => 'pivot_path',
  18                                           'default' => '/path/to/pivot/db/'),
  19          );
  20      }
  21  
  22      function validateData() {
  23          return sizeof($this->data);
  24      }
  25  
  26      function getInputFields() {
  27          return $this->inputFields;
  28      }
  29  
  30      function _getCategoryList() {
  31          $res = serendipity_fetchCategories('all');
  32          $ret = array(0 => NO_CATEGORY);
  33          if (is_array($res)) {
  34              foreach ($res as $v) {
  35                  $ret[$v['categoryid']] = $v['category_name'];
  36              }
  37          }
  38          return $ret;
  39      }
  40  
  41      function toTimestamp($string) {
  42          if (empty($string)) {
  43              return time();
  44          }
  45  
  46          $parts = explode('-', $string);
  47          return mktime($parts[3], $parts[4], 0, $parts[1], $parts[2], $parts[0]);
  48      }
  49  
  50      function &unserialize($file) {
  51          $c = file_get_contents($file);
  52          $entrydata = str_replace(array('<?php /* pivot */ die(); ?>', "\r"), array('', ''), $c);
  53          $entrydata = unserialize($entrydata);
  54  
  55          if (empty($entrydata) || !is_array($entrydata)) {
  56              $entrydata = str_replace(array('<?php /* pivot */ die(); ?>'), array(''), $c);
  57              $entrydata = unserialize($entrydata);
  58          }
  59  
  60          return $entrydata;
  61      }
  62  
  63      function import() {
  64          global $serendipity;
  65  
  66          $max_import = 9999;
  67  
  68          $serendipity['noautodiscovery'] = true;
  69          if (!is_dir($this->data['pivot_path']) || !is_readable($this->data['pivot_path'])) {
  70              $check_dir = $serendipity['serendipityPath'] . $this->data['pivot_path'];
  71              if (!is_dir($check_dir) || !is_readable($check_dir)) {
  72                  return sprintf(ERROR_NO_DIRECTORY, $this->data['pivot_path']);
  73              }
  74              $this->data['pivot_path'] = $check_dir;
  75          }
  76  
  77          printf('<br />' . CHECKING_DIRECTORY . '<br /><br />', $this->data['pivot_path']);
  78          if ($root = opendir($this->data['pivot_path'])) {
  79              // Fetch category data:
  80              $s9y_categories = serendipity_fetchCategories('all');
  81              $categories     = $this->unserialize($this->data['pivot_path'] . '/ser-cats.php');
  82              $pivot_to_s9y   = array(
  83                  'categories' => array()
  84              );
  85  
  86              foreach($categories AS $pivot_category_id => $category) {
  87                  $found = false;
  88                  $pivot_category = trim(stripslashes($category[0]));
  89                  foreach($s9y_categories AS $idx => $item) {
  90                      if ($pivot_category == $item['category_name']) {
  91                          $found = $item['categoryid'];
  92                          break;
  93                      }
  94                  }
  95  
  96                  if ($found) {
  97                      echo '&middot; Pivot Category "' . htmlspecialchars($pivot_category) . '" mapped to Serendipity ID ' . $found . '<br />';
  98                      $pivot_to_s9y['categories'][$pivot_category] = $found;
  99                  } else {
 100                      echo '&middot; Created Pivot Category "' . htmlspecialchars($pivot_category) . '".<br />';
 101                      $cat = array('category_name'        => $pivot_category,
 102                                   'category_description' => '',
 103                                   'parentid'             => 0,
 104                                   'category_left'        => 0,
 105                                   'category_right'       => 0);
 106  
 107  
 108                      serendipity_db_insert('category', $cat);
 109                      $pivot_to_s9y['categories'][$pivot_category] = serendipity_db_insert_id('category', 'categoryid');
 110                  }
 111              }
 112  
 113              $i = 0;
 114              while (false !== ($dir = readdir($root))) {
 115                  if ($dir{0} == '.') continue;
 116                  if (substr($dir, 0, 8) == 'standard') {
 117                      printf('&nbsp;&nbsp;&middot; ' . CHECKING_DIRECTORY . '...<br />', $dir);
 118                      $data = $this->unserialize($this->data['pivot_path'] . '/' . $dir . '/index-' . $dir . '.php');
 119  
 120                      if (empty($data) || !is_array($data) || count($data) < 1) {
 121                          echo '&nbsp;&nbsp;&nbsp;&nbsp;&middot; <strong style="color: red">FATAL: File <em>' . $dir . '/index-' . $dir . '.php</em> has no data!</strong><br />';
 122                          flush();
 123                          ob_flush();
 124                          continue;
 125                      }
 126  
 127                      foreach($data as $entry) {
 128                          $entryid = str_pad($entry['code'], 5, '0', STR_PAD_LEFT);
 129  
 130                          if ($i >= $max_import) {
 131                              echo '&nbsp;&nbsp;&nbsp;&nbsp;&middot; Skipping entry data for #' . $entryid . '<br />';
 132                              continue;
 133                          }
 134  
 135                          echo '&nbsp;&nbsp;&nbsp;&nbsp;&middot; Fetching entry data for #' . $entryid . '<br />';
 136                          $entrydata = $this->unserialize($this->data['pivot_path'] . '/' . $dir . '/' . $entryid . '.php');
 137                          if (empty($entrydata) || !is_array($entrydata) || count($entrydata) < 1) {
 138                              echo '&nbsp;&nbsp;&nbsp;&nbsp;&middot; <strong style="color: red">FATAL: File <em>' . $dir . '/' . $entryid . '.php</em> has no data!</strong><br />';
 139                              flush();
 140                              ob_flush();
 141                              continue;
 142                          }
 143  
 144                          $entry = array();
 145                          $entry['title'] = trim(stripslashes($entrydata['title']));
 146                          $entry['categories'] = array();
 147                          if (is_array($entrydata['category'])) {
 148                              foreach($entrydata['category'] AS $pivot_category) {
 149                                  $entry['categories'][] = $pivot_to_s9y['categories'][$pivot_category];
 150                              }
 151                          }
 152                          $entry['timestamp']         = $this->toTimestamp($entrydata['date']);
 153                          $entry['last_modified']     = (!empty($entrydata['edit_date']) ? $this->toTimestamp($entrydata['edit_date']) : $entry['timestamp']);
 154                          $entry['isdraft']           = ($entrydata['status'] == 'publish' ? 'false' : 'true');
 155                          $entry['body']              = stripslashes($entrydata['introduction']);
 156                          $entry['extended']          = stripslashes($entrydata['body']);
 157                          $entry['title']             = stripslashes($entrydata['title']);
 158                          $entry['authorid']          = $serendipity['authorid'];
 159                          $entry['author']            = $serendipity['serendipityUser'];
 160                          $entry['allow_comments']    = ($entrydata['allow_comments'] ? 'true' : 'false');
 161                          $entry['moderate_comments'] = 'false';
 162                          $entry['exflag']            = (!empty($entry['extended']) ? 1 : 0);
 163                          $entry['trackbacks']        = 0;
 164                          $entry['comments']          = (isset($entrydata['comments']) ? count($entrydata['comments']) : 0);
 165                          serendipity_updertEntry($entry);
 166                          $i++;
 167  
 168                          if (isset($entrydata['comments']) && count($entrydata['comments']) > 0) {
 169                              foreach($entrydata['comments'] as $comment) {
 170                                  $comment = array('entry_id ' => $entry['id'],
 171                                                   'parent_id' => 0,
 172                                                   'timestamp' => $this->toTimestamp($comment['date']),
 173                                                   'author'    => stripslashes($comment['name']),
 174                                                   'email'     => stripslashes($comment['email']),
 175                                                   'url'       => stripslashes($comment['url']),
 176                                                   'ip'        => stripslashes($comment['ip']),
 177                                                   'status'    => 'approved',
 178                                                   'body'      => stripslashes($comment['comment']),
 179                                                   'subscribed'=> ($comment['notify'] ? 'true' : 'false'),
 180                                                   'type'      => 'NORMAL');
 181  
 182                                  serendipity_db_insert('comments', $comment);
 183                              }
 184                          }
 185                          echo '&nbsp;&nbsp;&nbsp;&nbsp;&middot; <strong style="color: green">Entry #' . $entryid . ' imported</strong><br />';
 186                          flush();
 187                          ob_flush();
 188                      }
 189                  }
 190              }
 191          } else {
 192              return sprintf(ERROR_NO_DIRECTORY, $this->data['pivot_path']);
 193          }
 194  
 195          return true;
 196      }
 197  }
 198  
 199  return 'Serendipity_Import_Pivot';
 200  
 201  /* vim: set sts=4 ts=4 expandtab : */
 202  ?>


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