Code coverage for /20080809/modules/aggregator/aggregator.module

Line #Times calledCode
1
<?php
2
// $Id: aggregator.module,v 1.385 2008/08/08 20:09:22 dries Exp $
3
4
/**
5
 * @file
6
 * Used to aggregate syndicated content (RSS, RDF, and Atom).
7
 */
8
9
/**
10
 * Implementation of hook_help().
11
 */
12120
function aggregator_help($path, $arg) {
13
  switch ($path) {
1476
    case 'admin/help#aggregator':
1552
      $output = '<p>' . t('The aggregator is a powerful on-site syndicator
and news reader that gathers fresh content from RSS-, RDF-, and Atom-based
feeds made available across the web. Thousands of sites (particularly news
sites and blogs) publish their latest headlines and posts in feeds, using a
number of standardized XML-based formats. Formats supported by the
aggregator include <a href="@rss">RSS</a>, <a href="@rdf">RDF</a>, and <a
href="@atom">Atom</a>.', array('@rss' =>
'http://cyber.law.harvard.edu/rss/', '@rdf' => 'http://www.w3.org/RDF/',
'@atom' => 'http://www.atomenabled.org')) . '</p>';
1652
      $output .= '<p>' . t('Feeds contain feed items, or individual posts
published by the site providing the feed. Feeds may be grouped in
categories, generally by topic. Users view feed items in the <a
href="@aggregator">main aggregator display</a> or by <a
href="@aggregator-sources">their source</a>. Administrators can <a
href="@feededit">add, edit and delete feeds</a> and choose how often to
check each feed for newly updated items. The most recent items in either a
feed or category can be displayed as a block through the <a
href="@admin-block">blocks administration page</a>. A <a
href="@aggregator-opml">machine-readable OPML file</a> of all feeds is
available. A correctly configured <a href="@cron">cron maintenance task</a>
is required to update feeds automatically.', array('@aggregator' =>
url('aggregator'), '@aggregator-sources' => url('aggregator/sources'),
'@feededit' => url('admin/content/aggregator'), '@admin-block' =>
url('admin/build/block'), '@aggregator-opml' => url('aggregator/opml'),
'@cron' => url('admin/reports/status'))) . '</p>';
1752
      $output .= '<p>' . t('For more information, see the online handbook
entry for <a href="@aggregator">Aggregator module</a>.',
array('@aggregator' => 'http://drupal.org/handbook/modules/aggregator/')) .
'</p>';
1852
      return $output;
1974
    case 'admin/content/aggregator':
2012
      $output = '<p>' . t('Thousands of sites (particularly news sites and
blogs) publish their latest headlines and posts in feeds, using a number of
standardized XML-based formats. Formats supported by the aggregator include
<a href="@rss">RSS</a>, <a href="@rdf">RDF</a>, and <a
href="@atom">Atom</a>.', array('@rss' =>
'http://cyber.law.harvard.edu/rss/', '@rdf' => 'http://www.w3.org/RDF/',
'@atom' => 'http://www.atomenabled.org')) . '</p>';
2112
      $output .= '<p>' . t('Current feeds are listed below, and <a
href="@addfeed">new feeds may be added</a>. For each feed or feed category,
the <em>latest items</em> block may be enabled at the <a
href="@block">blocks administration page</a>.', array('@addfeed' =>
url('admin/content/aggregator/add/feed'), '@block' =>
url('admin/build/block'))) . '</p>';
2212
      return $output;
2362
    case 'admin/content/aggregator/add/feed':
2412
      return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may
only have one entry.') . '</p>';
2550
    case 'admin/content/aggregator/add/category':
262
      return '<p>' . t('Categories allow feed items from different feeds to
be grouped together. For example, several sport-related feeds may belong to
a category named <em>Sports</em>. Feed items may be grouped automatically
(by selecting a category when creating or editing a feed) or manually (via
the <em>Categorize</em> page available from feed item listings). Each
category provides its own feed page and block.') . '</p>';
2748
    case 'admin/content/aggregator/add/opml':
2813
      return '<p>' . t('<acronym title="Outline Processor Markup
Language">OPML</acronym> is an XML format used to exchange multiple feeds
between aggregators. A single OPML document may contain a collection of
many feeds. Drupal can parse such a file and import all feeds at once,
saving you the effort of adding them manually. You may either upload a
local file from your computer or enter a URL where Drupal can download
it.') . '</p>';
290
  }
3035
}
31
32
/**
33
 * Implementation of hook_theme().
34
 */
35120
function aggregator_theme() {
36
  return array(
37
    'aggregator_wrapper' => array(
3810
      'arguments' => array('content' => NULL),
3910
      'file' => 'aggregator.pages.inc',
4010
      'template' => 'aggregator-wrapper',
4110
    ),
42
    'aggregator_categorize_items' => array(
4310
      'arguments' => array('form' => NULL),
4410
      'file' => 'aggregator.pages.inc',
4510
    ),
46
    'aggregator_feed_source' => array(
4710
      'arguments' => array('feed' => NULL),
4810
      'file' => 'aggregator.pages.inc',
4910
      'template' => 'aggregator-feed-source',
5010
    ),
51
    'aggregator_block_item' => array(
5210
      'arguments' => array('item' => NULL, 'feed' => 0),
5310
    ),
54
    'aggregator_summary_items' => array(
5510
      'arguments' => array('summary_items' => NULL, 'source' => NULL),
5610
      'file' => 'aggregator.pages.inc',
5710
      'template' => 'aggregator-summary-items',
5810
    ),
59
    'aggregator_summary_item' => array(
6010
      'arguments' => array('item' => NULL),
6110
      'file' => 'aggregator.pages.inc',
6210
      'template' => 'aggregator-summary-item',
6310
    ),
64
    'aggregator_item' => array(
6510
      'arguments' => array('item' => NULL),
6610
      'file' => 'aggregator.pages.inc',
6710
      'template' => 'aggregator-item',
6810
    ),
69
    'aggregator_page_opml' => array(
7010
      'arguments' => array('feeds' => NULL),
7110
      'file' => 'aggregator.pages.inc',
7210
    ),
73
    'aggregator_page_rss' => array(
7410
      'arguments' => array('feeds' => NULL, 'category' => NULL),
7510
      'file' => 'aggregator.pages.inc',
7610
    ),
7710
  );
780
}
79
80
/**
81
 * Implementation of hook_menu().
82
 */
83120
function aggregator_menu() {
8411
  $items['admin/content/aggregator'] = array(
8511
    'title' => 'Feed aggregator',
8611
    'description' => "Configure which content your site aggregates from
other sites, how often it polls them, and how they're categorized.",
8711
    'page callback' => 'aggregator_admin_overview',
8811
    'access arguments' => array('administer news feeds'),
89
  );
9011
  $items['admin/content/aggregator/add/feed'] = array(
9111
    'title' => 'Add feed',
9211
    'page callback' => 'drupal_get_form',
9311
    'page arguments' => array('aggregator_form_feed'),
9411
    'access arguments' => array('administer news feeds'),
9511
    'type' => MENU_LOCAL_TASK,
9611
    'parent' => 'admin/content/aggregator',
97
  );
9811
  $items['admin/content/aggregator/add/category'] = array(
9911
    'title' => 'Add category',
10011
    'page callback' => 'drupal_get_form',
10111
    'page arguments' => array('aggregator_form_category'),
10211
    'access arguments' => array('administer news feeds'),
10311
    'type' => MENU_LOCAL_TASK,
10411
    'parent' => 'admin/content/aggregator',
105
  );
10611
  $items['admin/content/aggregator/add/opml'] = array(
10711
    'title' => 'Import OPML',
10811
    'page callback' => 'drupal_get_form',
10911
    'page arguments' => array('aggregator_form_opml'),
11011
    'access arguments' => array('administer news feeds'),
11111
    'type' => MENU_LOCAL_TASK,
11211
    'parent' => 'admin/content/aggregator',
113
  );
11411
  $items['admin/content/aggregator/remove/%aggregator_feed'] = array(
11511
    'title' => 'Remove items',
11611
    'page callback' => 'drupal_get_form',
11711
    'page arguments' => array('aggregator_admin_remove_feed', 4),
11811
    'access arguments' => array('administer news feeds'),
11911
    'type' => MENU_CALLBACK,
120
  );
12111
  $items['admin/content/aggregator/update/%aggregator_feed'] = array(
12211
    'title' => 'Update items',
12311
    'page callback' => 'aggregator_admin_refresh_feed',
12411
    'page arguments' => array(4),
12511
    'access arguments' => array('administer news feeds'),
12611
    'type' => MENU_CALLBACK,
127
  );
12811
  $items['admin/content/aggregator/list'] = array(
12911
    'title' => 'List',
13011
    'type' => MENU_DEFAULT_LOCAL_TASK,
13111
    'weight' => -10,
132
  );
13311
  $items['admin/content/aggregator/settings'] = array(
13411
    'title' => 'Settings',
13511
    'page callback' => 'drupal_get_form',
13611
    'page arguments' => array('aggregator_admin_settings'),
13711
    'type' => MENU_LOCAL_TASK,
13811
    'weight' => 10,
13911
    'access arguments' => array('administer news feeds'),
140
  );
14111
  $items['aggregator'] = array(
14211
    'title' => 'Feed aggregator',
14311
    'page callback' => 'aggregator_page_last',
14411
    'access arguments' => array('access news feeds'),
14511
    'weight' => 5,
146
  );
14711
  $items['aggregator/sources'] = array(
14811
    'title' => 'Sources',
14911
    'page callback' => 'aggregator_page_sources',
15011
    'access arguments' => array('access news feeds'),
151
  );
15211
  $items['aggregator/categories'] = array(
15311
    'title' => 'Categories',
15411
    'page callback' => 'aggregator_page_categories',
15511
    'access callback' => '_aggregator_has_categories',
156
  );
15711
  $items['aggregator/rss'] = array(
15811
    'title' => 'RSS feed',
15911
    'page callback' => 'aggregator_page_rss',
16011
    'access arguments' => array('access news feeds'),
16111
    'type' => MENU_CALLBACK,
162
  );
16311
  $items['aggregator/opml'] = array(
16411
    'title' => 'OPML feed',
16511
    'page callback' => 'aggregator_page_opml',
16611
    'access arguments' => array('access news feeds'),
16711
    'type' => MENU_CALLBACK,
168
  );
16911
  $items['aggregator/categories/%aggregator_category'] = array(
17011
    'title callback' => '_aggregator_category_title',
17111
    'title arguments' => array(2),
17211
    'page callback' => 'aggregator_page_category',
17311
    'page arguments' => array(2),
17411
    'access callback' => 'user_access',
17511
    'access arguments' => array('access news feeds'),
176
  );
17711
  $items['aggregator/categories/%aggregator_category/view'] = array(
17811
    'title' => 'View',
17911
    'type' => MENU_DEFAULT_LOCAL_TASK,
18011
    'weight' => -10,
181
  );
18211
  $items['aggregator/categories/%aggregator_category/categorize'] = array(
18311
    'title' => 'Categorize',
18411
    'page callback' => 'drupal_get_form',
18511
    'page arguments' => array('aggregator_page_category', 2),
18611
    'access arguments' => array('administer news feeds'),
18711
    'type' => MENU_LOCAL_TASK,
188
  );
18911
  $items['aggregator/categories/%aggregator_category/configure'] = array(
19011
    'title' => 'Configure',
19111
    'page callback' => 'drupal_get_form',
19211
    'page arguments' => array('aggregator_form_category', 2),
19311
    'access arguments' => array('administer news feeds'),
19411
    'type' => MENU_LOCAL_TASK,
19511
    'weight' => 1,
196
  );
19711
  $items['aggregator/sources/%aggregator_feed'] = array(
19811
    'page callback' => 'aggregator_page_source',
19911
    'page arguments' => array(2),
20011
    'access arguments' => array('access news feeds'),
20111
    'type' => MENU_CALLBACK,
202
  );
20311
  $items['aggregator/sources/%aggregator_feed/view'] = array(
20411
    'title' => 'View',
20511
    'type' => MENU_DEFAULT_LOCAL_TASK,
20611
    'weight' => -10,
207
  );
20811
  $items['aggregator/sources/%aggregator_feed/categorize'] = array(
20911
    'title' => 'Categorize',
21011
    'page callback' => 'drupal_get_form',
21111
    'page arguments' => array('aggregator_page_source', 2),
21211
    'access arguments' => array('administer news feeds'),
21311
    'type' => MENU_LOCAL_TASK,
214
  );
21511
  $items['aggregator/sources/%aggregator_feed/configure'] = array(
21611
    'title' => 'Configure',
21711
    'page callback' => 'drupal_get_form',
21811
    'page arguments' => array('aggregator_form_feed', 2),
21911
    'access arguments' => array('administer news feeds'),
22011
    'type' => MENU_LOCAL_TASK,
22111
    'weight' => 1,
222
  );
22311
  $items['admin/content/aggregator/edit/feed/%aggregator_feed'] = array(
22411
    'title' => 'Edit feed',
22511
    'page callback' => 'drupal_get_form',
22611
    'page arguments' => array('aggregator_form_feed', 5),
22711
    'access arguments' => array('administer news feeds'),
22811
    'type' => MENU_CALLBACK,
229
  );
23011
  $items['admin/content/aggregator/edit/category/%aggregator_category'] =
array(
23111
    'title' => 'Edit category',
23211
    'page callback' => 'drupal_get_form',
23311
    'page arguments' => array('aggregator_form_category', 5),
23411
    'access arguments' => array('administer news feeds'),
23511
    'type' => MENU_CALLBACK,
236
  );
237
23811
  return $items;
2390
}
240
241
/**
242
 * Menu callback.
243
 *
244
 * @return
245
 *   An aggregator category title.
246
 */
247120
function _aggregator_category_title($category) {
2480
  return $category['title'];
2490
}
250
251
/**
252
 * Implementation of hook_init().
253
 */
254120
function aggregator_init() {
255107
  drupal_add_css(drupal_get_path('module', 'aggregator') .
'/aggregator.css');
256107
}
257
258
/**
259
 * Find out whether there are any aggregator categories.
260
 *
261
 * @return
262
 *   TRUE if there is at least one category and the user has access to
them, FALSE otherwise.
263
 */
264120
function _aggregator_has_categories() {
2652
  return user_access('access news feeds') && db_result(db_query('SELECT
COUNT(*) FROM {aggregator_category}'));
2660
}
267
268
/**
269
 * Implementation of hook_perm().
270
 */
271120
function aggregator_perm() {
272
  return array(
2737
    'administer news feeds' => t('Add, edit or delete news feeds that are
aggregated to your site.'),
2747
    'access news feeds' => t('View aggregated news feed items.'),
2757
  );
2760
}
277
278
/**
279
 * Implementation of hook_cron().
280
 *
281
 * Checks news feeds for updates once their refresh interval has elapsed.
282
 */
283120
function aggregator_cron() {
2840
  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked +
refresh < %d', time());
2850
  while ($feed = db_fetch_array($result)) {
2860
    aggregator_refresh($feed);
2870
  }
2880
}
289
290
/**
291
 * Implementation of hook_block().
292
 *
293
 * Generates blocks for the latest news items in each category and feed.
294
 */
295120
function aggregator_block($op = 'list', $delta = '', $edit = array()) {
2960
  if (user_access('access news feeds')) {
2970
    if ($op == 'list') {
2980
      $result = db_query('SELECT cid, title FROM {aggregator_category}
ORDER BY title');
2990
      while ($category = db_fetch_object($result)) {
3000
        $block['category-' . $category->cid]['info'] = t('!title category
latest items', array('!title' => $category->title));
3010
      }
3020
      $result = db_query('SELECT fid, title FROM {aggregator_feed} ORDER BY
fid');
3030
      while ($feed = db_fetch_object($result)) {
3040
        $block['feed-' . $feed->fid]['info'] = t('!title feed latest
items', array('!title' => $feed->title));
3050
      }
3060
    }
3070
    elseif ($op == 'configure') {
3080
      list($type, $id) = explode('-', $delta);
3090
      if ($type == 'category') {
3100
        $value = db_result(db_query('SELECT block FROM
{aggregator_category} WHERE cid = %d', $id));
3110
      }
312
      else {
3130
        $value = db_result(db_query('SELECT block FROM {aggregator_feed}
WHERE fid = %d', $id));
314
      }
3150
      $form['block'] = array('#type' => 'select', '#title' => t('Number of
news items in block'), '#default_value' => $value, '#options' =>
drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20)));
3160
      return $form;
3170
    }
3180
    elseif ($op == 'save') {
3190
      list($type, $id) = explode('-', $delta);
3200
      if ($type == 'category') {
3210
        $value = db_query('UPDATE {aggregator_category} SET block = %d
WHERE cid = %d', $edit['block'], $id);
3220
      }
323
      else {
3240
        $value = db_query('UPDATE {aggregator_feed} SET block = %d WHERE
fid = %d', $edit['block'], $id);
325
      }
3260
    }
3270
    elseif ($op == 'view') {
3280
      list($type, $id) = explode('-', $delta);
329
      switch ($type) {
3300
        case 'feed':
3310
          if ($feed = db_fetch_object(db_query('SELECT fid, title, block
FROM {aggregator_feed} WHERE fid = %d', $id))) {
3320
            $block['subject'] = check_plain($feed->title);
3330
            $result = db_query_range('SELECT * FROM {aggregator_item} WHERE
fid = %d ORDER BY timestamp DESC, iid DESC', $feed->fid, 0, $feed->block);
3340
            $read_more = theme('more_link', url('aggregator/sources/' .
$feed->fid), t("View this feed's recent news."));
3350
          }
3360
          break;
337
3380
        case 'category':
3390
          if ($category = db_fetch_object(db_query('SELECT cid, title,
block FROM {aggregator_category} WHERE cid = %d', $id))) {
3400
            $block['subject'] = check_plain($category->title);
3410
            $result = db_query_range('SELECT i.* FROM
{aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid =
i.iid WHERE ci.cid = %d ORDER BY i.timestamp DESC, i.iid DESC',
$category->cid, 0, $category->block);
3420
            $read_more = theme('more_link', url('aggregator/categories/' .
$category->cid), t("View this category's recent news."));
3430
          }
3440
          break;
3450
      }
3460
      $items = array();
3470
      while ($item = db_fetch_object($result)) {
3480
        $items[] = theme('aggregator_block_item', $item);
3490
      }
350
351
      // Only display the block if there are items to show.
3520
      if (count($items) > 0) {
3530
        $block['content'] = theme('item_list', $items) . $read_more;
3540
      }
3550
    }
3560
    if (isset($block)) {
3570
      return $block;
3580
    }
3590
  }
3600
}
361
362
/**
363
 * Add/edit/delete aggregator categories.
364
 *
365
 * @param $edit
366
 *   An associative array describing the category to be
added/edited/deleted.
367
 */
368120
function aggregator_save_category($edit) {
3691
  $link_path = 'aggregator/categories/';
3701
  if (!empty($edit['cid'])) {
3710
    $link_path .= $edit['cid'];
3720
    if (!empty($edit['title'])) {
3730
      db_query("UPDATE {aggregator_category} SET title = '%s', description
= '%s' WHERE cid = %d", $edit['title'], $edit['description'],
$edit['cid']);
3740
      $op = 'update';
3750
    }
376
    else {
3770
      db_query('DELETE FROM {aggregator_category} WHERE cid = %d',
$edit['cid']);
378
      // Make sure there is no active block for this category.
3790
      db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'",
'aggregator', 'category-' . $edit['cid']);
3800
      $edit['title'] = '';
3810
      $op = 'delete';
382
    }
3830
  }
3841
  elseif (!empty($edit['title'])) {
385
    // A single unique id for bundles and feeds, to use in blocks.
3861
    db_query("INSERT INTO {aggregator_category} (title, description, block)
VALUES ('%s', '%s', 5)", $edit['title'], $edit['description']);
3871
    $link_path .= db_last_insert_id('aggregator_category', 'cid');
3881
    $op = 'insert';
3891
  }
3901
  if (isset($op)) {
3911
    menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
3921
  }
3931
}
394
395
/**
396
 * Add/edit/delete an aggregator feed.
397
 *
398
 * @param $edit
399
 *   An associative array describing the feed to be added/edited/deleted.
400
 */
401120
function aggregator_save_feed($edit) {
40214
  if (!empty($edit['fid'])) {
403
    // An existing feed is being modified, delete the category listings.
4047
    db_query('DELETE FROM {aggregator_category_feed} WHERE fid = %d',
$edit['fid']);
4057
  }
40614
  if (!empty($edit['fid']) && !empty($edit['title'])) {
4071
    db_query("UPDATE {aggregator_feed} SET title = '%s', url = '%s',
refresh = %d WHERE fid = %d", $edit['title'], $edit['url'],
$edit['refresh'], $edit['fid']);
4081
  }
40913
  elseif (!empty($edit['fid'])) {
4106
    $items = array();
4116
    $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d',
$edit['fid']);
4126
    while ($item = db_fetch_object($result)) {
4130
      $items[] = "iid = $item->iid";
4140
    }
4156
    if (!empty($items)) {
4160
      db_query('DELETE FROM {aggregator_category_item} WHERE ' . implode('
OR ', $items));
4170
    }
4186
    db_query('DELETE FROM {aggregator_feed} WHERE fid = %d',
$edit['fid']);
4196
    db_query('DELETE FROM {aggregator_item} WHERE fid = %d',
$edit['fid']);
420
    // Make sure there is no active block for this feed.
4216
    db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'",
'aggregator', 'feed-' . $edit['fid']);
4226
  }
4237
  elseif (!empty($edit['title'])) {
4247
    db_query("INSERT INTO {aggregator_feed} (title, url, refresh, block,
description, image) VALUES ('%s', '%s', %d, 5, '', '')", $edit['title'],
$edit['url'], $edit['refresh']);
425
    // A single unique ID for bundles and feeds, to use in blocks.
4267
    $edit['fid'] = db_last_insert_id('aggregator_feed', 'fid');
4277
  }
42814
  if (!empty($edit['title'])) {
429
    // The feed is being saved, save the categories as well.
4308
    if (!empty($edit['category'])) {
4312
      foreach ($edit['category'] as $cid => $value) {
4322
        if ($value) {
4331
          db_query('INSERT INTO {aggregator_category_feed} (fid, cid)
VALUES (%d, %d)', $edit['fid'], $cid);
4341
        }
4352
      }
4362
    }
4378
  }
43814
}
439
440
/**
441
 * Removes all items from a feed.
442
 *
443
 * @param $feed
444
 *   An associative array describing the feed to be cleared.
445
 */
446120
function aggregator_remove($feed) {
4472
  $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d',
$feed['fid']);
4482
  while ($item = db_fetch_object($result)) {
4490
    $items[] = "iid = $item->iid";
4500
  }
4512
  if (!empty($items)) {
4520
    db_query('DELETE FROM {aggregator_category_item} WHERE ' . implode(' OR
', $items));
4530
  }
4542
  db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $feed['fid']);
4552
  db_query("UPDATE {aggregator_feed} SET checked = 0, etag = '', modified =
0 WHERE fid = %d", $feed['fid']);
4562
  drupal_set_message(t('The news items from %site have been removed.',
array('%site' => $feed['title'])));
4572
}
458
459
/**
460
 * Callback function used by the XML parser.
461
 */
462120
function aggregator_element_start($parser, $name, $attributes) {
4633
  global $item, $element, $tag, $items, $channel;
464
465
  switch ($name) {
4663
    case 'IMAGE':
4673
    case 'TEXTINPUT':
4683
    case 'CONTENT':
4693
    case 'SUMMARY':
4703
    case 'TAGLINE':
4713
    case 'SUBTITLE':
4723
    case 'LOGO':
4733
    case 'INFO':
4740
      $element = $name;
4750
      break;
4763
    case 'ID':
4770
      if ($element != 'ITEM') {
4780
        $element = $name;
4790
      }
4803
    case 'LINK':
4813
      if (!empty($attributes['REL']) && $attributes['REL'] == 'alternate')
{
4820
        if ($element == 'ITEM') {
4830
          $items[$item]['LINK'] = $attributes['HREF'];
4840
        }
485
        else {
4860
          $channel['LINK'] = $attributes['HREF'];
487
        }
4880
      }
4893
      break;
4903
    case 'ITEM':
4910
      $element = $name;
4920
      $item += 1;
4930
      break;
4943
    case 'ENTRY':
4950
      $element = 'ITEM';
4960
      $item += 1;
4970
      break;
4980
  }
499
5003
  $tag = $name;
5013
}
502
503
/**
504
 * Call-back function used by the XML parser.
505
 */
506120
function aggregator_element_end($parser, $name) {
5073
  global $element;
508
509
  switch ($name) {
5103
    case 'IMAGE':
5113
    case 'TEXTINPUT':
5123
    case 'ITEM':
5133
    case 'ENTRY':
5143
    case 'CONTENT':
5153
    case 'INFO':
5160
      $element = '';
5170
      break;
5183
    case 'ID':
5190
      if ($element == 'ID') {
5200
        $element = '';
5210
      }
5220
  }
5233
}
524
525
/**
526
 * Callback function used by the XML parser.
527
 */
528120
function aggregator_element_data($parser, $data) {
5293
  global $channel, $element, $items, $item, $image, $tag;
5303
  $items += array($item => array());
531
  switch ($element) {
5323
    case 'ITEM':
5330
      $items[$item] += array($tag => '');
5340
      $items[$item][$tag] .= $data;
5350
      break;
5363
    case 'IMAGE':
5373
    case 'LOGO':
5380
      $image += array($tag => '');
5390
      $image[$tag] .= $data;
5400
      break;
5413
    case 'LINK':
5420
      if ($data) {
5430
        $items[$item] += array($tag => '');
5440
        $items[$item][$tag] .= $data;
5450
      }
5460
      break;
5473
    case 'CONTENT':
5480
      $items[$item] += array('CONTENT' => '');
5490
      $items[$item]['CONTENT'] .= $data;
5500
      break;
5513
    case 'SUMMARY':
5520
      $items[$item] += array('SUMMARY' => '');
5530
      $items[$item]['SUMMARY'] .= $data;
5540
      break;
5553
    case 'TAGLINE':
5563
    case 'SUBTITLE':
5570
      $channel += array('DESCRIPTION' => '');
5580
      $channel['DESCRIPTION'] .= $data;
5590
      break;
5603
    case 'INFO':
5613
    case 'ID':
5623
    case 'TEXTINPUT':
563
      // The sub-element is not supported. However, we must recognize
564
      // it or its contents will end up in the item array.
5650
      break;
5663
    default:
5673
      $channel += array($tag => '');
5683
      $channel[$tag] .= $data;
5693
  }
5703
}
571
572
/**
573
 * Checks a news feed for new items.
574
 *
575
 * @param $feed
576
 *   An associative array describing the feed to be refreshed.
577
 */
578120
function aggregator_refresh($feed) {
5793
  global $channel, $image;
580
581
  // Generate conditional GET headers.
5823
  $headers = array();
5833
  if ($feed['etag']) {
5840
    $headers['If-None-Match'] = $feed['etag'];
5850
  }
5863
  if ($feed['modified']) {
5870
    $headers['If-Modified-Since'] = gmdate('D, d M Y H:i:s',
$feed['modified']) . ' GMT';
5880
  }
589
590
  // Request feed.
5913
  $result = drupal_http_request($feed['url'], $headers);
592
593
  // Process HTTP response code.
5943
  switch ($result->code) {
5953
    case 304:
5960
      db_query('UPDATE {aggregator_feed} SET checked = %d WHERE fid = %d',
time(), $feed['fid']);
5970
      drupal_set_message(t('There is no new syndicated content from
%site.', array('%site' => $feed['title'])));
5980
      break;
5993
    case 301:
6000
      $feed['url'] = $result->redirect_url;
6010
      watchdog('aggregator', 'Updated URL for feed %title to %url.',
array('%title' => $feed['title'], '%url' => $feed['url']));
602
      // Do not break here.
6033
    case 200:
6043
    case 302:
6053
    case 307:
606
      // Filter the input data.
6073
      if (aggregator_parse_feed($result->data, $feed)) {
6083
        $modified = empty($result->headers['Last-Modified']) ? 0 :
strtotime($result->headers['Last-Modified']);
609
610
        // Prepare the channel data.
6113
        foreach ($channel as $key => $value) {
6123
          $channel[$key] = trim($value);
6133
        }
614
615
        // Prepare the image data (if any).
6163
        foreach ($image as $key => $value) {
6170
          $image[$key] = trim($value);
6180
        }
619
6203
        if (!empty($image['LINK']) && !empty($image['URL']) &&
!empty($image['TITLE'])) {
621
          // TODO: we should really use theme_image() here, but that only
works with
622
          // local images. It won't work with images fetched with a URL
unless PHP version > 5.
6230
          $image = '<a href="' . check_url($image['LINK']) . '"
class="feed-image"><img src="' . check_url($image['URL']) . '" alt="' .
check_plain($image['TITLE']) . '" /></a>';
6240
        }
625
        else {
6263
          $image = NULL;
627
        }
628
6293
        $etag = empty($result->headers['ETag']) ? '' :
$result->headers['ETag'];
630
        // Update the feed data.
6313
        db_query("UPDATE {aggregator_feed} SET url = '%s', checked = %d,
link = '%s', description = '%s', image = '%s', etag = '%s', modified = %d
WHERE fid = %d", $feed['url'], time(), $channel['LINK'],
$channel['DESCRIPTION'], $image, $etag, $modified, $feed['fid']);
632
633
        // Clear the cache.
6343
        cache_clear_all();
635
6363
        watchdog('aggregator', 'There is new syndicated content from
%site.', array('%site' => $feed['title']));
6373
        drupal_set_message(t('There is new syndicated content from %site.',
array('%site' => $feed['title'])));
6383
        break;
6390
      }
6400
      $result->error = t('feed not parseable');
641
      // Do not break here..
6420
    default:
6430
      watchdog('aggregator', 'The feed from %site seems to be broken, due
to "%error".', array('%site' => $feed['title'], '%error' => $result->code .
' ' . $result->error), WATCHDOG_WARNING);
6440
      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)));
6450
      module_invoke('system', 'check_http_request');
6460
  }
6473
}
648
649
/**
650
 * Parse the W3C date/time format, a subset of ISO 8601.
651
 *
652
 * PHP date parsing functions do not handle this format.
653
 * See http://www.w3.org/TR/NOTE-datetime for more information.
654
 * Originally from MagpieRSS (http://magpierss.sourceforge.net/).
655
 *
656
 * @param $date_str
657
 *   A string with a potentially W3C DTF date.
658
 * @return
659
 *   A timestamp if parsed successfully or FALSE if not.
660
 */
661120
function aggregator_parse_w3cdtf($date_str) {
6620
  if
(preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/',
$date_str, $match)) {
6630
    list($year, $month, $day, $hours, $minutes, $seconds) =
array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
664
    // Calculate the epoch for current date assuming GMT.
6650
    $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
6660
    if ($match[10] != 'Z') { // Z is zulu time, aka GMT
6670
      list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9],
$match[10]);
668
      // Zero out the variables.
6690
      if (!$tz_hour) {
6700
        $tz_hour = 0;
6710
      }
6720
      if (!$tz_min) {
6730
        $tz_min = 0;
6740
      }
6750
      $offset_secs = (($tz_hour * 60) + $tz_min) * 60;
676
      // Is timezone ahead of GMT?  If yes, subtract offset.
6770
      if ($tz_mod == '+') {
6780
        $offset_secs *= -1;
6790
      }
6800
      $epoch += $offset_secs;
6810
    }
6820
    return $epoch;
6830
  }
684
  else {
6850
    return FALSE;
686
  }
6870
}
688
689
/**
690
 * Parse a feed and store its items.
691
 *
692
 * @param $data
693
 *   The feed data.
694
 * @param $feed
695
 *   An associative array describing the feed to be parsed.
696
 * @return
697
 *   FALSE on error, TRUE otherwise.
698
 */
699120
function aggregator_parse_feed(&$data, $feed) {
7003
  global $items, $image, $channel;
701
702
  // Unset the global variables before we use them.
7033
  unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']);
7043
  $items = array();
7053
  $image = array();
7063
  $channel = array();
707
708
  // Parse the data.
7093
  $xml_parser = drupal_xml_parser_create($data);
7103
  xml_set_element_handler($xml_parser, 'aggregator_element_start',
'aggregator_element_end');
7113
  xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
712
7133
  if (!xml_parse($xml_parser, $data, 1)) {
7140
    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);
7150
    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');
7160
    return FALSE;
7170
  }
7183
  xml_parser_free($xml_parser);
719
720
  // We reverse the array such that we store the first item last, and the
last
721
  // item first. In the database, the newest item should be at the top.
7223
  $items = array_reverse($items);
723
724
  // Initialize variables.
7253
  $title = $link = $author = $description = $guid = NULL;
7263
  foreach ($items as $item) {
7273
    unset($title, $link, $author, $description, $guid);
728
729
    // Prepare the item:
7303
    foreach ($item as $key => $value) {
7310
      $item[$key] = trim($value);
7320
    }
733
734
    // Resolve the item's title. If no title is found, we use up to 40
735
    // characters of the description ending at a word boundary, but not
736
    // splitting potential entities.
7373
    if (!empty($item['TITLE'])) {
7380
      $title = $item['TITLE'];
7390
    }
7403
    elseif (!empty($item['DESCRIPTION'])) {
7410
      $title = preg_replace('/^(.*)[^\w;&].*?$/', "\\1",
truncate_utf8($item['DESCRIPTION'], 40));
7420
    }
743
    else {
7443
      $title = '';
745
    }
746
747
    // Resolve the items link.
7483
    if (!empty($item['LINK'])) {
7490
      $link = $item['LINK'];
7500
    }
751
    else {
7523
      $link = $feed['link'];
753
    }
7543
    $guid = isset($item['GUID']) ? $item['GUID'] : '';
755
756
    // Atom feeds have a CONTENT and/or SUMMARY tag instead of a
DESCRIPTION tag.
7573
    if (!empty($item['CONTENT:ENCODED'])) {
7580
      $item['DESCRIPTION'] = $item['CONTENT:ENCODED'];
7590
    }
7603
    elseif (!empty($item['SUMMARY'])) {
7610
      $item['DESCRIPTION'] = $item['SUMMARY'];
7620
    }
7633
    elseif (!empty($item['CONTENT'])) {
7640
      $item['DESCRIPTION'] = $item['CONTENT'];
7650
    }
766
767
    // Try to resolve and parse the item's publication date. If no date is
768
    // found, use the current date instead.
7693
    $date = 'now';
7703
    foreach (array('PUBDATE', 'DC:DATE', 'DCTERMS:ISSUED',
'DCTERMS:CREATED', 'DCTERMS:MODIFIED', 'ISSUED', 'CREATED', 'MODIFIED',
'PUBLISHED', 'UPDATED') as $key) {
7713
      if (!empty($item[$key])) {
7720
        $date = $item[$key];
7730
        break;
7740
      }
7753
    }
776
7773
    $timestamp = strtotime($date); // As of PHP 5.1.0, strtotime returns
FALSE on failure instead of -1.
778
7793
    if ($timestamp <= 0) {
7800
      $timestamp = aggregator_parse_w3cdtf($date); //
Aggregator_parse_w3cdtf() returns FALSE on failure.
7810
      if (!$timestamp) {
782
        // Better than nothing.
7830
        $timestamp = time();
7840
      }
7850
    }
786
787
    // Save this item. Try to avoid duplicate entries as much as possible.
If
788
    // we find a duplicate entry, we resolve it and pass along its ID is
such
789
    // that we can update it if needed.
7903
    if (!empty($guid)) {
7910
      $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item}
WHERE fid = %d AND guid = '%s'", $feed['fid'], $guid));
7920
    }
7933
    else if ($link && $link != $feed['link'] && $link != $feed['url']) {
7940
      $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item}
WHERE fid = %d AND link = '%s'", $feed['fid'], $link));
7950
    }
796
    else {
7973
      $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item}
WHERE fid = %d AND title = '%s'", $feed['fid'], $title));
798
    }
7993
    $item += array('AUTHOR' => '', 'DESCRIPTION' => '');
8003
    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));
8013
  }
802
803
  // Remove all items that are older than flush item timer.
8043
  $age = time() - variable_get('aggregator_clear', 9676800);
8053
  $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d AND
timestamp < %d', $feed['fid'], $age);
806
8073
  $items = array();
8083
  $num_rows = FALSE;
8093
  while ($item = db_fetch_object($result)) {
8100
    $items[] = $item->iid;
8110
    $num_rows = TRUE;
8120
  }
8133
  if ($num_rows) {
8140
    db_query('DELETE FROM {aggregator_category_item} WHERE iid IN (' .
implode(', ', $items) . ')');
8150
    db_query('DELETE FROM {aggregator_item} WHERE fid = %d AND timestamp <
%d', $feed['fid'], $age);
8160
  }
817
8183
  return TRUE;
8190
}
820
821
/**
822
 * Add/edit/delete an aggregator item.
823
 *
824
 * @param $edit
825
 *   An associative array describing the item to be added/edited/deleted.
826
 */
827120
function aggregator_save_item($edit) {
8283
  if ($edit['iid'] && $edit['title']) {
8290
    db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s',
author = '%s', description = '%s', guid = '%s', timestamp = %d WHERE iid =
%d", $edit['title'], $edit['link'], $edit['author'], $edit['description'],
$edit['guid'], $edit['timestamp'], $edit['iid']);
8300
  }
8313
  elseif ($edit['iid']) {
8320
    db_query('DELETE FROM {aggregator_item} WHERE iid = %d',
$edit['iid']);
8330
    db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d',
$edit['iid']);
8340
  }
8353
  elseif ($edit['title'] && $edit['link']) {
8360
    db_query("INSERT INTO {aggregator_item} (fid, title, link, author,
description, timestamp, guid) VALUES (%d, '%s', '%s', '%s', '%s', %d,
'%s')", $edit['fid'], $edit['title'], $edit['link'], $edit['author'],
$edit['description'], $edit['timestamp'], $edit['guid']);
8370
    $edit['iid'] = db_last_insert_id('aggregator_item', 'iid');
838
    // file the items in the categories indicated by the feed
8390
    $categories = db_query('SELECT cid FROM {aggregator_category_feed}
WHERE fid = %d', $edit['fid']);
8400
    while ($category = db_fetch_object($categories)) {
8410
      db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES
(%d, %d)', $category->cid, $edit['iid']);
8420
    }
8430
  }
8443
}
845
846
/**
847
 * Load an aggregator feed.
848
 *
849
 * @param $fid
850
 *   The feed id.
851
 * @return
852
 *   An associative array describing the feed.
853
 */
854120
function aggregator_feed_load($fid) {
85524
  static $feeds;
85624
  if (!isset($feeds[$fid])) {
85724
    $feeds[$fid] = db_fetch_array(db_query('SELECT * FROM {aggregator_feed}
WHERE fid = %d', $fid));
85824
  }
859
86024
  return $feeds[$fid];
8610
}
862
863
/**
864
 * Load an aggregator category.
865
 *
866
 * @param $cid
867
 *   The category id.
868
 * @return
869
 *   An associative array describing the category.
870
 */
871120
function aggregator_category_load($cid) {
8720
  static $categories;
8730
  if (!isset($categories[$cid])) {
8740
    $categories[$cid] = db_fetch_array(db_query('SELECT * FROM
{aggregator_category} WHERE cid = %d', $cid));
8750
  }
876
8770
  return $categories[$cid];
8780
}
879
880
/**
881
 * Format an individual feed item for display in the block.
882
 *
883
 * @param $item
884
 *   The item to be displayed.
885
 * @param $feed
886
 *   Not used.
887
 * @return
888
 *   The item HTML.
889
 * @ingroup themeable
890
 */
891120
function theme_aggregator_block_item($item, $feed = 0) {
8920
  global $user;
893
8940
  $output = '';
8950
  if ($user->uid && module_exists('blog') && user_access('create blog
entries')) {
8960
    if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog
it'))) {
8970
      $output .= '<div class="icon">' . l($image, 'node/add/blog',
array('attributes' => array('title' => t('Comment on this news item in your
personal blog.'), 'class' => 'blog-it'), 'query' => "iid=$item->iid",
'html' => TRUE)) . '</div>';
8980
    }
8990
  }
900
901
  // Display the external link to the item.
9020
  $output .= '<a href="' . check_url($item->link) . '">' .
check_plain($item->title) . "</a>\n";
903
9040
  return $output;
9050
}
906
907
/**
908
 * Safely render HTML content, as allowed.
909
 *
910
 * @param $value
911
 *   The content to be filtered.
912
 * @return
913
 *   The filtered content.
914
 */
915120
function aggregator_filter_xss($value) {
9162
  return filter_xss($value, preg_split('/\s+|<|>/',
variable_get('aggregator_allowed_html_tags', '<a> <b> <br> <dd> <dl> <dt>
<em> <i> <li> <ol> <p> <strong> <u> <ul>'), -1, PREG_SPLIT_NO_EMPTY));
9170
}
918
919
/**
920
 * Helper function for drupal_map_assoc.
921
 *
922
 * @param $count
923
 *   Items count.
924
 * @return
925
 *   Plural-formatted "@count items"
926
 */
927120
function _aggregator_items($count) {
9280
  return format_plural($count, '1 item', '@count items');
9290
}
930120