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

Line #Times calledCode
1
<?php
2
// $Id: aggregator.pages.inc,v 1.22 2008/10/20 12:57:35 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('sum');
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($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('source', $feed);
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) {
63
  // If there are two arguments then we are called as a form, $arg1 is
64
  // $form_state and $arg2 is $category. Otherwise, $arg1 is $category.
650
  $category = is_array($arg2) ? $arg2 : $arg1;
66
670
  drupal_add_feed(url('aggregator/rss/' . $category['cid']),
variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title',
array('@title' => $category['title'])));
68
69
  // It is safe to include the cid in the query because it's loaded from
the
70
  // database by aggregator_category_load.
710
  $items = aggregator_feed_items_load('category', $category);
72
730
  return _aggregator_page_list($items, arg(3));
740
}
75
76
/**
77
 * Load feed items
78
 *
79
 * @param $type
80
 *   The filter for the items. Possible values: 'sum', 'source', 'category'
81
 * @param $data
82
 *   Feed or category data for filtering
83
 * @return
84
 *   An array of the feed items.
85
 */
8612
function aggregator_feed_items_load($type, $data = NULL) {
872
  $items = array();
882
  $range_limit = 20;
89
  switch ($type) {
902
    case 'sum':
910
      $result = db_query_range('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', 0, $range_limit);
920
      break;
932
    case 'source':
942
      $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid =
:fid ORDER BY timestamp DESC, iid DESC', array(':fid' => $data->fid), 0,
$range_limit);
952
      break;
960
    case 'category':
970
      $result = db_query_range('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 =
:cid ORDER BY timestamp DESC, i.iid DESC', array(':cid' => $data['cid']),
0, $range_limit);
980
      break;
990
  }
100
1012
  foreach ($result as $item) {
1020
    $item->categories = 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 = :iid ORDER BY c.title', array(':iid' =>
$item->iid))->fetchAll();
1030
    $items[] = $item;
1040
  }
105
1062
  return $items;
1070
}
108
109
/**
110
 * Prints an aggregator page listing a number of feed items.
111
 *
112
 * Various menu callbacks use this function to print their feeds.
113
 *
114
 * @param $items
115
 *   The items to be listed.
116
 * @param $op
117
 *   Which form should be added to the items. Only 'categorize' is now
recognized.
118
 * @param $feed_source
119
 *   The feed source URL.
120
 * @return
121
 *   The items HTML.
122
 */
12312
function _aggregator_page_list($items, $op, $feed_source = '') {
1242
  if (user_access('administer news feeds') && ($op == 'categorize')) {
125
    // Get form data.
1260
    $output = aggregator_categorize_items($items, $feed_source);
1270
  }
128
  else {
129
    // Assemble themed output.
1302
    $output = $feed_source;
1312
    foreach ($items as $item) {
1320
      $output .= theme('aggregator_item', $item);
1330
    }
1342
    $output = theme('aggregator_wrapper', $output);
135
  }
136
1372
  return $output;
1380
}
139
140
/**
141
 * Form builder; build the page list form.
142
 *
143
 * @param $items
144
 *   An array of the feed items.
145
 * @param $feed_source
146
 *   The feed source URL.
147
 * @return
148
 *   The form structure.
149
 * @ingroup forms
150
 * @see aggregator_categorize_items_validate()
151
 * @see aggregator_categorize_items_submit()
152
 */
15312
function aggregator_categorize_items($items, $feed_source = '') {
1540
  $form['#submit'][] = 'aggregator_categorize_items_submit';
1550
  $form['#validate'][] = 'aggregator_categorize_items_validate';
1560
  $form['#theme'] = 'aggregator_categorize_items';
1570
  $form['feed_source'] = array(
1580
    '#value' => $feed_source,
159
  );
1600
  $categories = array();
1610
  $done = FALSE;
1620
  $form['items'] = array();
1630
  $form['categories'] = array(
1640
    '#tree' => TRUE,
165
  );
1660
  foreach ($items as $item) {
1670
    $form['items'][$item->iid] = array('#markup' =>
theme('aggregator_item', $item));
1680
    $form['categories'][$item->iid] = array();
1690
    $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 = :iid', array(':iid' => $item->iid));
1700
    $selected = array();
1710
    foreach ($categories_result as $category) {
1720
      if (!$done) {
1730
        $categories[$category->cid] = check_plain($category->title);
1740
      }
1750
      if ($category->iid) {
1760
        $selected[] = $category->cid;
1770
      }
1780
    }
1790
    $done = TRUE;
1800
    $form['categories'][$item->iid] = array(
1810
      '#type' => variable_get('aggregator_category_selector',
'checkboxes'),
1820
      '#default_value' => $selected,
1830
      '#options' => $categories,
1840
      '#size' => 10,
185
      '#multiple' => TRUE
1860
    );
1870
  }
1880
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save
categories'));
189
1900
  return $form;
1910
}
192
193
/**
194
 * Validate aggregator_categorize_items() form submissions.
195
 */
19612
function aggregator_categorize_items_validate($form, &$form_state) {
1970
  if (!user_access('administer news feeds')) {
1980
    form_error($form, t('You are not allowed to categorize this feed
item.'));
1990
  }
2000
}
201
202
/**
203
 * Process aggregator_categorize_items() form submissions.
204
 */
20512
function aggregator_categorize_items_submit($form, &$form_state) {
2060
  if (!empty($form_state['values']['categories'])) {
2070
    foreach ($form_state['values']['categories'] as $iid => $selection) {
2080
      db_delete('aggregator_category_item')
2090
        ->condition('iid', $iid)
2100
        ->execute();
2110
      $insert = db_insert('aggregator_category_item')->fields(array('iid',
'cid'));
2120
      $has_values = FALSE;
2130
      foreach ($selection as $cid) {
2140
        if ($cid && $iid) {
2150
          $has_values = TRUE;
2160
          $insert->values(array(
2170
            'iid' => $iid,
2180
            'cid' => $cid,
2190
          ));
2200
        }
2210
      }
2220
      if ($has_values) {
2230
        $insert->execute();
2240
      }
2250
    }
2260
  }
2270
  drupal_set_message(t('The categories have been saved.'));
2280
}
229
230
/**
231
 * Theme the page list form for assigning categories.
232
 *
233
 * @param $form
234
 *   An associative array containing the structure of the form.
235
 * @return
236
 *   The output HTML.
237
 * @ingroup themeable
238
 */
23912
function theme_aggregator_categorize_items($form) {
2400
  $output = drupal_render($form['feed_source']);
2410
  $rows = array();
2420
  if ($form['items']) {
2430
    foreach (element_children($form['items']) as $key) {
2440
      if (is_array($form['items'][$key])) {
2450
        $rows[] = array(
2460
          drupal_render($form['items'][$key]),
2470
          array('data' => drupal_render($form['categories'][$key]), 'class'
=> 'categorize-item'),
248
        );
2490
      }
2500
    }
2510
  }
2520
  $output .= theme('table', array('', t('Categorize')), $rows);
2530
  $output .= drupal_render($form['submit']);
2540
  $output .= drupal_render($form);
255
2560
  return theme('aggregator_wrapper', $output);
2570
}
258
259
/**
260
 * Process variables for aggregator-wrapper.tpl.php.
261
 *
262
 * @see aggregator-wrapper.tpl.php
263
 */
26412
function template_preprocess_aggregator_wrapper(&$variables) {
2652
  $variables['pager'] = theme('pager', NULL, 20, 0);
2662
}
267
268
/**
269
 * Process variables for aggregator-item.tpl.php.
270
 *
271
 * @see aggregator-item.tpl.php
272
 */
27312
function template_preprocess_aggregator_item(&$variables) {
2740
  $item = $variables['item'];
275
2760
  $variables['feed_url'] = check_url($item->link);
2770
  $variables['feed_title'] = check_plain($item->title);
2780
  $variables['content'] = aggregator_filter_xss($item->description);
279
2800
  $variables['source_url'] = '';
2810
  $variables['source_title'] = '';
2820
  if (isset($item->ftitle) && isset($item->fid)) {
2830
    $variables['source_url'] = url("aggregator/sources/$item->fid");
2840
    $variables['source_title'] = check_plain($item->ftitle);
2850
  }
2860
  if (date('Ymd', $item->timestamp) == date('Ymd')) {
2870
    $variables['source_date'] = t('%ago ago', array('%ago' =>
format_interval(REQUEST_TIME - $item->timestamp)));
2880
  }
289
  else {
2900
    $variables['source_date'] = format_date($item->timestamp, 'custom',
variable_get('date_format_medium', 'D, m/d/Y - H:i'));
291
  }
292
2930
  $variables['categories'] = array();
2940
  foreach ($item->categories as $category) {
2950
    $variables['categories'][$category->cid] = l($category->title,
'aggregator/categories/' . $category->cid);
2960
  }
2970
}
298
299
/**
300
 * Menu callback; displays all the feeds used by the aggregator.
301
 */
30212
function aggregator_page_sources() {
3030
  $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');
304
3050
  $output = '';
3060
  foreach ($result as $feed) {
307
    // Most recent items:
3080
    $summary_items = array();
3090
    if (variable_get('aggregator_summary_items', 3)) {
3100
      $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM
{aggregator_item} i WHERE i.fid = :fid ORDER BY i.timestamp DESC',
array(':fid' => $feed->fid), 0, variable_get('aggregator_summary_items',
3));
3110
      foreach ($items as $item) {
3120
        $summary_items[] = theme('aggregator_summary_item', $item);
3130
      }
3140
    }
3150
    $feed->url = url('aggregator/sources/' . $feed->fid);
3160
    $output .= theme('aggregator_summary_items', $summary_items, $feed);
3170
  }
3180
  $output .= theme('feed_icon', url('aggregator/opml'), t('OPML feed'));
319
3200
  return theme('aggregator_wrapper', $output);
3210
}
322
323
/**
324
 * Menu callback; displays all the categories used by the aggregator.
325
 */
32612
function aggregator_page_categories() {
3270
  $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');
328
3290
  $output = '';
3300
  foreach ($result as $category) {
3310
    if (variable_get('aggregator_summary_items', 3)) {
3320
      $summary_items = array();
3330
      $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 = :cid ORDER BY i.timestamp DESC', array(':cid'
=> $category->cid), 0, variable_get('aggregator_summary_items', 3));
3340
      foreach ($items as $item) {
3350
        $summary_items[] = theme('aggregator_summary_item', $item);
3360
      }
3370
    }
3380
    $category->url = url('aggregator/categories/' . $category->cid);
3390
    $output .= theme('aggregator_summary_items', $summary_items,
$category);
3400
  }
341
3420
  return theme('aggregator_wrapper', $output);
3430
}
344
345
/**
346
 * Menu callback; generate an RSS 0.92 feed of aggregator items or
categories.
347
 */
34812
function aggregator_page_rss() {
3490
  $result = NULL;
350
  // arg(2) is the passed cid, only select for that category.
3510
  if (arg(2)) {
3520
    $category = db_query('SELECT cid, title FROM {aggregator_category}
WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
3530
    $result = db_query_range('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 =
:cid ORDER BY timestamp DESC, i.iid DESC', array(':cid' => $category->cid),
0, variable_get('feed_default_items', 10));
3540
  }
355
  // Or, get the default aggregator items.
356
  else {
3570
    $category = NULL;
3580
    $result = db_query_range('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', 0,
variable_get('feed_default_items', 10));
359
  }
360
3610
  $feeds = $result->fetchAll();
3620
  return theme('aggregator_page_rss', $feeds, $category);
3630
}
364
365
/**
366
 * Theme the RSS output.
367
 *
368
 * @param $feeds
369
 *   An array of the feeds to theme.
370
 * @param $category
371
 *   A common category, if any, for all the feeds.
372
 * @ingroup themeable
373
 */
37412
function theme_aggregator_page_rss($feeds, $category = NULL) {
3750
  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
376
3770
  $items = '';
3780
  $feed_length = variable_get('feed_item_length', 'teaser');
3790
  foreach ($feeds as $feed) {
380
    switch ($feed_length) {
3810
      case 'teaser':
3820
        $teaser = node_teaser($feed->description);
3830
        if ($teaser != $feed->description) {
3840
          $teaser .= '<p><a href="' . check_url($feed->link) . '">' .
t('read more') . "</a></p>\n";
3850
        }
3860
        $feed->description = $teaser;
3870
        break;
3880
      case 'title':
3890
        $feed->description = '';
3900
        break;
3910
    }
3920
    $items .= format_rss_item($feed->ftitle . ': ' . $feed->title,
$feed->link, $feed->description, array('pubDate' => date('r',
$feed->timestamp)));
3930
  }
394
3950
  $site_name = variable_get('site_name', 'Drupal');
3960
  $url = url((isset($category) ? 'aggregator/categories/' . $category->cid
: 'aggregator'), array('absolute' => TRUE));
3970
  $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));
398
3990
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
4000
  $output .= "<rss version=\"2.0\">\n";
4010
  $output .= format_rss_channel(t('@site_name aggregator',
array('@site_name' => $site_name)), $url, $description, $items);
4020
  $output .= "</rss>\n";
403
4040
  print $output;
4050
}
406
407
/**
408
 * Menu callback; generates an OPML representation of all feeds.
409
 *
410
 * @param $cid
411
 *   If set, feeds are exported only from a category with this ID.
Otherwise, all feeds are exported.
412
 * @return
413
 *   The output XML.
414
 */
41512
function aggregator_page_opml($cid = NULL) {
4160
  if ($cid) {
4170
    $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 = :cid ORDER
BY title', array(':cid' => $cid));
4180
  }
419
  else {
4200
    $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
421
  }
422
4230
  $feeds = $result->fetchAll();
4240
  return theme('aggregator_page_opml', $feeds);
4250
}
426
427
/**
428
 * Theme the OPML feed output.
429
 *
430
 * @param $feeds
431
 *   An array of the feeds to theme.
432
 * @ingroup themeable
433
 */
43412
function theme_aggregator_page_opml($feeds) {
4350
  drupal_set_header('Content-Type: text/xml; charset=utf-8');
436
4370
  $output  = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
4380
  $output .= "<opml version=\"1.1\">\n";
4390
  $output .= "<head>\n";
4400
  $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) .
"</title>\n";
4410
  $output .= '<dateModified>' . gmdate('r') . "</dateModified>\n";
4420
  $output .= "</head>\n";
4430
  $output .= "<body>\n";
4440
  foreach ($feeds as $feed) {
4450
    $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="'
. check_url($feed->url) . "\" />\n";
4460
  }
4470
  $output .= "</body>\n";
4480
  $output .= "</opml>\n";
449
4500
  print $output;
4510
}
452
453
/**
454
 * Process variables for aggregator-summary-items.tpl.php.
455
 *
456
 * @see aggregator-summary-item.tpl.php
457
 */
45812
function template_preprocess_aggregator_summary_items(&$variables) {
4590
  $variables['title'] = check_plain($variables['source']->title);
4600
  $variables['summary_list'] = theme('item_list',
$variables['summary_items']);
4610
  $variables['source_url'] = $variables['source']->url;
4620
}
463
464
/**
465
 * Process variables for aggregator-summary-item.tpl.php.
466
 *
467
 * @see aggregator-summary-item.tpl.php
468
 */
46912
function template_preprocess_aggregator_summary_item(&$variables) {
4700
  $item = $variables['item'];
471
4720
  $variables['feed_url'] = check_url($item->link);
4730
  $variables['feed_title'] = check_plain($item->title);
4740
  $variables['feed_age'] = t('%age old', array('%age' =>
format_interval(REQUEST_TIME - $item->timestamp)));
475
4760
  $variables['source_url'] = '';
4770
  $variables['source_title'] = '';
4780
  if (!empty($item->feed_link)) {
4790
    $variables['source_url'] = check_url($item->feed_link);
4800
    $variables['source_title'] = check_plain($item->feed_title);
4810
  }
4820
}
483
484
/**
485
 * Process variables for aggregator-feed-source.tpl.php.
486
 *
487
 * @see aggregator-feed-source.tpl.php
488
 */
48912
function template_preprocess_aggregator_feed_source(&$variables) {
4902
  $feed = $variables['feed'];
491
4922
  $variables['source_icon'] = theme('feed_icon', $feed->url, t('!title
feed', array('!title' => $feed->title)));
4932
  $variables['source_image'] = $feed->image;
4942
  $variables['source_description'] =
aggregator_filter_xss($feed->description);
4952
  $variables['source_url'] = check_url(url($feed->link, array('absolute' =>
TRUE)));
496
4972
  if ($feed->checked) {
4980
    $variables['last_checked'] = t('@time ago', array('@time' =>
format_interval(REQUEST_TIME - $feed->checked)));
4990
  }
500
  else {
5012
    $variables['last_checked'] = t('never');
502
  }
503
5042
  if (user_access('administer news feeds')) {
5052
    $variables['last_checked'] = l($variables['last_checked'],
'admin/content/aggregator');
5062
  }
5072
}
50812