Projet

Général

Profil

Paste
Télécharger (7,36 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / plugins / FeedsSimplePieParser.inc @ 3aa14731

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsSimplePieParser and related classes.
6
 */
7

    
8
/**
9
 * Adapter to present SimplePie_Enclosure as FeedsEnclosure object.
10
 */
11
class FeedsSimplePieEnclosure extends FeedsEnclosure {
12
  protected $simplepie_enclosure;
13
  private $_serialized_simplepie_enclosure;
14

    
15
  /**
16
   * Constructor requires SimplePie enclosure object.
17
   */
18
  function __construct(SimplePie_Enclosure $enclosure) {
19
    $this->simplepie_enclosure = $enclosure;
20
  }
21

    
22
  /**
23
   * Serialization helper.
24
   *
25
   * Handle the simplepie enclosure class separately ourselves.
26
   */
27
  public function __sleep() {
28
    $this->_serialized_simplepie_enclosure = serialize($this->simplepie_enclosure);
29
    return array('_serialized_simplepie_enclosure');
30
  }
31

    
32
  /**
33
   * Unserialization helper.
34
   *
35
   * Ensure that the simplepie class definitions are loaded for the enclosure when unserializing.
36
   */
37
   public function __wakeup() {
38
     feeds_include_simplepie();
39
     $this->simplepie_enclosure = unserialize($this->_serialized_simplepie_enclosure);
40
  }
41

    
42
  /**
43
   * Override parent::getValue().
44
   */
45
  public function getValue() {
46
    return $this->simplepie_enclosure->get_link();
47
  }
48

    
49
  /**
50
   * Override parent::getMIMEType().
51
   */
52
  public function getMIMEType() {
53
    return $this->simplepie_enclosure->get_real_type();
54
  }
55

    
56
}
57

    
58
/**
59
 * Class definition for Common Syndication Parser.
60
 *
61
 * Parses RSS and Atom feeds.
62
 */
63
class FeedsSimplePieParser extends FeedsParser {
64

    
65
  /**
66
   * Implements FeedsParser::parse().
67
   */
68
  public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
69
    feeds_include_simplepie();
70

    
71
    // Initialize SimplePie.
72
    $parser = new SimplePie();
73
    $parser->set_raw_data($fetcher_result->getRaw());
74
    $parser->set_stupidly_fast(TRUE);
75
    $parser->encode_instead_of_strip(FALSE);
76
    // @todo Is caching effective when we pass in raw data?
77
    $parser->enable_cache(TRUE);
78
    $parser->set_cache_location($this->cacheDirectory());
79
    $parser->init();
80

    
81
    // Construct the standard form of the parsed feed
82
    $result = new FeedsParserResult();
83
    $result->title = html_entity_decode(($title = $parser->get_title()) ? $title : $this->createTitle($parser->get_description()));
84
    $result->description = $parser->get_description();
85
    $result->link = html_entity_decode($parser->get_link());
86

    
87
    $items_num = $parser->get_item_quantity();
88
    for ($i = 0; $i < $items_num; $i++) {
89
      $item = array();
90
      $simplepie_item = $parser->get_item($i);
91
      $item['title'] = html_entity_decode(($title = $simplepie_item->get_title()) ? $title : $this->createTitle($simplepie_item->get_content()));
92
      $item['description'] = $simplepie_item->get_content();
93
      $item['url'] = html_entity_decode($simplepie_item->get_link());
94
      // Use UNIX time. If no date is defined, fall back to REQUEST_TIME.
95
      $item['timestamp'] = $simplepie_item->get_date("U");
96
      if (empty($item['timestamp'])) {
97
        $item['timestamp'] = REQUEST_TIME;
98
      }
99
      $item['guid'] = $simplepie_item->get_id();
100
      // Use URL as GUID if there is no GUID.
101
      if (empty($item['guid'])) {
102
        $item['guid'] = $item['url'];
103
      }
104
      $author = $simplepie_item->get_author();
105
      $item['author_name'] = isset($author->name) ? html_entity_decode($author->name) : '';
106
      $item['author_link'] = isset($author->link) ? $author->link : '';
107
      $item['author_email'] = isset($author->email) ? $author->email : '';
108
      // Enclosures
109
      $enclosures = $simplepie_item->get_enclosures();
110
      if (is_array($enclosures)) {
111
        foreach ($enclosures as $enclosure) {
112
          $item['enclosures'][] = new FeedsSimplePieEnclosure($enclosure);
113
        }
114
      }
115
      // Location
116
      $latitude = $simplepie_item->get_latitude();
117
      $longitude = $simplepie_item->get_longitude();
118
      if (!is_null($latitude) && !is_null($longitude)) {
119
        $item['location_latitude'][] = $latitude;
120
        $item['location_longitude'][] = $longitude;
121
      }
122
      // Extract tags related to the item
123
      $simplepie_tags = $simplepie_item->get_categories();
124
      $tags = array();
125
      $domains = array();
126
      if (count($simplepie_tags) > 0) {
127
        foreach ($simplepie_tags as $tag) {
128
          $tags[] = (string) $tag->term;
129
          $domain = (string) $tag->get_scheme();
130
          if (!empty($domain)) {
131
            if (!isset($domains[$domain])) {
132
              $domains[$domain] = array();
133
            }
134
            $domains[$domain][] = count($tags) - 1;
135
          }
136
        }
137
      }
138
      $item['domains'] = $domains;
139
      $item['tags'] = $tags;
140

    
141
      // Allow parsing to be extended.
142
      $this->parseExtensions($item, $simplepie_item);
143
      $item['raw'] = $simplepie_item->data;
144

    
145
      $result->items[] = $item;
146
    }
147
    // Release parser.
148
    unset($parser);
149

    
150
    return $result;
151
  }
152

    
153
  /**
154
   * Allow extension of FeedsSimplePie item parsing.
155
   */
156
  protected function parseExtensions(&$item, $simplepie_item) {}
157

    
158
  /**
159
   * Return mapping sources.
160
   */
161
  public function getMappingSources() {
162
    return array(
163
      'title' => array(
164
        'name' => t('Title'),
165
        'description' => t('Title of the feed item.'),
166
      ),
167
      'description' => array(
168
        'name' => t('Description'),
169
        'description' => t('Description of the feed item.'),
170
      ),
171
      'author_name' => array(
172
        'name' => t('Author name'),
173
        'description' => t('Name of the feed item\'s author.'),
174
      ),
175
      'author_link' => array(
176
        'name' => t('Author link'),
177
        'description' => t('Link to the feed item\'s author.'),
178
      ),
179
      'author_email' => array(
180
        'name' => t('Author email'),
181
        'description' => t('Email address of the feed item\'s author.'),
182
      ),
183
      'timestamp' => array(
184
        'name' => t('Published date'),
185
        'description' => t('Published date as UNIX time GMT of the feed item.'),
186
      ),
187
      'url' => array(
188
        'name' => t('Item URL (link)'),
189
        'description' => t('URL of the feed item.'),
190
      ),
191
      'guid' => array(
192
        'name' => t('Item GUID'),
193
        'description' => t('Global Unique Identifier of the feed item.'),
194
      ),
195
      'tags' => array(
196
        'name' => t('Categories'),
197
        'description' => t('An array of categories that have been assigned to the feed item.'),
198
      ),
199
      'domains' => array(
200
        'name' => t('Category domains'),
201
        'description' => t('Domains of the categories.'),
202
      ),
203
      'location_latitude' => array(
204
        'name' => t('Latitudes'),
205
        'description' => t('An array of latitudes assigned to the feed item.'),
206
      ),
207
      'location_longitude' => array(
208
        'name' => t('Longitudes'),
209
        'description' => t('An array of longitudes assigned to the feed item.'),
210
      ),
211
      'enclosures' => array(
212
        'name' => t('Enclosures'),
213
        'description' => t('An array of enclosures attached to the feed item.'),
214
      ),
215
    ) + parent::getMappingSources();
216
  }
217

    
218
  /**
219
   * Returns cache directory. Creates it if it doesn't exist.
220
   */
221
  protected function cacheDirectory() {
222
    $directory = 'public://simplepie';
223
    file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
224
    return $directory;
225
  }
226

    
227
  /**
228
   * Generate a title from a random text.
229
   */
230
  protected function createTitle($text = FALSE) {
231
    // Explode to words and use the first 3 words.
232
    $words = preg_split("/[\s,]+/", $text);
233
    $words = array_slice($words, 0, 3);
234
    return implode(' ', $words);
235
  }
236

    
237
}