Projet

Général

Profil

Paste
Télécharger (20,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / libraries / common_syndication_parser.inc @ b5aa1857

1
<?php
2

    
3
/**
4
 * @file
5
 *   Downloading and parsing functions for Common Syndication Parser.
6
 *   Pillaged from FeedAPI common syndication parser.
7
 *
8
 * @todo Restructure. OO could work wonders here.
9
 * @todo Write unit tests.
10
 * @todo Keep in Feeds project or host on Drupal?
11
 */
12

    
13
/**
14
 * Parse the feed into a data structure.
15
 *
16
 * @param string $string
17
 *   The feed object (contains the URL or the parsed XML structure).
18
 *
19
 * @return array|false
20
 *   The structured datas extracted from the feed or FALSE in case of failures.
21
 */
22
function common_syndication_parser_parse($string) {
23
  // SimpleXML can only deal with XML declaration at the start of the document,
24
  // so remove any surrounding whitespace.
25
  $string = trim($string);
26

    
27
  @ $xml = simplexml_load_string($string, NULL, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NOCDATA);
28

    
29
  // Got a malformed XML.
30
  if ($xml === FALSE || is_null($xml)) {
31
    return FALSE;
32
  }
33
  $feed_type = _parser_common_syndication_feed_format_detect($xml);
34
  if ($feed_type ==  "atom1.0") {
35
    return _parser_common_syndication_atom10_parse($xml);
36
  }
37
  if ($feed_type == "RSS2.0" || $feed_type == "RSS0.91" || $feed_type == "RSS0.92") {
38
    return _parser_common_syndication_RSS20_parse($xml);
39
  }
40
  if ($feed_type == "RDF") {
41
    return _parser_common_syndication_RDF10_parse($xml);
42
  }
43
  return FALSE;
44
}
45

    
46
/**
47
 * Determine the feed format of a SimpleXML parsed object structure.
48
 *
49
 * @param SimpleXMLElement $xml
50
 *   SimpleXML-preprocessed feed.
51
 *
52
 * @return string|false
53
 *   The feed format short description or FALSE if not compatible.
54
 */
55
function _parser_common_syndication_feed_format_detect($xml) {
56
  if (!is_object($xml)) {
57
    return FALSE;
58
  }
59
  $attr = $xml->attributes();
60
  $type = strtolower($xml->getName());
61
  if (isset($xml->entry) && $type == "feed") {
62
    return "atom1.0";
63
  }
64
  if ($type == "rss" && $attr["version"] == "2.0") {
65
    return "RSS2.0";
66
  }
67
  if ($type == "rdf" && isset($xml->channel)) {
68
    return "RDF";
69
  }
70
  if ($type == "rss" && $attr["version"] == "0.91") {
71
    return "RSS0.91";
72
  }
73
  if ($type == "rss" && $attr["version"] == "0.92") {
74
    return "RSS0.92";
75
  }
76
  return FALSE;
77
}
78

    
79
/**
80
 * Parse atom feeds.
81
 */
82
function _parser_common_syndication_atom10_parse($feed_XML) {
83
  $parsed_source = array();
84

    
85
  $ns = array(
86
    "georss" => "http://www.georss.org/georss",
87
  );
88

    
89
  $base = _parser_common_syndication_atom10_parse_base_url($feed_XML);
90

    
91
  // Detect the title
92
  $parsed_source['title'] = isset($feed_XML->title) ? _parser_common_syndication_title("{$feed_XML->title}") : "";
93
  // Detect the description
94
  $parsed_source['description'] = isset($feed_XML->subtitle) ? "{$feed_XML->subtitle}" : "";
95

    
96
  $parsed_source['link'] = _parser_common_syndication_link($feed_XML->link);
97
  if ($base && !valid_url($parsed_source['link'], TRUE) && valid_url($parsed_source['link'])) {
98
    $parsed_source['link'] = $base . $parsed_source['link'];
99
  }
100

    
101
  $parsed_source['items'] = array();
102

    
103
  foreach ($feed_XML->entry as $news) {
104
    $georss = (array)$news->children($ns["georss"]);
105
    $geoname = '';
106
    if (isset($georss['featureName'])) {
107
      $geoname = "{$georss['featureName']}";
108
    }
109

    
110
    $latlon =
111
    $lat =
112
    $lon = NULL;
113
    if (isset($georss['point'])) {
114
      $latlon = explode(' ', $georss['point']);
115
      $lat = "{$latlon[0]}";
116
      $lon = "{$latlon[1]}";
117
      if (!$geoname) {
118
        $geoname = "{$lat} {$lon}";
119
      }
120
    }
121

    
122
    $additional_taxonomies = array();
123
    if (isset($news->category)) {
124
      $additional_taxonomies['ATOM Categories'] = array();
125
      $additional_taxonomies['ATOM Domains'] = array();
126
      foreach ($news->category as $category) {
127
        if (isset($category['scheme'])) {
128
          $domain = "{$category['scheme']}";
129
          if (!empty($domain)) {
130
              if (!isset($additional_taxonomies['ATOM Domains'][$domain])) {
131
                $additional_taxonomies['ATOM Domains'][$domain] = array();
132
              }
133
              $additional_taxonomies['ATOM Domains'][$domain][] = count($additional_taxonomies['ATOM Categories']) - 1;
134
          }
135
        }
136
        $additional_taxonomies['ATOM Categories'][] = "{$category['term']}";
137
      }
138
    }
139

    
140
    $title = "{$news->title}";
141

    
142
    $body = '';
143
    if (!empty($news->content)) {
144
      foreach ($news->content->children() as $child)  {
145
        $body .= $child->asXML();
146
      }
147
      $body .= "{$news->content}";
148
    }
149
    elseif (!empty($news->summary)) {
150
      foreach ($news->summary->children() as $child)  {
151
        $body .= $child->asXML();
152
      }
153
      $body .= "{$news->summary}";
154
    }
155

    
156
    $original_author = '';
157
    if (!empty($news->source->author->name)) {
158
      $original_author = "{$news->source->author->name}";
159
    }
160
    elseif (!empty($news->author->name)) {
161
      $original_author = "{$news->author->name}";
162
    }
163
    elseif (!empty($feed_XML->author->name)) {
164
      $original_author = "{$feed_XML->author->name}";
165
    }
166

    
167
    $item = array();
168
    $item['title'] = _parser_common_syndication_title($title, $body);
169
    $item['description'] = $body;
170
    $item['author_name'] = $original_author;
171

    
172
    // Fall back to updated for timestamp if both published and issued are
173
    // empty.
174
    if (isset($news->published)) {
175
      $item['timestamp'] = _parser_common_syndication_parse_date("{$news->published}");
176
    }
177
    elseif (isset($news->issued)) {
178
       $item['timestamp'] = _parser_common_syndication_parse_date("{$news->issued}");
179
    }
180
    elseif (isset($news->updated)) {
181
      $item['timestamp'] = _parser_common_syndication_parse_date("{$news->updated}");
182
    }
183

    
184
    $item['guid'] = (string) $news->id;
185

    
186
    $item['url'] = _parser_common_syndication_link($news->link);
187

    
188
    if (!$item['url'] && !empty($news->content['src']) && valid_url($news->content['src'], TRUE)) {
189
      $item['url'] = (string) $news->content['src'];
190
    }
191

    
192
    if (!strlen($item['url']) && $item['guid'] && valid_url($item['guid'], TRUE)) {
193
      $item['url'] = $item['guid'];
194
    }
195

    
196
    if (!valid_url($item['url'], TRUE) && valid_url($item['url'])) {
197
      if ($item_base = _parser_common_syndication_atom10_parse_base_url($news)) {
198
        $item['url'] = $item_base . $item['url'];
199
      }
200
      elseif ($base) {
201
        $item['url'] = $base . $item['url'];
202
      }
203
    }
204

    
205
    // Fall back on URL if GUID is empty.
206
    if (!strlen($item['guid'])) {
207
      $item['guid'] = $item['url'];
208
    }
209

    
210
    $item['geolocations'] = array();
211
    if ($lat && $lon) {
212
      $item['geolocations'] = array(
213
        array(
214
          'name' => $geoname,
215
          'lat' => $lat,
216
          'lon' => $lon,
217
        ),
218
      );
219
    }
220
    $item['tags'] = isset($additional_taxonomies['ATOM Categories']) ? $additional_taxonomies['ATOM Categories'] : array();
221
    $item['domains'] = isset($additional_taxonomies['ATOM Domains']) ? $additional_taxonomies['ATOM Domains'] : array();
222
    $parsed_source['items'][] = $item;
223
  }
224

    
225
  return $parsed_source;
226
}
227

    
228
/**
229
 * Finds the base URL of an Atom document.
230
 *
231
 * @param SimpleXMLElement $xml
232
 *   The XML document.
233
 *
234
 * @return string|false
235
 *   Returns the base URL or false on failure.
236
 */
237
function _parser_common_syndication_atom10_parse_base_url(SimpleXMLElement $xml) {
238
  $base = $xml->attributes('xml', TRUE)->base;
239
  if (!$base) {
240
    $base = $xml['base'];
241
  }
242

    
243
  if ($base && valid_url($base, TRUE)) {
244
    return rtrim($base, '/') . '/';
245
  }
246

    
247
  // Try to build a base from the self link.
248
  foreach ($xml->xpath('*[local-name() = "link" and @rel="self" and @href]') as $self) {
249
    if (valid_url($self['href'], TRUE)) {
250
      return _parser_common_syndication_string_url_path((string) $self['href']);
251
    }
252
  }
253

    
254
  // Try to build a base from the alternate link.
255
  foreach ($xml->xpath('*[local-name() = "link" and @rel="alternate" and @href]') as $alternate) {
256
    if (valid_url($alternate['href'], TRUE)) {
257
      return _parser_common_syndication_string_url_path((string) $alternate['href']);
258
    }
259
  }
260

    
261
  return FALSE;
262
}
263

    
264
/**
265
 * Removes the path parts of an absolute URL.
266
 *
267
 * @param string $url
268
 *   The absolute URL.
269
 *
270
 * @return string
271
 *   The absolute URL with the path stripped.
272
 */
273
function _parser_common_syndication_string_url_path($url) {
274
  $pos = strpos($url, '/', strpos($url, '//') + 2);
275

    
276
  return $pos ? substr($url, 0, $pos + 1) : $url . '/';
277
}
278

    
279
/**
280
 * Parse RDF Site Summary (RSS) 1.0 feeds in RDF/XML format.
281
 *
282
 * @see http://web.resource.org/rss/1.0/
283
 */
284
function _parser_common_syndication_RDF10_parse($feed_XML) {
285
  // Declare some canonical standard prefixes for well-known namespaces:
286
  static $canonical_namespaces = array(
287
    'rdf'      => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
288
    'rdfs'     => 'http://www.w3.org/2000/01/rdf-schema#',
289
    'xsi'      => 'http://www.w3.org/2001/XMLSchema-instance#',
290
    'xsd'      => 'http://www.w3.org/2001/XMLSchema#',
291
    'owl'      => 'http://www.w3.org/2002/07/owl#',
292
    'dc'       => 'http://purl.org/dc/elements/1.1/',
293
    'dcterms'  => 'http://purl.org/dc/terms/',
294
    'dcmitype' => 'http://purl.org/dc/dcmitype/',
295
    'foaf'     => 'http://xmlns.com/foaf/0.1/',
296
    'rss'      => 'http://purl.org/rss/1.0/',
297
  );
298

    
299
  // Get all namespaces declared in the feed element.
300
  $namespaces = $feed_XML->getNamespaces(TRUE);
301

    
302
  // Process the <rss:channel> resource containing feed metadata:
303
  foreach ($feed_XML->children($canonical_namespaces['rss'])->channel as $rss_channel) {
304
    $parsed_source = array(
305
      'title'       => _parser_common_syndication_title((string) $rss_channel->title),
306
      'description' => (string) $rss_channel->description,
307
      'link'        => (string) $rss_channel->link,
308
      'items'       => array(),
309
    );
310
    break;
311
  }
312

    
313
  // Process each <rss:item> resource contained in the feed:
314
  foreach ($feed_XML->children($canonical_namespaces['rss'])->item as $rss_item) {
315

    
316
    // Extract all available RDF statements from the feed item's RDF/XML
317
    // tags, allowing for both the item's attributes and child elements to
318
    // contain RDF properties:
319
    $rdf_data = array();
320
    foreach ($namespaces as $ns => $ns_uri) {
321
      // Note that we attempt to normalize the found property name
322
      // namespaces to well-known 'standard' prefixes where possible, as the
323
      // feed may in principle use any arbitrary prefixes and we should
324
      // still be able to correctly handle it.
325
      foreach ($rss_item->attributes($ns_uri) as $attr_name => $attr_value) {
326
        $ns_prefix = ($ns_prefix = array_search($ns_uri, $canonical_namespaces)) ? $ns_prefix : $ns;
327
        $rdf_data[$ns_prefix . ':' . $attr_name][] = (string) $attr_value;
328
      }
329
      foreach ($rss_item->children($ns_uri) as $rss_property) {
330
        $ns_prefix = ($ns_prefix = array_search($ns_uri, $canonical_namespaces)) ? $ns_prefix : $ns;
331
        $rdf_data[$ns_prefix . ':' . $rss_property->getName()][] = (string) $rss_property;
332
      }
333
    }
334

    
335
    // Declaratively define mappings that determine how to construct the result object.
336
    $item = _parser_common_syndication_RDF10_item($rdf_data, array(
337
      'title'       => array('rss:title', 'dc:title'),
338
      'description' => array('rss:description', 'dc:description', 'content:encoded'),
339
      'url'         => array('rss:link', 'rdf:about'),
340
      'author_name' => array('dc:creator', 'dc:publisher'),
341
      'guid'        => 'rdf:about',
342
      'timestamp'   => 'dc:date',
343
      'tags'        => 'dc:subject'
344
    ));
345

    
346
    // Special handling for the title:
347
    $item['title'] = _parser_common_syndication_title($item['title'], $item['description']);
348

    
349
    // Parse any date/time values into Unix timestamps:
350
    $item['timestamp'] = _parser_common_syndication_parse_date($item['timestamp']);
351

    
352
    // If no GUID found, use the URL of the feed.
353
    if (empty($item['guid'])) {
354
      $item['guid'] = $item['url'];
355
    }
356

    
357
    // Add every found RDF property to the feed item.
358
    $item['rdf'] = array();
359
    foreach ($rdf_data as $rdf_property => $rdf_value) {
360
      // looks nicer in the mapper UI
361
      // @todo Revisit, not used with feedapi mapper anymore.
362
      $rdf_property = str_replace(':', '_', $rdf_property);
363
      $item['rdf'][$rdf_property] = $rdf_value;
364
    }
365

    
366
    $parsed_source['items'][] = $item;
367
  }
368

    
369
  return $parsed_source;
370
}
371

    
372
function _parser_common_syndication_RDF10_property($rdf_data, $rdf_properties = array()) {
373
  $rdf_properties = is_array($rdf_properties) ? $rdf_properties : array_slice(func_get_args(), 1);
374
  foreach ($rdf_properties as $rdf_property) {
375
    if ($rdf_property && !empty($rdf_data[$rdf_property])) {
376
      // remove empty strings
377
      return array_filter($rdf_data[$rdf_property], 'strlen');
378
    }
379
  }
380
}
381

    
382
function _parser_common_syndication_RDF10_item($rdf_data, $mappings) {
383
  foreach ($mappings as $k => $v) {
384
    $values = _parser_common_syndication_RDF10_property($rdf_data, $v);
385
    $mappings[$k] = !is_array($values) || count($values) > 1 ? $values : reset($values);
386
  }
387
  return $mappings;
388
}
389

    
390
/**
391
 * Parse RSS2.0 feeds.
392
 */
393
function _parser_common_syndication_RSS20_parse($feed_XML) {
394

    
395
  $ns = array(
396
    "content" => "http://purl.org/rss/1.0/modules/content/",
397
     "dc" => "http://purl.org/dc/elements/1.1/",
398
     "georss" => "http://www.georss.org/georss",
399
  );
400

    
401
  $parsed_source = array();
402
  // Detect the title.
403
  $parsed_source['title'] = isset($feed_XML->channel->title) ? _parser_common_syndication_title("{$feed_XML->channel->title}") : "";
404
  // Detect the description.
405
  $parsed_source['description'] = isset($feed_XML->channel->description) ? "{$feed_XML->channel->description}" : "";
406
  // Detect the link.
407
  $parsed_source['link'] = isset($feed_XML->channel->link) ? "{$feed_XML->channel->link}" : "";
408
  $parsed_source['items'] = array();
409

    
410
  foreach ($feed_XML->xpath('//item') as $news) {
411
    $title = $body = $original_author = $original_url = $guid = '';
412

    
413
    // Get optional source url.
414
    $source_url = (string) $news->source['url'];
415

    
416
    $category = $news->xpath('category');
417
    // Get children for current namespace.
418
    $content = (array)$news->children($ns["content"]);
419
    $dc      = (array)$news->children($ns["dc"]);
420
    $georss  = (array)$news->children($ns["georss"]);
421
    $news = (array) $news;
422
    $news['category'] = $category;
423

    
424
    if (isset($news['title'])) {
425
      $title = "{$news['title']}";
426
    }
427

    
428
    if (isset($news['description'])) {
429
      $body = "{$news['description']}";
430
    }
431
    // Some sources use content:encoded as description i.e.
432
    // PostNuke PageSetter module.
433
    if (isset($news['encoded'])) {  // content:encoded for PHP < 5.1.2.
434
      if (strlen($body) < strlen("{$news['encoded']}")) {
435
        $body = "{$news['encoded']}";
436
      }
437
    }
438
    if (isset($content['encoded'])) { // content:encoded for PHP >= 5.1.2.
439
      if (strlen($body) < strlen("{$content['encoded']}")) {
440
        $body = "{$content['encoded']}";
441
      }
442
    }
443
    if (!isset($body)) {
444
      $body = "{$news['title']}";
445
    }
446

    
447
    if (!empty($news['author'])) {
448
      $original_author = "{$news['author']}";
449
    }
450
    elseif (!empty($dc["creator"])) {
451
      $original_author = (string)$dc["creator"];
452
    }
453

    
454
    if (!empty($news['link'])) {
455
      $original_url = "{$news['link']}";
456
      $guid = $original_url;
457
    }
458

    
459
    if (!empty($news['guid'])) {
460
      $guid = "{$news['guid']}";
461
    }
462

    
463
    if (!empty($georss['featureName'])) {
464
      $geoname = "{$georss['featureName']}";
465
    }
466

    
467
    $lat =
468
    $lon =
469
    $latlon =
470
    $geoname = NULL;
471
    if (!empty($georss['point'])) {
472
      $latlon = explode(' ', $georss['point']);
473
      $lat = "{$latlon[0]}";
474
      $lon = "{$latlon[1]}";
475
      if (!$geoname) {
476
        $geoname = "$lat $lon";
477
      }
478
    }
479

    
480
    $additional_taxonomies = array();
481
    $additional_taxonomies['RSS Categories'] = array();
482
    $additional_taxonomies['RSS Domains'] = array();
483
    if (isset($news['category'])) {
484
      foreach ($news['category'] as $category) {
485
        $additional_taxonomies['RSS Categories'][] = "{$category}";
486
        if (isset($category['domain'])) {
487
          $domain = "{$category['domain']}";
488
          if (!empty($domain)) {
489
              if (!isset($additional_taxonomies['RSS Domains'][$domain])) {
490
                $additional_taxonomies['RSS Domains'][$domain] = array();
491
              }
492
              $additional_taxonomies['RSS Domains'][$domain][] = count($additional_taxonomies['RSS Categories']) - 1;
493
          }
494
        }
495
      }
496
    }
497

    
498
    $item = array();
499
    $item['title'] = _parser_common_syndication_title($title, $body);
500
    $item['description'] = $body;
501
    $item['author_name'] = $original_author;
502
    if (!empty($news['pubDate'])) {
503
      $item['timestamp'] = _parser_common_syndication_parse_date($news['pubDate']);
504
    }
505
    elseif (!empty($dc['date'])) {
506
      $item['timestamp'] = _parser_common_syndication_parse_date($dc['date']);
507
    }
508
    else {
509
      $item['timestamp'] = time();
510
    }
511
    $item['url'] = trim($original_url);
512
    $item['guid'] = $guid;
513
    if (!empty($news['source'])) {
514
      $item['source:title'] = $news['source'];
515
    }
516
    else {
517
      $item['source:title'] = NULL;
518
    }
519
    $item['source:url'] = trim($source_url);
520

    
521
    $item['geolocations'] = array();
522
    if (isset($geoname, $lat, $lon)) {
523
      $item['geolocations'] = array(
524
        array(
525
          'name' => $geoname,
526
          'lat' => $lat,
527
          'lon' => $lon,
528
        ),
529
      );
530
    }
531

    
532
    $item['domains'] = $additional_taxonomies['RSS Domains'];
533
    $item['tags'] = $additional_taxonomies['RSS Categories'];
534
    $parsed_source['items'][] = $item;
535
  }
536
  return $parsed_source;
537
}
538

    
539
/**
540
 * Parse a date comes from a feed.
541
 *
542
 * @param string $date_str
543
 *   The date string in various formats.
544
 *
545
 * @return int
546
 *   The timestamp of the string or the current time if can't be parsed.
547
 */
548
function _parser_common_syndication_parse_date($date_str) {
549
  // PHP < 5.3 doesn't like the GMT- notation for parsing timezones.
550
  $date_str = str_replace('GMT-', '-', $date_str);
551
  $date_str = str_replace('GMT+', '+', $date_str);
552
  $parsed_date = strtotime($date_str);
553

    
554
  if ($parsed_date === FALSE || $parsed_date == -1) {
555
    $parsed_date = _parser_common_syndication_parse_w3cdtf($date_str);
556
  }
557

    
558
  if (($parsed_date === FALSE || $parsed_date == -1)) {
559
    // PHP does not support the UT timezone. Fake it. The system that generated
560
    // this, Google Groups, probably meant UTC.
561
    $date_str = strtolower(trim($date_str));
562
    $last_three = substr($date_str, strlen($date_str) - 3, 3);
563

    
564
    if ($last_three == ' ut') {
565
      $parsed_date = strtotime($date_str . 'c');
566
    }
567
  }
568

    
569
  return $parsed_date === FALSE ? time() : $parsed_date;
570
}
571

    
572
/**
573
 * Parse the W3C date/time format, a subset of ISO 8601.
574
 *
575
 * PHP date parsing functions do not handle this format.
576
 * See http://www.w3.org/TR/NOTE-datetime for more information.
577
 * Originally from MagpieRSS (http://magpierss.sourceforge.net/).
578
 *
579
 * @param string $date_str
580
 *   A potentially W3C DTF date.
581
 *
582
 * @return int|false
583
 *   A timestamp if parsed successfully or FALSE if not.
584
 */
585
function _parser_common_syndication_parse_w3cdtf($date_str) {
586
  if (preg_match('/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/', $date_str, $match)) {
587
    list($year, $month, $day, $hours, $minutes, $seconds) = array($match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
588
    // Calculate the epoch for current date assuming GMT.
589
    $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
590
    if ($match[10] != 'Z') { // Z is zulu time, aka GMT
591
      list($tz_mod, $tz_hour, $tz_min) = array($match[8], $match[9], $match[10]);
592
      // Zero out the variables.
593
      if (!$tz_hour) {
594
        $tz_hour = 0;
595
      }
596
      if (!$tz_min) {
597
        $tz_min = 0;
598
      }
599
      $offset_secs = (($tz_hour * 60) + $tz_min) * 60;
600
      // Is timezone ahead of GMT?  If yes, subtract offset.
601
      if ($tz_mod == '+') {
602
        $offset_secs *= -1;
603
      }
604
      $epoch += $offset_secs;
605
    }
606
    return $epoch;
607
  }
608
  else {
609
    return FALSE;
610
  }
611
}
612

    
613
/**
614
 * Extract the link that points to the original content (back to site or
615
 * original article)
616
 *
617
 * @param array $links
618
 *   Array of SimpleXML objects
619
 *
620
 * @return string
621
 *   An URL if found. An empty string otherwise.
622
 */
623
function _parser_common_syndication_link($links) {
624
  $to_link = '';
625
  if (count($links) > 0) {
626
    foreach ($links as $link) {
627
      $link = $link->attributes();
628
      $to_link = isset($link["href"]) ? "{$link["href"]}" : "";
629
      if (isset($link["rel"])) {
630
        if ("{$link["rel"]}" == 'alternate') {
631
          break;
632
        }
633
      }
634
    }
635
  }
636
  return trim($to_link);
637
}
638

    
639
/**
640
 * Prepare raw data to be a title
641
 */
642
function _parser_common_syndication_title($title, $body = FALSE) {
643
  if (empty($title) && !empty($body)) {
644
    // Explode to words and use the first 3 words.
645
    $words = preg_split('/[\s,]+/', strip_tags($body));
646
    $title = implode(' ', array_slice($words, 0, 3));
647
  }
648
  return $title;
649
}