aggregator_parse_feed

Versions
4.6 – 7
aggregator_parse_feed(&$data, $feed)

Code

modules/aggregator.module, line 448

<?php
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'];                        // RSS 2.0
    else if ($item['DC:DATE']) $date = $item['DC:DATE'];                   // Dublin core
    else if ($item['DCTERMS:ISSUED']) $date = $item['DCTERMS:ISSUED'];     // Dublin core
    else if ($item['DCTERMS:CREATED']) $date = $item['DCTERMS:CREATED'];   // Dublin core
    else if ($item['DCTERMS:MODIFIED']) $date = $item['DCTERMS:MODIFIED']; // Dublin core
    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;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.