aggregator.module 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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 aggregatory category pages.
  263. *
  264. * @param $category
  265. * An aggregator category.
  266. *
  267. * @return
  268. * An 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. // Make sure there is no active block for this category.
  439. if (module_exists('block')) {
  440. db_delete('block')
  441. ->condition('module', 'aggregator')
  442. ->condition('delta', 'category-' . $edit['cid'])
  443. ->execute();
  444. }
  445. $edit['title'] = '';
  446. $op = 'delete';
  447. }
  448. }
  449. elseif (!empty($edit['title'])) {
  450. // A single unique id for bundles and feeds, to use in blocks.
  451. $link_path .= db_insert('aggregator_category')
  452. ->fields(array(
  453. 'title' => $edit['title'],
  454. 'description' => $edit['description'],
  455. 'block' => 5,
  456. ))
  457. ->execute();
  458. $op = 'insert';
  459. }
  460. if (isset($op)) {
  461. menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
  462. }
  463. }
  464. /**
  465. * Add/edit/delete an aggregator feed.
  466. *
  467. * @param $edit
  468. * An associative array describing the feed to be added/edited/deleted.
  469. */
  470. function aggregator_save_feed($edit) {
  471. if (!empty($edit['fid'])) {
  472. // An existing feed is being modified, delete the category listings.
  473. db_delete('aggregator_category_feed')
  474. ->condition('fid', $edit['fid'])
  475. ->execute();
  476. }
  477. if (!empty($edit['fid']) && !empty($edit['title'])) {
  478. db_update('aggregator_feed')
  479. ->condition('fid', $edit['fid'])
  480. ->fields(array(
  481. 'title' => $edit['title'],
  482. 'url' => $edit['url'],
  483. 'refresh' => $edit['refresh'],
  484. 'block' => $edit['block'],
  485. ))
  486. ->execute();
  487. }
  488. elseif (!empty($edit['fid'])) {
  489. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid']))->fetchCol();
  490. if ($iids) {
  491. db_delete('aggregator_category_item')
  492. ->condition('iid', $iids, 'IN')
  493. ->execute();
  494. }
  495. db_delete('aggregator_feed')->
  496. condition('fid', $edit['fid'])
  497. ->execute();
  498. db_delete('aggregator_item')
  499. ->condition('fid', $edit['fid'])
  500. ->execute();
  501. // Make sure there is no active block for this feed.
  502. if (module_exists('block')) {
  503. db_delete('block')
  504. ->condition('module', 'aggregator')
  505. ->condition('delta', 'feed-' . $edit['fid'])
  506. ->execute();
  507. }
  508. }
  509. elseif (!empty($edit['title'])) {
  510. $edit['fid'] = db_insert('aggregator_feed')
  511. ->fields(array(
  512. 'title' => $edit['title'],
  513. 'url' => $edit['url'],
  514. 'refresh' => $edit['refresh'],
  515. 'block' => $edit['block'],
  516. 'link' => '',
  517. 'description' => '',
  518. 'image' => '',
  519. ))
  520. ->execute();
  521. }
  522. if (!empty($edit['title'])) {
  523. // The feed is being saved, save the categories as well.
  524. if (!empty($edit['category'])) {
  525. foreach ($edit['category'] as $cid => $value) {
  526. if ($value) {
  527. db_insert('aggregator_category_feed')
  528. ->fields(array(
  529. 'fid' => $edit['fid'],
  530. 'cid' => $cid,
  531. ))
  532. ->execute();
  533. }
  534. }
  535. }
  536. }
  537. }
  538. /**
  539. * Removes all items from a feed.
  540. *
  541. * @param $feed
  542. * An object describing the feed to be cleared.
  543. */
  544. function aggregator_remove($feed) {
  545. _aggregator_get_variables();
  546. // Call hook_aggregator_remove() on all modules.
  547. module_invoke_all('aggregator_remove', $feed);
  548. // Reset feed.
  549. db_update('aggregator_feed')
  550. ->condition('fid', $feed->fid)
  551. ->fields(array(
  552. 'checked' => 0,
  553. 'hash' => '',
  554. 'etag' => '',
  555. 'modified' => 0,
  556. ))
  557. ->execute();
  558. }
  559. /**
  560. * Gets the fetcher, parser, and processors.
  561. *
  562. * @return
  563. * An array containing the fetcher, parser, and processors.
  564. */
  565. function _aggregator_get_variables() {
  566. // Fetch the feed.
  567. $fetcher = variable_get('aggregator_fetcher', 'aggregator');
  568. if ($fetcher == 'aggregator') {
  569. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.fetcher.inc';
  570. }
  571. $parser = variable_get('aggregator_parser', 'aggregator');
  572. if ($parser == 'aggregator') {
  573. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.parser.inc';
  574. }
  575. $processors = variable_get('aggregator_processors', array('aggregator'));
  576. if (in_array('aggregator', $processors)) {
  577. include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.processor.inc';
  578. }
  579. return array($fetcher, $parser, $processors);
  580. }
  581. /**
  582. * Checks a news feed for new items.
  583. *
  584. * @param $feed
  585. * An object describing the feed to be refreshed.
  586. */
  587. function aggregator_refresh($feed) {
  588. // Store feed URL to track changes.
  589. $feed_url = $feed->url;
  590. // Fetch the feed.
  591. list($fetcher, $parser, $processors) = _aggregator_get_variables();
  592. $success = module_invoke($fetcher, 'aggregator_fetch', $feed);
  593. // We store the hash of feed data in the database. When refreshing a
  594. // feed we compare stored hash and new hash calculated from downloaded
  595. // data. If both are equal we say that feed is not updated.
  596. $hash = hash('sha256', $feed->source_string);
  597. if ($success && ($feed->hash != $hash)) {
  598. // Parse the feed.
  599. if (module_invoke($parser, 'aggregator_parse', $feed)) {
  600. // Update feed with parsed data.
  601. db_merge('aggregator_feed')
  602. ->key(array('fid' => $feed->fid))
  603. ->fields(array(
  604. 'url' => $feed->url,
  605. 'link' => empty($feed->link) ? $feed->url : $feed->link,
  606. 'description' => empty($feed->description) ? '' : $feed->description,
  607. 'image' => empty($feed->image) ? '' : $feed->image,
  608. 'hash' => $hash,
  609. 'etag' => empty($feed->etag) ? '' : $feed->etag,
  610. 'modified' => empty($feed->modified) ? 0 : $feed->modified,
  611. ))
  612. ->execute();
  613. // Log if feed URL has changed.
  614. if ($feed->url != $feed_url) {
  615. watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->title, '%url' => $feed->url));
  616. }
  617. watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed->title));
  618. drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed->title)));
  619. // If there are items on the feed, let all enabled processors do their work on it.
  620. if (@count($feed->items)) {
  621. foreach ($processors as $processor) {
  622. module_invoke($processor, 'aggregator_process', $feed);
  623. }
  624. }
  625. }
  626. }
  627. else {
  628. drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed->title)));
  629. }
  630. // Regardless of successful or not, indicate that this feed has been checked.
  631. db_update('aggregator_feed')
  632. ->fields(array('checked' => REQUEST_TIME, 'queued' => 0))
  633. ->condition('fid', $feed->fid)
  634. ->execute();
  635. // Expire old feed items.
  636. if (function_exists('aggregator_expire')) {
  637. aggregator_expire($feed);
  638. }
  639. }
  640. /**
  641. * Loads an aggregator feed.
  642. *
  643. * @param $fid
  644. * The feed id.
  645. *
  646. * @return
  647. * An object describing the feed.
  648. */
  649. function aggregator_feed_load($fid) {
  650. $feeds = &drupal_static(__FUNCTION__);
  651. if (!isset($feeds[$fid])) {
  652. $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
  653. }
  654. return $feeds[$fid];
  655. }
  656. /**
  657. * Loads an aggregator category.
  658. *
  659. * @param $cid
  660. * The category id.
  661. *
  662. * @return
  663. * An associative array describing the category.
  664. */
  665. function aggregator_category_load($cid) {
  666. $categories = &drupal_static(__FUNCTION__);
  667. if (!isset($categories[$cid])) {
  668. $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
  669. }
  670. return $categories[$cid];
  671. }
  672. /**
  673. * Returns HTML for an individual feed item for display in the block.
  674. *
  675. * @param $variables
  676. * An associative array containing:
  677. * - item: The item to be displayed.
  678. * - feed: Not used.
  679. *
  680. * @ingroup themeable
  681. */
  682. function theme_aggregator_block_item($variables) {
  683. // Display the external link to the item.
  684. return '<a href="' . check_url($variables['item']->link) . '">' . check_plain($variables['item']->title) . "</a>\n";
  685. }
  686. /**
  687. * Safely renders HTML content, as allowed.
  688. *
  689. * @param $value
  690. * The content to be filtered.
  691. *
  692. * @return
  693. * The filtered content.
  694. */
  695. function aggregator_filter_xss($value) {
  696. 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));
  697. }
  698. /**
  699. * Checks and sanitizes the aggregator configuration.
  700. *
  701. * Goes through all fetchers, parsers and processors and checks whether they
  702. * are available. If one is missing resets to standard configuration.
  703. *
  704. * @return
  705. * TRUE if this function resets the configuration; FALSE if not.
  706. */
  707. function aggregator_sanitize_configuration() {
  708. $reset = FALSE;
  709. list($fetcher, $parser, $processors) = _aggregator_get_variables();
  710. if (!module_exists($fetcher)) {
  711. $reset = TRUE;
  712. }
  713. if (!module_exists($parser)) {
  714. $reset = TRUE;
  715. }
  716. foreach ($processors as $processor) {
  717. if (!module_exists($processor)) {
  718. $reset = TRUE;
  719. break;
  720. }
  721. }
  722. if ($reset) {
  723. variable_del('aggregator_fetcher');
  724. variable_del('aggregator_parser');
  725. variable_del('aggregator_processors');
  726. return TRUE;
  727. }
  728. return FALSE;
  729. }
  730. /**
  731. * Helper function for drupal_map_assoc.
  732. *
  733. * @param $count
  734. * Items count.
  735. *
  736. * @return
  737. * Plural-formatted "@count items"
  738. */
  739. function _aggregator_items($count) {
  740. return format_plural($count, '1 item', '@count items');
  741. }