Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
rss.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Feed
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * RSS Feed Parser class.
14  *
15  * @package Joomla.Platform
16  * @subpackage Feed
17  * @link http://cyber.law.harvard.edu/rss/rss.html
18  * @since 12.3
19  */
21 {
22  /**
23  * @var string The feed element name for the entry elements.
24  * @since 12.3
25  */
26  protected $entryElementName = 'item';
27 
28  /**
29  * @var string The feed format version.
30  * @since 12.3
31  */
32  protected $version;
33 
34  /**
35  * Method to handle the <category> element for the feed.
36  *
37  * @param JFeed $feed The JFeed object being built from the parsed feed.
38  * @param SimpleXMLElement $el The current XML element object to handle.
39  *
40  * @return void
41  *
42  * @since 12.3
43  */
44  protected function handleCategory(JFeed $feed, SimpleXMLElement $el)
45  {
46  // Get the data from the element.
47  $domain = (string) $el['domain'];
48  $category = (string) $el;
49 
50  $feed->addCategory($category, $domain);
51  }
52 
53  /**
54  * Method to handle the <cloud> element for the feed.
55  *
56  * @param JFeed $feed The JFeed object being built from the parsed feed.
57  * @param SimpleXMLElement $el The current XML element object to handle.
58  *
59  * @return void
60  *
61  * @since 12.3
62  */
63  protected function handleCloud(JFeed $feed, SimpleXMLElement $el)
64  {
65  $cloud = new stdClass;
66  $cloud->domain = (string) $el['domain'];
67  $cloud->port = (string) $el['port'];
68  $cloud->path = (string) $el['path'];
69  $cloud->protocol = (string) $el['protocol'];
70  $cloud->registerProcedure = (string) $el['registerProcedure'];
71 
72  $feed->cloud = $cloud;
73  }
74 
75  /**
76  * Method to handle the <copyright> element for the feed.
77  *
78  * @param JFeed $feed The JFeed object being built from the parsed feed.
79  * @param SimpleXMLElement $el The current XML element object to handle.
80  *
81  * @return void
82  *
83  * @since 12.3
84  */
85  protected function handleCopyright(JFeed $feed, SimpleXMLElement $el)
86  {
87  $feed->copyright = (string) $el;
88  }
89 
90  /**
91  * Method to handle the <description> element for the feed.
92  *
93  * @param JFeed $feed The JFeed object being built from the parsed feed.
94  * @param SimpleXMLElement $el The current XML element object to handle.
95  *
96  * @return void
97  *
98  * @since 12.3
99  */
100  protected function handleDescription(JFeed $feed, SimpleXMLElement $el)
101  {
102  $feed->description = (string) $el;
103  }
104 
105  /**
106  * Method to handle the <generator> element for the feed.
107  *
108  * @param JFeed $feed The JFeed object being built from the parsed feed.
109  * @param SimpleXMLElement $el The current XML element object to handle.
110  *
111  * @return void
112  *
113  * @since 12.3
114  */
115  protected function handleGenerator(JFeed $feed, SimpleXMLElement $el)
116  {
117  $feed->generator = (string) $el;
118  }
119 
120  /**
121  * Method to handle the <image> element for the feed.
122  *
123  * @param JFeed $feed The JFeed object being built from the parsed feed.
124  * @param SimpleXMLElement $el The current XML element object to handle.
125  *
126  * @return void
127  *
128  * @since 12.3
129  */
130  protected function handleImage(JFeed $feed, SimpleXMLElement $el)
131  {
132  // Create a feed link object for the image.
133  $image = new JFeedLink(
134  (string) $el->url,
135  null,
136  'logo',
137  null,
138  (string) $el->title
139  );
140 
141  // Populate extra fields if they exist.
142  $image->link = (string) $el->link;
143  $image->description = (string) $el->description;
144  $image->height = (string) $el->height;
145  $image->width = (string) $el->width;
146 
147  $feed->image = $image;
148  }
149 
150  /**
151  * Method to handle the <language> element for the feed.
152  *
153  * @param JFeed $feed The JFeed object being built from the parsed feed.
154  * @param SimpleXMLElement $el The current XML element object to handle.
155  *
156  * @return void
157  *
158  * @since 12.3
159  */
160  protected function handleLanguage(JFeed $feed, SimpleXMLElement $el)
161  {
162  $feed->language = (string) $el;
163  }
164 
165  /**
166  * Method to handle the <lastBuildDate> element for the feed.
167  *
168  * @param JFeed $feed The JFeed object being built from the parsed feed.
169  * @param SimpleXMLElement $el The current XML element object to handle.
170  *
171  * @return void
172  *
173  * @since 12.3
174  */
175  protected function handleLastBuildDate(JFeed $feed, SimpleXMLElement $el)
176  {
177  $feed->updatedDate = (string) $el;
178  }
179 
180  /**
181  * Method to handle the <link> element for the feed.
182  *
183  * @param JFeed $feed The JFeed object being built from the parsed feed.
184  * @param SimpleXMLElement $el The current XML element object to handle.
185  *
186  * @return void
187  *
188  * @since 12.3
189  */
190  protected function handleLink(JFeed $feed, SimpleXMLElement $el)
191  {
192  $link = new JFeedLink;
193  $link->uri = (string) $el['href'];
194  $feed->link = $link;
195  }
196 
197  /**
198  * Method to handle the <managingEditor> element for the feed.
199  *
200  * @param JFeed $feed The JFeed object being built from the parsed feed.
201  * @param SimpleXMLElement $el The current XML element object to handle.
202  *
203  * @return void
204  *
205  * @since 12.3
206  */
207  protected function handleManagingEditor(JFeed $feed, SimpleXMLElement $el)
208  {
209  $feed->author = $this->processPerson((string) $el);
210  }
211 
212  /**
213  * Method to handle the <skipDays> element for the feed.
214  *
215  * @param JFeed $feed The JFeed object being built from the parsed feed.
216  * @param SimpleXMLElement $el The current XML element object to handle.
217  *
218  * @return void
219  *
220  * @since 12.3
221  */
222  protected function handleSkipDays(JFeed $feed, SimpleXMLElement $el)
223  {
224  // Initialise the array.
225  $days = array();
226 
227  // Add all of the day values from the feed to the array.
228  foreach ($el->day as $day)
229  {
230  $days[] = (string) $day;
231  }
232 
233  $feed->skipDays = $days;
234  }
235 
236  /**
237  * Method to handle the <skipHours> element for the feed.
238  *
239  * @param JFeed $feed The JFeed object being built from the parsed feed.
240  * @param SimpleXMLElement $el The current XML element object to handle.
241  *
242  * @return void
243  *
244  * @since 12.3
245  */
246  protected function handleSkipHours(JFeed $feed, SimpleXMLElement $el)
247  {
248  // Initialise the array.
249  $hours = array();
250 
251  // Add all of the day values from the feed to the array.
252  foreach ($el->hour as $hour)
253  {
254  $hours[] = (int) $hour;
255  }
256 
257  $feed->skipHours = $hours;
258  }
259 
260  /**
261  * Method to handle the <pubDate> element for the feed.
262  *
263  * @param JFeed $feed The JFeed object being built from the parsed feed.
264  * @param SimpleXMLElement $el The current XML element object to handle.
265  *
266  * @return void
267  *
268  * @since 12.3
269  */
270  protected function handlePubDate(JFeed $feed, SimpleXMLElement $el)
271  {
272  $feed->publishedDate = (string) $el;
273  }
274 
275  /**
276  * Method to handle the <title> element for the feed.
277  *
278  * @param JFeed $feed The JFeed object being built from the parsed feed.
279  * @param SimpleXMLElement $el The current XML element object to handle.
280  *
281  * @return void
282  *
283  * @since 12.3
284  */
285  protected function handleTitle(JFeed $feed, SimpleXMLElement $el)
286  {
287  $feed->title = (string) $el;
288  }
289 
290  /**
291  * Method to handle the <ttl> element for the feed.
292  *
293  * @param JFeed $feed The JFeed object being built from the parsed feed.
294  * @param SimpleXMLElement $el The current XML element object to handle.
295  *
296  * @return void
297  *
298  * @since 12.3
299  */
300  protected function handleTtl(JFeed $feed, SimpleXMLElement $el)
301  {
302  $feed->ttl = (integer) $el;
303  }
304 
305  /**
306  * Method to handle the <webmaster> element for the feed.
307  *
308  * @param JFeed $feed The JFeed object being built from the parsed feed.
309  * @param SimpleXMLElement $el The current XML element object to handle.
310  *
311  * @return void
312  *
313  * @since 12.3
314  */
315  protected function handleWebmaster(JFeed $feed, SimpleXMLElement $el)
316  {
317  // Get the tag contents and split it over the first space.
318  $tmp = (string) $el;
319  $tmp = explode(' ', $tmp, 2);
320 
321  // This is really cheap parsing. Probably need to create a method to do this more robustly.
322  $name = null;
323 
324  if (isset($tmp[1]))
325  {
326  $name = trim($tmp[1], ' ()');
327  }
328 
329  $email = trim($tmp[0]);
330 
331  $feed->addContributor($name, $email, null, 'webmaster');
332  }
333 
334  /**
335  * Method to initialise the feed for parsing. Here we detect the version and advance the stream
336  * reader so that it is ready to parse feed elements.
337  *
338  * @return void
339  *
340  * @since 12.3
341  */
342  protected function initialise()
343  {
344  // Read the version attribute.
345  $this->version = $this->stream->getAttribute('version');
346 
347  // We want to move forward to the first element after the <channel> element.
348  $this->moveToNextElement('channel');
349  $this->moveToNextElement();
350  }
351 
352  /**
353  * Method to handle the feed entry element for the feed: <item>.
354  *
355  * @param JFeedEntry $entry The JFeedEntry object being built from the parsed feed entry.
356  * @param SimpleXMLElement $el The current XML element object to handle.
357  *
358  * @return void
359  *
360  * @since 12.3
361  */
362  protected function processFeedEntry(JFeedEntry $entry, SimpleXMLElement $el)
363  {
364  $entry->uri = (string) $el->link;
365  $entry->title = (string) $el->title;
366  $entry->publishedDate = (string) $el->pubDate;
367  $entry->updatedDate = (string) $el->pubDate;
368  $entry->content = (string) $el->description;
369  $entry->guid = (string) $el->guid;
370  $entry->comments = (string) $el->comments;
371 
372  // Add the feed entry author if available.
373  $author = (string) $el->author;
374 
375  if (!empty($author))
376  {
377  $entry->author = $this->processPerson($author);
378  }
379 
380  // Add any categories to the entry.
381  foreach ($el->category as $category)
382  {
383  $entry->addCategory((string) $category, (string) $category['domain']);
384  }
385 
386  // Add any enclosures to the entry.
387  foreach ($el->enclosure as $enclosure)
388  {
389  $link = new JFeedLink(
390  (string) $enclosure['url'],
391  null,
392  (string) $enclosure['type'],
393  null,
394  null,
395  (int) $enclosure['length']
396  );
397 
398  $entry->addLink($link);
399  }
400  }
401 
402  /**
403  * Method to parse a string with person data and return a JFeedPerson object.
404  *
405  * @param string $data The string to parse for a person.
406  *
407  * @return JFeedPerson
408  *
409  * @since 12.3
410  */
411  protected function processPerson($data)
412  {
413  // Create a new person object.
414  $person = new JFeedPerson;
415 
416  // This is really cheap parsing, but so far good enough. :)
417  $data = explode(' ', $data, 2);
418 
419  if (isset($data[1]))
420  {
421  $person->name = trim($data[1], ' ()');
422  }
423 
424  // Set the email for the person.
425  $person->email = trim($data[0]);
426 
427  return $person;
428  }
429 }