aggregator.module

You are here

  1. 7 modules/aggregator/aggregator.module
  2. 4.6 modules/aggregator.module
  3. 4.7 modules/aggregator.module
  4. 5 modules/aggregator/aggregator.module
  5. 6 modules/aggregator/aggregator.module
  6. 8 core/modules/aggregator/aggregator.module

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

Functions

Namesort descending Description
aggregator_block Implementation of hook_block().
aggregator_category_load Load an aggregator category.
aggregator_cron Implementation of hook_cron().
aggregator_element_data Call-back function used by the XML parser.
aggregator_element_end Call-back function used by the XML parser.
aggregator_element_start Call-back function used by the XML parser.
aggregator_feed_load Load an aggregator feed.
aggregator_filter_xss Safely render HTML content, as allowed.
aggregator_help Implementation of hook_help().
aggregator_init Implementation of hook_init().
aggregator_menu Implementation of hook_menu().
aggregator_parse_feed Parse a feed and store its items.
aggregator_parse_w3cdtf Parse the W3C date/time format, a subset of ISO 8601. PHP date parsing functions do not handle this format. See http://www.w3.org/TR/NOTE-datetime for more information. Originally from MagpieRSS (http://magpierss.sourceforge.net/).
aggregator_perm Implementation of hook_perm().
aggregator_refresh Checks a news feed for new items.
aggregator_remove Removes all items from a feed.
aggregator_save_category Add/edit/delete aggregator categories.
aggregator_save_feed Add/edit/delete an aggregator feed.
aggregator_save_item Add/edit/delete an aggregator item.
aggregator_theme Implementation of hook_theme()
theme_aggregator_block_item Format an individual feed item for display in the block.
_aggregator_category_title Menu callback.
_aggregator_has_categories Find out whether there are any aggregator categories.
_aggregator_items Helper function for drupal_map_assoc.

File

modules/aggregator/aggregator.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Used to aggregate syndicated content (RSS, RDF, and Atom).
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. */
  9. function aggregator_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#aggregator':
  12. $output = '<p>'. t('The aggregator is a powerful on-site syndicator and news reader that gathers 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 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>';
  13. $output .= '<p>'. t('Feeds contain feed items, or individual posts published by the site providing the feed. Feeds may be grouped in categories, generally by topic. Users view feed items in the <a href="@aggregator">main aggregator display</a> or by <a href="@aggregator-sources">their source</a>. Administrators can <a href="@feededit">add, edit and delete feeds</a> and choose how often to check each feed for newly updated items. The most recent items in either a feed or category can be displayed as a block through the <a href="@admin-block">blocks administration page</a>. A <a href="@aggregator-opml">machine-readable OPML file</a> of all feeds is available. A correctly configured <a href="@cron">cron maintenance task</a> is required to update feeds automatically.', array('@aggregator' => url('aggregator'), '@aggregator-sources' => url('aggregator/sources'), '@feededit' => url('admin/content/aggregator'), '@admin-block' => url('admin/build/block'), '@aggregator-opml' => url('aggregator/opml'), '@cron' => url('admin/reports/status'))) .'</p>';
  14. $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@aggregator">Aggregator module</a>.', array('@aggregator' => 'http://drupal.org/handbook/modules/aggregator/')) .'</p>';
  15. return $output;
  16. case 'admin/content/aggregator':
  17. $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>';
  18. $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/content/aggregator/add/feed'), '@block' => url('admin/build/block'))) .'</p>';
  19. return $output;
  20. case 'admin/content/aggregator/add/feed':
  21. return '<p>'. t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') .'</p>';
  22. case 'admin/content/aggregator/add/category':
  23. 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>';
  24. }
  25. }
  26. /**
  27. * Implementation of hook_theme()
  28. */
  29. function aggregator_theme() {
  30. return array(
  31. 'aggregator_wrapper' => array(
  32. 'arguments' => array('content' => NULL),
  33. 'file' => 'aggregator.pages.inc',
  34. 'template' => 'aggregator-wrapper',
  35. ),
  36. 'aggregator_categorize_items' => array(
  37. 'arguments' => array('form' => NULL),
  38. 'file' => 'aggregator.pages.inc',
  39. ),
  40. 'aggregator_feed_source' => array(
  41. 'arguments' => array('feed' => NULL),
  42. 'file' => 'aggregator.pages.inc',
  43. 'template' => 'aggregator-feed-source',
  44. ),
  45. 'aggregator_block_item' => array(
  46. 'arguments' => array('item' => NULL, 'feed' => 0),
  47. ),
  48. 'aggregator_summary_items' => array(
  49. 'arguments' => array('summary_items' => NULL, 'source' => NULL),
  50. 'file' => 'aggregator.pages.inc',
  51. 'template' => 'aggregator-summary-items',
  52. ),
  53. 'aggregator_summary_item' => array(
  54. 'arguments' => array('item' => NULL),
  55. 'file' => 'aggregator.pages.inc',
  56. 'template' => 'aggregator-summary-item',
  57. ),
  58. 'aggregator_item' => array(
  59. 'arguments' => array('item' => NULL),
  60. 'file' => 'aggregator.pages.inc',
  61. 'template' => 'aggregator-item',
  62. ),
  63. 'aggregator_page_opml' => array(
  64. 'arguments' => array('feeds' => NULL),
  65. 'file' => 'aggregator.pages.inc',
  66. ),
  67. 'aggregator_page_rss' => array(
  68. 'arguments' => array('feeds' => NULL, 'category' => NULL),
  69. 'file' => 'aggregator.pages.inc',
  70. ),
  71. );
  72. }
  73. /**
  74. * Implementation of hook_menu().
  75. */
  76. function aggregator_menu() {
  77. $items['admin/content/aggregator'] = array(
  78. 'title' => 'Feed aggregator',
  79. 'description' => "Configure which content your site aggregates from other sites, how often it polls them, and how they're categorized.",
  80. 'page callback' => 'aggregator_admin_overview',
  81. 'access arguments' => array('administer news feeds'),
  82. 'file' => 'aggregator.admin.inc',
  83. );
  84. $items['admin/content/aggregator/add/feed'] = array(
  85. 'title' => 'Add feed',
  86. 'page callback' => 'drupal_get_form',
  87. 'page arguments' => array('aggregator_form_feed'),
  88. 'access arguments' => array('administer news feeds'),
  89. 'type' => MENU_LOCAL_TASK,
  90. 'parent' => 'admin/content/aggregator',
  91. 'file' => 'aggregator.admin.inc',
  92. );
  93. $items['admin/content/aggregator/add/category'] = array(
  94. 'title' => 'Add category',
  95. 'page callback' => 'drupal_get_form',
  96. 'page arguments' => array('aggregator_form_category'),
  97. 'access arguments' => array('administer news feeds'),
  98. 'type' => MENU_LOCAL_TASK,
  99. 'parent' => 'admin/content/aggregator',
  100. 'file' => 'aggregator.admin.inc',
  101. );
  102. $items['admin/content/aggregator/remove/%aggregator_feed'] = array(
  103. 'title' => 'Remove items',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('aggregator_admin_remove_feed', 4),
  106. 'access arguments' => array('administer news feeds'),
  107. 'type' => MENU_CALLBACK,
  108. 'file' => 'aggregator.admin.inc',
  109. );
  110. $items['admin/content/aggregator/update/%aggregator_feed'] = array(
  111. 'title' => 'Update items',
  112. 'page callback' => 'aggregator_admin_refresh_feed',
  113. 'page arguments' => array(4),
  114. 'access arguments' => array('administer news feeds'),
  115. 'type' => MENU_CALLBACK,
  116. 'file' => 'aggregator.admin.inc',
  117. );
  118. $items['admin/content/aggregator/list'] = array(
  119. 'title' => 'List',
  120. 'type' => MENU_DEFAULT_LOCAL_TASK,
  121. 'weight' => -10,
  122. );
  123. $items['admin/content/aggregator/settings'] = array(
  124. 'title' => 'Settings',
  125. 'page callback' => 'drupal_get_form',
  126. 'page arguments' => array('aggregator_admin_settings'),
  127. 'type' => MENU_LOCAL_TASK,
  128. 'weight' => 10,
  129. 'access arguments' => array('administer news feeds'),
  130. 'file' => 'aggregator.admin.inc',
  131. );
  132. $items['aggregator'] = array(
  133. 'title' => 'Feed aggregator',
  134. 'page callback' => 'aggregator_page_last',
  135. 'access arguments' => array('access news feeds'),
  136. 'weight' => 5,
  137. 'file' => 'aggregator.pages.inc',
  138. );
  139. $items['aggregator/sources'] = array(
  140. 'title' => 'Sources',
  141. 'page callback' => 'aggregator_page_sources',
  142. 'access arguments' => array('access news feeds'),
  143. 'file' => 'aggregator.pages.inc',
  144. );
  145. $items['aggregator/categories'] = array(
  146. 'title' => 'Categories',
  147. 'page callback' => 'aggregator_page_categories',
  148. 'access callback' => '_aggregator_has_categories',
  149. 'file' => 'aggregator.pages.inc',
  150. );
  151. $items['aggregator/rss'] = array(
  152. 'title' => 'RSS feed',
  153. 'page callback' => 'aggregator_page_rss',
  154. 'access arguments' => array('access news feeds'),
  155. 'type' => MENU_CALLBACK,
  156. 'file' => 'aggregator.pages.inc',
  157. );
  158. $items['aggregator/opml'] = array(
  159. 'title' => 'OPML feed',
  160. 'page callback' => 'aggregator_page_opml',
  161. 'access arguments' => array('access news feeds'),
  162. 'type' => MENU_CALLBACK,
  163. 'file' => 'aggregator.pages.inc',
  164. );
  165. $items['aggregator/categories/%aggregator_category'] = array(
  166. 'title callback' => '_aggregator_category_title',
  167. 'title arguments' => array(2),
  168. 'page callback' => 'aggregator_page_category',
  169. 'page arguments' => array(2),
  170. 'access callback' => 'user_access',
  171. 'access arguments' => array('access news feeds'),
  172. 'file' => 'aggregator.pages.inc',
  173. );
  174. $items['aggregator/categories/%aggregator_category/view'] = array(
  175. 'title' => 'View',
  176. 'type' => MENU_DEFAULT_LOCAL_TASK,
  177. 'weight' => -10,
  178. );
  179. $items['aggregator/categories/%aggregator_category/categorize'] = array(
  180. 'title' => 'Categorize',
  181. 'page callback' => 'drupal_get_form',
  182. 'page arguments' => array('aggregator_page_category', 2),
  183. 'access arguments' => array('administer news feeds'),
  184. 'type' => MENU_LOCAL_TASK,
  185. 'file' => 'aggregator.pages.inc',
  186. );
  187. $items['aggregator/categories/%aggregator_category/configure'] = array(
  188. 'title' => 'Configure',
  189. 'page callback' => 'drupal_get_form',
  190. 'page arguments' => array('aggregator_form_category', 2),
  191. 'access arguments' => array('administer news feeds'),
  192. 'type' => MENU_LOCAL_TASK,
  193. 'weight' => 1,
  194. 'file' => 'aggregator.admin.inc',
  195. );
  196. $items['aggregator/sources/%aggregator_feed'] = array(
  197. 'page callback' => 'aggregator_page_source',
  198. 'page arguments' => array(2),
  199. 'access arguments' => array('access news feeds'),
  200. 'type' => MENU_CALLBACK,
  201. 'file' => 'aggregator.pages.inc',
  202. );
  203. $items['aggregator/sources/%aggregator_feed/view'] = array(
  204. 'title' => 'View',
  205. 'type' => MENU_DEFAULT_LOCAL_TASK,
  206. 'weight' => -10,
  207. );
  208. $items['aggregator/sources/%aggregator_feed/categorize'] = array(
  209. 'title' => 'Categorize',
  210. 'page callback' => 'drupal_get_form',
  211. 'page arguments' => array('aggregator_page_source', 2),
  212. 'access arguments' => array('administer news feeds'),
  213. 'type' => MENU_LOCAL_TASK,
  214. 'file' => 'aggregator.pages.inc',
  215. );
  216. $items['aggregator/sources/%aggregator_feed/configure'] = array(
  217. 'title' => 'Configure',
  218. 'page callback' => 'drupal_get_form',
  219. 'page arguments' => array('aggregator_form_feed', 2),
  220. 'access arguments' => array('administer news feeds'),
  221. 'type' => MENU_LOCAL_TASK,
  222. 'weight' => 1,
  223. 'file' => 'aggregator.admin.inc',
  224. );
  225. $items['admin/content/aggregator/edit/feed/%aggregator_feed'] = array(
  226. 'title' => 'Edit feed',
  227. 'page callback' => 'drupal_get_form',
  228. 'page arguments' => array('aggregator_form_feed', 5),
  229. 'access arguments' => array('administer news feeds'),
  230. 'type' => MENU_CALLBACK,
  231. 'file' => 'aggregator.admin.inc',
  232. );
  233. $items['admin/content/aggregator/edit/category/%aggregator_category'] = array(
  234. 'title' => 'Edit category',
  235. 'page callback' => 'drupal_get_form',
  236. 'page arguments' => array('aggregator_form_category', 5),
  237. 'access arguments' => array('administer news feeds'),
  238. 'type' => MENU_CALLBACK,
  239. 'file' => 'aggregator.admin.inc',
  240. );
  241. return $items;
  242. }
  243. /**
  244. * Menu callback.
  245. *
  246. * @return
  247. * An aggregator category title.
  248. */
  249. function _aggregator_category_title($category) {
  250. return $category['title'];
  251. }
  252. /**
  253. * Implementation of hook_init().
  254. */
  255. function aggregator_init() {
  256. drupal_add_css(drupal_get_path('module', 'aggregator') .'/aggregator.css');
  257. }
  258. /**
  259. * Find out whether there are any aggregator categories.
  260. *
  261. * @return
  262. * TRUE if there is at least one category and the user has access to them, FALSE otherwise.
  263. */
  264. function _aggregator_has_categories() {
  265. return user_access('access news feeds') && db_result(db_query('SELECT COUNT(*) FROM {aggregator_category}'));
  266. }
  267. /**
  268. * Implementation of hook_perm().
  269. */
  270. function aggregator_perm() {
  271. return array('administer news feeds', 'access news feeds');
  272. }
  273. /**
  274. * Implementation of hook_cron().
  275. *
  276. * Checks news feeds for updates once their refresh interval has elapsed.
  277. */
  278. function aggregator_cron() {
  279. $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < %d', time());
  280. while ($feed = db_fetch_array($result)) {
  281. aggregator_refresh($feed);
  282. }
  283. }
  284. /**
  285. * Implementation of hook_block().
  286. *
  287. * Generates blocks for the latest news items in each category and feed.
  288. */
  289. function aggregator_block($op = 'list', $delta = 0, $edit = array()) {
  290. if ($op == 'list') {
  291. $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
  292. while ($category = db_fetch_object($result)) {
  293. $block['category-'. $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
  294. }
  295. $result = db_query('SELECT fid, title FROM {aggregator_feed} ORDER BY fid');
  296. while ($feed = db_fetch_object($result)) {
  297. $block['feed-'. $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
  298. }
  299. }
  300. else if ($op == 'configure') {
  301. list($type, $id) = explode('-', $delta);
  302. if ($type == 'category') {
  303. $value = db_result(db_query('SELECT block FROM {aggregator_category} WHERE cid = %d', $id));
  304. }
  305. else {
  306. $value = db_result(db_query('SELECT block FROM {aggregator_feed} WHERE fid = %d', $id));
  307. }
  308. $form['block'] = array('#type' => 'select', '#title' => t('Number of news items in block'), '#default_value' => $value, '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
  309. return $form;
  310. }
  311. else if ($op == 'save') {
  312. list($type, $id) = explode('-', $delta);
  313. if ($type == 'category') {
  314. $value = db_query('UPDATE {aggregator_category} SET block = %d WHERE cid = %d', $edit['block'], $id);
  315. }
  316. else {
  317. $value = db_query('UPDATE {aggregator_feed} SET block = %d WHERE fid = %d', $edit['block'], $id);
  318. }
  319. }
  320. else if ($op == 'view') {
  321. if (user_access('access news feeds')) {
  322. list($type, $id) = explode('-', $delta);
  323. switch ($type) {
  324. case 'feed':
  325. if ($feed = db_fetch_object(db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE fid = %d', $id))) {
  326. $block['subject'] = check_plain($feed->title);
  327. $result = db_query_range('SELECT * FROM {aggregator_item} WHERE fid = %d ORDER BY timestamp DESC, iid DESC', $feed->fid, 0, $feed->block);
  328. $read_more = theme('more_link', url('aggregator/sources/'. $feed->fid), t("View this feed's recent news."));
  329. }
  330. break;
  331. case 'category':
  332. if ($category = db_fetch_object(db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = %d', $id))) {
  333. $block['subject'] = check_plain($category->title);
  334. $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = %d ORDER BY i.timestamp DESC, i.iid DESC', $category->cid, 0, $category->block);
  335. $read_more = theme('more_link', url('aggregator/categories/'. $category->cid), t("View this category's recent news."));
  336. }
  337. break;
  338. }
  339. $items = array();
  340. while ($item = db_fetch_object($result)) {
  341. $items[] = theme('aggregator_block_item', $item);
  342. }
  343. // Only display the block if there are items to show.
  344. if (count($items) > 0) {
  345. $block['content'] = theme('item_list', $items) . $read_more;
  346. }
  347. }
  348. }
  349. if (isset($block)) {
  350. return $block;
  351. }
  352. }
  353. /**
  354. * Add/edit/delete aggregator categories.
  355. *
  356. * @param $edit
  357. * An associative array describing the category to be added/edited/deleted.
  358. */
  359. function aggregator_save_category($edit) {
  360. $link_path = 'aggregator/categories/';
  361. if (!empty($edit['cid'])) {
  362. $link_path .= $edit['cid'];
  363. if (!empty($edit['title'])) {
  364. db_query("UPDATE {aggregator_category} SET title = '%s', description = '%s' WHERE cid = %d", $edit['title'], $edit['description'], $edit['cid']);
  365. $op = 'update';
  366. }
  367. else {
  368. db_query('DELETE FROM {aggregator_category} WHERE cid = %d', $edit['cid']);
  369. // Make sure there is no active block for this category.
  370. db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'", 'aggregator', 'category-' . $edit['cid']);
  371. $edit['title'] = '';
  372. $op = 'delete';
  373. }
  374. }
  375. else if (!empty($edit['title'])) {
  376. // A single unique id for bundles and feeds, to use in blocks
  377. db_query("INSERT INTO {aggregator_category} (title, description, block) VALUES ('%s', '%s', 5)", $edit['title'], $edit['description']);
  378. $link_path .= db_last_insert_id('aggregator_category', 'cid');
  379. $op = 'insert';
  380. }
  381. if (isset($op)) {
  382. menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
  383. }
  384. }
  385. /**
  386. * Add/edit/delete an aggregator feed.
  387. *
  388. * @param $edit
  389. * An associative array describing the feed to be added/edited/deleted.
  390. */
  391. function aggregator_save_feed($edit) {
  392. if (!empty($edit['fid'])) {
  393. // An existing feed is being modified, delete the category listings.
  394. db_query('DELETE FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
  395. }
  396. if (!empty($edit['fid']) && !empty($edit['title'])) {
  397. db_query("UPDATE {aggregator_feed} SET title = '%s', url = '%s', refresh = %d WHERE fid = %d", $edit['title'], $edit['url'], $edit['refresh'], $edit['fid']);
  398. }
  399. else if (!empty($edit['fid'])) {
  400. $items = array();
  401. $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $edit['fid']);
  402. while ($item = db_fetch_object($result)) {
  403. $items[] = "iid = $item->iid";
  404. }
  405. if (!empty($items)) {
  406. db_query('DELETE FROM {aggregator_category_item} WHERE '. implode(' OR ', $items));
  407. }
  408. db_query('DELETE FROM {aggregator_feed} WHERE fid = %d', $edit['fid']);
  409. db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $edit['fid']);
  410. // Make sure there is no active block for this feed.
  411. db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s'", 'aggregator', 'feed-' . $edit['fid']);
  412. }
  413. else if (!empty($edit['title'])) {
  414. db_query("INSERT INTO {aggregator_feed} (title, url, refresh, block, description, image) VALUES ('%s', '%s', %d, 5, '', '')", $edit['title'], $edit['url'], $edit['refresh']);
  415. // A single unique id for bundles and feeds, to use in blocks.
  416. $edit['fid'] = db_last_insert_id('aggregator_feed', 'fid');
  417. }
  418. if (!empty($edit['title'])) {
  419. // The feed is being saved, save the categories as well.
  420. if (!empty($edit['category'])) {
  421. foreach ($edit['category'] as $cid => $value) {
  422. if ($value) {
  423. db_query('INSERT INTO {aggregator_category_feed} (fid, cid) VALUES (%d, %d)', $edit['fid'], $cid);
  424. }
  425. }
  426. }
  427. }
  428. }
  429. /**
  430. * Removes all items from a feed.
  431. *
  432. * @param $feed
  433. * An associative array describing the feed to be cleared.
  434. */
  435. function aggregator_remove($feed) {
  436. $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d', $feed['fid']);
  437. while ($item = db_fetch_object($result)) {
  438. $items[] = "iid = $item->iid";
  439. }
  440. if (!empty($items)) {
  441. db_query('DELETE FROM {aggregator_category_item} WHERE '. implode(' OR ', $items));
  442. }
  443. db_query('DELETE FROM {aggregator_item} WHERE fid = %d', $feed['fid']);
  444. db_query("UPDATE {aggregator_feed} SET checked = 0, etag = '', modified = 0 WHERE fid = %d", $feed['fid']);
  445. drupal_set_message(t('The news items from %site have been removed.', array('%site' => $feed['title'])));
  446. }
  447. /**
  448. * Call-back function used by the XML parser.
  449. */
  450. function aggregator_element_start($parser, $name, $attributes) {
  451. global $item, $element, $tag, $items, $channel;
  452. switch ($name) {
  453. case 'IMAGE':
  454. case 'TEXTINPUT':
  455. case 'CONTENT':
  456. case 'SUMMARY':
  457. case 'TAGLINE':
  458. case 'SUBTITLE':
  459. case 'LOGO':
  460. case 'INFO':
  461. $element = $name;
  462. break;
  463. case 'ID':
  464. if ($element != 'ITEM') {
  465. $element = $name;
  466. }
  467. case 'LINK':
  468. if (!empty($attributes['REL']) && $attributes['REL'] == 'alternate') {
  469. if ($element == 'ITEM') {
  470. $items[$item]['LINK'] = $attributes['HREF'];
  471. }
  472. else {
  473. $channel['LINK'] = $attributes['HREF'];
  474. }
  475. }
  476. break;
  477. case 'ITEM':
  478. $element = $name;
  479. $item += 1;
  480. break;
  481. case 'ENTRY':
  482. $element = 'ITEM';
  483. $item += 1;
  484. break;
  485. }
  486. $tag = $name;
  487. }
  488. /**
  489. * Call-back function used by the XML parser.
  490. */
  491. function aggregator_element_end($parser, $name) {
  492. global $element;
  493. switch ($name) {
  494. case 'IMAGE':
  495. case 'TEXTINPUT':
  496. case 'ITEM':
  497. case 'ENTRY':
  498. case 'CONTENT':
  499. case 'INFO':
  500. $element = '';
  501. break;
  502. case 'ID':
  503. if ($element == 'ID') {
  504. $element = '';
  505. }
  506. }
  507. }
  508. /**
  509. * Call-back function used by the XML parser.
  510. */
  511. function aggregator_element_data($parser, $data) {
  512. global $channel, $element, $items, $item, $image, $tag;
  513. $items += array($item => array());
  514. switch ($element) {
  515. case 'ITEM':
  516. $items[$item] += array($tag => '');
  517. $items[$item][$tag] .= $data;
  518. break;
  519. case 'IMAGE':
  520. case 'LOGO':
  521. $image += array($tag => '');
  522. $image[$tag] .= $data;
  523. break;
  524. case 'LINK':
  525. if ($data) {
  526. $items[$item] += array($tag => '');
  527. $items[$item][$tag] .= $data;
  528. }
  529. break;
  530. case 'CONTENT':
  531. $items[$item] += array('CONTENT' => '');
  532. $items[$item]['CONTENT'] .= $data;
  533. break;
  534. case 'SUMMARY':
  535. $items[$item] += array('SUMMARY' => '');
  536. $items[$item]['SUMMARY'] .= $data;
  537. break;
  538. case 'TAGLINE':
  539. case 'SUBTITLE':
  540. $channel += array('DESCRIPTION' => '');
  541. $channel['DESCRIPTION'] .= $data;
  542. break;
  543. case 'INFO':
  544. case 'ID':
  545. case 'TEXTINPUT':
  546. // The sub-element is not supported. However, we must recognize
  547. // it or its contents will end up in the item array.
  548. break;
  549. default:
  550. $channel += array($tag => '');
  551. $channel[$tag] .= $data;
  552. }
  553. }
  554. /**
  555. * Checks a news feed for new items.
  556. *
  557. * @param $feed
  558. * An associative array describing the feed to be refreshed.
  559. */
  560. function aggregator_refresh($feed) {
  561. global $channel, $image;
  562. // Generate conditional GET headers.
  563. $headers = array();
  564. if ($feed['etag']) {
  565. $headers['If-None-Match'] = $feed['etag'];
  566. }
  567. if ($feed['modified']) {
  568. $headers['If-Modified-Since'] = gmdate('D, d M Y H:i:s', $feed['modified']) .' GMT';
  569. }
  570. // Request feed.
  571. $result = drupal_http_request($feed['url'], $headers);
  572. // Process HTTP response code.
  573. switch ($result->code) {
  574. case 304:
  575. db_query('UPDATE {aggregator_feed} SET checked = %d WHERE fid = %d', time(), $feed['fid']);
  576. drupal_set_message(t('There is no new syndicated content from %site.', array('%site' => $feed['title'])));
  577. break;
  578. case 301:
  579. $feed['url'] = $result->redirect_url;
  580. watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed['title'], '%url' => $feed['url']));
  581. // Deliberate no break.
  582. case 200:
  583. case 302:
  584. case 307:
  585. // Filter the input data:
  586. if (aggregator_parse_feed($result->data, $feed)) {
  587. $modified = empty($result->headers['Last-Modified']) ? 0 : strtotime($result->headers['Last-Modified']);
  588. // Prepare the channel data.
  589. foreach ($channel as $key => $value) {
  590. $channel[$key] = trim($value);
  591. }
  592. // Prepare the image data (if any).
  593. foreach ($image as $key => $value) {
  594. $image[$key] = trim($value);
  595. }
  596. if (!empty($image['LINK']) && !empty($image['URL']) && !empty($image['TITLE'])) {
  597. // Note, we should really use theme_image() here but that only works with local images it won't work with images fetched with a URL unless PHP version > 5
  598. $image = '<a href="'. check_url($image['LINK']) .'" class="feed-image"><img src="'. check_url($image['URL']) .'" alt="'. check_plain($image['TITLE']) .'" /></a>';
  599. }
  600. else {
  601. $image = NULL;
  602. }
  603. $etag = empty($result->headers['ETag']) ? '' : $result->headers['ETag'];
  604. // Update the feed data.
  605. db_query("UPDATE {aggregator_feed} SET url = '%s', checked = %d, link = '%s', description = '%s', image = '%s', etag = '%s', modified = %d WHERE fid = %d", $feed['url'], time(), $channel['LINK'], $channel['DESCRIPTION'], $image, $etag, $modified, $feed['fid']);
  606. // Clear the cache.
  607. cache_clear_all();
  608. watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed['title']));
  609. drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed['title'])));
  610. }
  611. break;
  612. default:
  613. watchdog('aggregator', 'The feed from %site seems to be broken, due to "%error".', array('%site' => $feed['title'], '%error' => $result->code .' '. $result->error), WATCHDOG_WARNING);
  614. drupal_set_message(t('The feed from %site seems to be broken, because of error "%error".', array('%site' => $feed['title'], '%error' => $result->code .' '. $result->error)));
  615. }
  616. }
  617. /**
  618. * Parse the W3C date/time format, a subset of ISO 8601. PHP date parsing
  619. * functions do not handle this format.
  620. * See http://www.w3.org/TR/NOTE-datetime for more information.
  621. * Originally from MagpieRSS (http://magpierss.sourceforge.net/).
  622. *
  623. * @param $date_str
  624. * A string with a potentially W3C DTF date.
  625. * @return
  626. * A timestamp if parsed successfully or FALSE if not.
  627. */
  628. function aggregator_parse_w3cdtf($date_str) {
  629. if (preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/', $date_str, $match)) {
  630. list($year, $month, $day, $hours, $minutes, $seconds) = array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
  631. // calc epoch for current date assuming GMT
  632. $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
  633. if ($match[10] != 'Z') { // Z is zulu time, aka GMT
  634. list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9], $match[10]);
  635. // zero out the variables
  636. if (!$tz_hour) {
  637. $tz_hour = 0;
  638. }
  639. if (!$tz_min) {
  640. $tz_min = 0;
  641. }
  642. $offset_secs = (($tz_hour * 60) + $tz_min) * 60;
  643. // is timezone ahead of GMT? then subtract offset
  644. if ($tz_mod == '+') {
  645. $offset_secs *= -1;
  646. }
  647. $epoch += $offset_secs;
  648. }
  649. return $epoch;
  650. }
  651. else {
  652. return FALSE;
  653. }
  654. }
  655. /**
  656. * Parse a feed and store its items.
  657. *
  658. * @param $data
  659. * The feed data.
  660. * @param $feed
  661. * An associative array describing the feed to be parsed.
  662. * @return
  663. * 0 on error, 1 otherwise.
  664. */
  665. function aggregator_parse_feed(&$data, $feed) {
  666. global $items, $image, $channel;
  667. // Unset the global variables before we use them:
  668. unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']);
  669. $items = array();
  670. $image = array();
  671. $channel = array();
  672. // parse the data:
  673. $xml_parser = drupal_xml_parser_create($data);
  674. xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end');
  675. xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
  676. if (!xml_parse($xml_parser, $data, 1)) {
  677. watchdog('aggregator', 'The feed from %site seems to be broken, due to an error "%error" on line %line.', array('%site' => $feed['title'], '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser)), WATCHDOG_WARNING);
  678. drupal_set_message(t('The feed from %site seems to be broken, because of error "%error" on line %line.', array('%site' => $feed['title'], '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser))), 'error');
  679. return 0;
  680. }
  681. xml_parser_free($xml_parser);
  682. // We reverse the array such that we store the first item last, and the last
  683. // item first. In the database, the newest item should be at the top.
  684. $items = array_reverse($items);
  685. // Initialize variables.
  686. $title = $link = $author = $description = $guid = NULL;
  687. foreach ($items as $item) {
  688. unset($title, $link, $author, $description, $guid);
  689. // Prepare the item:
  690. foreach ($item as $key => $value) {
  691. $item[$key] = trim($value);
  692. }
  693. // Resolve the item's title. If no title is found, we use up to 40
  694. // characters of the description ending at a word boundary but not
  695. // splitting potential entities.
  696. if (!empty($item['TITLE'])) {
  697. $title = $item['TITLE'];
  698. }
  699. elseif (!empty($item['DESCRIPTION'])) {
  700. $title = preg_replace('/^(.*)[^\w;&].*?$/', "\\1", truncate_utf8($item['DESCRIPTION'], 40));
  701. }
  702. else {
  703. $title = '';
  704. }
  705. // Resolve the items link.
  706. if (!empty($item['LINK'])) {
  707. $link = $item['LINK'];
  708. }
  709. else {
  710. $link = $feed['link'];
  711. }
  712. // Atom feeds use ID rather than GUID.
  713. if (isset($item['GUID'])) {
  714. $guid = $item['GUID'];
  715. }
  716. elseif (isset($item['ID'])) {
  717. $guid = $item['ID'];
  718. }
  719. else {
  720. $guid = '';
  721. }
  722. // Atom feeds have a CONTENT and/or SUMMARY tag instead of a DESCRIPTION tag.
  723. if (!empty($item['CONTENT:ENCODED'])) {
  724. $item['DESCRIPTION'] = $item['CONTENT:ENCODED'];
  725. }
  726. else if (!empty($item['SUMMARY'])) {
  727. $item['DESCRIPTION'] = $item['SUMMARY'];
  728. }
  729. else if (!empty($item['CONTENT'])) {
  730. $item['DESCRIPTION'] = $item['CONTENT'];
  731. }
  732. // Try to resolve and parse the item's publication date. If no date is
  733. // found, we use the current date instead.
  734. $date = 'now';
  735. foreach (array('PUBDATE', 'DC:DATE', 'DCTERMS:ISSUED', 'DCTERMS:CREATED', 'DCTERMS:MODIFIED', 'ISSUED', 'CREATED', 'MODIFIED', 'PUBLISHED', 'UPDATED') as $key) {
  736. if (!empty($item[$key])) {
  737. $date = $item[$key];
  738. break;
  739. }
  740. }
  741. $timestamp = strtotime($date); // As of PHP 5.1.0, strtotime returns FALSE on failure instead of -1.
  742. if ($timestamp <= 0) {
  743. $timestamp = aggregator_parse_w3cdtf($date); // Returns FALSE on failure
  744. if (!$timestamp) {
  745. $timestamp = time(); // better than nothing
  746. }
  747. }
  748. // Resolve dc:creator tag as the item author if author tag is not set.
  749. if (empty($item['AUTHOR']) && !empty($item['DC:CREATOR'])) {
  750. $item['AUTHOR'] = $item['DC:CREATOR'];
  751. }
  752. // Save this item. Try to avoid duplicate entries as much as possible. If
  753. // we find a duplicate entry, we resolve it and pass along its ID is such
  754. // that we can update it if needed.
  755. if (!empty($guid)) {
  756. $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND guid = '%s'", $feed['fid'], $guid));
  757. }
  758. else if ($link && $link != $feed['link'] && $link != $feed['url']) {
  759. $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND link = '%s'", $feed['fid'], $link));
  760. }
  761. else {
  762. $entry = db_fetch_object(db_query("SELECT iid FROM {aggregator_item} WHERE fid = %d AND title = '%s'", $feed['fid'], $title));
  763. }
  764. $item += array('AUTHOR' => '', 'DESCRIPTION' => '');
  765. aggregator_save_item(array('iid' => (isset($entry->iid) ? $entry->iid: ''), 'fid' => $feed['fid'], 'timestamp' => $timestamp, 'title' => $title, 'link' => $link, 'author' => $item['AUTHOR'], 'description' => $item['DESCRIPTION'], 'guid' => $guid));
  766. }
  767. // Remove all items that are older than flush item timer.
  768. $age = time() - variable_get('aggregator_clear', 9676800);
  769. $result = db_query('SELECT iid FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed['fid'], $age);
  770. $items = array();
  771. $num_rows = FALSE;
  772. while ($item = db_fetch_object($result)) {
  773. $items[] = $item->iid;
  774. $num_rows = TRUE;
  775. }
  776. if ($num_rows) {
  777. db_query('DELETE FROM {aggregator_category_item} WHERE iid IN ('. implode(', ', $items) .')');
  778. db_query('DELETE FROM {aggregator_item} WHERE fid = %d AND timestamp < %d', $feed['fid'], $age);
  779. }
  780. return 1;
  781. }
  782. /**
  783. * Add/edit/delete an aggregator item.
  784. *
  785. * @param $edit
  786. * An associative array describing the item to be added/edited/deleted.
  787. */
  788. function aggregator_save_item($edit) {
  789. if ($edit['iid'] && $edit['title']) {
  790. db_query("UPDATE {aggregator_item} SET title = '%s', link = '%s', author = '%s', description = '%s', guid = '%s', timestamp = %d WHERE iid = %d", $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['guid'], $edit['timestamp'], $edit['iid']);
  791. }
  792. else if ($edit['iid']) {
  793. db_query('DELETE FROM {aggregator_item} WHERE iid = %d', $edit['iid']);
  794. db_query('DELETE FROM {aggregator_category_item} WHERE iid = %d', $edit['iid']);
  795. }
  796. else if ($edit['title'] && $edit['link']) {
  797. db_query("INSERT INTO {aggregator_item} (fid, title, link, author, description, timestamp, guid) VALUES (%d, '%s', '%s', '%s', '%s', %d, '%s')", $edit['fid'], $edit['title'], $edit['link'], $edit['author'], $edit['description'], $edit['timestamp'], $edit['guid']);
  798. $edit['iid'] = db_last_insert_id('aggregator_item', 'iid');
  799. // file the items in the categories indicated by the feed
  800. $categories = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = %d', $edit['fid']);
  801. while ($category = db_fetch_object($categories)) {
  802. db_query('INSERT INTO {aggregator_category_item} (cid, iid) VALUES (%d, %d)', $category->cid, $edit['iid']);
  803. }
  804. }
  805. }
  806. /**
  807. * Load an aggregator feed.
  808. *
  809. * @param $fid
  810. * The feed id.
  811. * @return
  812. * An associative array describing the feed.
  813. */
  814. function aggregator_feed_load($fid) {
  815. static $feeds;
  816. if (!isset($feeds[$fid])) {
  817. $feeds[$fid] = db_fetch_array(db_query('SELECT * FROM {aggregator_feed} WHERE fid = %d', $fid));
  818. }
  819. return $feeds[$fid];
  820. }
  821. /**
  822. * Load an aggregator category.
  823. *
  824. * @param $cid
  825. * The category id.
  826. * @return
  827. * An associative array describing the category.
  828. */
  829. function aggregator_category_load($cid) {
  830. static $categories;
  831. if (!isset($categories[$cid])) {
  832. $categories[$cid] = db_fetch_array(db_query('SELECT * FROM {aggregator_category} WHERE cid = %d', $cid));
  833. }
  834. return $categories[$cid];
  835. }
  836. /**
  837. * Format an individual feed item for display in the block.
  838. *
  839. * @param $item
  840. * The item to be displayed.
  841. * @param $feed
  842. * Not used.
  843. * @return
  844. * The item HTML.
  845. * @ingroup themeable
  846. */
  847. function theme_aggregator_block_item($item, $feed = 0) {
  848. global $user;
  849. $output = '';
  850. if ($user->uid && module_exists('blog') && user_access('create blog entries')) {
  851. if ($image = theme('image', 'misc/blog.png', t('blog it'), t('blog it'))) {
  852. $output .= '<div class="icon">'. l($image, 'node/add/blog', array('attributes' => array('title' => t('Comment on this news item in your personal blog.'), 'class' => 'blog-it'), 'query' => "iid=$item->iid", 'html' => TRUE)) .'</div>';
  853. }
  854. }
  855. // Display the external link to the item.
  856. $output .= '<a href="'. check_url($item->link) .'">'. check_plain($item->title) ."</a>\n";
  857. return $output;
  858. }
  859. /**
  860. * Safely render HTML content, as allowed.
  861. *
  862. * @param $value
  863. * The content to be filtered.
  864. * @return
  865. * The filtered content.
  866. */
  867. function aggregator_filter_xss($value) {
  868. 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));
  869. }
  870. /**
  871. * Helper function for drupal_map_assoc.
  872. *
  873. * @param $count
  874. * Items count.
  875. * @return
  876. * Plural-formatted "@count items"
  877. */
  878. function _aggregator_items($count) {
  879. return format_plural($count, '1 item', '@count items');
  880. }