aggregator.pages.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. * Loads and optionally filters feed items.
  80. *
  81. * @param $type
  82. * The type of filter for the items. Possible values are:
  83. * - sum: No filtering.
  84. * - source: Filter the feed items, limiting the result to items from a
  85. * single source.
  86. * - category: Filter the feed items by category.
  87. * @param $data
  88. * Feed or category data used for filtering. The type and value of $data
  89. * depends on $type:
  90. * - source: $data is an object with $data->fid identifying the feed used to
  91. * as filter.
  92. * - category: $data is an array with $data['cid'] being the category id to
  93. * filter on.
  94. * The $data parameter is not used when $type is 'sum'.
  95. *
  96. * @return
  97. * An array of the feed items.
  98. */
  99. function aggregator_feed_items_load($type, $data = NULL) {
  100. $items = array();
  101. switch ($type) {
  102. case 'sum':
  103. $query = db_select('aggregator_item', 'i');
  104. $query->join('aggregator_feed', 'f', 'i.fid = f.fid');
  105. $query->fields('i');
  106. $query->addField('f', 'title', 'ftitle');
  107. $query->addField('f', 'link', 'flink');
  108. break;
  109. case 'source':
  110. $query = db_select('aggregator_item', 'i');
  111. $query
  112. ->fields('i')
  113. ->condition('i.fid', $data->fid);
  114. break;
  115. case 'category':
  116. $query = db_select('aggregator_category_item', 'c');
  117. $query->leftJoin('aggregator_item', 'i', 'c.iid = i.iid');
  118. $query->leftJoin('aggregator_feed', 'f', 'i.fid = f.fid');
  119. $query
  120. ->fields('i')
  121. ->condition('cid', $data['cid']);
  122. $query->addField('f', 'title', 'ftitle');
  123. $query->addField('f', 'link', 'flink');
  124. break;
  125. }
  126. $result = $query
  127. ->extend('PagerDefault')
  128. ->limit(20)
  129. ->orderBy('i.timestamp', 'DESC')
  130. ->orderBy('i.iid', 'DESC')
  131. ->execute();
  132. foreach ($result as $item) {
  133. $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();
  134. $items[] = $item;
  135. }
  136. return $items;
  137. }
  138. /**
  139. * Prints an aggregator page listing a number of feed items.
  140. *
  141. * Various menu callbacks use this function to print their feeds.
  142. *
  143. * @param $items
  144. * The items to be listed.
  145. * @param $op
  146. * Which form should be added to the items. Only 'categorize' is now
  147. * recognized.
  148. * @param $feed_source
  149. * The feed source URL.
  150. *
  151. * @return
  152. * The rendered list of items for a feed.
  153. */
  154. function _aggregator_page_list($items, $op, $feed_source = '') {
  155. if (user_access('administer news feeds') && ($op == 'categorize')) {
  156. // Get form data.
  157. $output = aggregator_categorize_items($items, $feed_source);
  158. }
  159. else {
  160. // Assemble themed output.
  161. $output = $feed_source;
  162. foreach ($items as $item) {
  163. $output .= theme('aggregator_item', array('item' => $item));
  164. }
  165. $output = theme('aggregator_wrapper', array('content' => $output));
  166. }
  167. return $output;
  168. }
  169. /**
  170. * Form constructor to build the page list form.
  171. *
  172. * @param $items
  173. * An array of the feed items.
  174. * @param $feed_source
  175. * The feed source URL.
  176. *
  177. * @ingroup forms
  178. * @see aggregator_categorize_items_submit()
  179. */
  180. function aggregator_categorize_items($items, $feed_source = '') {
  181. $form['#submit'][] = 'aggregator_categorize_items_submit';
  182. $form['#theme'] = 'aggregator_categorize_items';
  183. $form['feed_source'] = array(
  184. '#value' => $feed_source,
  185. );
  186. $categories = array();
  187. $done = FALSE;
  188. $form['items'] = array();
  189. $form['categories'] = array(
  190. '#tree' => TRUE,
  191. );
  192. foreach ($items as $item) {
  193. $form['items'][$item->iid] = array('#markup' => theme('aggregator_item', array('item' => $item)));
  194. $form['categories'][$item->iid] = array();
  195. $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));
  196. $selected = array();
  197. foreach ($categories_result as $category) {
  198. if (!$done) {
  199. $categories[$category->cid] = check_plain($category->title);
  200. }
  201. if ($category->iid) {
  202. $selected[] = $category->cid;
  203. }
  204. }
  205. $done = TRUE;
  206. $form['categories'][$item->iid] = array(
  207. '#type' => variable_get('aggregator_category_selector', 'checkboxes'),
  208. '#default_value' => $selected,
  209. '#options' => $categories,
  210. '#size' => 10,
  211. '#multiple' => TRUE
  212. );
  213. }
  214. $form['actions'] = array('#type' => 'actions');
  215. $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save categories'));
  216. return $form;
  217. }
  218. /**
  219. * Form submission handler for aggregator_categorize_items().
  220. */
  221. function aggregator_categorize_items_submit($form, &$form_state) {
  222. if (!empty($form_state['values']['categories'])) {
  223. foreach ($form_state['values']['categories'] as $iid => $selection) {
  224. db_delete('aggregator_category_item')
  225. ->condition('iid', $iid)
  226. ->execute();
  227. $insert = db_insert('aggregator_category_item')->fields(array('iid', 'cid'));
  228. $has_values = FALSE;
  229. foreach ($selection as $cid) {
  230. if ($cid && $iid) {
  231. $has_values = TRUE;
  232. $insert->values(array(
  233. 'iid' => $iid,
  234. 'cid' => $cid,
  235. ));
  236. }
  237. }
  238. if ($has_values) {
  239. $insert->execute();
  240. }
  241. }
  242. }
  243. drupal_set_message(t('The categories have been saved.'));
  244. }
  245. /**
  246. * Returns HTML for the aggregator page list form for assigning categories.
  247. *
  248. * @param $variables
  249. * An associative array containing:
  250. * - form: A render element representing the form.
  251. *
  252. * @ingroup themeable
  253. */
  254. function theme_aggregator_categorize_items($variables) {
  255. $form = $variables['form'];
  256. $output = drupal_render($form['feed_source']);
  257. $rows = array();
  258. if (!empty($form['items'])) {
  259. foreach (element_children($form['items']) as $key) {
  260. $rows[] = array(
  261. drupal_render($form['items'][$key]),
  262. array('data' => drupal_render($form['categories'][$key]), 'class' => array('categorize-item')),
  263. );
  264. }
  265. }
  266. $output .= theme('table', array('header' => array('', t('Categorize')), 'rows' => $rows));
  267. $output .= drupal_render($form['submit']);
  268. $output .= drupal_render_children($form);
  269. return theme('aggregator_wrapper', array('content' => $output));
  270. }
  271. /**
  272. * Processes variables for aggregator-wrapper.tpl.php.
  273. *
  274. * @see aggregator-wrapper.tpl.php
  275. */
  276. function template_preprocess_aggregator_wrapper(&$variables) {
  277. $variables['pager'] = theme('pager');
  278. }
  279. /**
  280. * Processes variables for aggregator-item.tpl.php.
  281. *
  282. * @see aggregator-item.tpl.php
  283. */
  284. function template_preprocess_aggregator_item(&$variables) {
  285. $item = $variables['item'];
  286. $variables['feed_url'] = check_url($item->link);
  287. $variables['feed_title'] = check_plain($item->title);
  288. $variables['content'] = aggregator_filter_xss($item->description);
  289. $variables['source_url'] = '';
  290. $variables['source_title'] = '';
  291. if (isset($item->ftitle) && isset($item->fid)) {
  292. $variables['source_url'] = url("aggregator/sources/$item->fid");
  293. $variables['source_title'] = check_plain($item->ftitle);
  294. }
  295. if (date('Ymd', $item->timestamp) == date('Ymd')) {
  296. $variables['source_date'] = t('%ago ago', array('%ago' => format_interval(REQUEST_TIME - $item->timestamp)));
  297. }
  298. else {
  299. $variables['source_date'] = format_date($item->timestamp, 'custom', variable_get('date_format_medium', 'D, m/d/Y - H:i'));
  300. }
  301. $variables['categories'] = array();
  302. foreach ($item->categories as $category) {
  303. $variables['categories'][$category->cid] = l($category->title, 'aggregator/categories/' . $category->cid);
  304. }
  305. }
  306. /**
  307. * Menu callback; displays all the feeds used by the aggregator.
  308. */
  309. function aggregator_page_sources() {
  310. $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');
  311. $output = '';
  312. foreach ($result as $feed) {
  313. // Most recent items:
  314. $summary_items = array();
  315. if (variable_get('aggregator_summary_items', 3)) {
  316. $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));
  317. foreach ($items as $item) {
  318. $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
  319. }
  320. }
  321. $feed->url = url('aggregator/sources/' . $feed->fid);
  322. $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $feed));
  323. }
  324. $output .= theme('feed_icon', array('url' => 'aggregator/opml', 'title' => t('OPML feed')));
  325. return theme('aggregator_wrapper', array('content' => $output));
  326. }
  327. /**
  328. * Menu callback; displays all the categories used by the aggregator.
  329. */
  330. function aggregator_page_categories() {
  331. $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');
  332. $output = '';
  333. foreach ($result as $category) {
  334. if (variable_get('aggregator_summary_items', 3)) {
  335. $summary_items = array();
  336. $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));
  337. foreach ($items as $item) {
  338. $summary_items[] = theme('aggregator_summary_item', array('item' => $item));
  339. }
  340. }
  341. $category->url = url('aggregator/categories/' . $category->cid);
  342. $output .= theme('aggregator_summary_items', array('summary_items' => $summary_items, 'source' => $category));
  343. }
  344. return theme('aggregator_wrapper', array('content' => $output));
  345. }
  346. /**
  347. * Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
  348. */
  349. function aggregator_page_rss() {
  350. $result = NULL;
  351. // arg(2) is the passed cid, only select for that category.
  352. if (arg(2)) {
  353. $category = db_query('SELECT cid, title FROM {aggregator_category} WHERE cid = :cid', array(':cid' => arg(2)))->fetchObject();
  354. $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));
  355. }
  356. // Or, get the default aggregator items.
  357. else {
  358. $category = NULL;
  359. $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));
  360. }
  361. $feeds = $result->fetchAll();
  362. return theme('aggregator_page_rss', array('feeds' => $feeds, 'category' => $category));
  363. }
  364. /**
  365. * Prints the RSS page for a feed.
  366. *
  367. * @param $variables
  368. * An associative array containing:
  369. * - feeds: An array of the feeds to theme.
  370. * - category: A common category, if any, for all the feeds.
  371. *
  372. * @return void
  373. *
  374. * @ingroup themeable
  375. */
  376. function theme_aggregator_page_rss($variables) {
  377. $feeds = $variables['feeds'];
  378. $category = $variables['category'];
  379. drupal_add_http_header('Content-Type', 'application/rss+xml; charset=utf-8');
  380. $items = '';
  381. $feed_length = variable_get('feed_item_length', 'fulltext');
  382. foreach ($feeds as $feed) {
  383. switch ($feed_length) {
  384. case 'teaser':
  385. $summary = text_summary($feed->description, NULL, variable_get('aggregator_teaser_length', 600));
  386. if ($summary != $feed->description) {
  387. $summary .= '<p><a href="' . check_url($feed->link) . '">' . t('read more') . "</a></p>\n";
  388. }
  389. $feed->description = $summary;
  390. break;
  391. case 'title':
  392. $feed->description = '';
  393. break;
  394. }
  395. $items .= format_rss_item($feed->ftitle . ': ' . $feed->title, $feed->link, $feed->description, array('pubDate' => date('r', $feed->timestamp)));
  396. }
  397. $site_name = variable_get('site_name', 'Drupal');
  398. $url = url((isset($category) ? 'aggregator/categories/' . $category->cid : 'aggregator'), array('absolute' => TRUE));
  399. $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));
  400. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  401. $output .= "<rss version=\"2.0\">\n";
  402. $output .= format_rss_channel(t('@site_name aggregator', array('@site_name' => $site_name)), $url, $description, $items);
  403. $output .= "</rss>\n";
  404. print $output;
  405. }
  406. /**
  407. * Menu callback; generates an OPML representation of all feeds.
  408. *
  409. * @param $cid
  410. * If set, feeds are exported only from a category with this ID. Otherwise, all feeds are exported.
  411. * @return
  412. * The output XML.
  413. */
  414. function aggregator_page_opml($cid = NULL) {
  415. if ($cid) {
  416. $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));
  417. }
  418. else {
  419. $result = db_query('SELECT * FROM {aggregator_feed} ORDER BY title');
  420. }
  421. $feeds = $result->fetchAll();
  422. return theme('aggregator_page_opml', array('feeds' => $feeds));
  423. }
  424. /**
  425. * Prints the OPML page for a feed.
  426. *
  427. * @param $variables
  428. * An associative array containing:
  429. * - feeds: An array of the feeds to theme.
  430. *
  431. * @return void
  432. *
  433. * @ingroup themeable
  434. */
  435. function theme_aggregator_page_opml($variables) {
  436. $feeds = $variables['feeds'];
  437. drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
  438. $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
  439. $output .= "<opml version=\"1.1\">\n";
  440. $output .= "<head>\n";
  441. $output .= '<title>' . check_plain(variable_get('site_name', 'Drupal')) . "</title>\n";
  442. $output .= '<dateModified>' . gmdate(DATE_RFC2822, REQUEST_TIME) . "</dateModified>\n";
  443. $output .= "</head>\n";
  444. $output .= "<body>\n";
  445. foreach ($feeds as $feed) {
  446. $output .= '<outline text="' . check_plain($feed->title) . '" xmlUrl="' . check_url($feed->url) . "\" />\n";
  447. }
  448. $output .= "</body>\n";
  449. $output .= "</opml>\n";
  450. print $output;
  451. }
  452. /**
  453. * Processes variables for aggregator-summary-items.tpl.php.
  454. *
  455. * @see aggregator-summary-items.tpl.php
  456. */
  457. function template_preprocess_aggregator_summary_items(&$variables) {
  458. $variables['title'] = check_plain($variables['source']->title);
  459. $variables['summary_list'] = theme('item_list', array('items' => $variables['summary_items']));
  460. $variables['source_url'] = $variables['source']->url;
  461. }
  462. /**
  463. * Processes variables for aggregator-summary-item.tpl.php.
  464. *
  465. * @see aggregator-summary-item.tpl.php
  466. */
  467. function template_preprocess_aggregator_summary_item(&$variables) {
  468. $item = $variables['item'];
  469. $variables['feed_url'] = check_url($item->link);
  470. $variables['feed_title'] = check_plain($item->title);
  471. $variables['feed_age'] = t('%age old', array('%age' => format_interval(REQUEST_TIME - $item->timestamp)));
  472. $variables['source_url'] = '';
  473. $variables['source_title'] = '';
  474. if (!empty($item->feed_link)) {
  475. $variables['source_url'] = check_url($item->feed_link);
  476. $variables['source_title'] = check_plain($item->feed_title);
  477. }
  478. }
  479. /**
  480. * Processes variables for aggregator-feed-source.tpl.php.
  481. *
  482. * @see aggregator-feed-source.tpl.php
  483. */
  484. function template_preprocess_aggregator_feed_source(&$variables) {
  485. $feed = $variables['feed'];
  486. $variables['source_icon'] = theme('feed_icon', array('url' => $feed->url, 'title' => t('!title feed', array('!title' => $feed->title))));
  487. if (!empty($feed->image) && !empty($feed->title) && !empty($feed->link)) {
  488. $variables['source_image'] = l(theme('image', array('path' => $feed->image, 'alt' => $feed->title)), $feed->link, array('html' => TRUE, 'attributes' => array('class' => 'feed-image')));
  489. }
  490. else {
  491. $variables['source_image'] = '';
  492. }
  493. $variables['source_description'] = aggregator_filter_xss($feed->description);
  494. $variables['source_url'] = check_url(url($feed->link, array('absolute' => TRUE)));
  495. if ($feed->checked) {
  496. $variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked)));
  497. }
  498. else {
  499. $variables['last_checked'] = t('never');
  500. }
  501. if (user_access('administer news feeds')) {
  502. $variables['last_checked'] = l($variables['last_checked'], 'admin/config/services/aggregator');
  503. }
  504. }