2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * AtomElement class for XML_Feed_Parser package
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.
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: AtomElement.php,v 1.19 2007/03/26 12:43:11 jystewart Exp $
21 * @link http://pear.php.net/package/XML_Feed_Parser/
25 * This class provides support for atom entries. It will usually be called by
26 * XML_Feed_Parser_Atom with which it shares many methods.
28 * @author James Stewart <james@jystewart.net>
29 * @version Release: 1.0.3
30 * @package XML_Feed_Parser
32 class XML_Feed_Parser_AtomElement extends XML_Feed_Parser_Atom
35 * This will be a reference to the parent object for when we want
36 * to use a 'fallback' rule
37 * @var XML_Feed_Parser_Atom
42 * When performing XPath queries we will use this prefix
45 private $xpathPrefix = '';
48 * xml:base values inherited by the element
54 * Here we provide a few mappings for those very special circumstances in
55 * which it makes sense to map back to the RSS2 spec or to manage other
56 * compatibilities (eg. with the Univeral Feed Parser). Key is the other version's
57 * name for the command, value is an array consisting of the equivalent in our atom
58 * api and any attributes needed to make the mapping.
61 protected $compatMap = array(
62 'guid' => array('id'),
63 'links' => array('link'),
64 'tags' => array('category'),
65 'contributors' => array('contributor'));
68 * Our specific element map
71 protected $map = array(
72 'author' => array('Person', 'fallback'),
73 'contributor' => array('Person'),
74 'id' => array('Text', 'fail'),
75 'published' => array('Date'),
76 'updated' => array('Date', 'fail'),
77 'title' => array('Text', 'fail'),
78 'rights' => array('Text', 'fallback'),
79 'summary' => array('Text'),
80 'content' => array('Content'),
81 'link' => array('Link'),
82 'enclosure' => array('Enclosure'),
83 'category' => array('Category'));
86 * Store useful information for later.
88 * @param DOMElement $element - this item as a DOM element
89 * @param XML_Feed_Parser_Atom $parent - the feed of which this is a member
91 function __construct(DOMElement $element, $parent, $xmlBase = '')
93 $this->model = $element;
94 $this->parent = $parent;
95 $this->xmlBase = $xmlBase;
96 $this->xpathPrefix = "//atom:entry[atom:id='" . $this->id . "']/";
97 $this->xpath = $this->parent->xpath;
101 * Provides access to specific aspects of the author data for an atom entry
103 * Author data at the entry level is more complex than at the feed level.
104 * If atom:author is not present for the entry we need to look for it in
105 * an atom:source child of the atom:entry. If it's not there either, then
106 * we look to the parent for data.
111 function getAuthor($arguments)
113 /* Find out which part of the author data we're looking for */
114 if (isset($arguments['param'])) {
115 $parameter = $arguments['param'];
120 $test = $this->model->getElementsByTagName('author');
121 if ($test->length > 0) {
122 $item = $test->item(0);
123 return $item->getElementsByTagName($parameter)->item(0)->nodeValue;
126 $source = $this->model->getElementsByTagName('source');
127 if ($source->length > 0) {
128 $test = $this->model->getElementsByTagName('author');
129 if ($test->length > 0) {
130 $item = $test->item(0);
131 return $item->getElementsByTagName($parameter)->item(0)->nodeValue;
134 return $this->parent->getAuthor($arguments);
138 * Returns the content of the content element or info on a specific attribute
140 * This element may or may not be present. It cannot be present more than
141 * once. It may have a 'src' attribute, in which case there's no content
142 * If not present, then the entry must have link with rel="alternate".
143 * If there is content we return it, if not and there's a 'src' attribute
144 * we return the value of that instead. The method can take an 'attribute'
145 * argument, in which case we return the value of that attribute if present.
146 * eg. $item->content("type") will return the type of the content. It is
147 * recommended that all users check the type before getting the content to
148 * ensure that their script is capable of handling the type of returned data.
149 * (data carried in the content element can be either 'text', 'html', 'xhtml',
150 * or any standard MIME type).
152 * @return string|false
154 protected function getContent($method, $arguments = array())
156 $attribute = empty($arguments[0]) ? false : $arguments[0];
157 $tags = $this->model->getElementsByTagName('content');
159 if ($tags->length == 0) {
163 $content = $tags->item(0);
165 if (! $content->hasAttribute('type')) {
166 $content->setAttribute('type', 'text');
168 if (! empty($attribute)) {
169 return $content->getAttribute($attribute);
172 $type = $content->getAttribute('type');
174 if (! empty($attribute)) {
175 if ($content->hasAttribute($attribute))
177 return $content->getAttribute($attribute);
182 if ($content->hasAttribute('src')) {
183 return $content->getAttribute('src');
186 return $this->parseTextConstruct($content);
190 * For compatibility, this method provides a mapping to access enclosures.
192 * The Atom spec doesn't provide for an enclosure element, but it is
193 * generally supported using the link element with rel='enclosure'.
195 * @param string $method - for compatibility with our __call usage
196 * @param array $arguments - for compatibility with our __call usage
197 * @return array|false
199 function getEnclosure($method, $arguments = array())
201 $offset = isset($arguments[0]) ? $arguments[0] : 0;
202 $query = "//atom:entry[atom:id='" . $this->getText('id', false) .
203 "']/atom:link[@rel='enclosure']";
205 $encs = $this->parent->xpath->query($query);
206 if ($encs->length > $offset) {
208 if (! $encs->item($offset)->hasAttribute('href')) {
211 $attrs = $encs->item($offset)->attributes;
212 $length = $encs->item($offset)->hasAttribute('length') ?
213 $encs->item($offset)->getAttribute('length') : false;
215 'url' => $attrs->getNamedItem('href')->value,
216 'type' => $attrs->getNamedItem('type')->value,
217 'length' => $length);
218 } catch (Exception $e) {
226 * Get details of this entry's source, if available/relevant
228 * Where an atom:entry is taken from another feed then the aggregator
229 * is supposed to include an atom:source element which replicates at least
230 * the atom:id, atom:title, and atom:updated metadata from the original
231 * feed. Atom:source therefore has a very similar structure to atom:feed
232 * and if we find it we will return it as an XML_Feed_Parser_Atom object.
234 * @return XML_Feed_Parser_Atom|false
238 $test = $this->model->getElementsByTagName('source');
239 if ($test->length == 0) {
242 $source = new XML_Feed_Parser_Atom($test->item(0));
246 * Get the entry as an XML string
248 * Return an XML serialization of the feed, should it be required. Most
249 * users however, will already have a serialization that they used when
250 * instantiating the object.
252 * @return string XML serialization of element
254 function __toString()
256 $simple = simplexml_import_dom($this->model);
257 return $simple->asXML();