Skip navigation
Help

aggregator.processor.inc

  1. drupal
    1. 7 drupal/modules/aggregator/aggregator.processor.inc

Processor functions for the aggregator module.

Functions & methods

NameDescription
aggregator_aggregator_processImplements hook_aggregator_process().
aggregator_aggregator_process_infoImplements hook_aggregator_process_info().
aggregator_aggregator_removeImplements hook_aggregator_remove().
aggregator_expireExpire feed items on $feed that are older than aggregator_clear.
aggregator_form_aggregator_admin_form_alterImplements hook_form_aggregator_admin_form_alter().
aggregator_save_itemAdd/edit/delete an aggregator item.
_aggregator_charactersHelper function for teaser length choices.

File

drupal/modules/aggregator/aggregator.processor.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Processor functions for the aggregator module.
  5. */
  6. /**
  7. * Implements hook_aggregator_process_info().
  8. */
  9. function aggregator_aggregator_process_info() {
  10. return array(
  11. 'title' => t('Default processor'),
  12. 'description' => t('Creates lightweight records from feed items.'),
  13. );
  14. }
  15. /**
  16. * Implements hook_aggregator_process().
  17. */
  18. function aggregator_aggregator_process($feed) {
  19. if (is_object($feed)) {
  20. if (is_array($feed->items)) {
  21. foreach ($feed->items as $item) {
  22. // Save this item. Try to avoid duplicate entries as much as possible. If
  23. // we find a duplicate entry, we resolve it and pass along its ID is such
  24. // that we can update it if needed.
  25. if (!empty($item['guid'])) {
  26. $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND guid = :guid", array(':fid' => $feed->fid, ':guid' => $item['guid']))->fetchObject();
  27. }
  28. elseif ($item['link'] && $item['link'] != $feed->link && $item['link'] != $feed->url) {
  29. $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND link = :link", array(':fid' => $feed->fid, ':link' => $item['link']))->fetchObject();
  30. }
  31. else {
  32. $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND title = :title", array(':fid' => $feed->fid, ':title' => $item['title']))->fetchObject();
  33. }
  34. if (!$item['timestamp']) {
  35. $item['timestamp'] = isset($entry->timestamp) ? $entry->timestamp : REQUEST_TIME;
  36. }
  37. aggregator_save_item(array('iid' => (isset($entry->iid) ? $entry->iid : ''), 'fid' => $feed->fid, 'timestamp' => $item['timestamp'], 'title' => $item['title'], 'link' => $item['link'], 'author' => $item['author'], 'description' => $item['description'], 'guid' => $item['guid']));
  38. }
  39. }
  40. }
  41. }
  42. /**
  43. * Implements hook_aggregator_remove().
  44. */
  45. function aggregator_aggregator_remove($feed) {
  46. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchCol();
  47. if ($iids) {
  48. db_delete('aggregator_category_item')
  49. ->condition('iid', $iids, 'IN')
  50. ->execute();
  51. }
  52. db_delete('aggregator_item')
  53. ->condition('fid', $feed->fid)
  54. ->execute();
  55. drupal_set_message(t('The news items from %site have been removed.', array('%site' => $feed->title)));
  56. }
  57. /**
  58. * Implements hook_form_aggregator_admin_form_alter().
  59. *
  60. * Form alter aggregator module's own form to keep processor functionality
  61. * separate from aggregator API functionality.
  62. */
  63. function aggregator_form_aggregator_admin_form_alter(&$form, $form_state) {
  64. if (in_array('aggregator', variable_get('aggregator_processors', array('aggregator')))) {
  65. $info = module_invoke('aggregator', 'aggregator_process', 'info');
  66. $items = drupal_map_assoc(array(3, 5, 10, 15, 20, 25), '_aggregator_items');
  67. $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
  68. $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
  69. // Only wrap into a collapsible fieldset if there is a basic configuration.
  70. if (isset($form['basic_conf'])) {
  71. $form['modules']['aggregator'] = array(
  72. '#type' => 'fieldset',
  73. '#title' => t('Default processor settings'),
  74. '#description' => $info['description'],
  75. '#collapsible' => TRUE,
  76. '#collapsed' => !in_array('aggregator', variable_get('aggregator_processors', array('aggregator'))),
  77. );
  78. }
  79. else {
  80. $form['modules']['aggregator'] = array();
  81. }
  82. $form['modules']['aggregator']['aggregator_summary_items'] = array(
  83. '#type' => 'select',
  84. '#title' => t('Number of items shown in listing pages'),
  85. '#default_value' => variable_get('aggregator_summary_items', 3),
  86. '#empty_value' => 0,
  87. '#options' => $items,
  88. );
  89. $form['modules']['aggregator']['aggregator_clear'] = array(
  90. '#type' => 'select',
  91. '#title' => t('Discard items older than'),
  92. '#default_value' => variable_get('aggregator_clear', 9676800),
  93. '#options' => $period,
  94. '#description' => t('Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
  95. );
  96. $form['modules']['aggregator']['aggregator_category_selector'] = array(
  97. '#type' => 'radios',
  98. '#title' => t('Select categories using'),
  99. '#default_value' => variable_get('aggregator_category_selector', 'checkboxes'),
  100. '#options' => array('checkboxes' => t('checkboxes'),
  101. 'select' => t('multiple selector')),
  102. '#description' => t('For a small number of categories, checkboxes are easier to use, while a multiple selector works well with large numbers of categories.'),
  103. );
  104. $form['modules']['aggregator']['aggregator_teaser_length'] = array(
  105. '#type' => 'select',
  106. '#title' => t('Length of trimmed description'),
  107. '#default_value' => 600,
  108. '#options' => drupal_map_assoc(array(0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000), '_aggregator_characters'),
  109. '#description' => t("The maximum number of characters used in the trimmed version of content.")
  110. );
  111. }
  112. }
  113. /**
  114. * Helper function for teaser length choices.
  115. */
  116. function _aggregator_characters($length) {
  117. return ($length == 0) ? t('Unlimited') : format_plural($length, '1 character', '@count characters');
  118. }
  119. /**
  120. * Add/edit/delete an aggregator item.
  121. *
  122. * @param $edit
  123. * An associative array describing the item to be added/edited/deleted.
  124. */
  125. function aggregator_save_item($edit) {
  126. if ($edit['title'] && empty($edit['iid'])) {
  127. $edit['iid'] = db_insert('aggregator_item')
  128. ->fields(array(
  129. 'title' => $edit['title'],
  130. 'link' => $edit['link'],
  131. 'author' => $edit['author'],
  132. 'description' => $edit['description'],
  133. 'guid' => $edit['guid'],
  134. 'timestamp' => $edit['timestamp'],
  135. 'fid' => $edit['fid'],
  136. ))
  137. ->execute();
  138. }
  139. if ($edit['iid'] && !$edit['title']) {
  140. db_delete('aggregator_item')
  141. ->condition('iid', $edit['iid'])
  142. ->execute();
  143. db_delete('aggregator_category_item')
  144. ->condition('iid', $edit['iid'])
  145. ->execute();
  146. }
  147. elseif ($edit['title'] && $edit['link']) {
  148. // file the items in the categories indicated by the feed
  149. $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $edit['fid']));
  150. foreach ($result as $category) {
  151. db_merge('aggregator_category_item')
  152. ->key(array(
  153. 'iid' => $edit['iid'],
  154. 'cid' => $category->cid,
  155. ))
  156. ->execute();
  157. }
  158. }
  159. }
  160. /**
  161. * Expire feed items on $feed that are older than aggregator_clear.
  162. *
  163. * @param $feed
  164. * Object describing feed.
  165. */
  166. function aggregator_expire($feed) {
  167. $aggregator_clear = variable_get('aggregator_clear', 9676800);
  168. if ($aggregator_clear != AGGREGATOR_CLEAR_NEVER) {
  169. // Remove all items that are older than flush item timer.
  170. $age = REQUEST_TIME - $aggregator_clear;
  171. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid AND timestamp < :timestamp', array(
  172. ':fid' => $feed->fid,
  173. ':timestamp' => $age,
  174. ))
  175. ->fetchCol();
  176. if ($iids) {
  177. db_delete('aggregator_category_item')
  178. ->condition('iid', $iids, 'IN')
  179. ->execute();
  180. db_delete('aggregator_item')
  181. ->condition('iid', $iids, 'IN')
  182. ->execute();
  183. }
  184. }
  185. }