You are here

function aggregator_parse_feed in Drupal 4

Same name and namespace in other branches
  1. 5 modules/aggregator/aggregator.module \aggregator_parse_feed()
  2. 6 modules/aggregator/aggregator.module \aggregator_parse_feed()
  3. 7 modules/aggregator/aggregator.parser.inc \aggregator_parse_feed()
1 call to aggregator_parse_feed()
aggregator_refresh in modules/aggregator.module
Checks a news feed for new items.

File

modules/aggregator.module, line 809
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', t('The RSS-feed from %site seems to be broken, due to an error "%error" on 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_WARNING);
    drupal_set_message(t('The RSS-feed from %site seems to be broken, because of error "%error" on 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),
    )), '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) {
      $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 ($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'];
    }

    /**
     * Atom feeds have a CONTENT and/or SUMMARY tag instead of a DESCRIPTION tag
     */
    if ($item['CONTENT:ENCODED']) {
      $item['DESCRIPTION'] = $item['CONTENT:ENCODED'];
    }
    else {
      if ($item['SUMMARY']) {
        $item['DESCRIPTION'] = $item['SUMMARY'];
      }
      else {
        if ($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.
     */
    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 {
              if ($item['ISSUED']) {
                $date = $item['ISSUED'];
              }
              else {
                if ($item['CREATED']) {
                  $date = $item['CREATED'];
                }
                else {
                  if ($item['MODIFIED']) {
                    $date = $item['MODIFIED'];
                  }
                  else {
                    $date = 'now';
                  }
                }
              }
            }
          }
        }
      }
    }
    $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
      }
    }

    /*
     ** 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;
}