Newer
Older
Alex Barth
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
// $Id$
/**
* Class definition for Common Syndication Parser.
*
* Parses RSS and Atom feeds.
*/
class FeedsSimplePieParser extends FeedsParser {
/**
* Parses a raw string and returns a Feed object from it.
*/
public function parse(FeedsFetcherResult $fetcherResult, FeedsSource $source) {
if ($fetcherResult->type == 'text/filepath') {
$string = file_get_contents($fetcherResult->value);
}
else {
$string = $fetcherResult->value;
}
feeds_include_library('simplepie.inc', 'simplepie');
// Initialize SimplePie.
$parser = new SimplePie();
$parser->set_raw_data($string);
$parser->set_stupidly_fast(TRUE);
$parser->encode_instead_of_strip(FALSE);
// @todo: is caching effective when we pass in raw data?
$parser->enable_cache(TRUE);
$parser->set_cache_location($this->cacheDirectory());
$parser->init();
// Construct the standard form of the parsed feed
$feed = array();
$feed['title'] = ($title = $parser->get_title()) ? $title : $this->createTitle($parser->get_description());
$feed['description'] = $parser->get_description();
$feed['link'] = html_entity_decode($parser->get_link());
$feed['items'] = array();
$items_num = $parser->get_item_quantity();
for ($i = 0; $i < $items_num; $i++) {
$item = array();
$simplepie_item = $parser->get_item($i);
$item['title'] = ($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());
// U = std. unix timestamp
$item['timestamp'] = $simplepie_item->get_date("U");
$item['guid'] = $simplepie_item->get_id();
// Use URL as GUID if there is no GUID.
if (empty($item['guid'])) {
$item['guid'] = $item['url'];
}
$author = $simplepie_item->get_author();
$item['author_name'] = $author->name;
$item['author_link'] = $author->link;
$item['author_email'] = $author->email;
Alex Barth
committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Enclosures
$enclosures = $simplepie_item->get_enclosures();
if (is_array($enclosures)) {
foreach ($enclosures as $enclosure) {
$mime = $enclosure->get_real_type();
if ($mime != '') {
list($type, $subtype) = split('/', $mime);
$item['enclosures'][$type][$subtype][] = $enclosure;
}
}
}
// Location
$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;
}
// Extract tags related to the item
$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;
// Stick the raw data onto the feed item.
$item['raw'] = $simplepie_item->data;
$feed['items'][] = $item;
}
// Release parser.
unset($parser);
return new FeedsParserResult($feed, 'syndication');
}
/**
* Return mapping sources.
*/
public function getMappingSources() {
return array(
'title' => t('Title'),
'description' => t('Description'),
'author_name' => t('Author name'),
'author_link' => t('Author link'),
'author_email' => t('Author email'),
Alex Barth
committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'timestamp' => t('Published date'),
'date_raw' => t('Published date (raw)'),
'url' => t('Item URL (link)'),
'guid' => t('Item GUID'),
'tags' => t('Categories'),
'domains' => t('Category domains'),
'location_latitude' => t('Latitudes'),
'location_longitude' => t('Longitudes'),
'enclosures' => t('Enclosures'),
);
}
/**
* Returns cache directory. Creates it if it doesn't exist.
*/
protected function cacheDirectory() {
$directory = file_directory_path() .'/simplepie';
file_check_directory($directory, TRUE);
return $directory;
}
/**
* Generate a title from a random text.
*/
protected function createTitle($text = FALSE) {
// Explode to words and use the first 3 words.
$words = preg_split("/[\s,]+/", $text);
$words = array_slice($words, 0, 3);
return implode(' ', $words);
}
}