aggregator.module 23.6 KB
Newer Older
1
<?php
Dries's avatar
Dries committed
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5
/**
 * @file
6
 * Used to aggregate syndicated content (RSS, RDF, and Atom).
Dries's avatar
 
Dries committed
7 8
 */

Dries's avatar
 
Dries committed
9
/**
10
 * Implement hook_help().
Dries's avatar
 
Dries committed
11
 */
12 13
function aggregator_help($path, $arg) {
  switch ($path) {
Kjartan's avatar
Kjartan committed
14
    case 'admin/help#aggregator':
15 16 17
      $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>';
      $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>';
      $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>';
18
      return $output;
19
    case 'admin/content/aggregator':
20
      $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>';
21
      $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>';
22
      return $output;
23
    case 'admin/content/aggregator/add/feed':
24
      return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
25
    case 'admin/content/aggregator/add/category':
26
      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>';
27 28
    case 'admin/content/aggregator/add/opml':
      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>';
Dries's avatar
 
Dries committed
29
  }
30 31
}

32
/**
33
 * Implement hook_theme().
34 35 36
 */
function aggregator_theme() {
  return array(
37 38
    'aggregator_wrapper' => array(
      'arguments' => array('content' => NULL),
39
      'file' => 'aggregator.pages.inc',
40 41 42
      'template' => 'aggregator-wrapper',
    ),
    'aggregator_categorize_items' => array(
43
      'arguments' => array('form' => NULL),
44
      'file' => 'aggregator.pages.inc',
45
    ),
46
    'aggregator_feed_source' => array(
47
      'arguments' => array('feed' => NULL),
48
      'file' => 'aggregator.pages.inc',
49
      'template' => 'aggregator-feed-source',
50 51 52 53
    ),
    'aggregator_block_item' => array(
      'arguments' => array('item' => NULL, 'feed' => 0),
    ),
54 55
    'aggregator_summary_items' => array(
      'arguments' => array('summary_items' => NULL, 'source' => NULL),
56
      'file' => 'aggregator.pages.inc',
57 58
      'template' => 'aggregator-summary-items',
    ),
59 60
    'aggregator_summary_item' => array(
      'arguments' => array('item' => NULL),
61
      'file' => 'aggregator.pages.inc',
62
      'template' => 'aggregator-summary-item',
63
    ),
64
    'aggregator_item' => array(
65
      'arguments' => array('item' => NULL),
66
      'file' => 'aggregator.pages.inc',
67 68 69 70
      'template' => 'aggregator-item',
    ),
    'aggregator_page_opml' => array(
      'arguments' => array('feeds' => NULL),
71
      'file' => 'aggregator.pages.inc',
72 73 74
    ),
    'aggregator_page_rss' => array(
      'arguments' => array('feeds' => NULL, 'category' => NULL),
75
      'file' => 'aggregator.pages.inc',
76 77
    ),
  );
78
}
79

80
/**
81
 * Implement hook_menu().
82
 */
83 84
function aggregator_menu() {
  $items['admin/content/aggregator'] = array(
85
    'title' => 'Feed aggregator',
86
    'description' => "Configure which content your site aggregates from other sites, how often it polls them, and how they're categorized.",
87 88 89 90
    'page callback' => 'aggregator_admin_overview',
    'access arguments' => array('administer news feeds'),
  );
  $items['admin/content/aggregator/add/feed'] = array(
91
    'title' => 'Add feed',
92 93 94 95
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed'),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
96
    'parent' => 'admin/content/aggregator',
97 98
  );
  $items['admin/content/aggregator/add/category'] = array(
99
    'title' => 'Add category',
100 101 102 103
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_category'),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
104
    'parent' => 'admin/content/aggregator',
105
  );
106 107 108 109 110 111 112 113
  $items['admin/content/aggregator/add/opml'] = array(
    'title' => 'Import OPML',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_opml'),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
    'parent' => 'admin/content/aggregator',
  );
114
  $items['admin/content/aggregator/remove/%aggregator_feed'] = array(
115
    'title' => 'Remove items',
Gábor Hojtsy's avatar
Gábor Hojtsy committed
116 117
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_admin_remove_feed', 4),
118 119 120
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
121
  $items['admin/content/aggregator/update/%aggregator_feed'] = array(
122
    'title' => 'Update items',
123 124 125 126 127 128
    'page callback' => 'aggregator_admin_refresh_feed',
    'page arguments' => array(4),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/content/aggregator/list'] = array(
129
    'title' => 'List',
130 131 132
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
133 134 135
  $items['admin/settings/aggregator'] = array(
    'title' => 'Aggregator',
    'description' => 'Configure the behavior of the feed aggregator, including when to discard feed items and how to present feed items and categories.',
136
    'page callback' => 'drupal_get_form',
137
    'page arguments' => array('aggregator_admin_form'),
138 139 140
    'access arguments' => array('administer news feeds'),
  );
  $items['aggregator'] = array(
141
    'title' => 'Feed aggregator',
142 143 144 145 146
    'page callback' => 'aggregator_page_last',
    'access arguments' => array('access news feeds'),
    'weight' => 5,
  );
  $items['aggregator/sources'] = array(
147
    'title' => 'Sources',
148
    'page callback' => 'aggregator_page_sources',
149 150
    'access arguments' => array('access news feeds'),
  );
151
  $items['aggregator/categories'] = array(
152
    'title' => 'Categories',
153
    'page callback' => 'aggregator_page_categories',
154
    'access callback' => '_aggregator_has_categories',
155 156
  );
  $items['aggregator/rss'] = array(
157
    'title' => 'RSS feed',
158 159 160 161 162
    'page callback' => 'aggregator_page_rss',
    'access arguments' => array('access news feeds'),
    'type' => MENU_CALLBACK,
  );
  $items['aggregator/opml'] = array(
163
    'title' => 'OPML feed',
164 165 166 167
    'page callback' => 'aggregator_page_opml',
    'access arguments' => array('access news feeds'),
    'type' => MENU_CALLBACK,
  );
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  $items['aggregator/categories/%aggregator_category'] = array(
    'title callback' => '_aggregator_category_title',
    'title arguments' => array(2),
    'page callback' => 'aggregator_page_category',
    'page arguments' => array(2),
    'access callback' => 'user_access',
    'access arguments' => array('access news feeds'),
  );
  $items['aggregator/categories/%aggregator_category/view'] = array(
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['aggregator/categories/%aggregator_category/categorize'] = array(
    'title' => 'Categorize',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_page_category', 2),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
  );
  $items['aggregator/categories/%aggregator_category/configure'] = array(
    'title' => 'Configure',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_category', 2),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
196
  $items['aggregator/sources/%aggregator_feed'] = array(
197
    'page callback' => 'aggregator_page_source',
198
    'page arguments' => array(2),
199
    'access arguments' => array('access news feeds'),
200 201
    'type' => MENU_CALLBACK,
  );
202
  $items['aggregator/sources/%aggregator_feed/view'] = array(
203
    'title' => 'View',
204 205 206
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
207
  $items['aggregator/sources/%aggregator_feed/categorize'] = array(
208
    'title' => 'Categorize',
209
    'page callback' => 'drupal_get_form',
210
    'page arguments' => array('aggregator_page_source', 2),
211 212 213
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
  );
214
  $items['aggregator/sources/%aggregator_feed/configure'] = array(
215
    'title' => 'Configure',
216 217 218 219 220 221
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed', 2),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
222
  $items['admin/content/aggregator/edit/feed/%aggregator_feed'] = array(
223
    'title' => 'Edit feed',
224 225 226 227 228
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed', 5),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
229
  $items['admin/content/aggregator/edit/category/%aggregator_category'] = array(
230
    'title' => 'Edit category',
231 232 233 234 235
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_category', 5),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
236 237 238 239

  return $items;
}

240 241 242 243 244 245
/**
 * Menu callback.
 *
 * @return
 *   An aggregator category title.
 */
246 247 248 249
function _aggregator_category_title($category) {
  return $category['title'];
}

250
/**
251
 * Implement hook_init().
252
 */
253
function aggregator_init() {
254
  drupal_add_css(drupal_get_path('module', 'aggregator') . '/aggregator.css');
255
}
256

257 258 259 260 261 262
/**
 * Find out whether there are any aggregator categories.
 *
 * @return
 *   TRUE if there is at least one category and the user has access to them, FALSE otherwise.
 */
263
function _aggregator_has_categories() {
264
  return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
265
}
266

Dries's avatar
 
Dries committed
267
/**
268
 * Implement hook_perm().
Dries's avatar
 
Dries committed
269
 */
Kjartan's avatar
Kjartan committed
270
function aggregator_perm() {
271
  return array(
272 273 274 275 276 277 278 279
    'administer news feeds' => array(
      'title' => t('Administer news feeds'),
      'description' => t('Add, edit or delete news feeds that are aggregated to your site.'),
    ),
    'access news feeds' => array(
      'title' => t('Access news feeds'),
      'description' => t('View aggregated news feed items.'),
    ),
280
  );
Dries's avatar
 
Dries committed
281 282
}

Dries's avatar
 
Dries committed
283
/**
284
 * Implement hook_cron().
Dries's avatar
 
Dries committed
285 286 287
 *
 * Checks news feeds for updates once their refresh interval has elapsed.
 */
Dries's avatar
 
Dries committed
288
function aggregator_cron() {
289
  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh > 0', array(':time' => REQUEST_TIME));
290
  foreach ($result as $feed) {
291
    aggregator_refresh($feed);
Dries's avatar
 
Dries committed
292 293 294
  }
}

Dries's avatar
 
Dries committed
295
/**
296
 * Implement hook_block_list().
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
 */
function aggregator_block_list() {
  $block = array();
  $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
  foreach ($result as $category) {
    $block['category-' . $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
  }
  $result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
  foreach ($result as $feed) {
    $block['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
  }
  return $block;
}

/**
312
 * Implement hook_block_configure().
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
 */
function aggregator_block_configure($delta = '') {
  list($type, $id) = explode('-', $delta);
  if ($type == 'category') {
    $value = db_query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
    $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))
    );
    return $form;
  }
}

/**
329
 * Implement hook_block_save().
330 331 332 333
 */
function aggregator_block_save($delta = '', $edit = array()) {
  list($type, $id) = explode('-', $delta);
  if ($type == 'category') {
334
    db_update('aggregator_category')
335
      ->fields(array('block' => $edit['block']))
336
      ->condition('cid', $id)
337 338 339 340 341
      ->execute();
  }
}

/**
342
 * Implement hook_block_view().
Dries's avatar
 
Dries committed
343 344 345
 *
 * Generates blocks for the latest news items in each category and feed.
 */
346
function aggregator_block_view($delta = '') {
Dries's avatar
 
Dries committed
347
  if (user_access('access news feeds')) {
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    $block = array();
    list($type, $id) = explode('-', $delta);
    switch ($type) {
      case 'feed':
        if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
          $block['subject'] = check_plain($feed->title);
          $result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", array(':fid' => $id), 0, $feed->block);
          $read_more = theme('more_link', url('aggregator/sources/' . $feed->fid), t("View this feed's recent news."));
        }
        break;

      case 'category':
        if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
          $block['subject'] = check_plain($category->title);
          $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC', array(':cid' => $category->cid), 0, $category->block);
          $read_more = theme('more_link', url('aggregator/categories/' . $category->cid), t("View this category's recent news."));
        }
        break;
Dries's avatar
Dries committed
366
    }
367 368 369
    $items = array();
    foreach ($result as $item) {
      $items[] = theme('aggregator_block_item', $item);
Dries's avatar
Dries committed
370
    }
371

372 373 374
    // Only display the block if there are items to show.
    if (count($items) > 0) {
      $block['content'] = theme('item_list', $items) . $read_more;
375
    }
376
    return $block;
Dries's avatar
 
Dries committed
377
  }
Dries's avatar
 
Dries committed
378 379
}

380 381
/**
 * Add/edit/delete aggregator categories.
382 383 384
 *
 * @param $edit
 *   An associative array describing the category to be added/edited/deleted.
385 386
 */
function aggregator_save_category($edit) {
387 388 389 390
  $link_path = 'aggregator/categories/';
  if (!empty($edit['cid'])) {
    $link_path .= $edit['cid'];
    if (!empty($edit['title'])) {
391 392 393 394 395 396 397
      db_merge('aggregator_category')
        ->key(array('cid' => $edit['cid']))
        ->fields(array(
          'title' => $edit['title'],
          'description' => $edit['description'],
        ))
        ->execute();
398 399 400
      $op = 'update';
    }
    else {
401 402 403
      db_delete('aggregator_category')
        ->condition('cid', $edit['cid'])
        ->execute();
404
      // Make sure there is no active block for this category.
405
      db_delete('block')
406 407 408
        ->condition('module', 'aggregator')
        ->condition('delta', 'category-' . $edit['cid'])
        ->execute();
409 410 411
      $edit['title'] = '';
      $op = 'delete';
    }
412
  }
413 414
  elseif (!empty($edit['title'])) {
    // A single unique id for bundles and feeds, to use in blocks.
415 416 417 418
    $link_path .= db_insert('aggregator_category')
      ->fields(array(
        'title' => $edit['title'],
        'description' => $edit['description'],
419
        'block' => 5,
420 421
      ))
      ->execute();
422 423 424 425
    $op = 'insert';
  }
  if (isset($op)) {
    menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
426 427 428 429 430
  }
}

/**
 * Add/edit/delete an aggregator feed.
431 432 433
 *
 * @param $edit
 *   An associative array describing the feed to be added/edited/deleted.
434 435
 */
function aggregator_save_feed($edit) {
436
  if (!empty($edit['fid'])) {
437
    // An existing feed is being modified, delete the category listings.
438 439 440
    db_delete('aggregator_category_feed')
      ->condition('fid', $edit['fid'])
      ->execute();
441
  }
442
  if (!empty($edit['fid']) && !empty($edit['title'])) {
443 444 445 446 447 448 449 450 451
    db_update('aggregator_feed')
      ->condition('fid', $edit['fid'])
      ->fields(array(
        'title' => $edit['title'],
        'url' => $edit['url'],
        'refresh' => $edit['refresh'],
        'block' => $edit['block'],
      ))
      ->execute();
452
  }
453
  elseif (!empty($edit['fid'])) {
454 455 456 457 458
    $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid']))->fetchCol();
    if ($iids) {
      db_delete('aggregator_category_item')
        ->condition('iid', $iids, 'IN')
        ->execute();
459
    }
460 461 462 463 464 465
    db_delete('aggregator_feed')->
      condition('fid', $edit['fid'])
      ->execute();
    db_delete('aggregator_item')
      ->condition('fid', $edit['fid'])
      ->execute();
466
    // Make sure there is no active block for this feed.
467
    db_delete('block')
468 469 470
      ->condition('module', 'aggregator')
      ->condition('delta', 'feed-' . $edit['fid'])
      ->execute();
471
  }
472
  elseif (!empty($edit['title'])) {
473 474 475 476 477 478 479 480 481 482
    $edit['fid'] = db_insert('aggregator_feed')
      ->fields(array(
        'title' => $edit['title'],
        'url' => $edit['url'],
        'refresh' => $edit['refresh'],
        'block' => $edit['block'],
        'description' => '',
        'image' => '',
      ))
      ->execute();
483

484
  }
485
  if (!empty($edit['title'])) {
486
    // The feed is being saved, save the categories as well.
487
    if (!empty($edit['category'])) {
488 489
      foreach ($edit['category'] as $cid => $value) {
        if ($value) {
490
          db_merge('aggregator_category_feed')
491
            ->key(array('fid' => $edit['fid']))
492 493 494 495
            ->fields(array(
              'cid' => $cid,
            ))
            ->execute();
496 497 498 499 500 501
        }
      }
    }
  }
}

502 503 504 505
/**
 * Removes all items from a feed.
 *
 * @param $feed
506
 *   An object describing the feed to be cleared.
507
 */
Dries's avatar
 
Dries committed
508
function aggregator_remove($feed) {
509 510 511
  // Call hook_aggregator_remove() on all modules.
  module_invoke_all('aggregator_remove', $feed);
  // Reset feed.
512
  db_merge('aggregator_feed')
513
    ->key(array('fid' => $feed->fid))
514 515 516
    ->fields(array(
      'checked' => 0,
      'hash' => '',
517
      'etag' => '',
518
      'modified' => 0,
519 520
      'description' => $feed->description,
      'image' => $feed->image,
521 522
    ))
    ->execute();
523 524
}

Dries's avatar
 
Dries committed
525 526
/**
 * Checks a news feed for new items.
527 528
 *
 * @param $feed
529
 *   An object describing the feed to be refreshed.
Dries's avatar
 
Dries committed
530
 */
Kjartan's avatar
Kjartan committed
531
function aggregator_refresh($feed) {
532 533 534 535 536 537 538 539 540 541 542 543 544 545
  // Fetch the feed.
  $fetcher = variable_get('aggregator_fetcher', 'aggregator');
  module_invoke($fetcher, 'aggregator_fetch', $feed);

  if ($feed->source_string !== FALSE) {
    // Parse the feed.
    $parser = variable_get('aggregator_parser', 'aggregator');
    module_invoke($parser, 'aggregator_parse', $feed);

    // If there are items on the feed, let all enabled processors do their work on it.
    if (@count($feed->items)) {
      $processors = variable_get('aggregator_processors', array('aggregator'));
      foreach ($processors as $processor) {
        module_invoke($processor, 'aggregator_process', $feed);
546 547
      }
    }
548
  }
549 550 551
  // Expire old feed items.
  if (drupal_function_exists('aggregator_expire')) {
    aggregator_expire($feed);
552 553 554
  }
}

555 556 557 558 559 560
/**
 * Load an aggregator feed.
 *
 * @param $fid
 *   The feed id.
 * @return
561
 *   An object describing the feed.
562
 */
563
function aggregator_feed_load($fid) {
564 565
  static $feeds;
  if (!isset($feeds[$fid])) {
566
    $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
567
  }
568

569
  return $feeds[$fid];
570 571
}

572 573 574 575 576 577 578 579
/**
 * Load an aggregator category.
 *
 * @param $cid
 *   The category id.
 * @return
 *   An associative array describing the category.
 */
580
function aggregator_category_load($cid) {
581 582
  static $categories;
  if (!isset($categories[$cid])) {
583
    $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
584
  }
585

586
  return $categories[$cid];
587 588
}

Dries's avatar
 
Dries committed
589 590 591
/**
 * Format an individual feed item for display in the block.
 *
592 593 594 595 596 597
 * @param $item
 *   The item to be displayed.
 * @param $feed
 *   Not used.
 * @return
 *   The item HTML.
Dries's avatar
 
Dries committed
598 599
 * @ingroup themeable
 */
Dries's avatar
Dries committed
600 601
function theme_aggregator_block_item($item, $feed = 0) {

Dries's avatar
 
Dries committed
602
  // Display the external link to the item.
603
  return '<a href="' . check_url($item->link) . '">' . check_plain($item->title) . "</a>\n";
Dries's avatar
Dries committed
604

Dries's avatar
Dries committed
605 606
}

607 608
/**
 * Safely render HTML content, as allowed.
609 610 611 612 613
 *
 * @param $value
 *   The content to be filtered.
 * @return
 *   The filtered content.
614 615
 */
function aggregator_filter_xss($value) {
616
  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));
617
}
618

619 620
/**
 * Check and sanitize aggregator configuration.
Dries's avatar
Dries committed
621
 *
622 623
 * Goes through all fetchers, parsers and processors and checks whether they are available.
 * If one is missing resets to standard configuration.
Dries's avatar
Dries committed
624
 *
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
 * @return
 *   TRUE if this function reset the configuration FALSE if not.
 */
function aggregator_sanitize_configuration() {
  $reset = FALSE;
  $fetcher = variable_get('aggregator_fetcher', 'aggregator');
  if (!module_exists($fetcher)) {
    $reset = TRUE;
  }
  $parser = variable_get('aggregator_parser', 'aggregator');
  if (!module_exists($parser)) {
    $reset = TRUE;
  }
  $processors = variable_get('aggregator_processors', array('aggregator'));
  foreach ($processors as $processor) {
    if (!module_exists($processor)) {
      $reset = TRUE;
      break;
    }
  }
  if ($reset) {
    variable_del('aggregator_fetcher');
    variable_del('aggregator_parser');
    variable_del('aggregator_processors');
    return TRUE;
  }
  return FALSE;
}

654 655
/**
 * Helper function for drupal_map_assoc.
656 657 658 659 660
 *
 * @param $count
 *   Items count.
 * @return
 *   Plural-formatted "@count items"
661 662
 */
function _aggregator_items($count) {
663
  return format_plural($count, '1 item', '@count items');
664
}