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$ 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 */ 32class 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 $this->setSanitizer($parent->getSanitizer()); 81 } 82 83 /** 84 * Get the value of the guid element, if specified 85 * 86 * guid is the closest RSS2 has to atom's ID. It is usually but not always a 87 * URI. The one attribute that RSS2 can posess is 'ispermalink' which specifies 88 * whether the guid is itself dereferencable. Use of guid is not obligatory, 89 * but is advisable. To get the guid you would call $item->id() (for atom 90 * compatibility) or $item->guid(). To check if this guid is a permalink call 91 * $item->guid("ispermalink"). 92 * 93 * @param string $method - the method name being called 94 * @param array $params - parameters required 95 * @return string the guid or value of ispermalink 96 */ 97 protected function getGuid($method, $params) 98 { 99 $attribute = (isset($params[0]) and $params[0] == 'ispermalink') ? 100 true : false; 101 $tag = $this->model->getElementsByTagName('guid'); 102 if ($tag->length > 0) { 103 if ($attribute) { 104 if ($tag->hasAttribute("ispermalink")) { 105 return $tag->getAttribute("ispermalink"); 106 } 107 } 108 return $tag->item(0)->nodeValue; 109 } 110 return false; 111 } 112 113 /** 114 * Access details of file enclosures 115 * 116 * The RSS2 spec is ambiguous as to whether an enclosure element must be 117 * unique in a given entry. For now we will assume it needn't, and allow 118 * for an offset. 119 * 120 * @param string $method - the method being called 121 * @param array $parameters - we expect the first of these to be our offset 122 * @return array|false 123 */ 124 protected function getEnclosure($method, $parameters) 125 { 126 $encs = $this->model->getElementsByTagName('enclosure'); 127 $offset = isset($parameters[0]) ? $parameters[0] : 0; 128 if ($encs->length > $offset) { 129 try { 130 if (! $encs->item($offset)->hasAttribute('url')) { 131 return false; 132 } 133 $attrs = $encs->item($offset)->attributes; 134 return array( 135 'url' => $attrs->getNamedItem('url')->value, 136 'length' => $attrs->getNamedItem('length')->value, 137 'type' => $attrs->getNamedItem('type')->value); 138 } catch (Exception $e) { 139 return false; 140 } 141 } 142 return false; 143 } 144 145 /** 146 * Get the entry source if specified 147 * 148 * source is an optional sub-element of item. Like atom:source it tells 149 * us about where the entry came from (eg. if it's been copied from another 150 * feed). It is not a rich source of metadata in the same way as atom:source 151 * and while it would be good to maintain compatibility by returning an 152 * XML_Feed_Parser_RSS2 element, it makes a lot more sense to return an array. 153 * 154 * @return array|false 155 */ 156 protected function getSource() 157 { 158 $get = $this->model->getElementsByTagName('source'); 159 if ($get->length) { 160 $source = $get->item(0); 161 $array = array( 162 'content' => $source->nodeValue); 163 foreach ($source->attributes as $attribute) { 164 $array[$attribute->name] = $attribute->value; 165 } 166 return $array; 167 } 168 return false; 169 } 170} 171 172?> 173