Code coverage for /20080809/modules/aggregator/aggregator.pages.inc

Line #Times calledCode
1
<?php
2
// $Id: aggregator.pages.inc,v 1.15 2008/07/16 21:59:25 dries Exp $
3
4
/**
5
 * @file
6
 * User page callbacks for the aggregator module.
7
 */
8
9
/**
10
 * Menu callback; displays the most recent items gathered from any feed.
11
 *
12
 * @return
13
 *   The items HTML.
14
 */
1512
function aggregator_page_last() {
160
  drupal_add_feed(url('aggregator/rss'), variable_get('site_name',
'Drupal') . ' ' . t('aggregator'));
17
180
  $items = aggregator_feed_items_load('SELECT i.*, f.title AS ftitle,
f.link AS flink FROM {aggregator_item} i INNER JOIN {aggregator_feed} f ON
i.fid = f.fid ORDER BY i.timestamp DESC, i.iid DESC');
19
200
  return _aggregator_page_list($items, arg(1));
210
}
22
23
/**
24
 * Menu callback; displays all the items captured from a particular feed.
25
 *
26
 * If there are two arguments then this function is the categorize form.
27
 *
28
 * @param $arg1
29
 *   If there are two arguments then $arg1 is $form_state. Otherwise, $arg1
is $feed.
30
 * @param $arg2
31
 *   If there are two arguments then $arg2 is feed.
32
 * @return
33
 *   The item's HTML.
34
 */
3512
function aggregator_page_source($arg1, $arg2 = NULL) {
36
  // If there are two arguments then this function is the categorize form,
and
37
  // $arg1 is $form_state and $arg2 is $feed. Otherwise, $arg1 is $feed.
382
  $feed = is_array($arg2) ? $arg2 : $arg1;
392
  $feed = (object)$feed;
402
  drupal_set_title(check_plain($feed->title));
412
  $feed_source = theme('aggregator_feed_source', $feed);
42
43
  // It is safe to include the fid in the query because it's loaded from
the
44
  // database by aggregator_feed_load.
452
  $items = aggregator_feed_items_load('SELECT * FROM {aggregator_item}
WHERE fid = ' . $feed->fid . ' ORDER BY timestamp DESC, iid DESC');
46
472
  return _aggregator_page_list($items, arg(3), $feed_source);
480
}
49
50
/**
51
 * Menu callback; displays all the items aggregated in a particular
category.
52
 *
53
 * If there are two arguments then this function is called as a form.
54
 *
55
 * @param $arg1
56
 *   If there are two arguments then $arg1 is $form_state. Otherwise, $arg1
is $category.
57
 * @param $arg2
58
 *   If there are two arguments then $arg2 is $category.
59
 * @return
60
 *   The items HTML.
61
 */
6212
function aggregator_page_category($arg1, $arg2 = NULL) {
630
  drupal_set_breadcrumb(array_merge(drupal_get_breadcrumb(),
array(l(t('Categories'), 'aggregator/categories'))));
64
  // If there are two arguments then we are called as a form, $arg1 is
65
  // $form_state and $arg2 is $category. Otherwise, $arg1 is $category.
660
  $category = is_array($arg2) ? $arg2 : $arg1;
67
680
  drupal_add_feed(url('aggregator/rss/' . $category['cid']),
variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title',
array('@title' => $category['title'])));
69
70
  // It is safe to include the cid in the query because it's loaded from
the
71
  // database by aggregator_category_load.
720
  $items = aggregator_feed_items_load('SELECT i.*, f.title AS ftitle,
f.link AS flink FROM {aggregator_category_item} c LEFT JOIN
{aggregator_item} i ON c.iid = i.iid LEFT JOIN {aggregator_feed} f ON i.fid
= f.fid WHERE cid = ' . $category['cid'] . ' ORDER BY timestamp DESC, i.iid
DESC');
73
740
  return _aggregator_page_list($items, arg(3));
750
}
76
77
/**
78
 * Load feed items by passing a SQL query.
79
 *
80
 * @param $sql
81
 *   The query to be executed.
82
 * @return
83
 *   An array of the feed items.
84
 */
8512
function aggregator_feed_items_load($sql) {
862
  $items = array();
872
  if (isset($sql)) {
882
    $result = pager_query($sql, 20);
892
    while ($item = db_fetch_object($result)) {
900
      $result_category = db_query('SELECT c.title, c.cid FROM
{aggregator_category_item} ci LEFT JOIN {aggregator_category} c ON ci.cid =
c.cid WHERE ci.iid = %d ORDER BY c.title', $item->iid);
910
      $item->categories = array();
920
      while ($item_categories = db_fetch_object($result_category)) {
930
        $item->categories[] = $item_categories;
940
      }
950
      $items[$item->iid] = $item;
960
    }
972
  }
98
992
  return $items;
1000
}
101
102
/**
103
 * Prints an aggregator page listing a number of feed items.
104
 *
105
 * Various menu callbacks use this function to print their feeds.
106
 *
107
 * @param $items
108
 *   The items to be listed.
109
 * @param $op
110
 *   Which form should be added to the items. Only 'categorize' is now
recognized.
111
 * @param $feed_source
112
 *   The feed source URL.
113
 * @return
114
 *   The items HTML.
115
 */
11612
function _aggregator_page_list($items, $op, $feed_source = '') {
1172
  if (user_access('administer news feeds') && ($op == 'categorize')) {
118
    // Get form data.
1190
    $output = aggregator_categorize_items($items, $feed_source);
1200
  }
121
  else {
122
    // Assemble themed output.
1232
    $output = $feed_source;
1242
    foreach ($items as $item) {
1250
      $output .= theme('aggregator_item', $item);
1260
    }
1272
    $output = theme('aggregator_wrapper', $output);
128
  }
129
1302
  return $output;
1310
}
132
133
/**
134
 * Form builder; build the page list form.
135
 *
136
 * @param $items
137
 *   An array of the feed items.
138
 * @param $feed_source
139
 *   The feed source URL.
140
 * @return
141
 *   The form structure.
142
 * @ingroup forms
143
 * @see aggregator_categorize_items_validate()
144
 * @see aggregator_categorize_items_submit()
145
 */
14612
function aggregator_categorize_items($items, $feed_source = '') {
1470
  $form['#submit'][] = 'aggregator_categorize_items_submit';
1480
  $form['#validate'][] = 'aggregator_categorize_items_validate';
1490
  $form['#theme'] = 'aggregator_categorize_items';
1500
  $form['feed_source'] = array(
1510
    '#value' => $feed_source,
152
  );
1530
  $categories = array();
1540
  $done = FALSE;
1550
  $form['items'] = array();
1560
  $form['categories'] = array(
1570
    '#tree' => TRUE,
158
  );
1590
  foreach ($items as $item) {
1600
    $form['items'][$item->iid] = array('#markup' =>
theme('aggregator_item', $item));
1610
    $form['categories'][$item->iid] = array();
1620
    $categories_result = db_query('SELECT c.cid, c.title, ci.iid FROM
{aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid =
ci.cid AND ci.iid = %d', $item->iid);
1630
    $selected = array();
1640
    while ($category = db_fetch_object($categories_result)) {
1650
      if (!$done) {
1660
        $categories[$category->cid] = check_plain($category->title);
1670
      }
1680
      if ($category->iid) {
1690
        $selected[] = $category->cid;
1700
      }
1710
    }
1720
    $done = TRUE;
1730
    $form['categories'][$item->iid] = array(
1740
      '#type' => variable_get('aggregator_category_selector',
'checkboxes'),
1750
      '#default_value' => $selected,
1760
      '#options' => $categories,
1770
      '#size' => 10,
178
      '#multiple' => TRUE
1790
    );
1800
  }
1810
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
categories'));
182
1830
  return $form;
1840
}
185
186
/**
187
 * Validate aggregator_categorize_items() form submissions.
188
 */
18912
function aggregator_categorize_items_validate($form, &$form_state) {
1900
  if (!user_access('administer news feeds')) {
1910
    form_error($form, t('You are not allowed to categorize this feed
item.'));
1920
  }
1930
}
194
195
/**
196
 * Process aggregator_categorize_items() form submissions.
197
 */
19812
function aggregator_categorize_items_submit($form, &$form_state) {
1990
  if (!empty($form_state['values']['categories'])) {
2000
    foreach ($form_state['values']['categories'] as $iid => $selection) {
2010
      db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d',
$iid);
2020
      foreach ($selection as $cid) {
2030
        if ($cid) {
2040
          db_query('INSERT INTO {aggregator_category_item} (cid, iid)
VALUES (%d, %d)', $cid, $iid);
2050
        }
2060
      }
2070
    }
2080
  }
2090
  drupal_set_message(t('The categories have been saved.'));
2100
}
211
212
/**
213
 * Theme the page list form for assigning categories.
214
 *
215
 * @param $form
216
 *   An associative array containing the structure of the form.
217
 * @return
218
 *   The output HTML.
219
 * @ingroup themeable
220
 */
22112
function theme_aggregator_categorize_items($form) {
2220
  $output = drupal_render($form['feed_source']);
2230
  $rows = array();
2240
  if ($form['items']) {
2250
    foreach (element_children($form['items']) as $key) {
2260
      if (is_array($form['items'][$key])) {
2270
        $rows[] = array(
2280
          drupal_render($form['items'][$key]),
2290
          array('data' => drupal_render($form['categories'][$key]), 'class'
=> 'categorize-item'),
230
        );
2310
      }
2320
    }
2330
  }
2340
  $output .= theme('table', array('', t('Categorize')), $rows);
2350
  $output .= drupal_render($form['submit']);
2360
  $output .= drupal_render($form);
237
2380
  return theme('aggregator_wrapper', $output);
2390
}
240
241
/**
242
 * Process variables for aggregator-wrapper.tpl.php.
243
 *
244
 * @see aggregator-wrapper.tpl.php
245
 */
24612
function template_preprocess_aggregator_wrapper(&$variables) {
2472
  $variables['pager'] = theme('pager', NULL, 20, 0);
2482
}
249
250
/**
251
 * Process variables for aggregator-item.tpl.php.
252
 *
253
 * @see aggregator-item.tpl.php
254
 */
25512
function template_preprocess_aggregator_item(&$variables) {
2560
  $item = $variables['item'];
257
2580
  $variables['feed_url'] = check_url($item->link);
2590
  $variables['feed_title'] = check_plain($item->title);
2600
  $variables['content'] = aggregator_filter_xss($item->description);
261
2620
  $variables['source_url'] = '';
2630
  $variables['source_title'] = '';
2640
  if (isset($item->ftitle) && isset($item->fid)) {
2650
    $variables['source_url'] = url("aggregator/sources/$item->fid");
2660
    $variables['source_title'] = check_plain($item->ftitle);
2670
  }
2680
  if (date('Ymd', $item->timestamp) == date('Ymd')) {
2690
    $variables['source_date'] = t('%ago ago', array('%ago' =>
format_interval(time() - $item->timestamp)));
2700
  }
271
  else {
2720
    $variables['source_date'] = format_date($item->timestamp, 'custom',
variable_get('date_format_medium', 'D, m/d/Y - H:i'));
273
  }
274
2750
  $variables['categories'] = array();
2760
  foreach ($item->categories as $category) {
2770
    $variables['categories'][$category->cid] = l($category->title,
'aggregator/categories/' . $category->cid);
2780
  }
2790
}
280
281
/**
282
 * Menu callback; displays all the feeds used by the aggregator.
283
 */
28412
function aggregator_page_sources() {
2850
  $result = db_query('SELECT f.fid, f.title, f.description, f.image,
MAX(i.timestamp) AS last FROM {aggregator_feed} f LEFT JOIN
{aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title,
f.description, f.image ORDER BY last DESC, f.title');
286
2870
  $output = '';
2880
  while ($feed = db_fetch_object($result)) {
289
    // Most recent items:
2900
    $summary_items = array();
2910
    if (variable_get('aggregator_summary_items', 3)) {
2920
      $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM
{aggregator_item} i WHERE i.fid = %d ORDER BY i.timestamp DESC',
$feed->fid, 0, variable_get('aggregator_summary_items', 3));
2930
      while ($item = db_fetch_object($items)) {
2940
        $summary_items[] = theme('aggregator_summary_item', $item);
2950
      }
2960
    }
2970
    $feed->url = url('aggregator/sources/' . $feed->fid);
2980
    $output .= theme('aggregator_summary_items', $summary_items, $feed);
2990
  }
3000
  $output .= theme('feed_icon', url('aggregator/opml'), t('OPML feed'));
301
3020
  return theme('aggregator_wrapper', $output);
3030
}
304
305
/**
306
 * Menu callback; displays all the categories used by the aggregator.
307
 */
30812
function aggregator_page_categories() {
3090
  $result = db_query('SELECT c.cid, c.title, c.description FROM
{aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid =
ci.cid LEFT JOIN {aggregator_item} i ON ci.iid = i.iid GROUP BY c.cid,
c.title, c.description');
310
3110
  $output = '';
3120
  while ($category = db_fetch_object($result)) {
3130
    if (variable_get('aggregator_summary_items', 3)) {
3140
      $summary_items = array();
3150
      $items = db_query_range('SELECT i.title, i.timestamp, i.link, f.title
as feed_title, f.link as feed_link FROM {aggregator_category_item} ci LEFT
JOIN {aggregator_item} i ON i.iid = ci.iid LEFT JOIN {aggregator_feed} f ON
i.fid = f.fid WHERE ci.cid = %d ORDER BY i.timestamp DESC', $category->cid,
0, variable_get('aggregator_summary_items', 3));
3160
      while ($item = db_fetch_object($items)) {
3170
        $summary_items[] = theme('aggregator_summary_item', $item);
3180
      }
3190
    }
3200
    $category->url = url('aggregator/categories/' . $category->cid);
3210
    $output .= theme('aggregator_summary_items', $summary_items,
$category);
3220
  }
323
3240
  return theme('aggregator_wrapper', $output);
3250
}
326
327
/**
328
 * Menu callback; generate an RSS 0.92 feed of aggregator items or
categories.
329
 */
33012
function aggregator_page_rss() {
3310
  $result = NULL;
332
  // arg(2) is the passed cid, only select for that category.
3330
  if (arg(2)) {
3340
    $category = db_fetch_object(db_query('SELECT cid, title FROM
{aggregator_category} WHERE cid = %d', arg(2)));
3350
    $sql = 'SELECT i.*, f.title AS ftitle, f.link AS flink FROM
{aggregator_category_item} c LEFT JOIN {aggregator_item} i ON c.iid = i.iid
LEFT JOIN {aggregator_feed} f ON i.fid = f.fid WHERE cid = %d ORDER BY
timestamp DESC, i.iid DESC';
3360
    $result = db_query_range($sql, $category->cid, 0,
variable_get('feed_default_items', 10));
3370
  }
338
  // Or, get the default aggregator items.
339
  else {
3400
    $category = NULL;
3410
    $sql = 'SELECT i.*, f.title AS ftitle, f.link AS flink FROM
{aggregator_item} i INNER JOIN {aggregator_feed} f ON i.fid = f.fid ORDER
BY i.timestamp DESC, i.iid DESC';
3420
    $result = db_query_range($sql, 0, variable_get('feed_default_items',
10));
343
  }
344
3450
  $feeds = array();
3460
  while ($item = db_fetch_object($result)) {
3470
    $feeds[] = $item;
3480
  }
349
3500
  return theme('aggregator_page_rss', $feeds, $category);
3510
}
352
353
/**
354
 * Theme the RSS output.
355
 *
356
 * @param $feeds
357
 *   An array of the feeds to theme.
358
 * @param $category
359
 *   A common category, if any, for all the feeds.
360
 * @ingroup themeable
361
 */
36212
function theme_aggregator_page_rss($feeds, $category = NULL) {
3630
  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
364
3650
  $items = '';
3660
  $feed_length = variable_get('feed_item_length', 'teaser');
3670
  foreach ($feeds as $feed) {
368
    switch ($feed_length) {
3690
      case 'teaser':
3700
        $teaser = node_teaser($feed->description);
3710
        if ($teaser != $feed->description) {
3720
          $teaser .= '<p><a href="' . check_url($feed->link) . '">' .
t('read more') . "</a></p>\n";
3730
        }
3740
        $feed->description = $teaser;
3750
        break;
3760
      case 'title':
3770
        $feed->description = '';
3780
        break;
3790
    }
3800
    $items .= format_rss_item($feed->ftitle . ': ' . $feed->title,
$feed->link, $feed->description, array('pubDate' => date('r',
$feed->timestamp)));
3810
  }
382
3830
  $site_name = variable_get('site_name', 'Drupal');
3840
  $url = url((isset($category) ? 'aggregator/categories/' . $category->cid
: 'aggregator'), array('absolute' => TRUE));
3850
  $description = isset($category) ? t('@site_name - aggregated feeds in
category @title', array('@site_name' => $site_name, '@title' =>
$category->title)) : t('@site_name - aggregated feeds', array('@site_name'
=> $site_name));
386
3870
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
3880
  $output .= "<rss version=\"2.0\">\n";
3890
  $output .= format_rss_channel(t('@site_name aggregator',
array('@site_name' => $site_name)), $url, $description, $items);
3900
  $output .= "</rss>\n";
391
3920
  print $output;
3930
}
394
395
/**
396
 * Menu callback; generates an OPML representation of all feeds.
397
 *
398
 * @param $cid
399
 *   If set, feeds are exported only from a category with this ID.
Otherwise, all feeds are exported.
400
 * @return
401
 *   The output XML.
402
 */
40312
function aggregator_page_opml($cid = NULL) {
4040
  if ($cid) {
4050
    $result = db_query('SELECT f.title, f.url FROM {aggregator_feed} f LEFT
JOIN {aggregator_category_feed} c on f.fid = c.fid WHERE c.cid = %d ORDER
BY title', $cid);
4060
  }
407
  else {
4080
    $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
409
  }
410
4110
  $feeds = array();
4120
  while ($item = db_fetch_object($result)) {
4130
    $feeds[] = $item;
4140
  }
415
4160
  return theme('aggregator_page_opml', $feeds);
4170
}
418
419
/**
420
 * Theme the OPML feed output.
421
 *
422
 * @param $feeds
423
 *   An array of the feeds to theme.
424
 * @ingroup themeable
425
 */
42612
function theme_aggregator_page_opml($feeds) {
4270
  drupal_set_header('Content-Type: text/xml; charset=utf-8');
428
4290
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
4300
  $output .= "<opml version=\"1.1\">\n";
4310
  $output .= "<head>\n";
4320
  $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) .
"</title>\n";
4330
  $output .= '<dateModified>' . gmdate('r') . "</dateModified>\n";
4340
  $output .= "</head>\n";
4350
  $output .= "<body>\n";
4360
  foreach ($feeds as $feed) {
4370
    $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="'
. check_url($feed->url) . "\" />\n";
4380
  }
4390
  $output .= "</body>\n";
4400
  $output .= "</opml>\n";
441
4420
  print $output;
4430
}
444
445
/**
446
 * Process variables for aggregator-summary-items.tpl.php.
447
 *
448
 * @see aggregator-summary-item.tpl.php
449
 */
45012
function template_preprocess_aggregator_summary_items(&$variables) {
4510
  $variables['title'] = check_plain($variables['source']->title);
4520
  $variables['summary_list'] = theme('item_list',
$variables['summary_items']);
4530
  $variables['source_url'] = $variables['source']->url;
4540
}
455
456
/**
457
 * Process variables for aggregator-summary-item.tpl.php.
458
 *
459
 * @see aggregator-summary-item.tpl.php
460
 */
46112
function template_preprocess_aggregator_summary_item(&$variables) {
4620
  $item = $variables['item'];
463
4640
  $variables['feed_url'] = check_url($item->link);
4650
  $variables['feed_title'] = check_plain($item->title);
4660
  $variables['feed_age'] = t('%age old', array('%age' =>
format_interval(time() - $item->timestamp)));
467
4680
  $variables['source_url'] = '';
4690
  $variables['source_title'] = '';
4700
  if (!empty($item->feed_link)) {
4710
    $variables['source_url'] = check_url($item->feed_link);
4720
    $variables['source_title'] = check_plain($item->feed_title);
4730
  }
4740
}
475
476
/**
477
 * Process variables for aggregator-feed-source.tpl.php.
478
 *
479
 * @see aggregator-feed-source.tpl.php
480
 */
48112
function template_preprocess_aggregator_feed_source(&$variables) {
4822
  $feed = $variables['feed'];
483
4842
  $variables['source_icon'] = theme('feed_icon', $feed->url, t('!title
feed', array('!title' => $feed->title)));
4852
  $variables['source_image'] = $feed->image;
4862
  $variables['source_description'] =
aggregator_filter_xss($feed->description);
4872
  $variables['source_url'] = check_url(url($feed->link, array('absolute' =>
TRUE)));
488
4892
  if ($feed->checked) {
4900
    $variables['last_checked'] = t('@time ago', array('@time' =>
format_interval(time() - $feed->checked)));
4910
  }
492
  else {
4932
    $variables['last_checked'] = t('never');
494
  }
495
4962
  if (user_access('administer news feeds')) {
4972
    $variables['last_checked'] = l($variables['last_checked'],
'admin/content/aggregator');
4982
  }
4992
}
50012