summaryrefslogtreecommitdiff
path: root/modules/aggregator/aggregator.fetcher.inc
blob: fcad2657810e382c9aeb6194b31689f90b571523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
// $Id$

/**
 * @file
 * Fetcher functions for the aggregator module.
 */

/**
 * Implement hook_aggregator_fetch_info().
 */
function aggregator_aggregator_fetch_info() {
  return array(
    'title' => t('Default fetcher'),
    'description' => t('Downloads data from a URL using Drupal\'s HTTP request handler.'),
  );
}

/**
 * Implement hook_aggregator_fetch().
 */
function aggregator_aggregator_fetch($feed) {
  $feed->source_string = FALSE;

  // Generate conditional GET headers.
  $headers = array();
  if ($feed->etag) {
    $headers['If-None-Match'] = $feed->etag;
  }
  if ($feed->modified) {
    $headers['If-Modified-Since'] = gmdate('D, d M Y H:i:s', $feed->modified) . ' GMT';
  }

  // Request feed.
  $result = drupal_http_request($feed->url, array('headers' => $headers));


  // Process HTTP response code.
  switch ($result->code) {
    case 304:
      db_update('aggregator_feed')
        ->fields(array('checked' => REQUEST_TIME))
        ->condition('fid', $feed->fid)
        ->execute();
      drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
      break;
    case 301:
      $feed->url = $result->redirect_url;
      $feed->redirected = TRUE;
      // Do not break here.
    case 200:
    case 302:
    case 307:
      // We store the md5 hash of feed data in the database. When refreshing a
      // feed we compare stored hash and new hash calculated from downloaded
      // data. If both are equal we say that feed is not updated.
      if (!isset($result->data)) {
        $result->data = '';
      }
      if (!isset($result->headers)) {
        $result->headers = array();
      }
      $md5 = md5($result->data);
      if ($feed->hash == $md5) {
        db_update('aggregator_feed')
          ->condition('fid', $feed->fid)
          ->fields(array('checked' => REQUEST_TIME))
          ->execute();
        drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
        break;
      }

      $feed->source_string = $result->data;
      $feed->http_headers = $result->headers;
      break;
    default:
      watchdog('aggregator', 'The feed from %site seems to be broken, due to "%error".', array('%site' => $feed->title, '%error' => $result->code . ' ' . $result->error), WATCHDOG_WARNING);
      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)));
  }
}