Same name and namespace in other branches
- 4.7.x modules/aggregator.module \aggregator_parse_feed()
- 5.x modules/aggregator/aggregator.module \aggregator_parse_feed()
- 6.x modules/aggregator/aggregator.module \aggregator_parse_feed()
- 7.x modules/aggregator/aggregator.parser.inc \aggregator_parse_feed()
File
- modules/
aggregator.module, line 448 - Used to aggregate syndicated content (RSS and RDF).
Code
function aggregator_parse_feed(&$data, $feed) {
global $items, $image, $channel;
// Unset the global variables before we use them:
unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']);
$items = array();
$image = array();
$channel = array();
// parse the data:
$xml_parser = drupal_xml_parser_create($data);
xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end');
xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
if (!xml_parse($xml_parser, $data, 1)) {
$message = t('Failed to parse RSS feed %site: %error at line %line.', array(
'%site' => theme('placeholder', $feed['title']),
'%error' => xml_error_string(xml_get_error_code($xml_parser)),
'%line' => xml_get_current_line_number($xml_parser),
));
watchdog('aggregator', $message, WATCHDOG_WARNING);
drupal_set_message($message, 'error');
return 0;
}
xml_parser_free($xml_parser);
/*
** We reverse the array such that we store the first item last,
** and the last item first. In the database, the newest item
** should be at the top.
*/
$items = array_reverse($items);
foreach ($items as $item) {
unset($title, $link, $author, $description);
// Prepare the item:
foreach ($item as $key => $value) {
// TODO: Make handling of aggregated HTML more flexible/configurable.
$value = decode_entities(trim($value));
if ($key != 'LINK' && $key != 'GUID') {
$value = filter_xss($value);
}
$item[$key] = $value;
}
/*
** Resolve the item's title. If no title is found, we use
** up to 40 characters of the description ending at a word
** boundary but not splitting potential entities.
*/
if ($item['TITLE']) {
$title = $item['TITLE'];
}
else {
$title = preg_replace('/^(.*)[^\\w;&].*?$/', "\\1", truncate_utf8($item['DESCRIPTION'], 40));
}
/*
** Resolve the items link.
*/
if ($item['LINK']) {
$link = $item['LINK'];
}
elseif ($item['GUID'] && strncmp($item['GUID'], 'http://', 7) == 0) {
$link = $item['GUID'];
}
else {
$link = $feed['link'];
}
/*
** Try to resolve and parse the item's publication date. If no
** date is found, we use the current date instead.
*/
if ($item['PUBDATE']) {
$date = $item['PUBDATE'];
}
else {
if ($item['DC:DATE']) {
$date = $item['DC:DATE'];
}
else {
if ($item['DCTERMS:ISSUED']) {
$date = $item['DCTERMS:ISSUED'];
}
else {
if ($item['DCTERMS:CREATED']) {
$date = $item['DCTERMS:CREATED'];
}
else {
if ($item['DCTERMS:MODIFIED']) {
$date = $item['DCTERMS:MODIFIED'];
}
else {
$date = 'now';
}
}
}
}
}
$timestamp = strtotime($date);
// strtotime() returns -1 on failure
if ($timestamp < 0) {
$timestamp = aggregator_parse_w3cdtf($date);
// also returns -1 on failure
if ($timestamp < 0) {
$timestamp = time();
// better than nothing
}
}
/*
** Save this item. Try to avoid duplicate entries as much as
** possible. If we find a duplicate entry, we resolve it and
** pass along it's ID such that we can update it if needed.
*/
if ($link && $link != $feed['link'] && $link != $feed['url']) {
$entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND link = '%s'", $feed['fid'], $link));
}
else {
$entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $feed['fid'], $title));
}
aggregator_save_item(array(
'iid' => $entry->iid,
'fid' => $feed['fid'],
'timestamp' => $timestamp,
'title' => $title,
'link' => $link,
'author' => $item['AUTHOR'],
'description' => $item['DESCRIPTION'],
));
}
/*
** Remove all items that are older than flush item timer:
*/
$age = time() - variable_get('aggregator_clear', 9676800);
$result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed['fid'], $age);
if (db_num_rows($result)) {
$items = array();
while ($item = db_fetch_object($result)) {
$items[] = $item->iid;
}
db_query('DELETE FROM {aggregator_category_item} WHERE iid IN (' . implode(', ', $items) . ')');
db_query('DELETE FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed['fid'], $age);
}
return 1;
}