Skip navigation
Help

aggregator.pages.inc

  1. drupal
    1. 6 drupal/modules/aggregator/aggregator.pages.inc
    2. 7 drupal/modules/aggregator/aggregator.pages.inc

User page callbacks for the aggregator module.

Functions & methods

NameDescription
aggregator_categorize_itemsForm builder; build the page list form.
aggregator_categorize_items_submitProcess aggregator_categorize_items() form submissions.
aggregator_feed_items_loadLoad feed items
aggregator_page_categoriesMenu callback; displays all the categories used by the aggregator.
aggregator_page_categoryMenu callback; displays all the items aggregated in a particular category.
aggregator_page_category_formMenu callback; displays a form containing items aggregated in a category.
aggregator_page_lastMenu callback; displays the most recent items gathered from any feed.
aggregator_page_opmlMenu callback; generates an OPML representation of all feeds.
aggregator_page_rssMenu callback; generate an RSS 0.92 feed of aggregator items or categories.
aggregator_page_sourceMenu callback; displays all the items captured from a particular feed.
aggregator_page_sourcesMenu callback; displays all the feeds used by the aggregator.
aggregator_page_source_formMenu callback; displays a form with all items captured from a feed.
template_preprocess_aggregator_feed_sourceProcess variables for aggregator-feed-source.tpl.php.
template_preprocess_aggregator_itemProcess variables for aggregator-item.tpl.php.
template_preprocess_aggregator_summary_itemProcess variables for aggregator-summary-item.tpl.php.
template_preprocess_aggregator_summary_itemsProcess variables for aggregator-summary-items.tpl.php.
template_preprocess_aggregator_wrapperProcess variables for aggregator-wrapper.tpl.php.
theme_aggregator_categorize_itemsReturns HTML for the aggregator page list form for assigning categories.
theme_aggregator_page_opmlPrints the OPML page for a feed.
theme_aggregator_page_rssPrints the RSS page for a feed.
_aggregator_page_listPrints an aggregator page listing a number of feed items.

File

drupal/modules/aggregator/aggregator.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the aggregator module.
  5. */
  6. /**
  7. * Menu callback; displays the most recent items gathered from any feed.
  8. *
  9. * @return
  10. * The items HTML.
  11. */
  12. function aggregator_page_last() {
  13. drupal_add_feed('aggregator/rss', variable_get('site_name', 'Drupal') . ' ' . t('aggregator'));
  14. $items = aggregator_feed_items_load('sum');
  15. return _aggregator_page_list($items, arg(1));
  16. }
  17. /**
  18. * Menu callback; displays all the items captured from a particular feed.
  19. *
  20. * @param $feed
  21. * The feed for which to display all items.
  22. *
  23. * @return
  24. * The rendered list of items for a feed.
  25. */
  26. function aggregator_page_source($feed) {
  27. drupal_set_title($feed->title);
  28. $feed_source = theme('aggregator_feed_source', array('feed' => $feed));
  29. // It is safe to include the fid in the query because it's loaded from the
  30. // database by aggregator_feed_load.
  31. $items = aggregator_feed_items_load('source', $feed);
  32. return _aggregator_page_list($items, arg(3), $feed_source);
  33. }
  34. /**
  35. * Menu callback; displays a form with all items captured from a feed.
  36. *
  37. * @param $feed
  38. * The feed for which to list all the aggregated items.
  39. *
  40. * @return
  41. * The rendered list of items for a feed.
  42. *
  43. * @see aggregator_page_source()
  44. */
  45. function aggregator_page_source_form($form, $form_state, $feed) {
  46. return aggregator_page_source($feed);
  47. }
  48. /**
  49. * Menu callback; displays all the items aggregated in a particular category.
  50. *
  51. * @param $category
  52. * The category for which to list all the aggregated items.
  53. *
  54. * @return
  55. * The rendered list of items for a category.
  56. */
  57. function aggregator_page_category($category) {
  58. drupal_add_feed('aggregator/rss/' . $category['cid'], variable_get('site_name', 'Drupal') . ' ' . t('aggregator - @title', array('@title' => $category['title'])));
  59. // It is safe to include the cid in the query because it's loaded from the
  60. // database by aggregator_category_load.
  61. $items = aggregator_feed_items_load('category', $category);
  62. return _aggregator_page_list($items, arg(3));
  63. }
  64. /**
  65. * Menu callback; displays a form containing items aggregated in a category.
  66. *
  67. * @param $category
  68. * The category for which to list all the aggregated items.
  69. *
  70. * @return
  71. * The rendered list of items for a category.
  72. *
  73. * @see aggregator_page_category()
  74. */
  75. function aggregator_page_category_form($form, $form_state, $category) {
  76. return aggregator_page_category($category);
  77. }
  78. /**
  79. * Load feed items
  80. *
  81. * @param $type
  82. * The filter for the items. Possible values: 'sum', 'source', 'category'
  83. * @param $data
  84. * Feed or category data for filtering
  85. * @return
  86. * An array of the feed items.
  87. */
  88. function aggregator_feed_items_load($type, $data = NULL) {
  89. $items = array();
  90. $range_limit = 20;
  91. switch ($type) {
  92. case 'sum':
  93. $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);
  94. break;
  95. case 'source':
  96. $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC', 0, $range_limit, array(':fid' => $data->fid));
  97. break;
  98. case 'category':
  99. $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', 0, $range_limit, array(':cid' => $data['cid']));
  100. break;
  101. }
  102. foreach ($result as $item) {
  103. $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();
  104. $items[] = $item;
  105. }
  106. return $items;
  107. }
  108. /**
  109. * Prints an aggregator page listing a number of feed items.
  110. *
  111. * Various menu callbacks use this function to print their feeds.
  112. *
  113. * @param $items
  114. * The items to be listed.
  115. * @param $op
  116. * Which form should be added to the items. Only 'categorize' is now recognized.
  117. * @param $feed_source
  118. * The feed source URL.
  119. * @return
  120. * The items HTML.
  121. */
  122. function _aggregator_page_list($items, $op, $feed_source = '') {
  123. if (user_access('administer news feeds') && ($op == 'categorize')) {
  124. // Get form data.
  125. $output = aggregator_categorize_items($items, $feed_source);
  126. }
  127. else {
  128. // Assemble themed output.
  129. $output = $feed_source;
  130. foreach ($items as $item) {
  131. $output .= theme('aggregator_item', array('item' => $item));
  132. }
  133. $output = theme('aggregator_wrapper', array('content' => $output));
  134. }
  135. return $output;
  136. }
  137. /**
  138. * Form builder; build the page list form.
  139. *
  140. * @param $items
  141. * An array of the feed items.
  142. * @param $feed_source
  143. * The feed source URL.
  144. * @return
  145. * The form structure.
  146. * @ingroup forms
  147. * @see aggregator_categorize_items_submit()
  148. */
  149. function aggregator_categorize_items($items, $feed_source = '') {
  150. $form['#submit'][] = 'aggregator_categorize_items_submit';
  151. $form['#theme'] = 'aggregator_categorize_items';
  152. $form['feed_source'] = array(
  153. '#value' => $feed_source,
  154. );
  155. $categories = array();
  156. $done = FALSE;
  157. $form['items'] = array();
  158. $form['categories'] = array(
  159. '#tree' => TRUE,
  160. );
  161. foreach ($items as $item) {
  162. $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', array('item' => $item)));
  163. $form['categories'][$item->iid] = array();
  164. $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));
  165. $selected = array();
  166. foreach ($categories_result as $category) {
  167. if (!$done) {
  168. $categories[$category->cid] = check_plain($category->title);
  169. }
  170. if ($category->iid) {
  171. $selected[] = $category->cid;
  172. }
  173. }
  174. $done = TRUE;
  175. $form['categories'][$item->iid] = array(
  176. '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
  177. '#default_value' => $selected,
  178. '#options' => $categories,
  179. '#size' => 10,
  180. '#multiple' => TRUE
  181. );
  182. }
  183. $form['actions'] = array('#type' => 'actions');
  184. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
  185. return $form;
  186. }
  187. /**
  188. * Process aggregator_categorize_items() form submissions.
  189. */
  190. function aggregator_categorize_items_submit($form, &$form_state) {
  191. if (!empty($form_state['values']['categories'])) {
  192. foreach ($form_state['values']['categories'] as $iid => $selection) {
  193. db_delete('aggregator_category_item')
  194. ->condition('iid', $iid)
  195. ->execute();
  196. $insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
  197. $has_values = FALSE;
  198. foreach ($selection as $cid) {
  199. if ($cid && $iid) {
  200. $has_values = TRUE;
  201. $insert->values(array(
  202. 'iid' => $iid,
  203. 'cid' => $cid,
  204. ));
  205. }
  206. }
  207. if ($has_values) {
  208. $insert->execute();
  209. }
  210. }
  211. }
  212. drupal_set_message(t('The categories have been saved.'));
  213. }
  214. /**
  215. * Returns HTML for the aggregator page list form for assigning categories.
  216. *
  217. * @param $variables
  218. * An associative array containing:
  219. * - form: A render element representing the form.
  220. *
  221. * @ingroup themeable
  222. */
  223. function theme_aggregator_categorize_items($variables) {
  224. $form = $variables['form'];
  225. $output = drupal_render($form['feed_source']);
  226. $rows = array();
  227. if (!empty($form['items'])) {
  228. foreach (element_children($form['items']) as $key) {
  229. $rows[] = array(
  230. drupal_render($form['items'][$key]),
  231. array('data' => drupal_render($form['categories'][$key]), 'class' => array('categorize-item')),
  232. );
  233. }
  234. }
  235. $output .= theme('table', array('header' => array('', t('Categorize')), 'rows' => $rows));
  236. $output .= drupal_render($form['submit']);
  237. $output .= drupal_render_children($form);
  238. return theme('aggregator_wrapper', array('content' => $output));
  239. }
  240. /**
  241. * Process variables for aggregator-wrapper.tpl.php.
  242. *
  243. * @see aggregator-wrapper.tpl.php
  244. */
  245. function template_preprocess_aggregator_wrapper(&$variables) {
  246. $variables['pager'] = theme('pager');
  247. }
  248. /**
  249. * Process variables for aggregator-item.tpl.php.
  250. *
  251. * @see aggregator-item.tpl.php
  252. */
  253. function template_preprocess_aggregator_item(&$variables) {
  254. $item = $variables['item'];
  255. $variables['feed_url'] = check_url($item->link);
  256. $variables['feed_title'] = check_plain($item->title);
  257. $variables['content'] = aggregator_filter_xss($item->description);
  258. $variables['source_url'] = '';
  259. $variables['source_title'] = '';
  260. if (isset($item->ftitle) && isset($item->fid)) {
  261. $variables['source_url'] = url("aggregator/sources/$item->fid");
  262. $variables['source_title'] = check_plain($item->ftitle);
  263. }
  264. if (date('Ymd', $item->timestamp) == date('Ymd')) {
  265. $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp)));
  266. }
  267. else {
  268. $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
  269. }
  270. $variables['categories'] = array();
  271. foreach ($item->categories as $category) {
  272. $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
  273. }
  274. }
  275. /**
  276. * Menu callback; displays all the feeds used by the aggregator.
  277. */
  278. function aggregator_page_sources() {
  279. $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');
  280. $output = '';
  281. foreach ($result as $feed) {
  282. // Most recent items:
  283. $summary_items = array();
  284. if (variable_get('aggregator_summary_items', 3)) {
  285. $items = db_query_range('SELECT i.title, i.timestamp, i.link FROM {aggregator_item} i WHERE i.fid = :fid ORDER BY i.timestamp DESC', 0, variable_get('aggregator_summary_items', 3), array(':fid' => $feed->fid));
  286. foreach ($items as $item) {
  287. $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
  288. }
  289. }
  290. $feed->url = url('aggregator/sources/' . $feed->fid);
  291. $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $feed));
  292. }
  293. $output .= theme('feed_icon', array('url' => 'aggregator/opml', 'title' => t('OPML feed')));
  294. return theme('aggregator_wrapper', array('content' => $output));
  295. }
  296. /**
  297. * Menu callback; displays all the categories used by the aggregator.
  298. */
  299. function aggregator_page_categories() {
  300. $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');
  301. $output = '';
  302. foreach ($result as $category) {
  303. if (variable_get('aggregator_summary_items', 3)) {
  304. $summary_items = array();
  305. $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', 0, variable_get('aggregator_summary_items', 3), array(':cid' => $category->cid));
  306. foreach ($items as $item) {
  307. $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
  308. }
  309. }
  310. $category->url = url('aggregator/categories/' . $category->cid);
  311. $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $category));
  312. }
  313. return theme('aggregator_wrapper', array('content' => $output));
  314. }
  315. /**
  316. * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
  317. */
  318. function aggregator_page_rss() {
  319. $result = NULL;
  320. // arg(2) is the passed cid, only select for that category.
  321. if (arg(2)) {
  322. $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
  323. $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', 0, variable_get('feed_default_items', 10), array(':cid' => $category->cid));
  324. }
  325. // Or, get the default aggregator items.
  326. else {
  327. $category = NULL;
  328. $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));
  329. }
  330. $feeds = $result->fetchAll();
  331. return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category));
  332. }
  333. /**
  334. * Prints the RSS page for a feed.
  335. *
  336. * @param $variables
  337. * An associative array containing:
  338. * - feeds: An array of the feeds to theme.
  339. * - category: A common category, if any, for all the feeds.
  340. *
  341. * @return void
  342. *
  343. * @ingroup themeable
  344. */
  345. function theme_aggregator_page_rss($variables) {
  346. $feeds = $variables['feeds'];
  347. $category = $variables['category'];
  348. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  349. $items = '';
  350. $feed_length = variable_get('feed_item_length', 'fulltext');
  351. foreach ($feeds as $feed) {
  352. switch ($feed_length) {
  353. case 'teaser':
  354. $summary = text_summary($feed->description, NULL, variable_get('aggregator_teaser_length', 600));
  355. if ($summary != $feed->description) {
  356. $summary .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
  357. }
  358. $feed->description = $summary;
  359. break;
  360. case 'title':
  361. $feed->description = '';
  362. break;
  363. }
  364. $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
  365. }
  366. $site_name = variable_get('site_name', 'Drupal');
  367. $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
  368. $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));
  369. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  370. $output .= "<rss version=\"2.0\">\n";
  371. $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
  372. $output .= "</rss>\n";
  373. print $output;
  374. }
  375. /**
  376. * Menu callback; generates an OPML representation of all feeds.
  377. *
  378. * @param $cid
  379. * If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
  380. * @return
  381. * The output XML.
  382. */
  383. function aggregator_page_opml($cid = NULL) {
  384. if ($cid) {
  385. $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));
  386. }
  387. else {
  388. $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
  389. }
  390. $feeds = $result->fetchAll();
  391. return theme('aggregator_page_opml', array('feeds' => $feeds));
  392. }
  393. /**
  394. * Prints the OPML page for a feed.
  395. *
  396. * @param $variables
  397. * An associative array containing:
  398. * - feeds: An array of the feeds to theme.
  399. *
  400. * @return void
  401. *
  402. * @ingroup themeable
  403. */
  404. function theme_aggregator_page_opml($variables) {
  405. $feeds = $variables['feeds'];
  406. drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
  407. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  408. $output .= "<opml version=\"1.1\">\n";
  409. $output .= "<head>\n";
  410. $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
  411. $output .= '<dateModified>' . gmdate(DATE_RFC2822, REQUEST_TIME) . "</dateModified>\n";
  412. $output .= "</head>\n";
  413. $output .= "<body>\n";
  414. foreach ($feeds as $feed) {
  415. $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
  416. }
  417. $output .= "</body>\n";
  418. $output .= "</opml>\n";
  419. print $output;
  420. }
  421. /**
  422. * Process variables for aggregator-summary-items.tpl.php.
  423. *
  424. * @see aggregator-summary-items.tpl.php
  425. */
  426. function template_preprocess_aggregator_summary_items(&$variables) {
  427. $variables['title'] = check_plain($variables['source']->title);
  428. $variables['summary_list'] = theme('item_list', array('items' => $variables['summary_items']));
  429. $variables['source_url'] = $variables['source']->url;
  430. }
  431. /**
  432. * Process variables for aggregator-summary-item.tpl.php.
  433. *
  434. * @see aggregator-summary-item.tpl.php
  435. */
  436. function template_preprocess_aggregator_summary_item(&$variables) {
  437. $item = $variables['item'];
  438. $variables['feed_url'] = check_url($item->link);
  439. $variables['feed_title'] = check_plain($item->title);
  440. $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
  441. $variables['source_url'] = '';
  442. $variables['source_title'] = '';
  443. if (!empty($item->feed_link)) {
  444. $variables['source_url'] = check_url($item->feed_link);
  445. $variables['source_title'] = check_plain($item->feed_title);
  446. }
  447. }
  448. /**
  449. * Process variables for aggregator-feed-source.tpl.php.
  450. *
  451. * @see aggregator-feed-source.tpl.php
  452. */
  453. function template_preprocess_aggregator_feed_source(&$variables) {
  454. $feed = $variables['feed'];
  455. $variables['source_icon'] = theme('feed_icon', array('url' => $feed->url, 'title' => t('!title feed', array('!title' => $feed->title))));
  456. $variables['source_image'] = $feed->image;
  457. $variables['source_description'] = aggregator_filter_xss($feed->description);
  458. $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
  459. if ($feed->checked) {
  460. $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
  461. }
  462. else {
  463. $variables['last_checked'] = t('never');
  464. }
  465. if (user_access('administer news feeds')) {
  466. $variables['last_checked'] = l($variables['last_checked'], 'admin/config/services/aggregator');
  467. }
  468. }