aggregator.module 23.2 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 125
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
  $items['admin/content/aggregator/settings'] = array(
126
    'title' => 'Settings',
127
    'page callback' => 'drupal_get_form',
128
    'page arguments' => array('aggregator_admin_form'),
129 130 131 132 133
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'access arguments' => array('administer news feeds'),
  );
  $items['aggregator'] = array(
134
    'title' => 'Feed aggregator',
135 136 137 138 139
    'page callback' => 'aggregator_page_last',
    'access arguments' => array('access news feeds'),
    'weight' => 5,
  );
  $items['aggregator/sources'] = array(
140
    'title' => 'Sources',
141
    'page callback' => 'aggregator_page_sources',
142 143
    'access arguments' => array('access news feeds'),
  );
144
  $items['aggregator/categories'] = array(
145
    'title' => 'Categories',
146
    'page callback' => 'aggregator_page_categories',
147
    'access callback' => '_aggregator_has_categories',
148 149
  );
  $items['aggregator/rss'] = array(
150
    'title' => 'RSS feed',
151 152 153 154 155
    'page callback' => 'aggregator_page_rss',
    'access arguments' => array('access news feeds'),
    'type' => MENU_CALLBACK,
  );
  $items['aggregator/opml'] = array(
156
    'title' => 'OPML feed',
157 158 159 160
    'page callback' => 'aggregator_page_opml',
    'access arguments' => array('access news feeds'),
    'type' => MENU_CALLBACK,
  );
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 188
  $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,
  );
189
  $items['aggregator/sources/%aggregator_feed'] = array(
190
    'page callback' => 'aggregator_page_source',
191
    'page arguments' => array(2),
192
    'access arguments' => array('access news feeds'),
193 194
    'type' => MENU_CALLBACK,
  );
195
  $items['aggregator/sources/%aggregator_feed/view'] = array(
196
    'title' => 'View',
197 198 199
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );
200
  $items['aggregator/sources/%aggregator_feed/categorize'] = array(
201
    'title' => 'Categorize',
202
    'page callback' => 'drupal_get_form',
203
    'page arguments' => array('aggregator_page_source', 2),
204 205 206
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
  );
207
  $items['aggregator/sources/%aggregator_feed/configure'] = array(
208
    'title' => 'Configure',
209 210 211 212 213 214
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed', 2),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
215
  $items['admin/content/aggregator/edit/feed/%aggregator_feed'] = array(
216
    'title' => 'Edit feed',
217 218 219 220 221
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_feed', 5),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
222
  $items['admin/content/aggregator/edit/category/%aggregator_category'] = array(
223
    'title' => 'Edit category',
224 225 226 227 228
    'page callback' => 'drupal_get_form',
    'page arguments' => array('aggregator_form_category', 5),
    'access arguments' => array('administer news feeds'),
    'type' => MENU_CALLBACK,
  );
229 230 231 232

  return $items;
}

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

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

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

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

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

Dries's avatar
 
Dries committed
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 326
 * 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') {
327
    db_update('aggregator_category')
328
      ->fields(array('block' => $edit['block']))
329
      ->condition('cid', $id)
330 331 332 333 334 335
      ->execute();
  }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

Dries's avatar
Dries committed
598 599
}

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

612 613
/**
 * Check and sanitize aggregator configuration.
Dries's avatar
Dries committed
614
 *
615 616
 * 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
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 646
 * @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;
}

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