aggregator.processor.inc 7.9 KB

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