aggregator.module 23.5 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 11
/**
 * Implementation of hook_help().
 */
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
 * Implementation of hook_theme().
34 35 36
 */
function aggregator_theme() {
  return array(
37 38 39 40 41 42
    'aggregator_wrapper' => array(
      'arguments' => array('content' => NULL),
      'file' => 'aggregator.pages.inc',
      '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 56 57 58
    'aggregator_summary_items' => array(
      'arguments' => array('summary_items' => NULL, 'source' => NULL),
      'file' => 'aggregator.pages.inc',
      '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 67 68 69 70 71 72 73 74
      'file' => 'aggregator.pages.inc',
      'template' => 'aggregator-item',
    ),
    'aggregator_page_opml' => array(
      'arguments' => array('feeds' => NULL),
      'file' => 'aggregator.pages.inc',
    ),
    'aggregator_page_rss' => array(
      'arguments' => array('feeds' => NULL, 'category' => NULL),
75
      'file' => 'aggregator.pages.inc',
76 77
    ),
  );
78
}
79

80 81 82
/**
 * Implementation of hook_menu().
 */
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 133
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/content/aggregator/settings'] = array(
134
    'title' => 'Settings',
135
    'page callback' => 'drupal_get_form',
136
    'page arguments' => array('aggregator_admin_form'),
137 138 139 140 141
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'access arguments' => array('administer news feeds'),
  );
  $items['aggregator'] = array(
142
    'title' => 'Feed aggregator',
143 144 145 146 147
    'page callback' => 'aggregator_page_last',
    'access arguments' => array('access news feeds'),
    'weight' => 5,
  );
  $items['aggregator/sources'] = array(
148
    'title' => 'Sources',
149
    'page callback' => 'aggregator_page_sources',
150 151
    'access arguments' => array('access news feeds'),
  );
152
  $items['aggregator/categories'] = array(
153
    'title' => 'Categories',
154
    'page callback' => 'aggregator_page_categories',
155
    'access callback' => '_aggregator_has_categories',
156 157
  );
  $items['aggregator/rss'] = array(
158
    'title' => 'RSS feed',
159 160 161 162 163
    'page callback' => 'aggregator_page_rss',
    'access arguments' => array('access news feeds'),
    'type' => MENU_CALLBACK,
  );
  $items['aggregator/opml'] = array(
164
    'title' => 'OPML feed',
165 166 167 168
    'page callback' => 'aggregator_page_opml',
    'access arguments' => array('access news feeds'),
    'type' => MENU_CALLBACK,
  );
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 196
  $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,
  );
197
  $items['aggregator/sources/%aggregator_feed'] = array(
198
    'page callback' => 'aggregator_page_source',
199
    'page arguments' => array(2),
200
    'access arguments' => array('access news feeds'),
201 202
    'type' => MENU_CALLBACK,
  );
203
  $items['aggregator/sources/%aggregator_feed/view'] = array(
204
    'title' => 'View',
205 206 207
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
208
  $items['aggregator/sources/%aggregator_feed/categorize'] = array(
209
    'title' => 'Categorize',
210
    'page callback' => 'drupal_get_form',
211
    'page arguments' => array('aggregator_page_source', 2),
212 213 214
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
  );
215
  $items['aggregator/sources/%aggregator_feed/configure'] = array(
216
    'title' => 'Configure',
217 218 219 220 221 222
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed', 2),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
223
  $items['admin/content/aggregator/edit/feed/%aggregator_feed'] = array(
224
    'title' => 'Edit feed',
225 226 227 228 229
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed', 5),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
230
  $items['admin/content/aggregator/edit/category/%aggregator_category'] = array(
231
    'title' => 'Edit category',
232 233 234 235 236
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_category', 5),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
237 238 239 240

  return $items;
}

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

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

258 259 260 261 262 263
/**
 * 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.
 */
264
function _aggregator_has_categories() {
265
  return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
266
}
267

Dries's avatar
 
Dries committed
268 269 270
/**
 * Implementation of hook_perm().
 */
Kjartan's avatar
Kjartan committed
271
function aggregator_perm() {
272
  return array(
273 274 275 276 277 278 279 280
    '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.'),
    ),
281
  );
Dries's avatar
 
Dries committed
282 283
}

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

Dries's avatar
 
Dries committed
296
/**
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
 * Implementation of hook_block_list().
 */
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;
}

/**
 * Implementation of hook_block_configure().
 */
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;
  }
}

/**
 * Implementation of hook_block_save().
 */
function aggregator_block_save($delta = '', $edit = array()) {
  list($type, $id) = explode('-', $delta);
  if ($type == 'category') {
    db_merge('aggregator_category')
      ->key(array('cid' => $id))
      ->fields(array('block' => $edit['block']))
      ->execute();
  }
}

/**
 * Implementation of hook_block_view().
Dries's avatar
 
Dries committed
344 345 346
 *
 * Generates blocks for the latest news items in each category and feed.
 */
347
function aggregator_block_view($delta = '') {
Dries's avatar
 
Dries committed
348
  if (user_access('access news feeds')) {
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    $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
367
    }
368 369 370
    $items = array();
    foreach ($result as $item) {
      $items[] = theme('aggregator_block_item', $item);
Dries's avatar
Dries committed
371
    }
372

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

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

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

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

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

Dries's avatar
 
Dries committed
526 527
/**
 * Checks a news feed for new items.
528 529
 *
 * @param $feed
530
 *   An object describing the feed to be refreshed.
Dries's avatar
 
Dries committed
531
 */
Kjartan's avatar
Kjartan committed
532
function aggregator_refresh($feed) {
533 534 535 536 537 538 539 540 541 542 543 544 545 546
  // 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);
547 548
      }
    }
549
  }
550 551 552
  // Expire old feed items.
  if (drupal_function_exists('aggregator_expire')) {
    aggregator_expire($feed);
553 554 555
  }
}

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

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

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

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

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

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

Dries's avatar
Dries committed
606 607
}

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

620 621
/**
 * Check and sanitize aggregator configuration.
Dries's avatar
Dries committed
622
 *
623 624
 * 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
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 654
 * @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;
}

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