1328 |
aurelien |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Class representing entries in an RSS2 feed.
|
|
|
6 |
*
|
|
|
7 |
* PHP versions 5
|
|
|
8 |
*
|
|
|
9 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
10 |
* that is available through the world-wide-web at the following URI:
|
|
|
11 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
12 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
13 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
14 |
*
|
|
|
15 |
* @category XML
|
|
|
16 |
* @package XML_Feed_Parser
|
|
|
17 |
* @author James Stewart <james@jystewart.net>
|
|
|
18 |
* @copyright 2005 James Stewart <james@jystewart.net>
|
|
|
19 |
* @license http://www.gnu.org/copyleft/lesser.html GNU LGPL 2.1
|
|
|
20 |
* @version CVS: $Id: RSS2Element.php 304308 2010-10-11 12:05:50Z clockwerx $
|
|
|
21 |
* @link http://pear.php.net/package/XML_Feed_Parser/
|
|
|
22 |
*/
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* This class provides support for RSS 2.0 entries. It will usually be
|
|
|
26 |
* called by XML_Feed_Parser_RSS2 with which it shares many methods.
|
|
|
27 |
*
|
|
|
28 |
* @author James Stewart <james@jystewart.net>
|
|
|
29 |
* @version Release: @package_version@
|
|
|
30 |
* @package XML_Feed_Parser
|
|
|
31 |
*/
|
|
|
32 |
class XmlFeedParserRss2Element extends XmlFeedParserRss2 {
|
|
|
33 |
/**
|
|
|
34 |
* This will be a reference to the parent object for when we want
|
|
|
35 |
* to use a 'fallback' rule
|
|
|
36 |
* @var XML_Feed_Parser_RSS2
|
|
|
37 |
*/
|
|
|
38 |
protected $parent;
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Our specific element map
|
|
|
42 |
* @var array
|
|
|
43 |
*/
|
|
|
44 |
protected $map = array(
|
|
|
45 |
'title' => array('Text'),
|
|
|
46 |
'guid' => array('Guid'),
|
|
|
47 |
'description' => array('Text'),
|
|
|
48 |
'author' => array('Text'),
|
|
|
49 |
'comments' => array('Text'),
|
|
|
50 |
'enclosure' => array('Enclosure'),
|
|
|
51 |
'pubDate' => array('Date'),
|
|
|
52 |
'source' => array('Source'),
|
|
|
53 |
'link' => array('Text'),
|
|
|
54 |
'content' => array('Content'));
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Here we map some elements to their atom equivalents. This is going to be
|
|
|
58 |
* quite tricky to pull off effectively (and some users' methods may vary)
|
|
|
59 |
* but is worth trying. The key is the atom version, the value is RSS2.
|
|
|
60 |
* @var array
|
|
|
61 |
*/
|
|
|
62 |
protected $compatMap = array(
|
|
|
63 |
'id' => array('guid'),
|
|
|
64 |
'updated' => array('lastBuildDate'),
|
|
|
65 |
'published' => array('pubdate'),
|
|
|
66 |
'guidislink' => array('guid', 'ispermalink'),
|
|
|
67 |
'summary' => array('description'));
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Store useful information for later.
|
|
|
71 |
*
|
|
|
72 |
* @param DOMElement $element - this item as a DOM element
|
|
|
73 |
* @param XML_Feed_Parser_RSS2 $parent - the feed of which this is a member
|
|
|
74 |
*/
|
|
|
75 |
function __construct(DOMElement $element, $parent, $xmlBase = '') {
|
|
|
76 |
$this->model = $element;
|
|
|
77 |
$this->parent = $parent;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
/**
|
|
|
81 |
* Get the value of the guid element, if specified
|
|
|
82 |
*
|
|
|
83 |
* guid is the closest RSS2 has to atom's ID. It is usually but not always a
|
|
|
84 |
* URI. The one attribute that RSS2 can posess is 'ispermalink' which specifies
|
|
|
85 |
* whether the guid is itself dereferencable. Use of guid is not obligatory,
|
|
|
86 |
* but is advisable. To get the guid you would call $item->id() (for atom
|
|
|
87 |
* compatibility) or $item->guid(). To check if this guid is a permalink call
|
|
|
88 |
* $item->guid("ispermalink").
|
|
|
89 |
*
|
|
|
90 |
* @param string $method - the method name being called
|
|
|
91 |
* @param array $params - parameters required
|
|
|
92 |
* @return string the guid or value of ispermalink
|
|
|
93 |
*/
|
|
|
94 |
protected function getGuid($method, $params) {
|
|
|
95 |
$attribute = (isset($params[0]) and $params[0] == 'ispermalink') ?
|
|
|
96 |
true : false;
|
|
|
97 |
$tag = $this->model->getElementsByTagName('guid');
|
|
|
98 |
if ($tag->length > 0) {
|
|
|
99 |
if ($attribute) {
|
|
|
100 |
if ($tag->hasAttribute("ispermalink")) {
|
|
|
101 |
return $tag->getAttribute("ispermalink");
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
return $tag->item(0)->nodeValue;
|
|
|
105 |
}
|
|
|
106 |
return false;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Access details of file enclosures
|
|
|
111 |
*
|
|
|
112 |
* The RSS2 spec is ambiguous as to whether an enclosure element must be
|
|
|
113 |
* unique in a given entry. For now we will assume it needn't, and allow
|
|
|
114 |
* for an offset.
|
|
|
115 |
*
|
|
|
116 |
* @param string $method - the method being called
|
|
|
117 |
* @param array $parameters - we expect the first of these to be our offset
|
|
|
118 |
* @return array|false
|
|
|
119 |
*/
|
|
|
120 |
protected function getEnclosure($method, $parameters) {
|
|
|
121 |
$encs = $this->model->getElementsByTagName('enclosure');
|
|
|
122 |
$offset = isset($parameters[0]) ? $parameters[0] : 0;
|
|
|
123 |
if ($encs->length > $offset) {
|
|
|
124 |
try {
|
|
|
125 |
if (! $encs->item($offset)->hasAttribute('url')) {
|
|
|
126 |
return false;
|
|
|
127 |
}
|
|
|
128 |
$attrs = $encs->item($offset)->attributes;
|
|
|
129 |
return array(
|
|
|
130 |
'url' => $attrs->getNamedItem('url')->value,
|
|
|
131 |
'length' => $attrs->getNamedItem('length')->value,
|
|
|
132 |
'type' => $attrs->getNamedItem('type')->value);
|
|
|
133 |
} catch (Exception $e) {
|
|
|
134 |
return false;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
return false;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get the entry source if specified
|
|
|
142 |
*
|
|
|
143 |
* source is an optional sub-element of item. Like atom:source it tells
|
|
|
144 |
* us about where the entry came from (eg. if it's been copied from another
|
|
|
145 |
* feed). It is not a rich source of metadata in the same way as atom:source
|
|
|
146 |
* and while it would be good to maintain compatibility by returning an
|
|
|
147 |
* XML_Feed_Parser_RSS2 element, it makes a lot more sense to return an array.
|
|
|
148 |
*
|
|
|
149 |
* @return array|false
|
|
|
150 |
*/
|
|
|
151 |
protected function getSource() {
|
|
|
152 |
$get = $this->model->getElementsByTagName('source');
|
|
|
153 |
if ($get->length) {
|
|
|
154 |
$source = $get->item(0);
|
|
|
155 |
$array = array(
|
|
|
156 |
'content' => $source->nodeValue);
|
|
|
157 |
foreach ($source->attributes as $attribute) {
|
|
|
158 |
$array[$attribute->name] = $attribute->value;
|
|
|
159 |
}
|
|
|
160 |
return $array;
|
|
|
161 |
}
|
|
|
162 |
return false;
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
?>
|