| 1 |
<?php
|
| 2 |
/* $Id: parser_simplepie.module,v 1.5 2007/07/23 17:27:08 aronnovak Exp $ */
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Parse the incoming URL with SimplePie then provide a data structure of the feed
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function parser_simplepie_help($section) {
|
| 13 |
switch($section) {
|
| 14 |
case 'admin/modules#description':
|
| 15 |
return t('Provide a common syndication parser for FeedAPI-compatible modules powered by SimplePie library.');
|
| 16 |
break;
|
| 17 |
}
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_feedapi_type().
|
| 22 |
* Define the source types that this module is able to handle
|
| 23 |
*
|
| 24 |
* @return
|
| 25 |
* The types
|
| 26 |
*/
|
| 27 |
function parser_simplepie_feedapi_type() {
|
| 28 |
return array("XML feed");
|
| 29 |
}
|
| 30 |
|
| 31 |
/**
|
| 32 |
* Implementation of hook_feedapi_compatible().
|
| 33 |
*
|
| 34 |
* @param $url
|
| 35 |
* The feed's url
|
| 36 |
* @return
|
| 37 |
* a string - feed type if the parser is able to process it, FALSE if it's not compatible
|
| 38 |
*/
|
| 39 |
function parser_simplepie_feedapi_compatible($url) {
|
| 40 |
include_once './'. drupal_get_path('module', 'parser_simplepie') .'/simplepie.inc';
|
| 41 |
$feed = new SimplePie();
|
| 42 |
$feed->enable_cache(FALSE);
|
| 43 |
$feed->set_feed_url($url);
|
| 44 |
if ($feed->init() == FALSE) {
|
| 45 |
return FALSE;
|
| 46 |
}
|
| 47 |
else {
|
| 48 |
return array_shift(parser_simplepie_feedapi_type());
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Implementation of hook_feedapi_parse().
|
| 54 |
* Uses SimplePie
|
| 55 |
*
|
| 56 |
* @param $url The feed's url
|
| 57 |
* @return
|
| 58 |
* The structured datas extracted from the feed
|
| 59 |
*/
|
| 60 |
function parser_simplepie_feedapi_parse($feed) {
|
| 61 |
include_once './'. drupal_get_path('module', 'parser_simplepie') .'/simplepie.inc';
|
| 62 |
$parser = new SimplePie();
|
| 63 |
$parser->enable_cache(FALSE);
|
| 64 |
$parser->set_feed_url($feed->url);
|
| 65 |
$parser->set_timeout(15);
|
| 66 |
// prevent SimplePie from using all of it's data santization since we use Drupal's input form
|
| 67 |
$parser->set_stupidly_fast(TRUE);
|
| 68 |
$parser->init();
|
| 69 |
if ($parser->error) {
|
| 70 |
return FALSE;
|
| 71 |
}
|
| 72 |
// Construct the standard form of the parsed feed
|
| 73 |
$parsed_source = new stdClass();
|
| 74 |
$parsed_source->title = $parser->get_title();
|
| 75 |
$parsed_source->description = $parser->get_description();
|
| 76 |
$parsed_source->options = new stdClass();
|
| 77 |
$parsed_source->options->link = $parser->get_link();
|
| 78 |
$parsed_source->items = array();
|
| 79 |
$items_num = $parser->get_item_quantity();
|
| 80 |
for ($i = 0; $i < $items_num; $i++) {
|
| 81 |
$curr_item = new stdClass();
|
| 82 |
$simplepie_item = $parser->get_item($i);
|
| 83 |
$curr_item->title = $simplepie_item->get_title();
|
| 84 |
$curr_item->description = $simplepie_item->get_content();
|
| 85 |
$curr_item->options = new stdClass();
|
| 86 |
$curr_item->options->link = $simplepie_item->get_link();
|
| 87 |
// U = std. unix timestamp
|
| 88 |
$curr_item->options->timestamp = $simplepie_item->get_date("U");
|
| 89 |
$curr_item->options->guid = $simplepie_item->get_id();
|
| 90 |
$curr_item->options->original_author = $simplepie_item->get_author();
|
| 91 |
// Extract tags related to the item
|
| 92 |
$simplepie_tags = $simplepie_item->get_categories();
|
| 93 |
$tags = array();
|
| 94 |
if (count($simplepie_tags) > 0) {
|
| 95 |
foreach ($simplepie_tags as $tag) {
|
| 96 |
$tags[] = (string) $tag->term;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
$curr_item->options->tags = $tags;
|
| 100 |
$parsed_source->items[] = $curr_item;
|
| 101 |
}
|
| 102 |
return $parsed_source;
|
| 103 |
}
|