[ Index ]
 

Code source de Serendipity 1.2

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

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

   1  <?php # $Id: generic.inc.php 1462 2006-10-27 09:18:19Z 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  require_once S9Y_PEAR_PATH . 'Onyx/RSS.php';
   6  
   7  class Serendipity_Import_Generic extends Serendipity_Import {
   8      var $info        = array('software' => IMPORT_GENERIC_RSS);
   9      var $data        = array();
  10      var $inputFields = array();
  11      var $force_recode = false;
  12  
  13      function Serendipity_Import_Generic($data) {
  14          $this->data = $data;
  15          $this->inputFields = array(array('text'    => RSS . ' ' . URL,
  16                                           'type'    => 'input',
  17                                           'name'    => 'url'),
  18  
  19                                     array('text'    => STATUS,
  20                                           'type'    => 'list',
  21                                           'name'    => 'type',
  22                                           'value'   => 'publish',
  23                                           'default' => array('draft' => DRAFT, 'publish' => PUBLISH)),
  24  
  25                                     array('text'    => RSS_IMPORT_CATEGORY,
  26                                           'type'    => 'list',
  27                                           'name'    => 'category',
  28                                           'value'   => 0,
  29                                           'default' => $this->_getCategoryList()),
  30  
  31                                     array('text'    => CHARSET,
  32                                           'type'    => 'list',
  33                                           'name'    => 'charset',
  34                                           'value'   => 'UTF-8',
  35                                           'default' => $this->getCharsets()),
  36  
  37                                      array('text'   => RSS_IMPORT_BODYONLY,
  38                                           'type'    => 'bool',
  39                                           'name'    => 'bodyonly',
  40                                           'value'   => 'false'));
  41      }
  42  
  43      function validateData() {
  44          return sizeof($this->data);
  45      }
  46  
  47      function getInputFields() {
  48          return $this->inputFields;
  49      }
  50  
  51      function _getCategoryList() {
  52          $res = serendipity_fetchCategories('all');
  53          $ret = array(0 => NO_CATEGORY);
  54          if (is_array($res)) {
  55              foreach ($res as $v) {
  56                  $ret[$v['categoryid']] = $v['category_name'];
  57              }
  58          }
  59          return $ret;
  60      }
  61  
  62      function buildEntry($item, &$entry) {
  63          global $serendipity;
  64  
  65          $bodyonly = serendipity_get_bool($this->data['bodyonly']);
  66  
  67          if ($item['description']) {
  68              $entry['body'] = $this->decode($item['description']);
  69          }
  70  
  71          if ($item['content:encoded']) {
  72              if (!isset($entry['body']) || $bodyonly) {
  73                  $data = &$entry['body'];
  74              } else {
  75                  $data = &$entry['extended'];
  76              }
  77  
  78              // See if the 'description' element is a substring of the 'content:encoded' part. If it is,
  79              // we will only fetch the full 'content:encoded' part. If it's not a substring, we append
  80              // the 'content:encoded' part to either body or extended entry (respecting the 'bodyonly'
  81              // switch). We substract 4 letters because of possible '...' additions to an entry.
  82              $testbody = substr(trim(strip_tags($entry['body'])), 0, -4);
  83              if ($testbody != substr(trim(strip_tags($item['content:encoded'])), 0, strlen($testbody))) {
  84                  $data .= $this->decode($item['content:encoded']);
  85              } else {
  86                  $data = $this->decode($item['content:encoded']);
  87              }
  88          }
  89  
  90          $entry['title'] = $this->decode($item['title']);
  91          $entry['timestamp'] = $this->decode(strtotime(isset($item['pubdate']) ? $item['pubdate'] : $item['dc:date']));
  92          if ($entry['timestamp'] == -1) {
  93              // strtotime does not seem to parse ISO 8601 dates
  94              if (preg_match('@^([0-9]{4})\-([0-9]{2})\-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})[\-\+]([0-9]{2}):([0-9]{2})$@', isset($item['pubdate']) ? $item['pubdate'] : $item['dc:date'], $timematch)) {
  95                  $entry['timestamp'] = mktime($timematch[4] - $timematch[7], $timematch[5] - $timematch[8], $timematch[6], $timematch[2], $timematch[3], $timematch[1]);
  96              } else {
  97                  $entry['timestamp'] = time();
  98              }
  99          }
 100  
 101          if ($this->data['type'] == 'draft') {
 102              $entry['isdraft'] = 'true';
 103          } else {
 104              $entry['isdraft'] = 'false';
 105          }
 106  
 107          if (!empty($item['category'])) {
 108              $cat = serendipity_fetchCategoryInfo(0, trim($this->decode($item['category'])));
 109              if (is_array($cat) && isset($cat['categoryid'])) {
 110                  $entry['categories'][] = $cat['categoryid'];
 111              }
 112          }
 113  
 114          if (!is_array($entry['categories'])) {
 115              $entry['categories'][] = $this->data['category'];
 116          }
 117  
 118          if (!isset($entry['extended'])) {
 119              $entry['extended'] = '';
 120          }
 121  
 122          $entry['allow_comments'] = true;
 123  
 124          return true;
 125      }
 126  
 127      function import() {
 128          global $serendipity;
 129  
 130          $c = &new Onyx_RSS($this->data['charset']);
 131          $c->parse($this->data['url']);
 132          $this->data['encoding'] = $c->rss['encoding'];
 133  
 134          $serendipity['noautodiscovery'] = 1;
 135          while ($item = $c->getNextItem()) {
 136              $entry    = array();
 137              if ($this->buildEntry($item, $entry)) {
 138                  serendipity_updertEntry($entry);
 139              }
 140          }
 141  
 142          return true;
 143      }
 144  }
 145  
 146  return 'Serendipity_Import_Generic';
 147  
 148  /* vim: set sts=4 ts=4 expandtab : */
 149  ?>


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