/[drupal]/contributions/modules/feedapi/feedapi_feed/feedapi_feed.module
ViewVC logotype

Contents of /contributions/modules/feedapi/feedapi_feed/feedapi_feed.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.16 - (show annotations) (download) (as text)
Tue Jul 24 12:51:33 2007 UTC (2 years, 4 months ago) by aronnovak
Branch: MAIN
CVS Tags: DRUPAL-5--0-1, HEAD
Branch point for: DRUPAL-5
Changes since 1.15: +5 -1 lines
File MIME type: text/x-php
Easy access to per-feed settings (feedapi_feed)
1 <?php
2 // $Id: feedapi_feed.module,v 1.15 2007/07/24 12:36:47 aronnovak Exp $
3
4 /**
5 * @file
6 * Handle how the feeds are represented
7 * Create and manage the nodes from the feeds
8 * Some basic possibilities: nodes, aggregator-like items
9 */
10
11 define('FEEDAPI_FEED_NEVER_DELETE_OLD', -1);
12
13 /**
14 * Implementation of hook_help().
15 */
16 function feedapi_feed_help($section) {
17 switch ($section) {
18 case 'admin/help#feedapi_feed':
19 return t('Provides a feed processor for the FeedAPI. It transforms feeds into nodes.');
20 }
21 }
22
23 /**
24 * Implementation of hook_menu().
25 */
26 function feedapi_feed_menu($may_cache) {
27 if ($may_cache) {
28 $items[] = array('path' => 'admin/settings/feedapi_feed',
29 'title' => t('Node-Feed processor'),
30 'callback' => 'drupal_get_form',
31 'callback arguments' => array('feedapi_feed_admin_settings'),
32 'type' => MENU_NORMAL_ITEM,
33 'access' => user_access('administer feedapi'),
34 );
35 }
36 return $items;
37 }
38
39 /**
40 * Implementation of hook_nodeapi().
41 */
42 function feedapi_feed_nodeapi(&$node, $op, $teaser, $page) {
43 if ($op == 'delete') {
44 $result = db_query("SELECT fid FROM {feedapi_node_feed} WHERE nid = %d", $node->nid);
45 if (db_num_rows($result) > 0) {
46 $feed = db_fetch_object($result);
47 feedapi_invoke_feedapi('purge', $feed);
48 }
49 }
50 }
51
52 /**
53 * Implementation of hook_link().
54 */
55 function feedapi_feed_link($type, $node = NULL, $teaser = FALSE) {
56 if ($type == 'node') {
57 $result = db_query("SELECT link, fid FROM {feedapi_node_feed} WHERE nid = %d", $node->nid);
58 if (db_num_rows($result) > 0) {
59 $feed = db_fetch_object($result);
60 if (variable_get('feedapi_feed_show_feed_origin_link', TRUE)) {
61 $links['feedapi_original'] = array(
62 'title' => t('Link to the site'),
63 'href' => $feed->link,
64 );
65 }
66 global $user;
67 feedapi_invoke_feedapi('load', $feed);
68 $own_feed = $feed->uid == $user->uid && user_access('handle own feeds') ? TRUE : FALSE;
69 if (variable_get('feedapi_feed_show_commands_link', TRUE) && (user_access('administer feedapi') || $own_feed)) {
70 $links['feedapi_refresh'] = array(
71 'title' => t('Refresh the feed'),
72 'href' => 'feed/'. $feed->fid .'/refresh',
73 );
74 $links['feedapi_delete'] = array(
75 'title' => t('Delete the feed'),
76 'href' => 'feed/'. $feed->fid .'/delete',
77 );
78 $links['feedapi_edit'] = array(
79 'title' => t('Adjust settings'),
80 'href' => 'feed/'. $feed->fid .'/edit',
81 );
82 }
83 return $links;
84 }
85 }
86 }
87
88 /**
89 * Implementation of hook_form_alter().
90 */
91 function feedapi_feed_form_alter($form_id, &$form) {
92 if ($form_id == 'feedapi_edit_page') {
93 $feed = new stdClass();
94 if (isset($form['fid']['#value'])) {
95 $feed->fid = $form['fid']['#value'];
96 feedapi_invoke_feedapi('load', $feed);
97 if (in_array('feedapi_feed', $feed->processors_feed)) {
98 $node = node_load($feed->nid);
99 if (isset($node->uid)) {
100 $user_current = user_load(array('uid' => $node->uid));
101 }
102 global $user;
103 $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 3628800, 4838400, 7257600, 15724800, 31536000), 'format_interval');
104 $period[FEEDAPI_FEED_NEVER_DELETE_OLD] = t('Never');
105 $form['processors_settings']['items_delete'] = array(
106 '#type' => 'select',
107 '#title' => t('Delete news items older than'),
108 '#options' => $period,
109 '#default_value' => is_numeric($feed->items_delete) ? $feed->items_delete : variable_get('feedapi_feed_items_delete', 15724800)
110 );
111 $form['processors_settings']['feed_content_type'] = array(
112 '#type' => 'select',
113 '#title' => t('The content-type of the feed'),
114 '#default_value' => isset($node->type) ? $node->type : variable_get('feedapi_feed_type', 'story'),
115 '#options' => drupal_map_assoc(array_keys(node_get_types())),
116 );
117 $form['processors_settings']['feed_node_user'] = array(
118 '#type' => 'textfield',
119 '#title' => t('The author of the feed node'),
120 '#default_value' => isset($user_current->name) ? $user_current->name : $user->name,
121 '#autocomplete_path' => 'user/autocomplete',
122 );
123 $form['processors_settings']['feed_node_date'] = array(
124 '#type' => 'radios',
125 '#title' => t('Date of the feed node'),
126 '#options' => array('current' => t('The current server time'), t('no') => 'Do not change'),
127 '#default_value' => 'no',
128 );
129 }
130 }
131 }
132 }
133
134 /**
135 * Create the given feed content-representation
136 * Implementation of hook_feedapi_save().
137 *
138 * @param stdClass $feed
139 * The object of the feed
140 * @return boolean
141 * TRUE on success, FALSE on fail
142 */
143 function feedapi_feed_feedapi_save($feed) {
144 $node = new stdClass();
145 $node->type = isset($feed->feed_content_type) ? $feed->feed_content_type: variable_get('feedapi_feed_type', 'story');
146 // Get the default options from the content type's default
147 $options = variable_get('node_options_'. $node->type, FALSE);
148 if (is_array($options)) {
149 $node->status = in_array('status', $options) ? 1 : 0;
150 $node->promote = in_array('promote', $options) ? 1 : 0;
151 $node->sticky = in_array('sticky', $options) ? 1 : 0;
152 }
153 if (isset($feed->feed_node_date)) {
154 switch ($feed->feed_node_date) {
155 case 'current':
156 $node->changed = $node->created = time();
157 break;
158 }
159 }
160 $node->title = $feed->title;
161 $node->body = $feed->description;
162 if (isset($feed->feed_node_user)) {
163 $user = user_load(array('name' => $feed->feed_node_user));
164 }
165 else {
166 global $user;
167 }
168 $node->uid = $user->uid;
169 $items_delete = isset($feed->items_delete) ? $feed->items_delete : variable_get('feedapi_feed_items_delete', 15724800);
170 if (!isset($feed->nid)) {
171 node_save($node);
172 $feed->nid = $node->nid;
173 db_query("INSERT INTO {feedapi_node_feed} (fid, nid, link, items_delete) VALUES (%d, %d, '%s', %d)", $feed->fid, $feed->nid, $feed->options->link, $items_delete);
174 }
175 else {
176 $node->nid = $feed->nid;
177 node_save($node);
178 db_query("UPDATE {feedapi_node_feed} SET items_delete = %d, link = '%s' WHERE fid = %d", $items_delete, $feed->options->link, $feed->fid);
179 }
180 return $feed;
181 }
182
183 /**
184 * Update the given feed content-representation
185 * Implementation of hook_feedapi_update().
186 *
187 * @param stdClass $feed
188 * The object of the feed
189 * @return boolean
190 * TRUE on success, FALSE on fail
191 */
192 function feedapi_feed_feedapi_update($feed) {
193 $node = db_fetch_object(db_query("SELECT nid FROM {feedapi_node_feed} WHERE fid = %d", $feed->fid));
194 $feed->nid = $node->nid;
195 feedapi_feed_feedapi_save($feed);
196 return $feed;
197 }
198
199 /**
200 * Delete the given feed node-representation
201 * Implementation of hook_feedapi_delete().
202 *
203 * @param stdClass $feed
204 * The object of the feed
205 * @return boolean
206 * TRUE on success, FALSE on fail
207 */
208 function feedapi_feed_feedapi_delete($feed) {
209 db_query("DELETE FROM {feedapi_node_feed} WHERE fid = %d", $feed->fid);
210 node_delete($feed->nid);
211 }
212
213 /**
214 * Load the feed if exists, tell if not exists
215 *
216 * @param $feed
217 * Feed object
218 * @return
219 * FALSE if the feed is not exist in the processor, the Feed object otherwise
220 */
221 function feedapi_feed_feedapi_load($feed) {
222 $item = db_fetch_object(db_query("SELECT * FROM {feedapi_node_feed} WHERE fid = %d", $feed->fid));
223 if (!is_numeric($item->nid)) {
224 return FALSE;
225 }
226 $feed->nid = $item->nid;
227 $feed->items_delete = $item->items_delete;
228 $feed->items_promote = $item->items_promote;
229 $node = node_load($feed->nid);
230 $feed->title = $node->title;
231 $feed->description = $node->body;
232 $feed->options->original_url = $item->link;
233 return $feed;
234 }
235
236 /**
237 * Check the format of the given URL or other validation
238 * Implementation of hook_feedapi_validate().
239 *
240 * @param string $url
241 * URL (not only web URLs)
242 * @return
243 * Feed object
244 */
245 function feedapi_feed_feedapi_validate($form_id, $form_values, $feed) {
246 // Here we can decide the validity of the URL
247 // Maybe it should checks if the URL returns 200 or not
248 $valid_url = "`(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?`";
249 if (preg_match($valid_url, $form_values['url'])) {
250 }
251 else {
252 form_set_error('url', t('The URL entered is not valid. It should be in the format of: %url_format', array('%url_format'=>'http://www.example.com/rss.xml')));
253 }
254 return $feed;
255 }
256
257 /**
258 * Tell which feed types are compatible with this submodule.
259 * For eg. "XML feed", "FTP", "mailing list"
260 * Implementation of hook_feedapi_type().
261 *
262 * @return array
263 * A string array of compatible types
264 */
265 function feedapi_feed_feedapi_type() {
266 return array("XML feed");
267 }
268
269 /**
270 * Get the content from the given URL
271 * Implementation of hook_feedapi_get().
272 *
273 * @param $url
274 * A valid URL (not only web URLs)
275 * @param $username
276 * If the URL use authentication, here you can supply the username for this
277 * @param $password
278 * If the URL use authentication, here you can supply the password for this
279 * @return string
280 * The data pulled from the URL
281 */
282 function feedapi_feed_feedapi_get($url, $username = NULL, $password = NULL) {
283 $method = 'GET';
284 $follow = 3;
285 $data = NULL;
286 $timeout = 15;
287
288 if (!function_exists('curl_init')) {
289 // Conventional way - use drupal internal download mechanism
290 $headers = array();
291 if ($username != NULL && $password != NULL) {
292 $headers['Authorization'] = 'Basic: '. base64_encode("$username:$password");
293 }
294 $result = drupal_http_request($url, $headers, $method, $data, $follow);
295 return $result->data;
296 }
297
298 // Solid way - use cURL
299 $ch = curl_init();
300 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
301 curl_setopt($ch, CURLOPT_URL, $url);
302 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
303 curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
304 curl_setopt($ch, CURLOPT_USERAGENT, 'Drupal');
305
306 if (strlen($username) > 0 && strlen($password) > 0) {
307 curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
308 }
309 $result = curl_exec($ch);
310
311 if (curl_errno($ch) != 0) {
312 watchdog("feedapi", $url . " : " . curl_error($ch), WATCHDOG_WARNING);
313 curl_close($ch);
314 unset($ch);
315 return FALSE;
316 }
317 //
318 return $result;
319
320 }
321
322 /**
323 * Delete expired items and return informations about the feed refreshing
324 * Implementation of hook_feedapi_expire().
325 *
326 * @param $feed
327 * The feed object
328 * @param $force
329 * If TRUE, do not examine that the feed needs refresh
330 * @return
331 * FALSE if the feed don't have to be refreshed. (forbidden if the $force is TRUE)
332 * The number of maximal new elements created otherwise
333 */
334 function feedapi_feed_feedapi_expire($feed, $force = FALSE) {
335 $needs_refresh = time() - $feed->checked > $feed->refresh;
336 if (($needs_refresh || $force) && $feed->items_delete != FEEDAPI_FEED_NEVER_DELETE_OLD) {
337 foreach ($feed->items as $item) {
338 if (time() - $item->arrived > $feed->items_delete) {
339 foreach($feed->processors_item as $processor) {
340 module_invoke($processor, 'feedapi_item_delete', $item);
341 }
342 }
343 }
344 }
345 if ($needs_refresh == FALSE && $force == FALSE) {
346 return FALSE;
347 }
348 return variable_get('feedapi_feed_items_one_round', 15);
349 }
350
351 /**
352 * Settings:
353 * Content-type of feeds
354 */
355 function feedapi_feed_admin_settings() {
356 $form['feedapi_feed_type'] = array(
357 '#type' => 'select', '#title' => t('Feed content-type'),
358 '#default_value' => variable_get('feedapi_feed_type', 'story'),
359 '#options' => drupal_map_assoc(array_keys(node_get_types())),
360 '#description' => t('The default type of the feeds.')
361 );
362 $form['feedapi_feed_items_one_round'] = array(
363 '#type' => 'textbox', '#title' => t('Process items in one round'),
364 '#default_value' => variable_get('feedapi_feed_items_one_round', 15),
365 '#description' => t('In one refresh round how many feed items should be processed.')
366 );
367 $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 3628800, 4838400, 7257600, 15724800, 31536000), 'format_interval');
368 $period[FEEDAPI_FEED_NEVER_DELETE_OLD] = t('Never');
369 $form['feedapi_feed_items_delete'] = array(
370 '#type' => 'select',
371 '#title' => t('Delete news items older than'),
372 '#options' => $period,
373 '#default_value' => variable_get('feedapi_feed_items_delete', 15724800)
374 );
375 $form['feedapi_feed_show_feed_origin_link'] = array(
376 '#type' => 'checkbox',
377 '#title' => t('At the node of the feed, show the link to the original site'),
378 '#default_value' => variable_get('feedapi_feed_show_feed_origin_link', TRUE),
379 );
380 $form['feedapi_feed_show_commands_link'] = array(
381 '#type' => 'checkbox',
382 '#title' => t('At the node of the feed, show the link to the feed commands (refresh / delete / etc.)'),
383 '#default_value' => variable_get('feedapi_feed_show_commands_link', TRUE),
384 );
385 $form['processors_settings']['feed_node_date'] = array(
386 '#type' => 'radios',
387 '#title' => t('Date of the feed node'),
388 '#options' => array('feed' => t('Comes from the feed'), 'current' => t('The current server time')),
389 '#default_value' => variable_get('feedapi_feed_node_date', 'feed'),
390 );
391 return system_settings_form($form);
392 }

  ViewVC Help
Powered by ViewVC 1.1.2