]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FeedSub/extlib/XML/Feed/Parser/RSS2Element.php
Initial functional version of feed subscription plugin, currently supporting only...
[quix0rs-gnu-social.git] / plugins / FeedSub / extlib / XML / Feed / Parser / RSS2Element.php
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,v 1.11 2006/07/26 21:18:47 jystewart Exp $
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: 1.0.3
30  * @package XML_Feed_Parser
31  */
32 class XML_Feed_Parser_RSS2Element extends XML_Feed_Parser_RSS2
33 {
34     /**
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_RSS2
38      */
39     protected $parent;
40
41     /**
42      * Our specific element map 
43      * @var array
44      */
45     protected $map = array(
46         'title' => array('Text'),
47         'guid' => array('Guid'),
48         'description' => array('Text'),
49         'author' => array('Text'),
50         'comments' => array('Text'),
51         'enclosure' => array('Enclosure'),
52         'pubDate' => array('Date'),
53         'source' => array('Source'),
54         'link' => array('Text'),
55         'content' => array('Content'));
56
57     /**
58      * Here we map some elements to their atom equivalents. This is going to be
59      * quite tricky to pull off effectively (and some users' methods may vary)
60      * but is worth trying. The key is the atom version, the value is RSS2.
61      * @var array
62      */
63     protected $compatMap = array(
64         'id' => array('guid'),
65         'updated' => array('lastBuildDate'),
66         'published' => array('pubdate'),
67         'guidislink' => array('guid', 'ispermalink'),
68         'summary' => array('description'));
69
70     /**
71      * Store useful information for later.
72      *
73      * @param   DOMElement  $element - this item as a DOM element
74      * @param   XML_Feed_Parser_RSS2    $parent - the feed of which this is a member
75      */
76     function __construct(DOMElement $element, $parent, $xmlBase = '')
77     {
78         $this->model = $element;
79         $this->parent = $parent;
80     }
81
82     /**
83      * Get the value of the guid element, if specified
84      *
85      * guid is the closest RSS2 has to atom's ID. It is usually but not always a
86      * URI. The one attribute that RSS2 can posess is 'ispermalink' which specifies
87      * whether the guid is itself dereferencable. Use of guid is not obligatory,
88      * but is advisable. To get the guid you would call $item->id() (for atom
89      * compatibility) or $item->guid(). To check if this guid is a permalink call
90      * $item->guid("ispermalink").
91      *
92      * @param   string  $method - the method name being called
93      * @param   array   $params - parameters required
94      * @return  string  the guid or value of ispermalink
95      */
96     protected function getGuid($method, $params)
97     {
98         $attribute = (isset($params[0]) and $params[0] == 'ispermalink') ? 
99             true : false;
100         $tag = $this->model->getElementsByTagName('guid');
101         if ($tag->length > 0) {
102             if ($attribute) {
103                 if ($tag->hasAttribute("ispermalink")) {
104                     return $tag->getAttribute("ispermalink");
105                 }
106             }
107             return $tag->item(0)->nodeValue;
108         }
109         return false;
110     }
111
112     /**
113      * Access details of file enclosures
114      *
115      * The RSS2 spec is ambiguous as to whether an enclosure element must be
116      * unique in a given entry. For now we will assume it needn't, and allow
117      * for an offset.
118      *
119      * @param   string $method - the method being called
120      * @param   array   $parameters - we expect the first of these to be our offset
121      * @return  array|false
122      */
123     protected function getEnclosure($method, $parameters)
124     {
125         $encs = $this->model->getElementsByTagName('enclosure');
126         $offset = isset($parameters[0]) ? $parameters[0] : 0;
127         if ($encs->length > $offset) {
128             try {
129                 if (! $encs->item($offset)->hasAttribute('url')) {
130                     return false;
131                 }
132                 $attrs = $encs->item($offset)->attributes;
133                 return array(
134                     'url' => $attrs->getNamedItem('url')->value,
135                     'length' => $attrs->getNamedItem('length')->value,
136                     'type' => $attrs->getNamedItem('type')->value);
137             } catch (Exception $e) {
138                 return false;
139             }
140         }
141         return false;
142     }
143
144     /**
145      * Get the entry source if specified
146      *
147      * source is an optional sub-element of item. Like atom:source it tells
148      * us about where the entry came from (eg. if it's been copied from another
149      * feed). It is not a rich source of metadata in the same way as atom:source
150      * and while it would be good to maintain compatibility by returning an
151      * XML_Feed_Parser_RSS2 element, it makes a lot more sense to return an array.
152      *
153      * @return array|false
154      */
155     protected function getSource()
156     {
157         $get = $this->model->getElementsByTagName('source');
158         if ($get->length) {
159             $source = $get->item(0);
160             $array = array(
161                 'content' => $source->nodeValue);
162             foreach ($source->attributes as $attribute) {
163                 $array[$attribute->name] = $attribute->value;
164             }
165             return $array;
166         }
167         return false;
168     }
169 }
170
171 ?>