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