aggregator.module 23.3 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
    'aggregator_wrapper' => array(
      'arguments' => array('content' => NULL),
      'template' => 'aggregator-wrapper',
    ),
    'aggregator_categorize_items' => array(
42 43
      'arguments' => array('form' => NULL),
    ),
44
    'aggregator_feed_source' => array(
45
      'arguments' => array('feed' => NULL),
46
      'template' => 'aggregator-feed-source',
47 48 49 50
    ),
    'aggregator_block_item' => array(
      'arguments' => array('item' => NULL, 'feed' => 0),
    ),
51 52 53 54
    'aggregator_summary_items' => array(
      'arguments' => array('summary_items' => NULL, 'source' => NULL),
      'template' => 'aggregator-summary-items',
    ),
55 56
    'aggregator_summary_item' => array(
      'arguments' => array('item' => NULL),
57
      'template' => 'aggregator-summary-item',
58
    ),
59
    'aggregator_item' => array(
60
      'arguments' => array('item' => NULL),
61 62 63 64 65 66 67
      'template' => 'aggregator-item',
    ),
    'aggregator_page_opml' => array(
      'arguments' => array('feeds' => NULL),
    ),
    'aggregator_page_rss' => array(
      'arguments' => array('feeds' => NULL, 'category' => NULL),
68 69
    ),
  );
70
}
71

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

  return $items;
}

232 233 234 235 236 237
/**
 * Menu callback.
 *
 * @return
 *   An aggregator category title.
 */
238 239 240 241
function _aggregator_category_title($category) {
  return $category['title'];
}

242 243 244
/**
 * Implementation of hook_init().
 */
245
function aggregator_init() {
246
  drupal_add_css(drupal_get_path('module', 'aggregator') . '/aggregator.css');
247
}
248

249 250 251 252 253 254
/**
 * 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.
 */
255
function _aggregator_has_categories() {
256
  return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
257
}
258

Dries's avatar
 
Dries committed
259 260 261
/**
 * Implementation of hook_perm().
 */
Kjartan's avatar
Kjartan committed
262
function aggregator_perm() {
263
  return array(
264 265 266 267 268 269 270 271
    '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.'),
    ),
272
  );
Dries's avatar
 
Dries committed
273 274
}

Dries's avatar
 
Dries committed
275 276 277 278 279
/**
 * Implementation of hook_cron().
 *
 * Checks news feeds for updates once their refresh interval has elapsed.
 */
Dries's avatar
 
Dries committed
280
function aggregator_cron() {
281
  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh > 0', array(':time' => REQUEST_TIME));
282
  foreach ($result as $feed) {
283
    aggregator_refresh($feed);
Dries's avatar
 
Dries committed
284 285 286
  }
}

Dries's avatar
 
Dries committed
287
/**
288 289 290 291 292 293 294 295 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
 * 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') {
326
    db_update('aggregator_category')
327
      ->fields(array('block' => $edit['block']))
328
      ->condition('cid', $id)
329 330 331 332 333 334
      ->execute();
  }
}

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

364 365 366
    // Only display the block if there are items to show.
    if (count($items) > 0) {
      $block['content'] = theme('item_list', $items) . $read_more;
367
    }
368
    return $block;
Dries's avatar
 
Dries committed
369
  }
Dries's avatar
 
Dries committed
370 371
}

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

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

476
  }
477
  if (!empty($edit['title'])) {
478
    // The feed is being saved, save the categories as well.
479
    if (!empty($edit['category'])) {
480 481
      foreach ($edit['category'] as $cid => $value) {
        if ($value) {
482
          db_merge('aggregator_category_feed')
483
            ->key(array('fid' => $edit['fid']))
484 485 486 487
            ->fields(array(
              'cid' => $cid,
            ))
            ->execute();
488 489 490 491 492 493
        }
      }
    }
  }
}

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

Dries's avatar
 
Dries committed
517 518
/**
 * Checks a news feed for new items.
519 520
 *
 * @param $feed
521
 *   An object describing the feed to be refreshed.
Dries's avatar
 
Dries committed
522
 */
Kjartan's avatar
Kjartan committed
523
function aggregator_refresh($feed) {
524 525 526 527 528 529 530 531 532 533 534 535 536 537
  // 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);
538 539
      }
    }
540
  }
541 542 543
  // Expire old feed items.
  if (drupal_function_exists('aggregator_expire')) {
    aggregator_expire($feed);
544 545 546
  }
}

547 548 549 550 551 552
/**
 * Load an aggregator feed.
 *
 * @param $fid
 *   The feed id.
 * @return
553
 *   An object describing the feed.
554
 */
555
function aggregator_feed_load($fid) {
556 557
  static $feeds;
  if (!isset($feeds[$fid])) {
558
    $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
559
  }
560

561
  return $feeds[$fid];
562 563
}

564 565 566 567 568 569 570 571
/**
 * Load an aggregator category.
 *
 * @param $cid
 *   The category id.
 * @return
 *   An associative array describing the category.
 */
572
function aggregator_category_load($cid) {
573 574
  static $categories;
  if (!isset($categories[$cid])) {
575
    $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
576
  }
577

578
  return $categories[$cid];
579 580
}

Dries's avatar
 
Dries committed
581 582 583
/**
 * Format an individual feed item for display in the block.
 *
584 585 586 587 588 589
 * @param $item
 *   The item to be displayed.
 * @param $feed
 *   Not used.
 * @return
 *   The item HTML.
Dries's avatar
 
Dries committed
590 591
 * @ingroup themeable
 */
Dries's avatar
Dries committed
592 593
function theme_aggregator_block_item($item, $feed = 0) {

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

Dries's avatar
Dries committed
597 598
}

599 600
/**
 * Safely render HTML content, as allowed.
601 602 603 604 605
 *
 * @param $value
 *   The content to be filtered.
 * @return
 *   The filtered content.
606 607
 */
function aggregator_filter_xss($value) {
608
  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));
609
}
610

611 612
/**
 * Check and sanitize aggregator configuration.
Dries's avatar
Dries committed
613
 *
614 615
 * 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
616
 *
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
 * @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;
}

646 647
/**
 * Helper function for drupal_map_assoc.
648 649 650 651 652
 *
 * @param $count
 *   Items count.
 * @return
 *   Plural-formatted "@count items"
653 654
 */
function _aggregator_items($count) {
655
  return format_plural($count, '1 item', '@count items');
656
}