bot_aggregator.module

Tracking 6.x-1.x branch
  1. drupal
    1. 6 contributions/bot/bot_aggregator/bot_aggregator.module

Enables the reporting of aggregated feed items to configured channels.

Functions & methods

NameDescription
bot_aggregator_feed_channelsReturn the list of all feed-to-channel configurations.
bot_aggregator_form_aggregator_form_feed_alterImplementation of hook_form_alter().
bot_aggregator_form_aggregator_form_feed_submitFAPI #submit handler; saves the feed-to-channel configuration.
bot_aggregator_helpImplementation of hook_help().
bot_aggregator_irc_bot_cron_fastestImplementation of hook_irc_bot_cron().
bot_aggregator_menuImplementation of hook_menu().
bot_aggregator_settingsConfigures the reporting of aggregated feed items via the bot.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables the reporting of aggregated feed items to configured channels.
  5. */
  6. /**
  7. * Implementation of hook_help().
  8. */
  9. function bot_aggregator_help($path, $arg) {
  10. switch ($path) {
  11. case 'irc:features':
  12. return array(t('Aggregator'));
  13. case 'irc:features#aggregator':
  14. return t('Enables the reporting of aggregated feed items to configured channels.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_menu().
  19. */
  20. function bot_aggregator_menu() {
  21. $items['admin/settings/bot/aggregator'] = array(
  22. 'access arguments' => array('administer bot'),
  23. 'description' => t('Configure the reporting of aggregated feed items via the bot.'),
  24. 'page callback' => 'drupal_get_form',
  25. 'page arguments' => array('bot_aggregator_settings'),
  26. 'title' => 'Bot Aggregator',
  27. );
  28. return $items;
  29. }
  30. /**
  31. * Implementation of hook_irc_bot_cron().
  32. */
  33. function bot_aggregator_irc_bot_cron_fastest() {
  34. $channels = bot_aggregator_feed_channels();
  35. $last_timestamp = variable_get('bot_aggregator_last_timestamp', 0);
  36. if ($last_timestamp == 0) { // first check ever? start 5 minutes ago.
  37. variable_set('bot_aggregator_last_timestamp', time() - (60 * 5));
  38. $last_timestamp = variable_get('bot_aggregator_last_timestamp', 0);
  39. }
  40. // every 15 seconds, we'll display a maximum of three feed items that have come in since our last timestamp.
  41. $results = db_query_range('SELECT ai.timestamp, ai.title, ai.link, af.fid, af.title AS feed_title FROM {aggregator_item} ai LEFT
  42. JOIN {aggregator_feed} af ON ai.fid = af.fid WHERE ai.timestamp > %d ORDER BY ai.timestamp ASC', $last_timestamp, 0, 3);
  43. while ($result = db_fetch_object($results)) {
  44. if (isset($channels[$result->fid])) {
  45. $message = t('!feed => !title => !link', array(
  46. '!feed' => decode_entities(strip_tags($result->feed_title)),
  47. '!title' => decode_entities(strip_tags($result->title)),
  48. '!link' => decode_entities(strip_tags($result->link)),
  49. ));
  50. foreach ($channels[$result->fid] as $channel) {
  51. bot_message($channel, $message);
  52. }
  53. }
  54. variable_set('bot_aggregator_last_timestamp', $result->timestamp);
  55. }
  56. }
  57. /**
  58. * Implementation of hook_form_alter().
  59. */
  60. function bot_aggregator_form_aggregator_form_feed_alter(&$form, $form_state) {
  61. // get a list of all joined channels, sans passwords.
  62. $joined_channels = preg_split('/\s*,\s*/', variable_get('bot_channels', '#test'));
  63. $channel_options = array(); // HOW MAY I HELP YOU!?
  64. foreach ($joined_channels as $k => $v) {
  65. $channel = preg_replace('/(.*) .*/', '\1', $v);
  66. $channel_options[$channel] = $channel;
  67. }
  68. $channels = bot_aggregator_feed_channels();
  69. // we don't use checkboxes because it doesn't want
  70. // to accept an array key name of "#channel".
  71. $form['bot_aggregator_channels'] = array (
  72. '#default_value' => $channels[$form['fid']['#value']],
  73. '#description' => t('Select the channels which this feed\'s items should be reported to. If this feed is new or has never been updated prior to selecting IRC channels, all feed items may be considered "new" and reported to IRC. This might cause 10 or more messages to appear before the bot gets into its normal update rhythm.'),
  74. '#multiple' => TRUE,
  75. '#options' => $channel_options,
  76. '#size' => 10,
  77. '#type' => 'select',
  78. '#title' => t('IRC channels'),
  79. );
  80. // put the save and delete buttons below the above.
  81. $form['delete']['#weight'] = $form['submit']['#weight'] = 5;
  82. // our custom submit handles the database saving.
  83. $form['#submit'][] = 'bot_aggregator_form_aggregator_form_feed_submit';
  84. }
  85. /**
  86. * FAPI #submit handler; saves the feed-to-channel configuration.
  87. */
  88. function bot_aggregator_form_aggregator_form_feed_submit($form, &$form_state) {
  89. db_query('DELETE FROM {bot_aggregator} WHERE fid = %d', $form_state['values']['fid']);
  90. // our config has been deleted above. add it back unless the entire feed is being deleted.
  91. if (count($form_state['values']['bot_aggregator_channels']) && $form_state['values']['op'] != t('Delete')) {
  92. $fid = isset($form_state['values']['fid']) ? $form_state['values']['fid'] // brand new addition? hunt for fid.
  93. : db_result(db_query('SELECT fid FROM {aggregator_feed} WHERE url = "%s"', $form_state['values']['url']));
  94. foreach ($form_state['values']['bot_aggregator_channels'] as $channel) {
  95. $record = new stdClass();
  96. $record->fid = $fid;
  97. $record->channel = $channel;
  98. drupal_write_record('bot_aggregator', $record);
  99. }
  100. }
  101. }
  102. /**
  103. * Return the list of all feed-to-channel configurations.
  104. */
  105. function bot_aggregator_feed_channels() {
  106. $channels = array(); // got nothing? return nothing.
  107. $results = db_query('SELECT * FROM {bot_aggregator}');
  108. while ($result = db_fetch_object($results)) {
  109. $channels[$result->fid][$result->channel] = $result->channel;
  110. } // NP: The Backyardigans with Julia nearby. Scarlett sleepy.
  111. return $channels;
  112. }
  113. /**
  114. * Configures the reporting of aggregated feed items via the bot.
  115. */
  116. function bot_aggregator_settings() {
  117. $form['redirection'] = array(
  118. '#value' => '<p>' . t('To define which channels an aggregated feed is reported to, edit the <a href="@url">feed configuration itself</a>.',
  119. array('@url' => url('admin/content/aggregator'))) . '</p>', // just a helpful bit of redirection in lieu of a README no one will see.
  120. );
  121. return $form;
  122. }