View source
<?php
class FeedsSimplePieEnclosure extends FeedsEnclosure {
protected $simplepie_enclosure;
private $_serialized_simplepie_enclosure;
function __construct(SimplePie_Enclosure $enclosure) {
$this->simplepie_enclosure = $enclosure;
}
public function __sleep() {
$this->_serialized_simplepie_enclosure = serialize($this->simplepie_enclosure);
return array(
'_serialized_simplepie_enclosure',
);
}
public function __wakeup() {
feeds_include_library('simplepie.inc', 'simplepie');
$this->simplepie_enclosure = unserialize($this->_serialized_simplepie_enclosure);
}
public function getValue() {
return $this->simplepie_enclosure
->get_link();
}
public function getMIMEType() {
return $this->simplepie_enclosure
->get_real_type();
}
}
class FeedsSimplePieParser extends FeedsParser {
public function parse(FeedsImportBatch $batch, FeedsSource $source) {
feeds_include_library('simplepie.inc', 'simplepie');
$parser = new SimplePie();
$parser
->set_raw_data($batch
->getRaw());
$parser
->set_stupidly_fast(TRUE);
$parser
->encode_instead_of_strip(FALSE);
$parser
->enable_cache(TRUE);
$parser
->set_cache_location($this
->cacheDirectory());
$parser
->init();
$batch->title = html_entity_decode(($title = $parser
->get_title()) ? $title : $this
->createTitle($parser
->get_description()));
$batch->description = $parser
->get_description();
$batch->link = html_entity_decode($parser
->get_link());
$items_num = $parser
->get_item_quantity();
for ($i = 0; $i < $items_num; $i++) {
$item = array();
$simplepie_item = $parser
->get_item($i);
$item['title'] = html_entity_decode(($title = $simplepie_item
->get_title()) ? $title : $this
->createTitle($simplepie_item
->get_content()));
$item['description'] = $simplepie_item
->get_content();
$item['url'] = html_entity_decode($simplepie_item
->get_link());
$item['timestamp'] = $simplepie_item
->get_date("U");
if (empty($item['timestamp'])) {
$item['timestamp'] = FEEDS_REQUEST_TIME;
}
$item['guid'] = $simplepie_item
->get_id();
if (empty($item['guid'])) {
$item['guid'] = $item['url'];
}
$author = $simplepie_item
->get_author();
$item['author_name'] = isset($author->name) ? html_entity_decode($author->name) : '';
$item['author_link'] = isset($author->link) ? $author->link : '';
$item['author_email'] = isset($author->email) ? $author->email : '';
$enclosures = $simplepie_item
->get_enclosures();
if (is_array($enclosures)) {
foreach ($enclosures as $enclosure) {
$item['enclosures'][] = new FeedsSimplePieEnclosure($enclosure);
}
}
$latitude = $simplepie_item
->get_latitude();
$longitude = $simplepie_item
->get_longitude();
if (!is_null($latitude) && !is_null($longitude)) {
$item['location_latitude'][] = $latitude;
$item['location_longitude'][] = $longitude;
}
$simplepie_tags = $simplepie_item
->get_categories();
$tags = array();
$domains = array();
if (count($simplepie_tags) > 0) {
foreach ($simplepie_tags as $tag) {
$tags[] = (string) $tag->term;
$domain = (string) $tag
->get_scheme();
if (!empty($domain)) {
if (!isset($domains[$domain])) {
$domains[$domain] = array();
}
$domains[$domain][] = count($tags) - 1;
}
}
}
$item['domains'] = $domains;
$item['tags'] = $tags;
$this
->parseExtensions($item, $simplepie_item);
$item['raw'] = $simplepie_item->data;
$batch->items[] = $item;
}
unset($parser);
}
protected function parseExtensions(&$item, $simplepie_item) {
}
public function getMappingSources() {
return array(
'title' => array(
'name' => t('Title'),
'description' => t('Title of the feed item.'),
),
'description' => array(
'name' => t('Description'),
'description' => t('Description of the feed item.'),
),
'author_name' => array(
'name' => t('Author name'),
'description' => t('Name of the feed item\'s author.'),
),
'author_link' => array(
'name' => t('Author link'),
'description' => t('Link to the feed item\'s author.'),
),
'author_email' => array(
'name' => t('Author email'),
'description' => t('Email address of the feed item\'s author.'),
),
'timestamp' => array(
'name' => t('Published date'),
'description' => t('Published date as UNIX time GMT of the feed item.'),
),
'url' => array(
'name' => t('Item URL (link)'),
'description' => t('URL of the feed item.'),
),
'guid' => array(
'name' => t('Item GUID'),
'description' => t('Global Unique Identifier of the feed item.'),
),
'tags' => array(
'name' => t('Categories'),
'description' => t('An array of categories that have been assigned to the feed item.'),
),
'domains' => array(
'name' => t('Category domains'),
'description' => t('Domains of the categories.'),
),
'location_latitude' => array(
'name' => t('Latitudes'),
'description' => t('An array of latitudes assigned to the feed item.'),
),
'location_longitude' => array(
'name' => t('Longitudes'),
'description' => t('An array of longitudes assigned to the feed item.'),
),
'enclosures' => array(
'name' => t('Enclosures'),
'description' => t('An array of enclosures attached to the feed item.'),
),
) + parent::getMappingSources();
}
protected function cacheDirectory() {
$directory = file_directory_path() . '/simplepie';
file_check_directory($directory, TRUE);
return $directory;
}
protected function createTitle($text = FALSE) {
$words = preg_split("/[\\s,]+/", $text);
$words = array_slice($words, 0, 3);
return implode(' ', $words);
}
}