Projet

Général

Profil

Paste
Télécharger (1,06 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / libraries / opml_parser.inc @ ed9a13f1

1
<?php
2

    
3
/**
4
 * @file
5
 * OPML Parser.
6
 */
7

    
8
/**
9
 * Parse OPML file.
10
 *
11
 * @param $raw
12
 *   File contents.
13
 *
14
 * @return array
15
 *   An array of the parsed OPML file.
16
 */
17
function opml_parser_parse($raw) {
18
  $feeds = $items = array();
19
  $xml = @ new SimpleXMLElement($raw);
20
  $feeds['title'] = (string) current($xml->xpath('//head/title'));
21

    
22
  // @todo Make xpath case insensitive.
23
  $outlines = $xml->xpath('//outline[@xmlUrl]');
24
  foreach ($outlines as $outline) {
25
    $item = array();
26
    foreach ($outline->attributes() as $k => $v) {
27
      if (in_array(strtolower($k), array('title', 'text', 'xmlurl'))) {
28
        $item[strtolower($k)] = (string) $v;
29
      }
30
    }
31

    
32
    // If no title, forge it from text.
33
    if (!isset($item['title']) && isset($item['text'])) {
34
      if (strlen($item['text']) < 40) {
35
        $item['title'] = $item['text'];
36
      }
37
      else {
38
        $item['title'] = trim(substr($item['text'], 0, 30)) . ' ...';
39
      }
40
    }
41
    if (isset($item['title']) && isset($item['xmlurl'])) {
42
      $items[] = $item;
43
    }
44
  }
45
  $feeds['items'] = $items;
46
  return $feeds;
47
}