| 1 |
<?php
|
| 2 |
// $Id: aggregator.fetcher.inc,v 1.9 2009/09/25 23:53:26 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Fetcher functions for the aggregator module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_aggregator_fetch_info().
|
| 11 |
*/
|
| 12 |
function aggregator_aggregator_fetch_info() {
|
| 13 |
return array(
|
| 14 |
'title' => t('Default fetcher'),
|
| 15 |
'description' => t('Downloads data from a URL using Drupal\'s HTTP request handler.'),
|
| 16 |
);
|
| 17 |
}
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implement hook_aggregator_fetch().
|
| 21 |
*/
|
| 22 |
function aggregator_aggregator_fetch($feed) {
|
| 23 |
$feed->source_string = FALSE;
|
| 24 |
|
| 25 |
// Generate conditional GET headers.
|
| 26 |
$headers = array();
|
| 27 |
if ($feed->etag) {
|
| 28 |
$headers['If-None-Match'] = $feed->etag;
|
| 29 |
}
|
| 30 |
if ($feed->modified) {
|
| 31 |
$headers['If-Modified-Since'] = gmdate(DATE_RFC1123, $feed->modified);
|
| 32 |
}
|
| 33 |
|
| 34 |
// Request feed.
|
| 35 |
$result = drupal_http_request($feed->url, array('headers' => $headers));
|
| 36 |
|
| 37 |
// Process HTTP response code.
|
| 38 |
switch ($result->code) {
|
| 39 |
case 304:
|
| 40 |
db_update('aggregator_feed')
|
| 41 |
->fields(array('checked' => REQUEST_TIME))
|
| 42 |
->condition('fid', $feed->fid)
|
| 43 |
->execute();
|
| 44 |
drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
|
| 45 |
break;
|
| 46 |
case 301:
|
| 47 |
$feed->url = $result->redirect_url;
|
| 48 |
// Do not break here.
|
| 49 |
case 200:
|
| 50 |
case 302:
|
| 51 |
case 307:
|
| 52 |
// We store the md5 hash of feed data in the database. When refreshing a
|
| 53 |
// feed we compare stored hash and new hash calculated from downloaded
|
| 54 |
// data. If both are equal we say that feed is not updated.
|
| 55 |
if (!isset($result->data)) {
|
| 56 |
$result->data = '';
|
| 57 |
}
|
| 58 |
if (!isset($result->headers)) {
|
| 59 |
$result->headers = array();
|
| 60 |
}
|
| 61 |
$md5 = md5($result->data);
|
| 62 |
if ($feed->hash == $md5) {
|
| 63 |
db_update('aggregator_feed')
|
| 64 |
->condition('fid', $feed->fid)
|
| 65 |
->fields(array('checked' => REQUEST_TIME))
|
| 66 |
->execute();
|
| 67 |
drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
|
| 68 |
break;
|
| 69 |
}
|
| 70 |
|
| 71 |
$feed->source_string = $result->data;
|
| 72 |
$feed->http_headers = $result->headers;
|
| 73 |
break;
|
| 74 |
default:
|
| 75 |
watchdog('aggregator', 'The feed from %site seems to be broken, due to "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING);
|
| 76 |
drupal_set_message(t('The feed from %site seems to be broken, because of error "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error)));
|
| 77 |
}
|
| 78 |
}
|