aggregator.processor.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Make sure the item title fits in 255 varchar column.
  38. $item['title'] = truncate_utf8($item['title'], 255, TRUE, TRUE);
  39. 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']));
  40. }
  41. }
  42. }
  43. }
  44. /**
  45. * Implements hook_aggregator_remove().
  46. */
  47. function aggregator_aggregator_remove($feed) {
  48. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchCol();
  49. if ($iids) {
  50. db_delete('aggregator_category_item')
  51. ->condition('iid', $iids, 'IN')
  52. ->execute();
  53. }
  54. db_delete('aggregator_item')
  55. ->condition('fid', $feed->fid)
  56. ->execute();
  57. drupal_set_message(t('The news items from %site have been removed.', array('%site' => $feed->title)));
  58. }
  59. /**
  60. * Implements hook_form_aggregator_admin_form_alter().
  61. *
  62. * Form alter aggregator module's own form to keep processor functionality
  63. * separate from aggregator API functionality.
  64. */
  65. function aggregator_form_aggregator_admin_form_alter(&$form, $form_state) {
  66. if (in_array('aggregator', variable_get('aggregator_processors', array('aggregator')))) {
  67. $info = module_invoke('aggregator', 'aggregator_process', 'info');
  68. $items = drupal_map_assoc(array(3, 5, 10, 15, 20, 25), '_aggregator_items');
  69. $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
  70. $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
  71. // Only wrap into a collapsible fieldset if there is a basic configuration.
  72. if (isset($form['basic_conf'])) {
  73. $form['modules']['aggregator'] = array(
  74. '#type' => 'fieldset',
  75. '#title' => t('Default processor settings'),
  76. '#description' => $info['description'],
  77. '#collapsible' => TRUE,
  78. '#collapsed' => !in_array('aggregator', variable_get('aggregator_processors', array('aggregator'))),
  79. );
  80. }
  81. else {
  82. $form['modules']['aggregator'] = array();
  83. }
  84. $form['modules']['aggregator']['aggregator_summary_items'] = array(
  85. '#type' => 'select',
  86. '#title' => t('Number of items shown in listing pages'),
  87. '#default_value' => variable_get('aggregator_summary_items', 3),
  88. '#empty_value' => 0,
  89. '#options' => $items,
  90. );
  91. $form['modules']['aggregator']['aggregator_clear'] = array(
  92. '#type' => 'select',
  93. '#title' => t('Discard items older than'),
  94. '#default_value' => variable_get('aggregator_clear', 9676800),
  95. '#options' => $period,
  96. '#description' => t('Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
  97. );
  98. $form['modules']['aggregator']['aggregator_category_selector'] = array(
  99. '#type' => 'radios',
  100. '#title' => t('Select categories using'),
  101. '#default_value' => variable_get('aggregator_category_selector', 'checkboxes'),
  102. '#options' => array('checkboxes' => t('checkboxes'),
  103. 'select' => t('multiple selector')),
  104. '#description' => t('For a small number of categories, checkboxes are easier to use, while a multiple selector works well with large numbers of categories.'),
  105. );
  106. $form['modules']['aggregator']['aggregator_teaser_length'] = array(
  107. '#type' => 'select',
  108. '#title' => t('Length of trimmed description'),
  109. '#default_value' => variable_get('aggregator_teaser_length', 600),
  110. '#options' => drupal_map_assoc(array(0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000), '_aggregator_characters'),
  111. '#description' => t("The maximum number of characters used in the trimmed version of content.")
  112. );
  113. }
  114. }
  115. /**
  116. * Creates display text for teaser length option values.
  117. *
  118. * Callback for drupal_map_assoc() within
  119. * aggregator_form_aggregator_admin_form_alter().
  120. */
  121. function _aggregator_characters($length) {
  122. return ($length == 0) ? t('Unlimited') : format_plural($length, '1 character', '@count characters');
  123. }
  124. /**
  125. * Adds/edits/deletes an aggregator item.
  126. *
  127. * @param $edit
  128. * An associative array describing the item to be added/edited/deleted.
  129. */
  130. function aggregator_save_item($edit) {
  131. if ($edit['title'] && empty($edit['iid'])) {
  132. $edit['iid'] = db_insert('aggregator_item')
  133. ->fields(array(
  134. 'title' => $edit['title'],
  135. 'link' => $edit['link'],
  136. 'author' => $edit['author'],
  137. 'description' => $edit['description'],
  138. 'guid' => $edit['guid'],
  139. 'timestamp' => $edit['timestamp'],
  140. 'fid' => $edit['fid'],
  141. ))
  142. ->execute();
  143. }
  144. if ($edit['iid'] && !$edit['title']) {
  145. db_delete('aggregator_item')
  146. ->condition('iid', $edit['iid'])
  147. ->execute();
  148. db_delete('aggregator_category_item')
  149. ->condition('iid', $edit['iid'])
  150. ->execute();
  151. }
  152. elseif ($edit['title'] && $edit['link']) {
  153. // file the items in the categories indicated by the feed
  154. $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $edit['fid']));
  155. foreach ($result as $category) {
  156. db_merge('aggregator_category_item')
  157. ->key(array(
  158. 'iid' => $edit['iid'],
  159. 'cid' => $category->cid,
  160. ))
  161. ->execute();
  162. }
  163. }
  164. }
  165. /**
  166. * Expires items from a feed depending on expiration settings.
  167. *
  168. * @param $feed
  169. * Object describing feed.
  170. */
  171. function aggregator_expire($feed) {
  172. $aggregator_clear = variable_get('aggregator_clear', 9676800);
  173. if ($aggregator_clear != AGGREGATOR_CLEAR_NEVER) {
  174. // Remove all items that are older than flush item timer.
  175. $age = REQUEST_TIME - $aggregator_clear;
  176. $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid AND timestamp < :timestamp', array(
  177. ':fid' => $feed->fid,
  178. ':timestamp' => $age,
  179. ))
  180. ->fetchCol();
  181. if ($iids) {
  182. db_delete('aggregator_category_item')
  183. ->condition('iid', $iids, 'IN')
  184. ->execute();
  185. db_delete('aggregator_item')
  186. ->condition('iid', $iids, 'IN')
  187. ->execute();
  188. }
  189. }
  190. }