PageRenderTime 36ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/s3db3.5.10/pearlib/XML/RSS.php

https://github.com/drobbins/s3db
PHP | 359 lines | 116 code | 49 blank | 194 comment | 17 complexity | b42083de8ca59645b77e8eb1740630ab MD5 | raw file
  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net> |
  17. // | |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: RSS.php,v 1.14 2003/03/13 20:32:00 mj Exp $
  21. //
  22. require_once 'XML/Parser.php';
  23. /**
  24. * RSS parser class.
  25. *
  26. * This class is a parser for Resource Description Framework (RDF) Site
  27. * Summary (RSS) documents. For more information on RSS see the
  28. * website of the RSS working group (http://www.purl.org/rss/).
  29. *
  30. * @author Martin Jansen <mj@php.net>
  31. * @version $Revision: 1.14 $
  32. * @access public
  33. */
  34. class XML_RSS extends XML_Parser
  35. {
  36. // {{{ properties
  37. /**
  38. * @var string
  39. */
  40. var $insideTag = '';
  41. /**
  42. * @var string
  43. */
  44. var $activeTag = '';
  45. /**
  46. * @var array
  47. */
  48. var $channel = array();
  49. /**
  50. * @var array
  51. */
  52. var $items = array();
  53. /**
  54. * @var array
  55. */
  56. var $item = array();
  57. /**
  58. * @var array
  59. */
  60. var $image = array();
  61. /**
  62. * @var array
  63. */
  64. var $textinput = array();
  65. /**
  66. * @var array
  67. */
  68. var $textinputs = array();
  69. /**
  70. * @var array
  71. */
  72. var $parentTags = array('CHANNEL', 'ITEM', 'IMAGE', 'TEXTINPUT');
  73. /**
  74. * @var array
  75. */
  76. var $channelTags = array('TITLE', 'LINK', 'DESCRIPTION', 'IMAGE',
  77. 'ITEMS', 'TEXTINPUT');
  78. /**
  79. * @var array
  80. */
  81. var $itemTags = array('TITLE', 'LINK', 'DESCRIPTION', 'PUBDATE');
  82. /**
  83. * @var array
  84. */
  85. var $imageTags = array('TITLE', 'URL', 'LINK');
  86. var $textinputTags = array('TITLE', 'DESCRIPTION', 'NAME', 'LINK');
  87. /**
  88. * List of allowed module tags
  89. *
  90. * Currently Dublin Core Metadata and the blogChannel RSS module
  91. * are supported.
  92. *
  93. * @var array
  94. */
  95. var $moduleTags = array('DC:TITLE', 'DC:CREATOR', 'DC:SUBJECT', 'DC:DESCRIPTION',
  96. 'DC:PUBLISHER', 'DC:CONTRIBUTOR', 'DC:DATE', 'DC:TYPE',
  97. 'DC:FORMAT', 'DC:IDENTIFIER', 'DC:SOURCE', 'DC:LANGUAGE',
  98. 'DC:RELATION', 'DC:COVERAGE', 'DC:RIGHTS',
  99. 'BLOGCHANNEL:BLOGROLL', 'BLOGCHANNEL:MYSUBSCRIPTIONS',
  100. 'BLOGCHANNEL:MYSUBSCRIPTIONS', 'BLOGCHANNEL:CHANGES');
  101. // }}}
  102. // {{{ Constructor
  103. /**
  104. * Constructor
  105. *
  106. * @access public
  107. * @param mixed File pointer or name of the RDF file.
  108. * @return void
  109. */
  110. function XML_RSS($handle = '')
  111. {
  112. $this->XML_Parser();
  113. if (@is_resource($handle)) {
  114. $this->setInput($handle);
  115. } elseif ($handle != '') {
  116. $this->setInputFile($handle);
  117. } else {
  118. $this->raiseError('No filename passed.');
  119. }
  120. }
  121. // }}}
  122. // {{{ startHandler()
  123. /**
  124. * Start element handler for XML parser
  125. *
  126. * @access private
  127. * @param object XML parser object
  128. * @param string XML element
  129. * @param array Attributes of XML tag
  130. * @return void
  131. */
  132. function startHandler($parser, $element, $attribs)
  133. {
  134. switch ($element) {
  135. case 'CHANNEL':
  136. case 'ITEM':
  137. case 'IMAGE':
  138. case 'TEXTINPUT':
  139. $this->insideTag = $element;
  140. break;
  141. default:
  142. $this->activeTag = $element;
  143. }
  144. }
  145. // }}}
  146. // {{{ endHandler()
  147. /**
  148. * End element handler for XML parser
  149. *
  150. * If the end of <item>, <channel>, <image> or <textinput>
  151. * is reached, this function updates the structure array
  152. * $this->struct[] and adds the field "type" to this array,
  153. * that defines the type of the current field.
  154. *
  155. * @access private
  156. * @param object XML parser object
  157. * @param string
  158. * @return void
  159. */
  160. function endHandler($parser, $element)
  161. {
  162. if ($element == $this->insideTag) {
  163. $this->insideTag = '';
  164. $this->struct[] = array_merge(array('type' => strtolower($element)),
  165. $this->last);
  166. }
  167. if ($element == 'ITEM') {
  168. $this->items[] = $this->item;
  169. $this->item = '';
  170. }
  171. if ($element == 'IMAGE') {
  172. $this->images[] = $this->image;
  173. $this->image = '';
  174. }
  175. if ($element == 'TEXTINPUT') {
  176. $this->textinputs = $this->textinput;
  177. $this->textinput = '';
  178. }
  179. $this->activeTag = '';
  180. }
  181. // }}}
  182. // {{{ cdataHandler()
  183. /**
  184. * Handler for character data
  185. *
  186. * @access private
  187. * @param object XML parser object
  188. * @param string CDATA
  189. * @return void
  190. */
  191. function cdataHandler($parser, $cdata)
  192. {
  193. if (in_array($this->insideTag, $this->parentTags)) {
  194. $tagName = strtolower($this->insideTag);
  195. $var = $this->{$tagName . 'Tags'};
  196. if (in_array($this->activeTag, $var) ||
  197. in_array($this->activeTag, $this->moduleTags)) {
  198. $this->_add($tagName, strtolower($this->activeTag),
  199. $cdata);
  200. }
  201. }
  202. }
  203. // }}}
  204. // {{{ defaultHandler()
  205. /**
  206. * Default handler for XML parser
  207. *
  208. * @access private
  209. * @param object XML parser object
  210. * @param string CDATA
  211. * @return void
  212. */
  213. function defaultHandler($parser, $cdata)
  214. {
  215. return;
  216. }
  217. // }}}
  218. // {{{ _add()
  219. /**
  220. * Add element to internal result sets
  221. *
  222. * @access private
  223. * @param string Name of the result set
  224. * @param string Fieldname
  225. * @param string Value
  226. * @return void
  227. * @see cdataHandler
  228. */
  229. function _add($type, $field, $value)
  230. {
  231. if (empty($this->{$type}) || empty($this->{$type}[$field])) {
  232. $this->{$type}[$field] = $value;
  233. } else {
  234. $this->{$type}[$field] .= $value;
  235. }
  236. $this->last = $this->{$type};
  237. }
  238. // }}}
  239. // {{{ getStructure()
  240. /**
  241. * Get complete structure of RSS file
  242. *
  243. * @access public
  244. * @return array
  245. */
  246. function getStructure()
  247. {
  248. return (array)$this->struct;
  249. }
  250. // }}}
  251. // {{{ getchannelInfo()
  252. /**
  253. * Get general information about current channel
  254. *
  255. * This function returns an array containing the information
  256. * that has been extracted from the <channel>-tag while parsing
  257. * the RSS file.
  258. *
  259. * @access public
  260. * @return array
  261. */
  262. function getChannelInfo()
  263. {
  264. return (array)$this->channel;
  265. }
  266. // }}}
  267. // {{{ getItems()
  268. /**
  269. * Get items from RSS file
  270. *
  271. * This function returns an array containing the set of items
  272. * that are provided by the RSS file.
  273. *
  274. * @access public
  275. * @return array
  276. */
  277. function getItems()
  278. {
  279. return (array)$this->items;
  280. }
  281. // }}}
  282. // {{{ getImages()
  283. /**
  284. * Get images from RSS file
  285. *
  286. * This function returns an array containing the set of images
  287. * that are provided by the RSS file.
  288. *
  289. * @access public
  290. * @return array
  291. */
  292. function getImages()
  293. {
  294. return (array)$this->images;
  295. }
  296. // }}}
  297. // {{{ getTextinputs()
  298. /**
  299. * Get text input fields from RSS file
  300. *
  301. * @access public
  302. * @return array
  303. */
  304. function getTextinputs()
  305. {
  306. return (array)$this->textinputs;
  307. }
  308. // }}}
  309. }
  310. ?>