| 1 |
<?php
|
| 2 |
// $Id: aggregator.parser.inc,v 1.5 2009/10/09 00:59:55 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Parser functions for the aggregator module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_aggregator_parse_info().
|
| 11 |
*/
|
| 12 |
function aggregator_aggregator_parse_info() {
|
| 13 |
return array(
|
| 14 |
'title' => t('Default parser'),
|
| 15 |
'description' => t('Parses RSS, Atom and RDF feeds.'),
|
| 16 |
);
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implement hook_aggregator_parse().
|
| 21 |
*/
|
| 22 |
function aggregator_aggregator_parse($feed) {
|
| 23 |
global $channel, $image;
|
| 24 |
|
| 25 |
// Filter the input data.
|
| 26 |
if (aggregator_parse_feed($feed->source_string, $feed)) {
|
| 27 |
$modified = empty($feed->http_headers['Last-Modified']) ? 0 : strtotime($feed->http_headers['Last-Modified']);
|
| 28 |
|
| 29 |
// Prepare the channel data.
|
| 30 |
foreach ($channel as $key => $value) {
|
| 31 |
$channel[$key] = trim($value);
|
| 32 |
}
|
| 33 |
|
| 34 |
// Prepare the image data (if any).
|
| 35 |
foreach ($image as $key => $value) {
|
| 36 |
$image[$key] = trim($value);
|
| 37 |
}
|
| 38 |
|
| 39 |
if (!empty($image['link']) && !empty($image['url']) && !empty($image['title'])) {
|
| 40 |
$image = l(theme('image', array('path' => $image['url'], 'alt' => $image['title'])), $image['link'], array('html' => TRUE));
|
| 41 |
}
|
| 42 |
else {
|
| 43 |
$image = '';
|
| 44 |
}
|
| 45 |
|
| 46 |
$etag = empty($feed->http_headers['ETag']) ? '' : $feed->http_headers['ETag'];
|
| 47 |
|
| 48 |
// Add parsed data to the feed object.
|
| 49 |
$feed->link = !empty($channel['LINK']) ? $channel['LINK'] : '';
|
| 50 |
$feed->description = !empty($channel['DESCRIPTION']) ? $channel['DESCRIPTION'] : '';
|
| 51 |
$feed->image = $image;
|
| 52 |
$feed->etag = $etag;
|
| 53 |
$feed->modified = $modified;
|
| 54 |
|
| 55 |
// Clear the cache.
|
| 56 |
cache_clear_all();
|
| 57 |
|
| 58 |
return TRUE;
|
| 59 |
}
|
| 60 |
|
| 61 |
return FALSE;
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Parse a feed and store its items.
|
| 66 |
*
|
| 67 |
* @param $data
|
| 68 |
* The feed data.
|
| 69 |
* @param $feed
|
| 70 |
* An object describing the feed to be parsed.
|
| 71 |
* @return
|
| 72 |
* FALSE on error, TRUE otherwise.
|
| 73 |
*/
|
| 74 |
function aggregator_parse_feed(&$data, $feed) {
|
| 75 |
global $items, $image, $channel;
|
| 76 |
|
| 77 |
// Unset the global variables before we use them.
|
| 78 |
unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']);
|
| 79 |
$items = array();
|
| 80 |
$image = array();
|
| 81 |
$channel = array();
|
| 82 |
|
| 83 |
// Parse the data.
|
| 84 |
$xml_parser = drupal_xml_parser_create($data);
|
| 85 |
xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end');
|
| 86 |
xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
|
| 87 |
|
| 88 |
if (!xml_parse($xml_parser, $data, 1)) {
|
| 89 |
watchdog('aggregator', 'The feed from %site seems to be broken, due to an error "%error" on line %line.', array('%site' => $feed->title, '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser)), WATCHDOG_WARNING);
|
| 90 |
drupal_set_message(t('The feed from %site seems to be broken, because of error "%error" on line %line.', array('%site' => $feed->title, '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser))), 'error');
|
| 91 |
return FALSE;
|
| 92 |
}
|
| 93 |
xml_parser_free($xml_parser);
|
| 94 |
|
| 95 |
// We reverse the array such that we store the first item last, and the last
|
| 96 |
// item first. In the database, the newest item should be at the top.
|
| 97 |
$items = array_reverse($items);
|
| 98 |
|
| 99 |
// Initialize items array.
|
| 100 |
$feed->items = array();
|
| 101 |
foreach ($items as $item) {
|
| 102 |
|
| 103 |
// Prepare the item:
|
| 104 |
foreach ($item as $key => $value) {
|
| 105 |
$item[$key] = trim($value);
|
| 106 |
}
|
| 107 |
|
| 108 |
// Resolve the item's title. If no title is found, we use up to 40
|
| 109 |
// characters of the description ending at a word boundary, but not
|
| 110 |
// splitting potential entities.
|
| 111 |
if (!empty($item['title'])) {
|
| 112 |
$item['title'] = $item['title'];
|
| 113 |
}
|
| 114 |
elseif (!empty($item['description'])) {
|
| 115 |
$item['title'] = preg_replace('/^(.*)[^\w;&].*?$/', "\\1", truncate_utf8($item['description'], 40));
|
| 116 |
}
|
| 117 |
else {
|
| 118 |
$item['title'] = '';
|
| 119 |
}
|
| 120 |
|
| 121 |
// Resolve the items link.
|
| 122 |
if (!empty($item['link'])) {
|
| 123 |
$item['link'] = $item['link'];
|
| 124 |
}
|
| 125 |
else {
|
| 126 |
$item['link'] = $feed->link;
|
| 127 |
}
|
| 128 |
$item['guid'] = isset($item['guid']) ? $item['guid'] : '';
|
| 129 |
|
| 130 |
// Atom feeds have a content and/or summary tag instead of a description tag.
|
| 131 |
if (!empty($item['content:encoded'])) {
|
| 132 |
$item['description'] = $item['content:encoded'];
|
| 133 |
}
|
| 134 |
elseif (!empty($item['summary'])) {
|
| 135 |
$item['description'] = $item['summary'];
|
| 136 |
}
|
| 137 |
elseif (!empty($item['content'])) {
|
| 138 |
$item['description'] = $item['content'];
|
| 139 |
}
|
| 140 |
|
| 141 |
// Try to resolve and parse the item's publication date.
|
| 142 |
$date = '';
|
| 143 |
foreach (array('pubdate', 'dc:date', 'dcterms:issued', 'dcterms:created', 'dcterms:modified', 'issued', 'created', 'modified', 'published', 'updated') as $key) {
|
| 144 |
if (!empty($item[$key])) {
|
| 145 |
$date = $item[$key];
|
| 146 |
break;
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
$item['timestamp'] = strtotime($date);
|
| 151 |
|
| 152 |
if ($item['timestamp'] === FALSE) {
|
| 153 |
$item['timestamp'] = aggregator_parse_w3cdtf($date); // Aggregator_parse_w3cdtf() returns FALSE on failure.
|
| 154 |
}
|
| 155 |
|
| 156 |
// Resolve dc:creator tag as the item author if author tag is not set.
|
| 157 |
if (empty($item['author']) && !empty($item['dc:creator'])) {
|
| 158 |
$item['author'] = $item['dc:creator'];
|
| 159 |
}
|
| 160 |
|
| 161 |
$item += array('author' => '', 'description' => '');
|
| 162 |
|
| 163 |
// Store on $feed object. This is where processors will look for parsed items.
|
| 164 |
$feed->items[] = $item;
|
| 165 |
}
|
| 166 |
|
| 167 |
return TRUE;
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Callback function used by the XML parser.
|
| 172 |
*/
|
| 173 |
function aggregator_element_start($parser, $name, $attributes) {
|
| 174 |
global $item, $element, $tag, $items, $channel;
|
| 175 |
|
| 176 |
$name = strtolower($name);
|
| 177 |
switch ($name) {
|
| 178 |
case 'image':
|
| 179 |
case 'textinput':
|
| 180 |
case 'content':
|
| 181 |
case 'summary':
|
| 182 |
case 'tagline':
|
| 183 |
case 'subtitle':
|
| 184 |
case 'logo':
|
| 185 |
case 'info':
|
| 186 |
$element = $name;
|
| 187 |
break;
|
| 188 |
case 'id':
|
| 189 |
if ($element != 'item') {
|
| 190 |
$element = $name;
|
| 191 |
}
|
| 192 |
case 'link':
|
| 193 |
if (!empty($attributes['rel']) && $attributes['rel'] == 'alternate') {
|
| 194 |
if ($element == 'item') {
|
| 195 |
$items[$item]['link'] = $attributes['href'];
|
| 196 |
}
|
| 197 |
else {
|
| 198 |
$channel['link'] = $attributes['href'];
|
| 199 |
}
|
| 200 |
}
|
| 201 |
break;
|
| 202 |
case 'item':
|
| 203 |
$element = $name;
|
| 204 |
$item += 1;
|
| 205 |
break;
|
| 206 |
case 'entry':
|
| 207 |
$element = 'item';
|
| 208 |
$item += 1;
|
| 209 |
break;
|
| 210 |
}
|
| 211 |
|
| 212 |
$tag = $name;
|
| 213 |
}
|
| 214 |
|
| 215 |
/**
|
| 216 |
* Call-back function used by the XML parser.
|
| 217 |
*/
|
| 218 |
function aggregator_element_end($parser, $name) {
|
| 219 |
global $element;
|
| 220 |
|
| 221 |
switch ($name) {
|
| 222 |
case 'image':
|
| 223 |
case 'textinput':
|
| 224 |
case 'item':
|
| 225 |
case 'entry':
|
| 226 |
case 'content':
|
| 227 |
case 'info':
|
| 228 |
$element = '';
|
| 229 |
break;
|
| 230 |
case 'id':
|
| 231 |
if ($element == 'id') {
|
| 232 |
$element = '';
|
| 233 |
}
|
| 234 |
}
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Callback function used by the XML parser.
|
| 239 |
*/
|
| 240 |
function aggregator_element_data($parser, $data) {
|
| 241 |
global $channel, $element, $items, $item, $image, $tag;
|
| 242 |
$items += array($item => array());
|
| 243 |
switch ($element) {
|
| 244 |
case 'item':
|
| 245 |
$items[$item] += array($tag => '');
|
| 246 |
$items[$item][$tag] .= $data;
|
| 247 |
break;
|
| 248 |
case 'image':
|
| 249 |
case 'logo':
|
| 250 |
$image += array($tag => '');
|
| 251 |
$image[$tag] .= $data;
|
| 252 |
break;
|
| 253 |
case 'link':
|
| 254 |
if ($data) {
|
| 255 |
$items[$item] += array($tag => '');
|
| 256 |
$items[$item][$tag] .= $data;
|
| 257 |
}
|
| 258 |
break;
|
| 259 |
case 'content':
|
| 260 |
$items[$item] += array('content' => '');
|
| 261 |
$items[$item]['content'] .= $data;
|
| 262 |
break;
|
| 263 |
case 'summary':
|
| 264 |
$items[$item] += array('summary' => '');
|
| 265 |
$items[$item]['summary'] .= $data;
|
| 266 |
break;
|
| 267 |
case 'tagline':
|
| 268 |
case 'subtitle':
|
| 269 |
$channel += array('description' => '');
|
| 270 |
$channel['description'] .= $data;
|
| 271 |
break;
|
| 272 |
case 'info':
|
| 273 |
case 'id':
|
| 274 |
case 'textinput':
|
| 275 |
// The sub-element is not supported. However, we must recognize
|
| 276 |
// it or its contents will end up in the item array.
|
| 277 |
break;
|
| 278 |
default:
|
| 279 |
$channel += array($tag => '');
|
| 280 |
$channel[$tag] .= $data;
|
| 281 |
}
|
| 282 |
}
|
| 283 |
|
| 284 |
/**
|
| 285 |
* Parse the W3C date/time format, a subset of ISO 8601.
|
| 286 |
*
|
| 287 |
* PHP date parsing functions do not handle this format.
|
| 288 |
* See http://www.w3.org/TR/NOTE-datetime for more information.
|
| 289 |
* Originally from MagpieRSS (http://magpierss.sourceforge.net/).
|
| 290 |
*
|
| 291 |
* @param $date_str
|
| 292 |
* A string with a potentially W3C DTF date.
|
| 293 |
* @return
|
| 294 |
* A timestamp if parsed successfully or FALSE if not.
|
| 295 |
*/
|
| 296 |
function aggregator_parse_w3cdtf($date_str) {
|
| 297 |
if (preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/', $date_str, $match)) {
|
| 298 |
list($year, $month, $day, $hours, $minutes, $seconds) = array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
|
| 299 |
// Calculate the epoch for current date assuming GMT.
|
| 300 |
$epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
|
| 301 |
if ($match[10] != 'Z') { // Z is zulu time, aka GMT
|
| 302 |
list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9], $match[10]);
|
| 303 |
// Zero out the variables.
|
| 304 |
if (!$tz_hour) {
|
| 305 |
$tz_hour = 0;
|
| 306 |
}
|
| 307 |
if (!$tz_min) {
|
| 308 |
$tz_min = 0;
|
| 309 |
}
|
| 310 |
$offset_secs = (($tz_hour * 60) + $tz_min) * 60;
|
| 311 |
// Is timezone ahead of GMT? If yes, subtract offset.
|
| 312 |
if ($tz_mod == '+') {
|
| 313 |
$offset_secs *= -1;
|
| 314 |
}
|
| 315 |
$epoch += $offset_secs;
|
| 316 |
}
|
| 317 |
return $epoch;
|
| 318 |
}
|
| 319 |
else {
|
| 320 |
return FALSE;
|
| 321 |
}
|
| 322 |
}
|