| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
function aggregator_aggregator_parse_info() {
|
| 13 |
|
| 14 |
'title' => t('Default parser'),
|
| 15 |
'description' => t('Parses RSS, Atom and RDF feeds.'),
|
| 16 |
);
|
| 17 |
}
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
function aggregator_aggregator_parse($feed) {
|
| 23 |
global $channel, $image;
|
| 24 |
|
| 25 |
|
| 26 |
if (aggregator_parse_feed($feed->source_string, $feed)) {
|
| 27 |
$modified = empty($feed->http_headers['Last-Modified']) ? 0 : strtotime($feed->http_headers['Last-Modified']);
|
| 28 |
|
| 29 |
|
| 30 |
foreach ($channel as $key => $value) {
|
| 31 |
$channel[$key] = trim($value);
|
| 32 |
}
|
| 33 |
|
| 34 |
|
| 35 |
foreach ($image as $key => $value) {
|
| 36 |
$image[$key] = trim($value);
|
| 37 |
}
|
| 38 |
|
| 39 |
if (!empty($image['link']) && !empty($image['url']) && !empty($image['title'])) {
|
| 40 |
$image = l(theme('image', $image['url'], $image['title']), $image['link'], array('html' => TRUE));
|
| 41 |
}
|
| 42 |
|
| 43 |
$image = '';
|
| 44 |
|
| 45 |
|
| 46 |
$etag = empty($feed->http_headers['ETag']) ? '' : $feed->http_headers['ETag'];
|
| 47 |
|
| 48 |
db_merge('aggregator_feed')
|
| 49 |
->key(array('fid' => $feed->fid))
|
| 50 |
->fields(array(
|
| 51 |
'url' => $feed->url,
|
| 52 |
'checked' => REQUEST_TIME,
|
| 53 |
'link' => !empty($channel['link']) ? $channel['link'] : '',
|
| 54 |
'description' => !empty($channel['description']) ? $channel['description'] : '',
|
| 55 |
'image' => $image,
|
| 56 |
'hash' => md5($feed->source_string),
|
| 57 |
'etag' => $etag,
|
| 58 |
'modified' => $modified,
|
| 59 |
))
|
| 60 |
->execute();
|
| 61 |
|
| 62 |
|
| 63 |
cache_clear_all();
|
| 64 |
|
| 65 |
if (isset($feed->redirected)) {
|
| 66 |
watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->title, '%url' => $feed->url));
|
| 67 |
}
|
| 68 |
|
| 69 |
watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed->title));
|
| 70 |
drupal_set_message(t('There is new syndicated content from %site.', array('%site' => $feed->title)));
|
| 71 |
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
function aggregator_parse_feed(&$data, $feed) {
|
| 86 |
global $items, $image, $channel;
|
| 87 |
|
| 88 |
|
| 89 |
unset($GLOBALS['element'], $GLOBALS['item'], $GLOBALS['tag']);
|
| 90 |
$items = array();
|
| 91 |
$image = array();
|
| 92 |
$channel = array();
|
| 93 |
|
| 94 |
|
| 95 |
$xml_parser = drupal_xml_parser_create($data);
|
| 96 |
xml_set_element_handler($xml_parser, 'aggregator_element_start', 'aggregator_element_end');
|
| 97 |
xml_set_character_data_handler($xml_parser, 'aggregator_element_data');
|
| 98 |
|
| 99 |
if (!xml_parse($xml_parser, $data, 1)) {
|
| 100 |
watchdog('aggregator', 'The feed from %site seems to be broken, due to an error "%error" on line %line.', array('%site' => $feed->title, '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser)), WATCHDOG_WARNING);
|
| 101 |
drupal_set_message(t('The feed from %site seems to be broken, because of error "%error" on line %line.', array('%site' => $feed->title, '%error' => xml_error_string(xml_get_error_code($xml_parser)), '%line' => xml_get_current_line_number($xml_parser))), 'error');
|
| 102 |
return FALSE;
|
| 103 |
}
|
| 104 |
xml_parser_free($xml_parser);
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
$items = array_reverse($items);
|
| 109 |
|
| 110 |
|
| 111 |
$feed->items = array();
|
| 112 |
foreach ($items as $item) {
|
| 113 |
|
| 114 |
|
| 115 |
foreach ($item as $key => $value) {
|
| 116 |
$item[$key] = trim($value);
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if (!empty($item['title'])) {
|
| 123 |
$item['title'] = $item['title'];
|
| 124 |
}
|
| 125 |
elseif (!empty($item['description'])) {
|
| 126 |
$item['title'] = preg_replace('/^(.*)[^\w;&].*?$/', "\\1", truncate_utf8($item['description'], 40));
|
| 127 |
}
|
| 128 |
|
| 129 |
$item['title'] = '';
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
if (!empty($item['link'])) {
|
| 134 |
$item['link'] = $item['link'];
|
| 135 |
}
|
| 136 |
|
| 137 |
$item['link'] = $feed->link;
|
| 138 |
|
| 139 |
$item['guid'] = isset($item['guid']) ? $item['guid'] : '';
|
| 140 |
|
| 141 |
|
| 142 |
if (!empty($item['content:encoded'])) {
|
| 143 |
$item['description'] = $item['content:encoded'];
|
| 144 |
}
|
| 145 |
elseif (!empty($item['summary'])) {
|
| 146 |
$item['description'] = $item['summary'];
|
| 147 |
}
|
| 148 |
elseif (!empty($item['content'])) {
|
| 149 |
$item['description'] = $item['content'];
|
| 150 |
}
|
| 151 |
|
| 152 |
|
| 153 |
$date = '';
|
| 154 |
foreach (array('pubdate', 'dc:date', 'dcterms:issued', 'dcterms:created', 'dcterms:modified', 'issued', 'created', 'modified', 'published', 'updated') as $key) {
|
| 155 |
if (!empty($item[$key])) {
|
| 156 |
$date = $item[$key];
|
| 157 |
break;
|
| 158 |
}
|
| 159 |
}
|
| 160 |
|
| 161 |
$item['timestamp'] = strtotime($date);
|
| 162 |
|
| 163 |
if ($item['timestamp'] === FALSE) {
|
| 164 |
$item['timestamp'] = aggregator_parse_w3cdtf($date); // Aggregator_parse_w3cdtf() returns FALSE on failure.
|
| 165 |
}
|
| 166 |
|
| 167 |
|
| 168 |
if (empty($item['author']) && !empty($item['dc:creator'])) {
|
| 169 |
$item['author'] = $item['dc:creator'];
|
| 170 |
}
|
| 171 |
|
| 172 |
$item += array('author' => '', 'description' => '');
|
| 173 |
|
| 174 |
|
| 175 |
$feed->items[] = $item;
|
| 176 |
}
|
| 177 |
|
| 178 |
return TRUE;
|
| 179 |
}
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
function aggregator_element_start($parser, $name, $attributes) {
|
| 185 |
global $item, $element, $tag, $items, $channel;
|
| 186 |
|
| 187 |
$name = strtolower($name);
|
| 188 |
|
| 189 |
case 'image':
|
| 190 |
case 'textinput':
|
| 191 |
case 'content':
|
| 192 |
case 'summary':
|
| 193 |
case 'tagline':
|
| 194 |
case 'subtitle':
|
| 195 |
case 'logo':
|
| 196 |
case 'info':
|
| 197 |
$element = $name;
|
| 198 |
break;
|
| 199 |
case 'id':
|
| 200 |
if ($element != 'item') {
|
| 201 |
$element = $name;
|
| 202 |
}
|
| 203 |
case 'link':
|
| 204 |
if (!empty($attributes['rel']) && $attributes['rel'] == 'alternate') {
|
| 205 |
if ($element == 'item') {
|
| 206 |
$items[$item]['link'] = $attributes['href'];
|
| 207 |
}
|
| 208 |
|
| 209 |
$channel['link'] = $attributes['href'];
|
| 210 |
|
| 211 |
}
|
| 212 |
break;
|
| 213 |
case 'item':
|
| 214 |
$element = $name;
|
| 215 |
$item += 1;
|
| 216 |
break;
|
| 217 |
case 'entry':
|
| 218 |
$element = 'item';
|
| 219 |
$item += 1;
|
| 220 |
break;
|
| 221 |
}
|
| 222 |
|
| 223 |
$tag = $name;
|
| 224 |
}
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
function aggregator_element_end($parser, $name) {
|
| 230 |
global $element;
|
| 231 |
|
| 232 |
|
| 233 |
case 'image':
|
| 234 |
case 'textinput':
|
| 235 |
case 'item':
|
| 236 |
case 'entry':
|
| 237 |
case 'content':
|
| 238 |
case 'info':
|
| 239 |
$element = '';
|
| 240 |
break;
|
| 241 |
case 'id':
|
| 242 |
if ($element == 'id') {
|
| 243 |
$element = '';
|
| 244 |
}
|
| 245 |
}
|
| 246 |
}
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
function aggregator_element_data($parser, $data) {
|
| 252 |
global $channel, $element, $items, $item, $image, $tag;
|
| 253 |
$items += array($item => array());
|
| 254 |
|
| 255 |
case 'item':
|
| 256 |
$items[$item] += array($tag => '');
|
| 257 |
$items[$item][$tag] .= $data;
|
| 258 |
break;
|
| 259 |
case 'image':
|
| 260 |
case 'logo':
|
| 261 |
$image += array($tag => '');
|
| 262 |
$image[$tag] .= $data;
|
| 263 |
break;
|
| 264 |
case 'link':
|
| 265 |
if ($data) {
|
| 266 |
$items[$item] += array($tag => '');
|
| 267 |
$items[$item][$tag] .= $data;
|
| 268 |
}
|
| 269 |
break;
|
| 270 |
case 'content':
|
| 271 |
$items[$item] += array('content' => '');
|
| 272 |
$items[$item]['content'] .= $data;
|
| 273 |
break;
|
| 274 |
case 'summary':
|
| 275 |
$items[$item] += array('summary' => '');
|
| 276 |
$items[$item]['summary'] .= $data;
|
| 277 |
break;
|
| 278 |
case 'tagline':
|
| 279 |
case 'subtitle':
|
| 280 |
$channel += array('description' => '');
|
| 281 |
$channel['description'] .= $data;
|
| 282 |
break;
|
| 283 |
case 'info':
|
| 284 |
case 'id':
|
| 285 |
case 'textinput':
|
| 286 |
|
| 287 |
|
| 288 |
break;
|
| 289 |
default:
|
| 290 |
$channel += array($tag => '');
|
| 291 |
$channel[$tag] .= $data;
|
| 292 |
}
|
| 293 |
}
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
function aggregator_parse_w3cdtf($date_str) {
|
| 308 |
if (preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/', $date_str, $match)) {
|
| 309 |
list($year, $month, $day, $hours, $minutes, $seconds) = array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
|
| 310 |
|
| 311 |
$epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
|
| 312 |
if ($match[10] != 'Z') { // Z is zulu time, aka GMT
|
| 313 |
list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9], $match[10]);
|
| 314 |
|
| 315 |
if (!$tz_hour) {
|
| 316 |
$tz_hour = 0;
|
| 317 |
}
|
| 318 |
if (!$tz_min) {
|
| 319 |
$tz_min = 0;
|
| 320 |
}
|
| 321 |
$offset_secs = (($tz_hour * 60) + $tz_min) * 60;
|
| 322 |
|
| 323 |
if ($tz_mod == '+') {
|
| 324 |
$offset_secs *= -1;
|
| 325 |
}
|
| 326 |
$epoch += $offset_secs;
|
| 327 |
}
|
| 328 |
return $epoch;
|
| 329 |
}
|
| 330 |
|
| 331 |
return FALSE;
|
| 332 |
|
| 333 |
}
|
| 334 |
|