daily pastebin goal
64%
SHARE
TWEET

SQLIImport - RSS Import Handler (Demo)

a guest Aug 17th, 2010 458 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * File containing demo import handler SQLIRSSImportHandler
  4.  * @copyright Copyright (C) 2010 - SQLi Agency. All rights reserved
  5.  * @licence http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2
  6.  * @author Jerome Vieilledent
  7.  * @version @@@VERSION@@@
  8.  * @package sqliimport
  9.  * @subpackage sourcehandlers
  10.  */
  11.  
  12. class SQLIRSSImportHandler extends SQLIImportAbstractHandler implements ISQLIImportHandler
  13. {
  14.   protected $rowIndex = 0;
  15.    
  16.   protected $rowCount;
  17.    
  18.   protected $currentGUID;
  19.    
  20.   /**
  21.   * Constructor
  22.   */
  23.   public function __construct( SQLIImportHandlerOptions $options = null )
  24.   {
  25.    parent::__construct( $options );
  26.    $this->remoteIDPrefix = $this->getHandlerIdentifier().'-';
  27.    $this->currentRemoteIDPrefix = $this->remoteIDPrefix;
  28.   }
  29.    
  30.   /**
  31.   * (non-PHPdoc)
  32.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::initialize()
  33.   */
  34.   public function initialize()
  35.   {
  36.    $rssFeedUrl = $this->handlerConfArray['RSSFeed'];
  37.    $xmlOptions = new SQLIXMLOptions( array(
  38.     'xml_path'   => $rssFeedUrl,
  39.     'xml_parser'  => 'simplexml'
  40.    ) );
  41.    $xmlParser = new SQLIXMLParser( $xmlOptions );
  42.    $this->dataSource = $xmlParser->parse();
  43.   }
  44.    
  45.   /**
  46.   * (non-PHPdoc)
  47.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::getProcessLength()
  48.   */
  49.   public function getProcessLength()
  50.   {
  51.    if( !isset( $this->rowCount ) )
  52.    {
  53.     $this->rowCount = count( $this->dataSource->channel->item );
  54.    }
  55.    return $this->rowCount;
  56.   }
  57.    
  58.   /**
  59.   * (non-PHPdoc)
  60.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::getNextRow()
  61.   */
  62.   public function getNextRow()
  63.   {
  64.    if( $this->rowIndex < $this->rowCount )
  65.    {
  66.     $row = $this->dataSource->channel->item[$this->rowIndex];
  67.     $this->rowIndex++;
  68.    }
  69.    else
  70.    {
  71.     $row = false; // We must return false if we already processed all rows
  72.    }
  73.    
  74.    return $row;
  75.   }
  76.    
  77.   /**
  78.   * (non-PHPdoc)
  79.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::process()
  80.   */
  81.   public function process( $row )
  82.   {
  83.    // $row is a SimpleXMLElement object
  84.    $this->currentGUID = $row->guid;
  85.    $contentOptions = new SQLIContentOptions( array(
  86.     'class_identifier'   => 'article',
  87.     'remote_id'    => (string)$row->guid
  88.    ) );
  89.    $content = SQLIContent::create( $contentOptions );
  90.    $content->fields->title = (string)$row->title;
  91.    $content->fields->author = (string)$row->author.'|noreply@sqli.com|-1'; // @see eZAuthorType::fromString()
  92.    
  93.    // Handle HTML content
  94.    $htmlMessage = (string)$row->description;
  95.    $parser = new SQLIXMLInputParser();
  96.    $parser->setParseLineBreaks( true );
  97.    $document = $parser->process( $htmlMessage ); // Result is a DOM Document
  98.    $content->fields->intro = eZXMLTextType::domString( $document );
  99.    
  100.    // Now publish content
  101.    $content->addLocation( SQLILocation::fromNodeID( $this->handlerConfArray['DefaultParentNodeID'] ) );
  102.    $publisher = SQLIContentPublisher::getInstance();
  103.    $publisher->publish( $content );
  104.    
  105.    // Free some memory. Internal methods eZContentObject::clearCache() and eZContentObject::resetDataMap() will be called
  106.    // @see SQLIContent::__destruct()
  107.    unset( $content );
  108.   }
  109.    
  110.   /**
  111.   * (non-PHPdoc)
  112.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::cleanup()
  113.   */
  114.   public function cleanup()
  115.   {
  116.    // Nothing to clean up
  117.    return;
  118.   }
  119.    
  120.   /**
  121.   * (non-PHPdoc)
  122.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::getHandlerName()
  123.   */
  124.   public function getHandlerName()
  125.   {
  126.    return 'RSS Import Handler';
  127.   }
  128.    
  129.   /**
  130.   * (non-PHPdoc)
  131.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::getHandlerIdentifier()
  132.   */
  133.   public function getHandlerIdentifier()
  134.   {
  135.    return 'rssimporthandler';
  136.   }
  137.    
  138.   /**
  139.   * (non-PHPdoc)
  140.   * @see extension/sqliimport/classes/sourcehandlers/ISQLIImportHandler::getProgressionNotes()
  141.   */
  142.   public function getProgressionNotes()
  143.   {
  144.    return 'Currently importing : '.$this->currentGUID;
  145.   }
  146. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top