1. 8.3.x core/modules/aggregator/aggregator.module
  2. 8.0.x core/modules/aggregator/aggregator.module
  3. 8.1.x core/modules/aggregator/aggregator.module
  4. 8.2.x core/modules/aggregator/aggregator.module
  5. 8.4.x core/modules/aggregator/aggregator.module
  6. 4.6.x modules/aggregator.module
  7. 4.7.x modules/aggregator.module
  8. 5.x modules/aggregator/aggregator.module
  9. 6.x modules/aggregator/aggregator.module
  10. 7.x modules/aggregator/aggregator.module

Used to aggregate syndicated content (RSS, RDF, and Atom).

File

modules/aggregator/aggregator.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Used to aggregate syndicated content (RSS, RDF, and Atom).
  5. */
  6. /**
  7. * Denotes that a feed's items should never expire.
  8. */
  9. define('AGGREGATOR_CLEAR_NEVER', 0);
  10. /**
  11. * Implements hook_help().
  12. */
  13. function aggregator_help($path, $arg) {
  14. switch ($path) {
  15. case 'admin/help#aggregator':
  16. $output = '';
  17. $output .= '<h3>' . t('About') . '</h3>';
  18. $output .= '<p>' . t('The Aggregator module is an on-site syndicator and news reader that gathers and displays 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 in feeds, using a number of standardized XML-based formats. For more information, see the online handbook entry for <a href="@aggregator-module">Aggregator module</a>.', array('@aggregator-module' => 'http://drupal.org/documentation/modules/aggregator', '@aggregator' => url('aggregator'))) . '</p>';
  19. $output .= '<h3>' . t('Uses') . '</h3>';
  20. $output .= '<dl>';
  21. $output .= '<dt>' . t('Viewing feeds') . '</dt>';
  22. $output .= '<dd>' . t('Feeds contain published content, and may be grouped in categories, generally by topic. Users view feed content in the <a href="@aggregator">main aggregator display</a>, or by <a href="@aggregator-sources">their source</a> (usually via an RSS feed reader). The most recent content in a feed or category can be displayed as a block through the <a href="@admin-block">Blocks administration page</a>.', array('@aggregator' => url('aggregator'), '@aggregator-sources' => url('aggregator/sources'), '@admin-block' => url('admin/structure/block'))) . '</a></dd>';
  23. $output .= '<dt>' . t('Adding, editing, and deleting feeds') . '</dt>';
  24. $output .= '<dd>' . t('Administrators can add, edit, and delete feeds, and choose how often to check each feed for newly updated items on the <a href="@feededit">Feed aggregator administration page</a>.', array('@feededit' => url('admin/config/services/aggregator'))) . '</dd>';
  25. $output .= '<dt>' . t('OPML integration') . '</dt>';
  26. $output .= '<dd>' . t('A <a href="@aggregator-opml">machine-readable OPML file</a> of all feeds is available. OPML is an XML-based file format used to share outline-structured information such as a list of RSS feeds. Feeds can also be <a href="@import-opml">imported via an OPML file</a>.', array('@aggregator-opml' => url('aggregator/opml'), '@import-opml' => url('admin/config/services/aggregator'))) . '</dd>';
  27. $output .= '<dt>' . t('Configuring cron') . '</dt>';
  28. $output .= '<dd>' . t('A correctly configured <a href="@cron">cron maintenance task</a> is required to update feeds automatically.', array('@cron' => 'http://drupal.org/cron')) . '</dd>';
  29. $output .= '</dl>';
  30. return $output;
  31. case 'admin/config/services/aggregator':
  32. $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>';
  33. $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/config/services/aggregator/add/feed'), '@block' => url('admin/structure/block'))) . '</p>';
  34. return $output;
  35. case 'admin/config/services/aggregator/add/feed':
  36. return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
  37. case 'admin/config/services/aggregator/add/category':
  38. 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>';
  39. case 'admin/config/services/aggregator/add/opml':
  40. 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>';
  41. }
  42. }
  43. /**
  44. * Implements hook_theme().
  45. */
  46. function aggregator_theme() {
  47. return array(
  48. 'aggregator_wrapper' => array(
  49. 'variables' => array('content' => NULL),
  50. 'file' => 'aggregator.pages.inc',
  51. 'template' => 'aggregator-wrapper',
  52. ),
  53. 'aggregator_categorize_items' => array(
  54. 'render element' => 'form',
  55. 'file' => 'aggregator.pages.inc',
  56. ),
  57. 'aggregator_feed_source' => array(
  58. 'variables' => array('feed' => NULL),
  59. 'file' => 'aggregator.pages.inc',
  60. 'template' => 'aggregator-feed-source',
  61. ),
  62. 'aggregator_block_item' => array(
  63. 'variables' => array('item' => NULL, 'feed' => 0),
  64. ),
  65. 'aggregator_summary_items' => array(
  66. 'variables' => array('summary_items' => NULL, 'source' => NULL),
  67. 'file' => 'aggregator.pages.inc',
  68. 'template' => 'aggregator-summary-items',
  69. ),
  70. 'aggregator_summary_item' => array(
  71. 'variables' => array('item' => NULL),
  72. 'file' => 'aggregator.pages.inc',
  73. 'template' => 'aggregator-summary-item',
  74. ),
  75. 'aggregator_item' => array(
  76. 'variables' => array('item' => NULL),
  77. 'file' => 'aggregator.pages.inc',
  78. 'template' => 'aggregator-item',
  79. ),
  80. 'aggregator_page_opml' => array(
  81. 'variables' => array('feeds' => NULL),
  82. 'file' => 'aggregator.pages.inc',
  83. ),
  84. 'aggregator_page_rss' => array(
  85. 'variables' => array('feeds' => NULL, 'category' => NULL),
  86. 'file' => 'aggregator.pages.inc',
  87. ),
  88. );
  89. }
  90. /**
  91. * Implements hook_menu().
  92. */
  93. function aggregator_menu() {
  94. $items['admin/config/services/aggregator'] = array(
  95. 'title' => 'Feed aggregator',
  96. 'description' => "Configure which content your site aggregates from other sites, how often it polls them, and how they're categorized.",
  97. 'page callback' => 'aggregator_admin_overview',
  98. 'access arguments' => array('administer news feeds'),
  99. 'weight' => 10,
  100. 'file' => 'aggregator.admin.inc',
  101. );
  102. $items['admin/config/services/aggregator/add/feed'] = array(
  103. 'title' => 'Add feed',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('aggregator_form_feed'),
  106. 'access arguments' => array('administer news feeds'),
  107. 'type' => MENU_LOCAL_ACTION,
  108. 'file' => 'aggregator.admin.inc',
  109. );
  110. $items['admin/config/services/aggregator/add/category'] = array(
  111. 'title' => 'Add category',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('aggregator_form_category'),
  114. 'access arguments' => array('administer news feeds'),
  115. 'type' => MENU_LOCAL_ACTION,
  116. 'file' => 'aggregator.admin.inc',
  117. );
  118. $items['admin/config/services/aggregator/add/opml'] = array(
  119. 'title' => 'Import OPML',
  120. 'page callback' => 'drupal_get_form',
  121. 'page arguments' => array('aggregator_form_opml'),
  122. 'access arguments' => array('administer news feeds'),
  123. 'type' => MENU_LOCAL_ACTION,
  124. 'file' => 'aggregator.admin.inc',
  125. );
  126. $items['admin/config/services/aggregator/remove/%aggregator_feed'] = array(
  127. 'title' => 'Remove items',
  128. 'page callback' => 'drupal_get_form',
  129. 'page arguments' => array('aggregator_admin_remove_feed', 5),
  130. 'access arguments' => array('administer news feeds'),
  131. 'file' => 'aggregator.admin.inc',
  132. );
  133. $items['admin/config/services/aggregator/update/%aggregator_feed'] = array(
  134. 'title' => 'Update items',
  135. 'page callback' => 'aggregator_admin_refresh_feed',
  136. 'page arguments' => array(5),
  137. 'access arguments' => array('administer news feeds'),
  138. 'file' => 'aggregator.admin.inc',
  139. );
  140. $items['admin/config/services/aggregator/list'] = array(
  141. 'title' => 'List',
  142. 'type' => MENU_DEFAULT_LOCAL_TASK,
  143. 'weight' => -10,
  144. );
  145. $items['admin/config/services/aggregator/settings'] = array(
  146. 'title' => 'Settings',
  147. 'description' => 'Configure the behavior of the feed aggregator, including when to discard feed items and how to present feed items and categories.',
  148. 'page callback' => 'drupal_get_form',
  149. 'page arguments' => array('aggregator_admin_form'),
  150. 'access arguments' => array('administer news feeds'),
  151. 'type' => MENU_LOCAL_TASK,
  152. 'file' => 'aggregator.admin.inc',
  153. );
  154. $items['aggregator'] = array(
  155. 'title' => 'Feed aggregator',
  156. 'page callback' => 'aggregator_page_last',
  157. 'access arguments' => array('access news feeds'),
  158. 'weight' => 5,
  159. 'file' => 'aggregator.pages.inc',
  160. );
  161. $items['aggregator/sources'] = array(
  162. 'title' => 'Sources',
  163. 'page callback' => 'aggregator_page_sources',
  164. 'access arguments' => array('access news feeds'),
  165. 'file' => 'aggregator.pages.inc',
  166. );
  167. $items['aggregator/categories'] = array(
  168. 'title' => 'Categories',
  169. 'page callback' => 'aggregator_page_categories',
  170. 'access callback' => '_aggregator_has_categories',
  171. 'file' => 'aggregator.pages.inc',
  172. );
  173. $items['aggregator/rss'] = array(
  174. 'title' => 'RSS feed',
  175. 'page callback' => 'aggregator_page_rss',
  176. 'access arguments' => array('access news feeds'),
  177. 'type' => MENU_CALLBACK,
  178. 'file' => 'aggregator.pages.inc',
  179. );
  180. $items['aggregator/opml'] = array(
  181. 'title' => 'OPML feed',
  182. 'page callback' => 'aggregator_page_opml',
  183. 'access arguments' => array('access news feeds'),
  184. 'type' => MENU_CALLBACK,
  185. 'file' => 'aggregator.pages.inc',
  186. );
  187. $items['aggregator/categories/%aggregator_category'] = array(
  188. 'title callback' => '_aggregator_category_title',
  189. 'title arguments' => array(2),
  190. 'page callback' => 'aggregator_page_category',
  191. 'page arguments' => array(2),
  192. 'access arguments' => array('access news feeds'),
  193. 'file' => 'aggregator.pages.inc',
  194. );
  195. $items['aggregator/categories/%aggregator_category/view'] = array(
  196. 'title' => 'View',
  197. 'type' => MENU_DEFAULT_LOCAL_TASK,
  198. 'weight' => -10,
  199. );
  200. $items['aggregator/categories/%aggregator_category/categorize'] = array(
  201. 'title' => 'Categorize',
  202. 'page callback' => 'drupal_get_form',
  203. 'page arguments' => array('aggregator_page_category_form', 2),
  204. 'access arguments' => array('administer news feeds'),
  205. 'type' => MENU_LOCAL_TASK,
  206. 'file' => 'aggregator.pages.inc',
  207. );
  208. $items['aggregator/categories/%aggregator_category/configure'] = array(
  209. 'title' => 'Configure',
  210. 'page callback' => 'drupal_get_form',
  211. 'page arguments' => array('aggregator_form_category', 2),
  212. 'access arguments' => array('administer news feeds'),
  213. 'type' => MENU_LOCAL_TASK,
  214. 'weight' => 1,
  215. 'file' => 'aggregator.admin.inc',
  216. );
  217. $items['aggregator/sources/%aggregator_feed'] = array(
  218. 'page callback' => 'aggregator_page_source',
  219. 'page arguments' => array(2),
  220. 'access arguments' => array('access news feeds'),
  221. 'file' => 'aggregator.pages.inc',
  222. );
  223. $items['aggregator/sources/%aggregator_feed/view'] = array(
  224. 'title' => 'View',
  225. 'type' => MENU_DEFAULT_LOCAL_TASK,
  226. 'weight' => -10,
  227. );
  228. $items['aggregator/sources/%aggregator_feed/categorize'] = array(
  229. 'title' => 'Categorize',
  230. 'page callback' => 'drupal_get_form',
  231. 'page arguments' => array('aggregator_page_source_form', 2),
  232. 'access arguments' => array('administer news feeds'),
  233. 'type' => MENU_LOCAL_TASK,
  234. 'file' => 'aggregator.pages.inc',
  235. );
  236. $items['aggregator/sources/%aggregator_feed/configure'] = array(
  237. 'title' => 'Configure',
  238. 'page callback' => 'drupal_get_form',
  239. 'page arguments' => array('aggregator_form_feed', 2),
  240. 'access arguments' => array('administer news feeds'),
  241. 'type' => MENU_LOCAL_TASK,
  242. 'weight' => 1,
  243. 'file' => 'aggregator.admin.inc',
  244. );
  245. $items['admin/config/services/aggregator/edit/feed/%aggregator_feed'] = array(
  246. 'title' => 'Edit feed',
  247. 'page callback' => 'drupal_get_form',
  248. 'page arguments' => array('aggregator_form_feed', 6),
  249. 'access arguments' => array('administer news feeds'),
  250. 'file' => 'aggregator.admin.inc',
  251. );
  252. $items['admin/config/services/aggregator/edit/category/%aggregator_category'] = array(
  253. 'title' => 'Edit category',
  254. 'page callback' => 'drupal_get_form',
  255. 'page arguments' => array('aggregator_form_category', 6),
  256. 'access arguments' => array('administer news feeds'),
  257. 'file' => 'aggregator.admin.inc',
  258. );
  259. return $items;
  260. }
  261. /**
  262. * Title callback: Returns a title for aggregator category pages.
  263. *
  264. * @param $category
  265. * An aggregator category.
  266. *
  267. * @return
  268. * A string with the aggregator category title.
  269. */
  270. function _aggregator_category_title($category) {
  271. return $category['title'];
  272. }
  273. /**
  274. * Determines whether there are any aggregator categories.
  275. *
  276. * @return
  277. * TRUE if there is at least one category and the user has access to them;
  278. * FALSE otherwise.
  279. */
  280. function _aggregator_has_categories() {
  281. return user_access('access news feeds') && (bool) db_query_range('SELECT 1 FROM {aggregator_category}', 0, 1)->fetchField();
  282. }
  283. /**
  284. * Implements hook_permission().
  285. */
  286. function aggregator_permission() {
  287. return array(
  288. 'administer news feeds' => array(
  289. 'title' => t('Administer news feeds'),
  290. ),
  291. 'access news feeds' => array(
  292. 'title' => t('View news feeds'),
  293. ),
  294. );
  295. }
  296. /**
  297. * Implements hook_cron().
  298. *
  299. * Queues news feeds for updates once their refresh interval has elapsed.
  300. */
  301. function aggregator_cron() {
  302. $result = db_query('SELECT * FROM {aggregator_feed} WHERE queued = 0 AND checked + refresh < :time AND refresh <> :never', array(
  303. ':time' => REQUEST_TIME,
  304. ':never' => AGGREGATOR_CLEAR_NEVER
  305. ));
  306. $queue = DrupalQueue::get('aggregator_feeds');
  307. foreach ($result as $feed) {
  308. if ($queue->createItem($feed)) {
  309. // Add timestamp to avoid queueing item more than once.
  310. db_update('aggregator_feed')
  311. ->fields(array('queued' => REQUEST_TIME))
  312. ->condition('fid', $feed->fid)
  313. ->execute();
  314. }
  315. }
  316. // Remove queued timestamp after 6 hours assuming the update has failed.
  317. db_update('aggregator_feed')
  318. ->fields(array('queued' => 0))
  319. ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
  320. ->execute();
  321. }
  322. /**
  323. * Implements hook_cron_queue_info().
  324. */
  325. function aggregator_cron_queue_info() {
  326. $queues['aggregator_feeds'] = array(
  327. 'worker callback' => 'aggregator_refresh',
  328. 'time' => 60,
  329. );
  330. return $queues;
  331. }
  332. /**
  333. * Implements hook_block_info().
  334. */
  335. function aggregator_block_info() {
  336. $blocks = array();
  337. $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
  338. foreach ($result as $category) {
  339. $blocks['category-' . $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
  340. }
  341. $result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
  342. foreach ($result as $feed) {
  343. $blocks['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
  344. }
  345. return $blocks;
  346. }
  347. /**
  348. * Implements hook_block_configure().
  349. */
  350. function aggregator_block_configure($delta = '') {
  351. list($type, $id) = explode('-', $delta);
  352. if ($type == 'category') {
  353. $value = db_query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
  354. $form['block'] = array(
  355. '#type' => 'select',
  356. '#title' => t('Number of news items in block'),
  357. '#default_value' => $value,
  358. '#options' => drupal_map_assoc(range(2, 20)),
  359. );
  360. return $form;
  361. }
  362. }
  363. /**
  364. * Implements hook_block_save().
  365. */
  366. function aggregator_block_save($delta = '', $edit = array()) {
  367. list($type, $id) = explode('-', $delta);
  368. if ($type == 'category') {
  369. db_update('aggregator_category')
  370. ->fields(array('block' => $edit['block']))
  371. ->condition('cid', $id)
  372. ->execute();
  373. }
  374. }
  375. /**
  376. * Implements hook_block_view().
  377. *
  378. * Generates blocks for the latest news items in each category and feed.
  379. */
  380. function aggregator_block_view($delta = '') {
  381. if (user_access('access news feeds')) {
  382. $block = array();
  383. list($type, $id) = explode('-', $delta);
  384. $result = FALSE;
  385. switch ($type) {
  386. case 'feed':
  387. if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
  388. $block['subject'] = check_plain($feed->title);
  389. $result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", 0, $feed->block, array(':fid' => $id));
  390. $read_more = theme('more_link', array('url' => 'aggregator/sources/' . $feed->fid, 'title' => t("View this feed's recent news.")));
  391. }
  392. break;
  393. case 'category':
  394. if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
  395. $block['subject'] = check_plain($category->title);
  396. $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', 0, $category->block, array(':cid' => $category->cid));
  397. $read_more = theme('more_link', array('url' => 'aggregator/categories/' . $category->cid, 'title' => t("View this category's recent news.")));
  398. }
  399. break;
  400. }
  401. $items = array();
  402. if (!empty($result)) {
  403. foreach ($result as $item) {
  404. $items[] = theme('aggregator_block_item', array('item' => $item));
  405. }
  406. }
  407. // Only display the block if there are items to show.
  408. if (count($items) > 0) {
  409. $block['content'] = theme('item_list', array('items' => $items)) . $read_more;
  410. }
  411. return $block;
  412. }
  413. }
  414. /**
  415. * Adds/edits/deletes aggregator categories.
  416. *
  417. * @param $edit
  418. * An associative array describing the category to be added/edited/deleted.
  419. */
  420. function aggregator_save_category($edit) {
  421. $link_path = 'aggregator/categories/';
  422. if (!empty($edit['cid'])) {
  423. $link_path .= $edit['cid'];
  424. if (!empty($edit['title'])) {
  425. db_merge('aggregator_category')
  426. ->key(array('cid' => $edit['cid']))
  427. ->fields(array(
  428. 'title' => $edit['title'],
  429. 'description' => $edit['description'],
  430. ))
  431. ->execute();
  432. $op = 'update';
  433. }
  434. else {
  435. db_delete('aggregator_category')
  436. ->condition('cid', $edit['cid'])
  437. ->execute();
  438. // Remove category from feeds.
  439. db_delete('aggregator_category_feed')
  440. ->condition('cid', $edit['cid'])
  441. ->execute();
  442. // Remove category from feed items.
  443. db_delete('aggregator_category_item')
  444. ->condition('cid', $edit['cid'])
  445. ->execute();
  446. // Make sure there is no active block for this category.
  447. if (module_exists('block')) {
  448. db_delete('block')
  449. ->condition('module', 'aggregator')
  450. ->condition('delta', 'category-' . $edit['cid'])
  451. ->execute();
  452. }
  453. $edit['title'] = '';
  454. $op = 'delete';
  455. }
  456. }
  457. elseif (!empty($edit['title'])) {
  458. // A single unique id for bundles and feeds, to use in blocks.
  459. $link_path .= db_insert('aggregator_category')
  460. ->fields(array(
  461. 'title' => $edit['title'],
  462. 'description' => $edit['description'],
  463. 'block' => 5,
  464. ))
  465. ->execute();
  466. $op = 'insert';
  467. }
  468. if (isset($op)) {
  469. menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
  470. }
  471. }
  472. /**
  473. * Add/edit/delete an aggregator feed.
  474. *
  475. * @param $edit
  476. * An associative array describing the feed to be added/edited/deleted.
  477. */
  478. function aggregator_save_feed($edit) {
  479. if (!empty($edit['fid'])) {
  480. // An existing feed is being modified, delete the category listings.
  481. db_delete('aggregator_category_feed')
  482. ->condition('fid', $edit['fid'])
  483. ->execute();
  484. }
  485. if (!empty($edit['fid']) && !empty($edit['title'])) {
  486. db_update('aggregator_feed')
  487. ->condition('fid', $edit['fid'])
  488. ->fields(array(
  489. 'title' => $edit['title'],
  490. 'url' => $edit['url'],
  491. 'refresh' => $edit['refresh'],
  492. 'block' => $edit['block'],
  493. ))
  494. ->execute();
  495. }
  496. elseif (!empty($edit['fid'])) {
  497. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid']))->fetchCol();
  498. if ($iids) {
  499. db_delete('aggregator_category_item')
  500. ->condition('iid', $iids, 'IN')
  501. ->execute();
  502. }
  503. db_delete('aggregator_feed')->
  504. condition('fid', $edit['fid'])
  505. ->execute();
  506. db_delete('aggregator_item')
  507. ->condition('fid', $edit['fid'])
  508. ->execute();
  509. // Make sure there is no active block for this feed.
  510. if (module_exists('block')) {
  511. db_delete('block')
  512. ->condition('module', 'aggregator')
  513. ->condition('delta', 'feed-' . $edit['fid'])
  514. ->execute();
  515. }
  516. }
  517. elseif (!empty($edit['title'])) {
  518. $edit['fid'] = db_insert('aggregator_feed')
  519. ->fields(array(
  520. 'title' => $edit['title'],
  521. 'url' => $edit['url'],
  522. 'refresh' => $edit['refresh'],
  523. 'block' => $edit['block'],
  524. 'link' => '',
  525. 'description' => '',
  526. 'image' => '',
  527. ))
  528. ->execute();
  529. }
  530. if (!empty($edit['title'])) {
  531. // The feed is being saved, save the categories as well.
  532. if (!empty($edit['category'])) {
  533. foreach ($edit['category'] as $cid => $value) {
  534. if ($value) {
  535. db_insert('aggregator_category_feed')
  536. ->fields(array(
  537. 'fid' => $edit['fid'],
  538. 'cid' => $cid,
  539. ))
  540. ->execute();
  541. }
  542. }
  543. }
  544. }
  545. }
  546. /**
  547. * Removes all items from a feed.
  548. *
  549. * @param $feed
  550. * An object describing the feed to be cleared.
  551. */
  552. function aggregator_remove($feed) {
  553. _aggregator_get_variables();
  554. // Call hook_aggregator_remove() on all modules.
  555. module_invoke_all('aggregator_remove', $feed);
  556. // Reset feed.
  557. db_update('aggregator_feed')
  558. ->condition('fid', $feed->fid)
  559. ->fields(array(
  560. 'checked' => 0,
  561. 'hash' => '',
  562. 'etag' => '',
  563. 'modified' => 0,
  564. ))
  565. ->execute();
  566. }
  567. /**
  568. * Gets the fetcher, parser, and processors.
  569. *
  570. * @return
  571. * An array containing the fetcher, parser, and processors.
  572. */
  573. function _aggregator_get_variables() {
  574. // Fetch the feed.
  575. $fetcher = variable_get('aggregator_fetcher', 'aggregator');
  576. if ($fetcher == 'aggregator') {
  577. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.fetcher.inc';
  578. }
  579. $parser = variable_get('aggregator_parser', 'aggregator');
  580. if ($parser == 'aggregator') {
  581. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.parser.inc';
  582. }
  583. $processors = variable_get('aggregator_processors', array('aggregator'));
  584. if (in_array('aggregator', $processors)) {
  585. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.processor.inc';
  586. }
  587. return array($fetcher, $parser, $processors);
  588. }
  589. /**
  590. * Checks a news feed for new items.
  591. *
  592. * @param $feed
  593. * An object describing the feed to be refreshed.
  594. */
  595. function aggregator_refresh($feed) {
  596. // Store feed URL to track changes.
  597. $feed_url = $feed->url;
  598. // Fetch the feed.
  599. list($fetcher, $parser, $processors) = _aggregator_get_variables();
  600. $success = module_invoke($fetcher, 'aggregator_fetch', $feed);
  601. // We store the hash of feed data in the database. When refreshing a
  602. // feed we compare stored hash and new hash calculated from downloaded
  603. // data. If both are equal we say that feed is not updated.
  604. $hash = hash('sha256', $feed->source_string);
  605. if ($success && ($feed->hash != $hash)) {
  606. // Parse the feed.
  607. if (module_invoke($parser, 'aggregator_parse', $feed)) {
  608. // Update feed with parsed data.
  609. db_merge('aggregator_feed')
  610. ->key(array('fid' => $feed->fid))
  611. ->fields(array(
  612. 'url' => $feed->url,
  613. 'link' => empty($feed->link) ? $feed->url : $feed->link,
  614. 'description' => empty($feed->description) ? '' : $feed->description,
  615. 'image' => empty($feed->image) ? '' : $feed->image,
  616. 'hash' => $hash,
  617. 'etag' => empty($feed->etag) ? '' : $feed->etag,
  618. 'modified' => empty($feed->modified) ? 0 : $feed->modified,
  619. ))
  620. ->execute();
  621. // Log if feed URL has changed.
  622. if ($feed->url != $feed_url) {
  623. watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->title, '%url' => $feed->url));
  624. }
  625. watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed->title));
  626. drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed->title)));
  627. // If there are items on the feed, let all enabled processors do their work on it.
  628. if (@count($feed->items)) {
  629. foreach ($processors as $processor) {
  630. module_invoke($processor, 'aggregator_process', $feed);
  631. }
  632. }
  633. }
  634. }
  635. else {
  636. drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
  637. }
  638. // Regardless of successful or not, indicate that this feed has been checked.
  639. db_update('aggregator_feed')
  640. ->fields(array('checked' => REQUEST_TIME, 'queued' => 0))
  641. ->condition('fid', $feed->fid)
  642. ->execute();
  643. // Expire old feed items.
  644. if (function_exists('aggregator_expire')) {
  645. aggregator_expire($feed);
  646. }
  647. }
  648. /**
  649. * Loads an aggregator feed.
  650. *
  651. * @param $fid
  652. * The feed id.
  653. *
  654. * @return
  655. * An object describing the feed.
  656. */
  657. function aggregator_feed_load($fid) {
  658. $feeds = &drupal_static(__FUNCTION__);
  659. if (!isset($feeds[$fid])) {
  660. $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
  661. }
  662. return $feeds[$fid];
  663. }
  664. /**
  665. * Loads an aggregator category.
  666. *
  667. * @param $cid
  668. * The category id.
  669. *
  670. * @return
  671. * An associative array describing the category.
  672. */
  673. function aggregator_category_load($cid) {
  674. $categories = &drupal_static(__FUNCTION__);
  675. if (!isset($categories[$cid])) {
  676. $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
  677. }
  678. return $categories[$cid];
  679. }
  680. /**
  681. * Returns HTML for an individual feed item for display in the block.
  682. *
  683. * @param $variables
  684. * An associative array containing:
  685. * - item: The item to be displayed.
  686. * - feed: Not used.
  687. *
  688. * @ingroup themeable
  689. */
  690. function theme_aggregator_block_item($variables) {
  691. // Display the external link to the item.
  692. return '<a href="' . check_url($variables['item']->link) . '">' . check_plain($variables['item']->title) . "</a>\n";
  693. }
  694. /**
  695. * Renders the HTML content safely, as allowed.
  696. *
  697. * @param $value
  698. * The content to be filtered.
  699. *
  700. * @return
  701. * The filtered content.
  702. */
  703. function aggregator_filter_xss($value) {
  704. 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));
  705. }
  706. /**
  707. * Checks and sanitizes the aggregator configuration.
  708. *
  709. * Goes through all fetchers, parsers and processors and checks whether they
  710. * are available. If one is missing, resets to standard configuration.
  711. *
  712. * @return
  713. * TRUE if this function resets the configuration; FALSE if not.
  714. */
  715. function aggregator_sanitize_configuration() {
  716. $reset = FALSE;
  717. list($fetcher, $parser, $processors) = _aggregator_get_variables();
  718. if (!module_exists($fetcher)) {
  719. $reset = TRUE;
  720. }
  721. if (!module_exists($parser)) {
  722. $reset = TRUE;
  723. }
  724. foreach ($processors as $processor) {
  725. if (!module_exists($processor)) {
  726. $reset = TRUE;
  727. break;
  728. }
  729. }
  730. if ($reset) {
  731. variable_del('aggregator_fetcher');
  732. variable_del('aggregator_parser');
  733. variable_del('aggregator_processors');
  734. return TRUE;
  735. }
  736. return FALSE;
  737. }
  738. /**
  739. * Helper function for drupal_map_assoc.
  740. *
  741. * @param $count
  742. * Items count.
  743. *
  744. * @return
  745. * A string that is plural-formatted as "@count items".
  746. */
  747. function _aggregator_items($count) {
  748. return format_plural($count, '1 item', '@count items');
  749. }

Functions

Namesort descending Description
aggregator_block_configure Implements hook_block_configure().
aggregator_block_info Implements hook_block_info().
aggregator_block_save Implements hook_block_save().
aggregator_block_view Implements hook_block_view().
aggregator_category_load Loads an aggregator category.
aggregator_cron Implements hook_cron().
aggregator_cron_queue_info Implements hook_cron_queue_info().
aggregator_feed_load Loads an aggregator feed.
aggregator_filter_xss Renders the HTML content safely, as allowed.
aggregator_help Implements hook_help().
aggregator_menu Implements hook_menu().
aggregator_permission Implements hook_permission().
aggregator_refresh Checks a news feed for new items.
aggregator_remove Removes all items from a feed.
aggregator_sanitize_configuration Checks and sanitizes the aggregator configuration.
aggregator_save_category Adds/edits/deletes aggregator categories.
aggregator_save_feed Add/edit/delete an aggregator feed.
aggregator_theme Implements hook_theme().
theme_aggregator_block_item Returns HTML for an individual feed item for display in the block.
_aggregator_category_title Title callback: Returns a title for aggregator category pages.
_aggregator_get_variables Gets the fetcher, parser, and processors.
_aggregator_has_categories Determines whether there are any aggregator categories.
_aggregator_items Helper function for drupal_map_assoc.

Constants

Namesort descending Description
AGGREGATOR_CLEAR_NEVER Denotes that a feed's items should never expire.