/[drupal]/drupal/modules/aggregator/aggregator.module
ViewVC logotype

Contents of /drupal/modules/aggregator/aggregator.module

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


Revision 1.428 - (show annotations) (download) (as text)
Sun Nov 1 17:50:45 2009 UTC (3 weeks, 5 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.427: +2 -2 lines
File MIME type: text/x-php
- Patch #402280 by mfb: fixed PDOexception.
1 <?php
2 // $Id: aggregator.module,v 1.427 2009/10/23 22:24:11 webchick Exp $
3
4 /**
5 * @file
6 * Used to aggregate syndicated content (RSS, RDF, and Atom).
7 */
8
9 /**
10 * Denotes that a feed's items should never expire.
11 */
12 define('AGGREGATOR_CLEAR_NEVER', 0);
13
14 /**
15 * Implement hook_help().
16 */
17 function aggregator_help($path, $arg) {
18 switch ($path) {
19 case 'admin/help#aggregator':
20 $output = '<p>' . t('The aggregator is a powerful on-site syndicator and news reader that gathers fresh content from RSS-, RDF-, and Atom-based feeds made available across the web. Thousands of sites (particularly news sites and blogs) publish their latest headlines and posts in feeds, using a number of standardized XML-based formats. Formats supported by the aggregator include <a href="@rss">RSS</a>, <a href="@rdf">RDF</a>, and <a href="@atom">Atom</a>.', array('@rss' => 'http://cyber.law.harvard.edu/rss/', '@rdf' => 'http://www.w3.org/RDF/', '@atom' => 'http://www.atomenabled.org')) . '</p>';
21 $output .= '<p>' . t('Feeds contain feed items, or individual posts published by the site providing the feed. Feeds may be grouped in categories, generally by topic. Users view feed items in the <a href="@aggregator">main aggregator display</a> or by <a href="@aggregator-sources">their source</a>. Administrators can <a href="@feededit">add, edit and delete feeds</a> and choose how often to check each feed for newly updated items. The most recent items in either a feed or category can be displayed as a block through the <a href="@admin-block">blocks administration page</a>. A <a href="@aggregator-opml">machine-readable OPML file</a> of all feeds is available. A correctly configured <a href="@cron">cron maintenance task</a> is required to update feeds automatically.', array('@aggregator' => url('aggregator'), '@aggregator-sources' => url('aggregator/sources'), '@feededit' => url('admin/config/services/aggregator'), '@admin-block' => url('admin/structure/block'), '@aggregator-opml' => url('aggregator/opml'), '@cron' => url('admin/reports/status'))) . '</p>';
22 $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@aggregator">Aggregator module</a>.', array('@aggregator' => 'http://drupal.org/handbook/modules/aggregator/')) . '</p>';
23 return $output;
24 case 'admin/config/services/aggregator':
25 $output = '<p>' . t('Thousands of sites (particularly news sites and blogs) publish their latest headlines and posts in feeds, using a number of standardized XML-based formats. Formats supported by the aggregator include <a href="@rss">RSS</a>, <a href="@rdf">RDF</a>, and <a href="@atom">Atom</a>.', array('@rss' => 'http://cyber.law.harvard.edu/rss/', '@rdf' => 'http://www.w3.org/RDF/', '@atom' => 'http://www.atomenabled.org')) . '</p>';
26 $output .= '<p>' . t('Current feeds are listed below, and <a href="@addfeed">new feeds may be added</a>. For each feed or feed category, the <em>latest items</em> block may be enabled at the <a href="@block">blocks administration page</a>.', array('@addfeed' => url('admin/config/services/aggregator/add/feed'), '@block' => url('admin/structure/block'))) . '</p>';
27 return $output;
28 case 'admin/config/services/aggregator/add/feed':
29 return '<p>' . t('Add a feed in RSS, RDF or Atom format. A feed may only have one entry.') . '</p>';
30 case 'admin/config/services/aggregator/add/category':
31 return '<p>' . t('Categories allow feed items from different feeds to be grouped together. For example, several sport-related feeds may belong to a category named <em>Sports</em>. Feed items may be grouped automatically (by selecting a category when creating or editing a feed) or manually (via the <em>Categorize</em> page available from feed item listings). Each category provides its own feed page and block.') . '</p>';
32 case 'admin/config/services/aggregator/add/opml':
33 return '<p>' . t('<acronym title="Outline Processor Markup Language">OPML</acronym> is an XML format used to exchange multiple feeds between aggregators. A single OPML document may contain a collection of many feeds. Drupal can parse such a file and import all feeds at once, saving you the effort of adding them manually. You may either upload a local file from your computer or enter a URL where Drupal can download it.') . '</p>';
34 }
35 }
36
37 /**
38 * Implement hook_theme().
39 */
40 function aggregator_theme() {
41 return array(
42 'aggregator_wrapper' => array(
43 'variables' => array('content' => NULL),
44 'file' => 'aggregator.pages.inc',
45 'template' => 'aggregator-wrapper',
46 ),
47 'aggregator_categorize_items' => array(
48 'render element' => 'form',
49 'file' => 'aggregator.pages.inc',
50 ),
51 'aggregator_feed_source' => array(
52 'variables' => array('feed' => NULL),
53 'file' => 'aggregator.pages.inc',
54 'template' => 'aggregator-feed-source',
55 ),
56 'aggregator_block_item' => array(
57 'variables' => array('item' => NULL, 'feed' => 0),
58 ),
59 'aggregator_summary_items' => array(
60 'variables' => array('summary_items' => NULL, 'source' => NULL),
61 'file' => 'aggregator.pages.inc',
62 'template' => 'aggregator-summary-items',
63 ),
64 'aggregator_summary_item' => array(
65 'variables' => array('item' => NULL),
66 'file' => 'aggregator.pages.inc',
67 'template' => 'aggregator-summary-item',
68 ),
69 'aggregator_item' => array(
70 'variables' => array('item' => NULL),
71 'file' => 'aggregator.pages.inc',
72 'template' => 'aggregator-item',
73 ),
74 'aggregator_page_opml' => array(
75 'variables' => array('feeds' => NULL),
76 'file' => 'aggregator.pages.inc',
77 ),
78 'aggregator_page_rss' => array(
79 'variables' => array('feeds' => NULL, 'category' => NULL),
80 'file' => 'aggregator.pages.inc',
81 ),
82 );
83 }
84
85 /**
86 * Implement hook_menu().
87 */
88 function aggregator_menu() {
89 $items['admin/config/services/aggregator'] = array(
90 'title' => 'Feed aggregator',
91 'description' => "Configure which content your site aggregates from other sites, how often it polls them, and how they're categorized.",
92 'page callback' => 'aggregator_admin_overview',
93 'access arguments' => array('administer news feeds'),
94 'file' => 'aggregator.admin.inc',
95 );
96 $items['admin/config/services/aggregator/add/feed'] = array(
97 'title' => 'Add feed',
98 'page callback' => 'drupal_get_form',
99 'page arguments' => array('aggregator_form_feed'),
100 'access arguments' => array('administer news feeds'),
101 'type' => MENU_LOCAL_ACTION,
102 'file' => 'aggregator.admin.inc',
103 );
104 $items['admin/config/services/aggregator/add/category'] = array(
105 'title' => 'Add category',
106 'page callback' => 'drupal_get_form',
107 'page arguments' => array('aggregator_form_category'),
108 'access arguments' => array('administer news feeds'),
109 'type' => MENU_LOCAL_ACTION,
110 'file' => 'aggregator.admin.inc',
111 );
112 $items['admin/config/services/aggregator/add/opml'] = array(
113 'title' => 'Import OPML',
114 'page callback' => 'drupal_get_form',
115 'page arguments' => array('aggregator_form_opml'),
116 'access arguments' => array('administer news feeds'),
117 'type' => MENU_LOCAL_ACTION,
118 'file' => 'aggregator.admin.inc',
119 );
120 $items['admin/config/services/aggregator/remove/%aggregator_feed'] = array(
121 'title' => 'Remove items',
122 'page callback' => 'drupal_get_form',
123 'page arguments' => array('aggregator_admin_remove_feed', 5),
124 'access arguments' => array('administer news feeds'),
125 'type' => MENU_CALLBACK,
126 'file' => 'aggregator.admin.inc',
127 );
128 $items['admin/config/services/aggregator/update/%aggregator_feed'] = array(
129 'title' => 'Update items',
130 'page callback' => 'aggregator_admin_refresh_feed',
131 'page arguments' => array(5),
132 'access arguments' => array('administer news feeds'),
133 'type' => MENU_CALLBACK,
134 'file' => 'aggregator.admin.inc',
135 );
136 $items['admin/config/services/aggregator/list'] = array(
137 'title' => 'List',
138 'type' => MENU_DEFAULT_LOCAL_TASK,
139 'weight' => -10,
140 );
141 $items['admin/config/services/aggregator/settings'] = array(
142 'title' => 'Settings',
143 'description' => 'Configure the behavior of the feed aggregator, including when to discard feed items and how to present feed items and categories.',
144 'page callback' => 'drupal_get_form',
145 'page arguments' => array('aggregator_admin_form'),
146 'access arguments' => array('administer news feeds'),
147 'type' => MENU_LOCAL_TASK,
148 'file' => 'aggregator.admin.inc',
149 );
150 $items['aggregator'] = array(
151 'title' => 'Feed aggregator',
152 'page callback' => 'aggregator_page_last',
153 'access arguments' => array('access news feeds'),
154 'weight' => 5,
155 'file' => 'aggregator.pages.inc',
156 );
157 $items['aggregator/sources'] = array(
158 'title' => 'Sources',
159 'page callback' => 'aggregator_page_sources',
160 'access arguments' => array('access news feeds'),
161 'file' => 'aggregator.pages.inc',
162 );
163 $items['aggregator/categories'] = array(
164 'title' => 'Categories',
165 'page callback' => 'aggregator_page_categories',
166 'access callback' => '_aggregator_has_categories',
167 'file' => 'aggregator.pages.inc',
168 );
169 $items['aggregator/rss'] = array(
170 'title' => 'RSS feed',
171 'page callback' => 'aggregator_page_rss',
172 'access arguments' => array('access news feeds'),
173 'type' => MENU_CALLBACK,
174 'file' => 'aggregator.pages.inc',
175 );
176 $items['aggregator/opml'] = array(
177 'title' => 'OPML feed',
178 'page callback' => 'aggregator_page_opml',
179 'access arguments' => array('access news feeds'),
180 'type' => MENU_CALLBACK,
181 'file' => 'aggregator.pages.inc',
182 );
183 $items['aggregator/categories/%aggregator_category'] = array(
184 'title callback' => '_aggregator_category_title',
185 'title arguments' => array(2),
186 'page callback' => 'aggregator_page_category',
187 'page arguments' => array(2),
188 'access callback' => 'user_access',
189 'access arguments' => array('access news feeds'),
190 'file' => 'aggregator.pages.inc',
191 );
192 $items['aggregator/categories/%aggregator_category/view'] = array(
193 'title' => 'View',
194 'type' => MENU_DEFAULT_LOCAL_TASK,
195 'weight' => -10,
196 );
197 $items['aggregator/categories/%aggregator_category/categorize'] = array(
198 'title' => 'Categorize',
199 'page callback' => 'drupal_get_form',
200 'page arguments' => array('aggregator_page_category', 2),
201 'access arguments' => array('administer news feeds'),
202 'type' => MENU_LOCAL_TASK,
203 'file' => 'aggregator.pages.inc',
204 );
205 $items['aggregator/categories/%aggregator_category/configure'] = array(
206 'title' => 'Configure',
207 'page callback' => 'drupal_get_form',
208 'page arguments' => array('aggregator_form_category', 2),
209 'access arguments' => array('administer news feeds'),
210 'type' => MENU_LOCAL_TASK,
211 'weight' => 1,
212 'file' => 'aggregator.admin.inc',
213 );
214 $items['aggregator/sources/%aggregator_feed'] = array(
215 'page callback' => 'aggregator_page_source',
216 'page arguments' => array(2),
217 'access arguments' => array('access news feeds'),
218 'type' => MENU_CALLBACK,
219 'file' => 'aggregator.pages.inc',
220 );
221 $items['aggregator/sources/%aggregator_feed/view'] = array(
222 'title' => 'View',
223 'type' => MENU_DEFAULT_LOCAL_TASK,
224 'weight' => -10,
225 );
226 $items['aggregator/sources/%aggregator_feed/categorize'] = array(
227 'title' => 'Categorize',
228 'page callback' => 'drupal_get_form',
229 'page arguments' => array('aggregator_page_source', 2),
230 'access arguments' => array('administer news feeds'),
231 'type' => MENU_LOCAL_TASK,
232 'file' => 'aggregator.pages.inc',
233 );
234 $items['aggregator/sources/%aggregator_feed/configure'] = array(
235 'title' => 'Configure',
236 'page callback' => 'drupal_get_form',
237 'page arguments' => array('aggregator_form_feed', 2),
238 'access arguments' => array('administer news feeds'),
239 'type' => MENU_LOCAL_TASK,
240 'weight' => 1,
241 'file' => 'aggregator.admin.inc',
242 );
243 $items['admin/config/services/aggregator/edit/feed/%aggregator_feed'] = array(
244 'title' => 'Edit feed',
245 'page callback' => 'drupal_get_form',
246 'page arguments' => array('aggregator_form_feed', 6),
247 'access arguments' => array('administer news feeds'),
248 'type' => MENU_CALLBACK,
249 'file' => 'aggregator.admin.inc',
250 );
251 $items['admin/config/services/aggregator/edit/category/%aggregator_category'] = array(
252 'title' => 'Edit category',
253 'page callback' => 'drupal_get_form',
254 'page arguments' => array('aggregator_form_category', 6),
255 'access arguments' => array('administer news feeds'),
256 'type' => MENU_CALLBACK,
257 'file' => 'aggregator.admin.inc',
258 );
259
260 return $items;
261 }
262
263 /**
264 * Menu callback.
265 *
266 * @return
267 * An aggregator category title.
268 */
269 function _aggregator_category_title($category) {
270 return $category['title'];
271 }
272
273 /**
274 * Implement hook_init().
275 */
276 function aggregator_init() {
277 drupal_add_css(drupal_get_path('module', 'aggregator') . '/aggregator.css');
278 }
279
280 /**
281 * Find out whether there are any aggregator categories.
282 *
283 * @return
284 * TRUE if there is at least one category and the user has access to them, FALSE otherwise.
285 */
286 function _aggregator_has_categories() {
287 return user_access('access news feeds') && db_query('SELECT COUNT(*) FROM {aggregator_category}')->fetchField();
288 }
289
290 /**
291 * Implement hook_permission().
292 */
293 function aggregator_permission() {
294 return array(
295 'administer news feeds' => array(
296 'title' => t('Administer news feeds'),
297 'description' => t('Add, edit or delete news feeds that are aggregated to your site.'),
298 ),
299 'access news feeds' => array(
300 'title' => t('Access news feeds'),
301 'description' => t('View aggregated news feed items.'),
302 ),
303 );
304 }
305
306 /**
307 * Implement hook_cron().
308 *
309 * Queues news feeds for updates once their refresh interval has elapsed.
310 */
311 function aggregator_cron() {
312 $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < :time AND refresh != :never', array(
313 ':time' => REQUEST_TIME,
314 ':never' => AGGREGATOR_CLEAR_NEVER
315 ));
316 $queue = DrupalQueue::get('aggregator_feeds');
317 foreach ($result as $feed) {
318 $queue->createItem($feed);
319 }
320 }
321
322 /**
323 * Implement hook_cron_queue_info().
324 */
325 function aggregator_cron_queue_info() {
326 $queues['aggregator_feeds'] = array(
327 'worker callback' => 'aggregator_refresh',
328 'time' => 60,
329 );
330 return $queues;
331 }
332
333 /**
334 * Implement hook_block_info().
335 */
336 function aggregator_block_info() {
337 $block = array();
338 $result = db_query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
339 foreach ($result as $category) {
340 $block['category-' . $category->cid]['info'] = t('!title category latest items', array('!title' => $category->title));
341 }
342 $result = db_query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
343 foreach ($result as $feed) {
344 $block['feed-' . $feed->fid]['info'] = t('!title feed latest items', array('!title' => $feed->title));
345 }
346 return $block;
347 }
348
349 /**
350 * Implement hook_block_configure().
351 */
352 function aggregator_block_configure($delta = '') {
353 list($type, $id) = explode('-', $delta);
354 if ($type == 'category') {
355 $value = db_query('SELECT block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchField();
356 $form['block'] = array(
357 '#type' => 'select',
358 '#title' => t('Number of news items in block'),
359 '#default_value' => $value,
360 '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))
361 );
362 return $form;
363 }
364 }
365
366 /**
367 * Implement hook_block_save().
368 */
369 function aggregator_block_save($delta = '', $edit = array()) {
370 list($type, $id) = explode('-', $delta);
371 if ($type == 'category') {
372 db_update('aggregator_category')
373 ->fields(array('block' => $edit['block']))
374 ->condition('cid', $id)
375 ->execute();
376 }
377 }
378
379 /**
380 * Implement hook_block_view().
381 *
382 * Generates blocks for the latest news items in each category and feed.
383 */
384 function aggregator_block_view($delta = '') {
385 if (user_access('access news feeds')) {
386 $block = array();
387 list($type, $id) = explode('-', $delta);
388 switch ($type) {
389 case 'feed':
390 if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
391 $block['subject'] = check_plain($feed->title);
392 $result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", 0, $feed->block, array(':fid' => $id));
393 $read_more = theme('more_link', array('url' => url('aggregator/sources/' . $feed->fid), 'title' => t("View this feed's recent news.")));
394 }
395 break;
396
397 case 'category':
398 if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
399 $block['subject'] = check_plain($category->title);
400 $result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC', 0, $category->block, array(':cid' => $category->cid));
401 $read_more = theme('more_link', array('url' => url('aggregator/categories/' . $category->cid), 'title' => t("View this category's recent news.")));
402 }
403 break;
404 }
405 $items = array();
406 foreach ($result as $item) {
407 $items[] = theme('aggregator_block_item', array('item' => $item));
408 }
409
410 // Only display the block if there are items to show.
411 if (count($items) > 0) {
412 $block['content'] = theme('item_list', array('items' => $items)) . $read_more;
413 }
414 return $block;
415 }
416 }
417
418 /**
419 * Add/edit/delete aggregator categories.
420 *
421 * @param $edit
422 * An associative array describing the category to be added/edited/deleted.
423 */
424 function aggregator_save_category($edit) {
425 $link_path = 'aggregator/categories/';
426 if (!empty($edit['cid'])) {
427 $link_path .= $edit['cid'];
428 if (!empty($edit['title'])) {
429 db_merge('aggregator_category')
430 ->key(array('cid' => $edit['cid']))
431 ->fields(array(
432 'title' => $edit['title'],
433 'description' => $edit['description'],
434 ))
435 ->execute();
436 $op = 'update';
437 }
438 else {
439 db_delete('aggregator_category')
440 ->condition('cid', $edit['cid'])
441 ->execute();
442 // Make sure there is no active block for this category.
443 db_delete('block')
444 ->condition('module', 'aggregator')
445 ->condition('delta', 'category-' . $edit['cid'])
446 ->execute();
447 $edit['title'] = '';
448 $op = 'delete';
449 }
450 }
451 elseif (!empty($edit['title'])) {
452 // A single unique id for bundles and feeds, to use in blocks.
453 $link_path .= db_insert('aggregator_category')
454 ->fields(array(
455 'title' => $edit['title'],
456 'description' => $edit['description'],
457 'block' => 5,
458 ))
459 ->execute();
460 $op = 'insert';
461 }
462 if (isset($op)) {
463 menu_link_maintain('aggregator', $op, $link_path, $edit['title']);
464 }
465 }
466
467 /**
468 * Add/edit/delete an aggregator feed.
469 *
470 * @param $edit
471 * An associative array describing the feed to be added/edited/deleted.
472 */
473 function aggregator_save_feed($edit) {
474 if (!empty($edit['fid'])) {
475 // An existing feed is being modified, delete the category listings.
476 db_delete('aggregator_category_feed')
477 ->condition('fid', $edit['fid'])
478 ->execute();
479 }
480 if (!empty($edit['fid']) && !empty($edit['title'])) {
481 db_update('aggregator_feed')
482 ->condition('fid', $edit['fid'])
483 ->fields(array(
484 'title' => $edit['title'],
485 'url' => $edit['url'],
486 'refresh' => $edit['refresh'],
487 'block' => $edit['block'],
488 ))
489 ->execute();
490 }
491 elseif (!empty($edit['fid'])) {
492 $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $edit['fid']))->fetchCol();
493 if ($iids) {
494 db_delete('aggregator_category_item')
495 ->condition('iid', $iids, 'IN')
496 ->execute();
497 }
498 db_delete('aggregator_feed')->
499 condition('fid', $edit['fid'])
500 ->execute();
501 db_delete('aggregator_item')
502 ->condition('fid', $edit['fid'])
503 ->execute();
504 // Make sure there is no active block for this feed.
505 db_delete('block')
506 ->condition('module', 'aggregator')
507 ->condition('delta', 'feed-' . $edit['fid'])
508 ->execute();
509 }
510 elseif (!empty($edit['title'])) {
511 $edit['fid'] = db_insert('aggregator_feed')
512 ->fields(array(
513 'title' => $edit['title'],
514 'url' => $edit['url'],
515 'refresh' => $edit['refresh'],
516 'block' => $edit['block'],
517 'description' => '',
518 'image' => '',
519 ))
520 ->execute();
521
522 }
523 if (!empty($edit['title'])) {
524 // The feed is being saved, save the categories as well.
525 if (!empty($edit['category'])) {
526 foreach ($edit['category'] as $cid => $value) {
527 if ($value) {
528 db_merge('aggregator_category_feed')
529 ->key(array('fid' => $edit['fid']))
530 ->fields(array(
531 'cid' => $cid,
532 ))
533 ->execute();
534 }
535 }
536 }
537 }
538 }
539
540 /**
541 * Removes all items from a feed.
542 *
543 * @param $feed
544 * An object describing the feed to be cleared.
545 */
546 function aggregator_remove($feed) {
547 _aggregator_get_variables();
548 // Call hook_aggregator_remove() on all modules.
549 module_invoke_all('aggregator_remove', $feed);
550 // Reset feed.
551 db_merge('aggregator_feed')
552 ->key(array('fid' => $feed->fid))
553 ->fields(array(
554 'checked' => 0,
555 'hash' => '',
556 'etag' => '',
557 'modified' => 0,
558 'description' => $feed->description,
559 'image' => $feed->image,
560 ))
561 ->execute();
562 }
563
564 function _aggregator_get_variables() {
565 // Fetch the feed.
566 $fetcher = variable_get('aggregator_fetcher', 'aggregator');
567 if ($fetcher == 'aggregator') {
568 include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.fetcher.inc';
569 }
570 $parser = variable_get('aggregator_parser', 'aggregator');
571 if ($parser == 'aggregator') {
572 include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.parser.inc';
573 }
574 $processors = variable_get('aggregator_processors', array('aggregator'));
575 if (in_array('aggregator', $processors)) {
576 include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'aggregator') . '/aggregator.processor.inc';
577 }
578 return array($fetcher, $parser, $processors);
579 }
580
581 /**
582 * Checks a news feed for new items.
583 *
584 * @param $feed
585 * An object describing the feed to be refreshed.
586 */
587 function aggregator_refresh($feed) {
588 // Store feed URL to track changes.
589 $feed_url = $feed->url;
590
591 // Fetch the feed.
592 list($fetcher, $parser, $processors) = _aggregator_get_variables();
593 module_invoke($fetcher, 'aggregator_fetch', $feed);
594
595 if ($feed->source_string !== FALSE) {
596 // Parse the feed.
597 if (module_invoke($parser, 'aggregator_parse', $feed)) {
598 // Update feed with parsed data.
599 db_merge('aggregator_feed')
600 ->key(array('fid' => $feed->fid))
601 ->fields(array(
602 'url' => $feed->url,
603 'checked' => REQUEST_TIME,
604 'link' => empty($feed->link) ? $feed->url : $feed->link,
605 'description' => empty($feed->description) ? '' : $feed->description,
606 'image' => empty($feed->image) ? '' : $feed->image,
607 'hash' => md5($feed->source_string),
608 'etag' => empty($feed->etag) ? '' : $feed->etag,
609 'modified' => empty($feed->modified) ? 0 : $feed->modified,
610 ))
611 ->execute();
612
613 // Log if feed URL has changed.
614 if ($feed->url != $feed_url) {
615 watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->title, '%url' => $feed->url));
616 }
617
618 watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed->title));
619 drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed->title)));
620
621 // If there are items on the feed, let all enabled processors do their work on it.
622 if (@count($feed->items)) {
623 $processors = variable_get('aggregator_processors', array('aggregator'));
624 foreach ($processors as $processor) {
625 module_invoke($processor, 'aggregator_process', $feed);
626 }
627 }
628 }
629 }
630
631 // Expire old feed items.
632 if (function_exists('aggregator_expire')) {
633 aggregator_expire($feed);
634 }
635 }
636
637 /**
638 * Load an aggregator feed.
639 *
640 * @param $fid
641 * The feed id.
642 * @return
643 * An object describing the feed.
644 */
645 function aggregator_feed_load($fid) {
646 $feeds = &drupal_static(__FUNCTION__);
647 if (!isset($feeds[$fid])) {
648 $feeds[$fid] = db_query('SELECT * FROM {aggregator_feed} WHERE fid = :fid', array(':fid' => $fid))->fetchObject();
649 }
650
651 return $feeds[$fid];
652 }
653
654 /**
655 * Load an aggregator category.
656 *
657 * @param $cid
658 * The category id.
659 * @return
660 * An associative array describing the category.
661 */
662 function aggregator_category_load($cid) {
663 $categories = &drupal_static(__FUNCTION__);
664 if (!isset($categories[$cid])) {
665 $categories[$cid] = db_query('SELECT * FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $cid))->fetchAssoc();
666 }
667
668 return $categories[$cid];
669 }
670
671 /**
672 * Format an individual feed item for display in the block.
673 *
674 * @param $variables
675 * An associative array containing:
676 * - item: The item to be displayed.
677 * - feed: Not used.
678 *
679 * @return
680 * The item HTML.
681 * @ingroup themeable
682 */
683 function theme_aggregator_block_item($variables) {
684 // Display the external link to the item.
685 return '<a href="' . check_url($variables['item']->link) . '">' . check_plain($variables['item']->title) . "</a>\n";
686 }
687
688 /**
689 * Safely render HTML content, as allowed.
690 *
691 * @param $value
692 * The content to be filtered.
693 * @return
694 * The filtered content.
695 */
696 function aggregator_filter_xss($value) {
697 return filter_xss($value, preg_split('/\s+|<|>/', variable_get('aggregator_allowed_html_tags', '<a> <b> <br> <dd> <dl> <dt> <em> <i> <li> <ol> <p> <strong> <u> <ul>'), -1, PREG_SPLIT_NO_EMPTY));
698 }
699
700 /**
701 * Check and sanitize aggregator configuration.
702 *
703 * Goes through all fetchers, parsers and processors and checks whether they are available.
704 * If one is missing resets to standard configuration.
705 *
706 * @return
707 * TRUE if this function reset the configuration FALSE if not.
708 */
709 function aggregator_sanitize_configuration() {
710 $reset = FALSE;
711 list($fetcher, $parser, $processors) = _aggregator_get_variables();
712 if (!module_exists($fetcher)) {
713 $reset = TRUE;
714 }
715 if (!module_exists($parser)) {
716 $reset = TRUE;
717 }
718 foreach ($processors as $processor) {
719 if (!module_exists($processor)) {
720 $reset = TRUE;
721 break;
722 }
723 }
724 if ($reset) {
725 variable_del('aggregator_fetcher');
726 variable_del('aggregator_parser');
727 variable_del('aggregator_processors');
728 return TRUE;
729 }
730 return FALSE;
731 }
732
733 /**
734 * Helper function for drupal_map_assoc.
735 *
736 * @param $count
737 * Items count.
738 * @return
739 * Plural-formatted "@count items"
740 */
741 function _aggregator_items($count) {
742 return format_plural($count, '1 item', '@count items');
743 }

  ViewVC Help
Powered by ViewVC 1.1.2