function aggregator_parse_feed in Drupal 6
Same name and namespace in other branches
- 4 modules/aggregator.module \aggregator_parse_feed()
- 5 modules/aggregator/aggregator.module \aggregator_parse_feed()
- 7 modules/aggregator/aggregator.parser.inc \aggregator_parse_feed()
Parse a feed and store its items.
Parameters
$data: The feed data.
$feed: An associative array describing the feed to be parsed.
Return value
0 on error, 1 otherwise.
1 call to aggregator_parse_feed()
- aggregator_refresh in modules/
aggregator/ aggregator.module - Checks a news feed for new items.
File
- modules/
aggregator/ aggregator.module, line 699 - Used to aggregate syndicated content (RSS, RDF, and Atom).
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)) {
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);
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');
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);
// Initialize variables.
$title = $link = $author = $description = $guid = NULL;
foreach ($items as $item) {
unset($title, $link, $author, $description, $guid);
// Prepare the item:
foreach ($item as $key => $value) {
$item[$key] = trim($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 (!empty($item['TITLE'])) {
$title = $item['TITLE'];
}
elseif (!empty($item['DESCRIPTION'])) {
$title = preg_replace('/^(.*)[^\\w;&].*?$/', "\\1", truncate_utf8($item['DESCRIPTION'], 40));
}
else {
$title = '';
}
// Resolve the items link.
if (!empty($item['LINK'])) {
$link = $item['LINK'];
}
else {
$link = $feed['link'];
}
// Atom feeds use ID rather than GUID.
if (isset($item['GUID'])) {
$guid = $item['GUID'];
}
elseif (isset($item['ID'])) {
$guid = $item['ID'];
}
else {
$guid = '';
}
// Atom feeds have a CONTENT and/or SUMMARY tag instead of a DESCRIPTION tag.
if (!empty($item['CONTENT:ENCODED'])) {
$item['DESCRIPTION'] = $item['CONTENT:ENCODED'];
}
else {
if (!empty($item['SUMMARY'])) {
$item['DESCRIPTION'] = $item['SUMMARY'];
}
else {
if (!empty($item['CONTENT'])) {
$item['DESCRIPTION'] = $item['CONTENT'];
}
}
}
// Try to resolve and parse the item's publication date. If no date is
// found, we use the current date instead.
$date = 'now';
foreach (array(
'PUBDATE',
'DC:DATE',
'DCTERMS:ISSUED',
'DCTERMS:CREATED',
'DCTERMS:MODIFIED',
'ISSUED',
'CREATED',
'MODIFIED',
'PUBLISHED',
'UPDATED',
) as $key) {
if (!empty($item[$key])) {
$date = $item[$key];
break;
}
}
$timestamp = strtotime($date);
// As of PHP 5.1.0, strtotime returns FALSE on failure instead of -1.
if ($timestamp <= 0) {
$timestamp = aggregator_parse_w3cdtf($date);
// Returns FALSE on failure
if (!$timestamp) {
$timestamp = time();
// better than nothing
}
}
// Resolve dc:creator tag as the item author if author tag is not set.
if (empty($item['AUTHOR']) && !empty($item['DC:CREATOR'])) {
$item['AUTHOR'] = $item['DC:CREATOR'];
}
// Save this item. Try to avoid duplicate entries as much as possible. If
// we find a duplicate entry, we resolve it and pass along its ID is such
// that we can update it if needed.
if (!empty($guid)) {
$entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND guid = '%s'", $feed['fid'], $guid));
}
else {
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));
}
}
$item += array(
'AUTHOR' => '',
'DESCRIPTION' => '',
);
aggregator_save_item(array(
'iid' => isset($entry->iid) ? $entry->iid : '',
'fid' => $feed['fid'],
'timestamp' => $timestamp,
'title' => $title,
'link' => $link,
'author' => $item['AUTHOR'],
'description' => $item['DESCRIPTION'],
'guid' => $guid,
));
}
// 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);
$items = array();
$num_rows = FALSE;
while ($item = db_fetch_object($result)) {
$items[] = $item->iid;
$num_rows = TRUE;
}
if ($num_rows) {
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;
}