Skip navigation
Help

aggregator.module

  1. drupal
    1. 6 drupal/modules/aggregator/aggregator.module
    2. 7 drupal/modules/aggregator/aggregator.module

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

Functions & methods

NameDescription
aggregator_block_configureImplements hook_block_configure().
aggregator_block_infoImplements hook_block_info().
aggregator_block_saveImplements hook_block_save().
aggregator_block_viewImplements hook_block_view().
aggregator_category_loadLoad an aggregator category.
aggregator_cronImplements hook_cron().
aggregator_cron_queue_infoImplements hook_cron_queue_info().
aggregator_feed_loadLoad an aggregator feed.
aggregator_filter_xssSafely render HTML content, as allowed.
aggregator_helpImplements hook_help().
aggregator_menuImplements hook_menu().
aggregator_permissionImplements hook_permission().
aggregator_refreshChecks a news feed for new items.
aggregator_removeRemoves all items from a feed.
aggregator_sanitize_configurationCheck and sanitize aggregator configuration.
aggregator_save_categoryAdd/edit/delete aggregator categories.
aggregator_save_feedAdd/edit/delete an aggregator feed.
aggregator_themeImplements hook_theme().
theme_aggregator_block_itemReturns HTML for an individual feed item for display in the block.
_aggregator_category_titleTitle callback for aggregatory category pages.
_aggregator_get_variables
_aggregator_has_categoriesFind out whether there are any aggregator categories.
_aggregator_itemsHelper function for drupal_map_assoc.

Constants

NameDescription
AGGREGATOR_CLEAR_NEVERDenotes that a feed's items should never expire.

File

drupal/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/handbook/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 for aggregatory category pages.
  263. *
  264. * @return
  265. * An aggregator category title.
  266. */
  267. function _aggregator_category_title($category) {
  268. return $category['title'];
  269. }
  270. /**
  271. * Find out whether there are any aggregator categories.
  272. *
  273. * @return
  274. * TRUE if there is at least one category and the user has access to them, FALSE
  275. * otherwise.
  276. */
  277. function _aggregator_has_categories() {
  278. return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
  279. }
  280. /**
  281. * Implements hook_permission().
  282. */
  283. function aggregator_permission() {
  284. return array(
  285. 'administer news feeds' => array(
  286. 'title' => t('Administer news feeds'),
  287. ),
  288. 'access news feeds' => array(
  289. 'title' => t('View news feeds'),
  290. ),
  291. );
  292. }
  293. /**
  294. * Implements hook_cron().
  295. *
  296. * Queues news feeds for updates once their refresh interval has elapsed.
  297. */
  298. function aggregator_cron() {
  299. $result = db_query('SELECT * FROM {aggregator_feed} WHERE queued = 0 AND checked + refresh < :time AND refresh <> :never', array(
  300. ':time' => REQUEST_TIME,
  301. ':never' => AGGREGATOR_CLEAR_NEVER
  302. ));
  303. $queue = DrupalQueue::get('aggregator_feeds');
  304. foreach ($result as $feed) {
  305. if ($queue->createItem($feed)) {
  306. // Add timestamp to avoid queueing item more than once.
  307. db_update('aggregator_feed')
  308. ->fields(array('queued' => REQUEST_TIME))
  309. ->condition('fid', $feed->fid)
  310. ->execute();
  311. }
  312. }
  313. // Remove queued timestamp after 6 hours assuming the update has failed.
  314. db_update('aggregator_feed')
  315. ->fields(array('queued' => 0))
  316. ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
  317. ->execute();
  318. }
  319. /**
  320. * Implements hook_cron_queue_info().
  321. */
  322. function aggregator_cron_queue_info() {
  323. $queues['aggregator_feeds'] = array(
  324. 'worker callback' => 'aggregator_refresh',
  325. 'time' => 60,
  326. );
  327. return $queues;
  328. }
  329. /**
  330. * Implements hook_block_info().
  331. */
  332. function aggregator_block_info() {
  333. $blocks = array();
  334. $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
  335. foreach ($result as $category) {
  336. $blocks['category-' . $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
  337. }
  338. $result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
  339. foreach ($result as $feed) {
  340. $blocks['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
  341. }
  342. return $blocks;
  343. }
  344. /**
  345. * Implements hook_block_configure().
  346. */
  347. function aggregator_block_configure($delta = '') {
  348. list($type, $id) = explode('-', $delta);
  349. if ($type == 'category') {
  350. $value = db_query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
  351. $form['block'] = array(
  352. '#type' => 'select',
  353. '#title' => t('Number of news items in block'),
  354. '#default_value' => $value,
  355. '#options' => drupal_map_assoc(range(2, 20)),
  356. );
  357. return $form;
  358. }
  359. }
  360. /**
  361. * Implements hook_block_save().
  362. */
  363. function aggregator_block_save($delta = '', $edit = array()) {
  364. list($type, $id) = explode('-', $delta);
  365. if ($type == 'category') {
  366. db_update('aggregator_category')
  367. ->fields(array('block' => $edit['block']))
  368. ->condition('cid', $id)
  369. ->execute();
  370. }
  371. }
  372. /**
  373. * Implements hook_block_view().
  374. *
  375. * Generates blocks for the latest news items in each category and feed.
  376. */
  377. function aggregator_block_view($delta = '') {
  378. if (user_access('access news feeds')) {
  379. $block = array();
  380. list($type, $id) = explode('-', $delta);
  381. switch ($type) {
  382. case 'feed':
  383. if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
  384. $block['subject'] = check_plain($feed->title);
  385. $result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", 0, $feed->block, array(':fid' => $id));
  386. $read_more = theme('more_link', array('url' => 'aggregator/sources/' . $feed->fid, 'title' => t("View this feed's recent news.")));
  387. }
  388. break;
  389. case 'category':
  390. if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
  391. $block['subject'] = check_plain($category->title);
  392. $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));
  393. $read_more = theme('more_link', array('url' => 'aggregator/categories/' . $category->cid, 'title' => t("View this category's recent news.")));
  394. }
  395. break;
  396. }
  397. $items = array();
  398. foreach ($result as $item) {
  399. $items[] = theme('aggregator_block_item', array('item' => $item));
  400. }
  401. // Only display the block if there are items to show.
  402. if (count($items) > 0) {
  403. $block['content'] = theme('item_list', array('items' => $items)) . $read_more;
  404. }
  405. return $block;
  406. }
  407. }
  408. /**
  409. * Add/edit/delete aggregator categories.
  410. *
  411. * @param $edit
  412. * An associative array describing the category to be added/edited/deleted.
  413. */
  414. function aggregator_save_category($edit) {
  415. $link_path = 'aggregator/categories/';
  416. if (!empty($edit['cid'])) {
  417. $link_path .= $edit['cid'];
  418. if (!empty($edit['title'])) {
  419. db_merge('aggregator_category')
  420. ->key(array('cid' => $edit['cid']))
  421. ->fields(array(
  422. 'title' => $edit['title'],
  423. 'description' => $edit['description'],
  424. ))
  425. ->execute();
  426. $op = 'update';
  427. }
  428. else {
  429. db_delete('aggregator_category')
  430. ->condition('cid', $edit['cid'])
  431. ->execute();
  432. // Make sure there is no active block for this category.
  433. db_delete('block')
  434. ->condition('module', 'aggregator')
  435. ->condition('delta', 'category-' . $edit['cid'])
  436. ->execute();
  437. $edit['title'] = '';
  438. $op = 'delete';
  439. }
  440. }
  441. elseif (!empty($edit['title'])) {
  442. // A single unique id for bundles and feeds, to use in blocks.
  443. $link_path .= db_insert('aggregator_category')
  444. ->fields(array(
  445. 'title' => $edit['title'],
  446. 'description' => $edit['description'],
  447. 'block' => 5,
  448. ))
  449. ->execute();
  450. $op = 'insert';
  451. }
  452. if (isset($op)) {
  453. menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
  454. }
  455. }
  456. /**
  457. * Add/edit/delete an aggregator feed.
  458. *
  459. * @param $edit
  460. * An associative array describing the feed to be added/edited/deleted.
  461. */
  462. function aggregator_save_feed($edit) {
  463. if (!empty($edit['fid'])) {
  464. // An existing feed is being modified, delete the category listings.
  465. db_delete('aggregator_category_feed')
  466. ->condition('fid', $edit['fid'])
  467. ->execute();
  468. }
  469. if (!empty($edit['fid']) && !empty($edit['title'])) {
  470. db_update('aggregator_feed')
  471. ->condition('fid', $edit['fid'])
  472. ->fields(array(
  473. 'title' => $edit['title'],
  474. 'url' => $edit['url'],
  475. 'refresh' => $edit['refresh'],
  476. 'block' => $edit['block'],
  477. ))
  478. ->execute();
  479. }
  480. elseif (!empty($edit['fid'])) {
  481. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid']))->fetchCol();
  482. if ($iids) {
  483. db_delete('aggregator_category_item')
  484. ->condition('iid', $iids, 'IN')
  485. ->execute();
  486. }
  487. db_delete('aggregator_feed')->
  488. condition('fid', $edit['fid'])
  489. ->execute();
  490. db_delete('aggregator_item')
  491. ->condition('fid', $edit['fid'])
  492. ->execute();
  493. // Make sure there is no active block for this feed.
  494. db_delete('block')
  495. ->condition('module', 'aggregator')
  496. ->condition('delta', 'feed-' . $edit['fid'])
  497. ->execute();
  498. }
  499. elseif (!empty($edit['title'])) {
  500. $edit['fid'] = db_insert('aggregator_feed')
  501. ->fields(array(
  502. 'title' => $edit['title'],
  503. 'url' => $edit['url'],
  504. 'refresh' => $edit['refresh'],
  505. 'block' => $edit['block'],
  506. 'description' => '',
  507. 'image' => '',
  508. ))
  509. ->execute();
  510. }
  511. if (!empty($edit['title'])) {
  512. // The feed is being saved, save the categories as well.
  513. if (!empty($edit['category'])) {
  514. foreach ($edit['category'] as $cid => $value) {
  515. if ($value) {
  516. db_insert('aggregator_category_feed')
  517. ->fields(array(
  518. 'fid' => $edit['fid'],
  519. 'cid' => $cid,
  520. ))
  521. ->execute();
  522. }
  523. }
  524. }
  525. }
  526. }
  527. /**
  528. * Removes all items from a feed.
  529. *
  530. * @param $feed
  531. * An object describing the feed to be cleared.
  532. */
  533. function aggregator_remove($feed) {
  534. _aggregator_get_variables();
  535. // Call hook_aggregator_remove() on all modules.
  536. module_invoke_all('aggregator_remove', $feed);
  537. // Reset feed.
  538. db_merge('aggregator_feed')
  539. ->key(array('fid' => $feed->fid))
  540. ->fields(array(
  541. 'checked' => 0,
  542. 'hash' => '',
  543. 'etag' => '',
  544. 'modified' => 0,
  545. 'description' => $feed->description,
  546. 'image' => $feed->image,
  547. ))
  548. ->execute();
  549. }
  550. function _aggregator_get_variables() {
  551. // Fetch the feed.
  552. $fetcher = variable_get('aggregator_fetcher', 'aggregator');
  553. if ($fetcher == 'aggregator') {
  554. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.fetcher.inc';
  555. }
  556. $parser = variable_get('aggregator_parser', 'aggregator');
  557. if ($parser == 'aggregator') {
  558. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.parser.inc';
  559. }
  560. $processors = variable_get('aggregator_processors', array('aggregator'));
  561. if (in_array('aggregator', $processors)) {
  562. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.processor.inc';
  563. }
  564. return array($fetcher, $parser, $processors);
  565. }
  566. /**
  567. * Checks a news feed for new items.
  568. *
  569. * @param $feed
  570. * An object describing the feed to be refreshed.
  571. */
  572. function aggregator_refresh($feed) {
  573. // Store feed URL to track changes.
  574. $feed_url = $feed->url;
  575. // Fetch the feed.
  576. list($fetcher, $parser, $processors) = _aggregator_get_variables();
  577. $success = module_invoke($fetcher, 'aggregator_fetch', $feed);
  578. // We store the hash of feed data in the database. When refreshing a
  579. // feed we compare stored hash and new hash calculated from downloaded
  580. // data. If both are equal we say that feed is not updated.
  581. $hash = hash('sha256', $feed->source_string);
  582. if ($success && ($feed->hash != $hash)) {
  583. // Parse the feed.
  584. if (module_invoke($parser, 'aggregator_parse', $feed)) {
  585. // Update feed with parsed data.
  586. db_merge('aggregator_feed')
  587. ->key(array('fid' => $feed->fid))
  588. ->fields(array(
  589. 'url' => $feed->url,
  590. 'link' => empty($feed->link) ? $feed->url : $feed->link,
  591. 'description' => empty($feed->description) ? '' : $feed->description,
  592. 'image' => empty($feed->image) ? '' : $feed->image,
  593. 'hash' => $hash,
  594. 'etag' => empty($feed->etag) ? '' : $feed->etag,
  595. 'modified' => empty($feed->modified) ? 0 : $feed->modified,
  596. ))
  597. ->execute();
  598. // Log if feed URL has changed.
  599. if ($feed->url != $feed_url) {
  600. watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->title, '%url' => $feed->url));
  601. }
  602. watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed->title));
  603. drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed->title)));
  604. // If there are items on the feed, let all enabled processors do their work on it.
  605. if (@count($feed->items)) {
  606. foreach ($processors as $processor) {
  607. module_invoke($processor, 'aggregator_process', $feed);
  608. }
  609. }
  610. }
  611. }
  612. else {
  613. drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
  614. }
  615. // Regardless of successful or not, indicate that this feed has been checked.
  616. db_update('aggregator_feed')
  617. ->fields(array('checked' => REQUEST_TIME, 'queued' => 0))
  618. ->condition('fid', $feed->fid)
  619. ->execute();
  620. // Expire old feed items.
  621. if (function_exists('aggregator_expire')) {
  622. aggregator_expire($feed);
  623. }
  624. }
  625. /**
  626. * Load an aggregator feed.
  627. *
  628. * @param $fid
  629. * The feed id.
  630. * @return
  631. * An object describing the feed.
  632. */
  633. function aggregator_feed_load($fid) {
  634. $feeds = &drupal_static(__FUNCTION__);
  635. if (!isset($feeds[$fid])) {
  636. $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
  637. }
  638. return $feeds[$fid];
  639. }
  640. /**
  641. * Load an aggregator category.
  642. *
  643. * @param $cid
  644. * The category id.
  645. * @return
  646. * An associative array describing the category.
  647. */
  648. function aggregator_category_load($cid) {
  649. $categories = &drupal_static(__FUNCTION__);
  650. if (!isset($categories[$cid])) {
  651. $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
  652. }
  653. return $categories[$cid];
  654. }
  655. /**
  656. * Returns HTML for an individual feed item for display in the block.
  657. *
  658. * @param $variables
  659. * An associative array containing:
  660. * - item: The item to be displayed.
  661. * - feed: Not used.
  662. *
  663. * @ingroup themeable
  664. */
  665. function theme_aggregator_block_item($variables) {
  666. // Display the external link to the item.
  667. return '<a href="' . check_url($variables['item']->link) . '">' . check_plain($variables['item']->title) . "</a>\n";
  668. }
  669. /**
  670. * Safely render HTML content, as allowed.
  671. *
  672. * @param $value
  673. * The content to be filtered.
  674. * @return
  675. * The filtered content.
  676. */
  677. function aggregator_filter_xss($value) {
  678. 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));
  679. }
  680. /**
  681. * Check and sanitize aggregator configuration.
  682. *
  683. * Goes through all fetchers, parsers and processors and checks whether they are
  684. * available.
  685. * If one is missing resets to standard configuration.
  686. *
  687. * @return
  688. * TRUE if this function reset the configuration FALSE if not.
  689. */
  690. function aggregator_sanitize_configuration() {
  691. $reset = FALSE;
  692. list($fetcher, $parser, $processors) = _aggregator_get_variables();
  693. if (!module_exists($fetcher)) {
  694. $reset = TRUE;
  695. }
  696. if (!module_exists($parser)) {
  697. $reset = TRUE;
  698. }
  699. foreach ($processors as $processor) {
  700. if (!module_exists($processor)) {
  701. $reset = TRUE;
  702. break;
  703. }
  704. }
  705. if ($reset) {
  706. variable_del('aggregator_fetcher');
  707. variable_del('aggregator_parser');
  708. variable_del('aggregator_processors');
  709. return TRUE;
  710. }
  711. return FALSE;
  712. }
  713. /**
  714. * Helper function for drupal_map_assoc.
  715. *
  716. * @param $count
  717. * Items count.
  718. * @return
  719. * Plural-formatted "@count items"
  720. */
  721. function _aggregator_items($count) {
  722. return format_plural($count, '1 item', '@count items');
  723. }