Projet

Général

Profil

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

root / drupal7 / sites / all / modules / feeds / plugins / FeedsSimplePieParser.inc @ 2c8c2b87

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 seperately 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
 * Class definition for Common Syndication Parser.
59
 *
60
 * Parses RSS and Atom feeds.
61
 */
62
class FeedsSimplePieParser extends FeedsParser {
63

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

    
70
    // Please be quiet SimplePie.
71
    $level = error_reporting();
72
    error_reporting($level ^ E_DEPRECATED ^ E_STRICT);
73

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

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

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

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

    
148
      $result->items[] = $item;
149
    }
150
    // Release parser.
151
    unset($parser);
152
    // Set error reporting back to its previous value.
153
    error_reporting($level);
154
    return $result;
155
  }
156

    
157
  /**
158
   * Allow extension of FeedsSimplePie item parsing.
159
   */
160
  protected function parseExtensions(&$item, $simplepie_item) {}
161

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

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

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