aggregator.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. }