aggregator.module 25 KB
Newer Older
1
<?php
2
// $Id$
3

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

9 10 11 12 13
/**
 * Denotes that a feed's items should never expire.
 */
define('AGGREGATOR_CLEAR_NEVER', 0);

14
/**
15
 * Implement hook_help().
16
 */
17 18
function aggregator_help($path, $arg) {
  switch ($path) {
19
    case 'admin/help#aggregator':
20
      $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>';
21
      $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/config/services/aggregator'), '@admin-block' => url('admin/structure/block'), '@aggregator-opml' => url('aggregator/opml'), '@cron' => url('admin/reports/status'))) . '</p>';
22
      $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>';
23
      return $output;
24
    case 'admin/config/services/aggregator':
25
      $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>';
26
      $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/config/services/aggregator/add/feed'), '@block' => url('admin/structure/block'))) . '</p>';
27
      return $output;
28
    case 'admin/config/services/aggregator/add/feed':
29
      return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
30
    case 'admin/config/services/aggregator/add/category':
31
      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>';
32
    case 'admin/config/services/aggregator/add/opml':
33
      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>';
34
  }
35 36
}

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

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

  return $items;
}

263 264 265 266 267 268
/**
 * Menu callback.
 *
 * @return
 *   An aggregator category title.
 */
269 270 271 272
function _aggregator_category_title($category) {
  return $category['title'];
}

273
/**
274
 * Implement hook_init().
275
 */
276
function aggregator_init() {
277
  drupal_add_css(drupal_get_path('module', 'aggregator') . '/aggregator.css');
278
}
279

280 281 282 283 284 285
/**
 * 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.
 */
286
function _aggregator_has_categories() {
287
  return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
288
}
289

290
/**
291
 * Implement hook_permission().
292
 */
293
function aggregator_permission() {
294
  return array(
295 296 297 298 299 300 301 302
    '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.'),
    ),
303
  );
Dries's avatar
Dries committed
304 305
}

306
/**
307
 * Implement hook_cron().
308 309 310
 *
 * Checks news feeds for updates once their refresh interval has elapsed.
 */
311
function aggregator_cron() {
312 313 314 315
  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh != :never', array(
    ':time' => REQUEST_TIME,
    ':never' => AGGREGATOR_CLEAR_NEVER
  ));
316
  foreach ($result as $feed) {
317
    aggregator_refresh($feed);
Dries's avatar
Dries committed
318 319 320
  }
}

321
/**
322
 * Implement hook_block_info().
323
 */
324
function aggregator_block_info() {
325 326 327 328 329 330 331 332 333 334 335 336 337
  $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;
}

/**
338
 * Implement hook_block_configure().
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
 */
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;
  }
}

/**
355
 * Implement hook_block_save().
356 357 358 359
 */
function aggregator_block_save($delta = '', $edit = array()) {
  list($type, $id) = explode('-', $delta);
  if ($type == 'category') {
360
    db_update('aggregator_category')
361
      ->fields(array('block' => $edit['block']))
362
      ->condition('cid', $id)
363 364 365 366 367
      ->execute();
  }
}

/**
368
 * Implement hook_block_view().
369 370 371
 *
 * Generates blocks for the latest news items in each category and feed.
 */
372
function aggregator_block_view($delta = '') {
373
  if (user_access('access news feeds')) {
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    $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
392
    }
393 394 395
    $items = array();
    foreach ($result as $item) {
      $items[] = theme('aggregator_block_item', $item);
Dries's avatar
Dries committed
396
    }
397

398 399 400
    // Only display the block if there are items to show.
    if (count($items) > 0) {
      $block['content'] = theme('item_list', $items) . $read_more;
401
    }
402
    return $block;
403
  }
404 405
}

406 407
/**
 * Add/edit/delete aggregator categories.
408 409 410
 *
 * @param $edit
 *   An associative array describing the category to be added/edited/deleted.
411 412
 */
function aggregator_save_category($edit) {
413 414 415 416
  $link_path = 'aggregator/categories/';
  if (!empty($edit['cid'])) {
    $link_path .= $edit['cid'];
    if (!empty($edit['title'])) {
417 418 419 420 421 422 423
      db_merge('aggregator_category')
        ->key(array('cid' => $edit['cid']))
        ->fields(array(
          'title' => $edit['title'],
          'description' => $edit['description'],
        ))
        ->execute();
424 425 426
      $op = 'update';
    }
    else {
427 428 429
      db_delete('aggregator_category')
        ->condition('cid', $edit['cid'])
        ->execute();
430
      // Make sure there is no active block for this category.
431
      db_delete('block')
432 433 434
        ->condition('module', 'aggregator')
        ->condition('delta', 'category-' . $edit['cid'])
        ->execute();
435 436 437
      $edit['title'] = '';
      $op = 'delete';
    }
438
  }
439 440
  elseif (!empty($edit['title'])) {
    // A single unique id for bundles and feeds, to use in blocks.
441 442 443 444
    $link_path .= db_insert('aggregator_category')
      ->fields(array(
        'title' => $edit['title'],
        'description' => $edit['description'],
445
        'block' => 5,
446 447
      ))
      ->execute();
448 449 450 451
    $op = 'insert';
  }
  if (isset($op)) {
    menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
452 453 454 455 456
  }
}

/**
 * Add/edit/delete an aggregator feed.
457 458 459
 *
 * @param $edit
 *   An associative array describing the feed to be added/edited/deleted.
460 461
 */
function aggregator_save_feed($edit) {
462
  if (!empty($edit['fid'])) {
463
    // An existing feed is being modified, delete the category listings.
464 465 466
    db_delete('aggregator_category_feed')
      ->condition('fid', $edit['fid'])
      ->execute();
467
  }
468
  if (!empty($edit['fid']) && !empty($edit['title'])) {
469 470 471 472 473 474 475 476 477
    db_update('aggregator_feed')
      ->condition('fid', $edit['fid'])
      ->fields(array(
        'title' => $edit['title'],
        'url' => $edit['url'],
        'refresh' => $edit['refresh'],
        'block' => $edit['block'],
      ))
      ->execute();
478
  }
479
  elseif (!empty($edit['fid'])) {
480 481 482 483 484
    $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();
485
    }
486 487 488 489 490 491
    db_delete('aggregator_feed')->
      condition('fid', $edit['fid'])
      ->execute();
    db_delete('aggregator_item')
      ->condition('fid', $edit['fid'])
      ->execute();
492
    // Make sure there is no active block for this feed.
493
    db_delete('block')
494 495 496
      ->condition('module', 'aggregator')
      ->condition('delta', 'feed-' . $edit['fid'])
      ->execute();
497
  }
498
  elseif (!empty($edit['title'])) {
499 500 501 502 503 504 505 506 507 508
    $edit['fid'] = db_insert('aggregator_feed')
      ->fields(array(
        'title' => $edit['title'],
        'url' => $edit['url'],
        'refresh' => $edit['refresh'],
        'block' => $edit['block'],
        'description' => '',
        'image' => '',
      ))
      ->execute();
509

510
  }
511
  if (!empty($edit['title'])) {
512
    // The feed is being saved, save the categories as well.
513
    if (!empty($edit['category'])) {
514 515
      foreach ($edit['category'] as $cid => $value) {
        if ($value) {
516
          db_merge('aggregator_category_feed')
517
            ->key(array('fid' => $edit['fid']))
518 519 520 521
            ->fields(array(
              'cid' => $cid,
            ))
            ->execute();
522 523 524 525 526 527
        }
      }
    }
  }
}

528 529 530 531
/**
 * Removes all items from a feed.
 *
 * @param $feed
532
 *   An object describing the feed to be cleared.
533
 */
534
function aggregator_remove($feed) {
535
  _aggregator_get_variables();
536 537 538
  // Call hook_aggregator_remove() on all modules.
  module_invoke_all('aggregator_remove', $feed);
  // Reset feed.
539
  db_merge('aggregator_feed')
540
    ->key(array('fid' => $feed->fid))
541 542 543
    ->fields(array(
      'checked' => 0,
      'hash' => '',
544
      'etag' => '',
545
      'modified' => 0,
546 547
      'description' => $feed->description,
      'image' => $feed->image,
548 549
    ))
    ->execute();
550 551
}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
function _aggregator_get_variables() {
  // Fetch the feed.
  $fetcher = variable_get('aggregator_fetcher', 'aggregator');
  if ($fetcher == 'aggregator') {
    include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.fetcher.inc';
  }
  $parser = variable_get('aggregator_parser', 'aggregator');
  if ($parser == 'aggregator') {
    include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.parser.inc';
  }
  $processors = variable_get('aggregator_processors', array('aggregator'));
  if (in_array('aggregator', $processors)) {
    include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.processor.inc';
  }
  return array($fetcher, $parser, $processors);
}

569 570
/**
 * Checks a news feed for new items.
571 572
 *
 * @param $feed
573
 *   An object describing the feed to be refreshed.
574
 */
575
function aggregator_refresh($feed) {
576
  list($fetcher, $parser, $processors) = _aggregator_get_variables();
577 578 579 580 581 582 583 584 585 586
  module_invoke($fetcher, 'aggregator_fetch', $feed);

  if ($feed->source_string !== FALSE) {
    // Parse the feed.
    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)) {
      foreach ($processors as $processor) {
        module_invoke($processor, 'aggregator_process', $feed);
587 588
      }
    }
589
  }
590
  // Expire old feed items.
591
  if (function_exists('aggregator_expire')) {
592
    aggregator_expire($feed);
593 594 595
  }
}

596 597 598 599 600 601
/**
 * Load an aggregator feed.
 *
 * @param $fid
 *   The feed id.
 * @return
602
 *   An object describing the feed.
603
 */
604
function aggregator_feed_load($fid) {
605
  $feeds = &drupal_static(__FUNCTION__);
606
  if (!isset($feeds[$fid])) {
607
    $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
608
  }
609

610
  return $feeds[$fid];
611 612
}

613 614 615 616 617 618 619 620
/**
 * Load an aggregator category.
 *
 * @param $cid
 *   The category id.
 * @return
 *   An associative array describing the category.
 */
621
function aggregator_category_load($cid) {
622
  $categories = &drupal_static(__FUNCTION__);
623
  if (!isset($categories[$cid])) {
624
    $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
625
  }
626

627
  return $categories[$cid];
628 629
}

630 631 632
/**
 * Format an individual feed item for display in the block.
 *
633 634 635 636 637 638
 * @param $item
 *   The item to be displayed.
 * @param $feed
 *   Not used.
 * @return
 *   The item HTML.
639 640
 * @ingroup themeable
 */
Dries's avatar
Dries committed
641 642
function theme_aggregator_block_item($item, $feed = 0) {

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

Dries's avatar
Dries committed
646 647
}

648 649
/**
 * Safely render HTML content, as allowed.
650 651 652 653 654
 *
 * @param $value
 *   The content to be filtered.
 * @return
 *   The filtered content.
655 656
 */
function aggregator_filter_xss($value) {
657
  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));
658
}
659

660 661
/**
 * Check and sanitize aggregator configuration.
Dries's avatar
Dries committed
662
 *
663 664
 * 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
665
 *
666 667 668 669 670
 * @return
 *   TRUE if this function reset the configuration FALSE if not.
 */
function aggregator_sanitize_configuration() {
  $reset = FALSE;
671
  list($fetcher, $parser, $processors) = _aggregator_get_variables();
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  if (!module_exists($fetcher)) {
    $reset = TRUE;
  }
  if (!module_exists($parser)) {
    $reset = TRUE;
  }
  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;
}

693 694
/**
 * Helper function for drupal_map_assoc.
695 696 697 698 699
 *
 * @param $count
 *   Items count.
 * @return
 *   Plural-formatted "@count items"
700 701
 */
function _aggregator_items($count) {
702
  return format_plural($count, '1 item', '@count items');
703
}