PageRenderTime 53ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/tools/pear_xml_parser/Parser.php

https://gitlab.com/staging06/myproject
PHP | 351 lines | 177 code | 24 blank | 150 comment | 44 complexity | 13c5c5221ca2db0a181c6c039bed3ca1 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Key gateway class for XML_Feed_Parser package
  5. *
  6. * PHP versions 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category XML
  15. * @package XML_Feed_Parser
  16. * @author James Stewart <james@jystewart.net>
  17. * @copyright 2005 James Stewart <james@jystewart.net>
  18. * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
  19. * @version CVS: $Id: Parser.php 6844 2011-06-03 14:46:51Z dMetzger $
  20. * @link http://pear.php.net/package/XML_Feed_Parser/
  21. */
  22. /**
  23. * XML_Feed_Parser_Type is an abstract class required by all of our
  24. * feed types. It makes sense to load it here to keep the other files
  25. * clean.
  26. */
  27. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/Type.php');
  28. /**
  29. * We will throw exceptions when errors occur.
  30. */
  31. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/Exception.php');
  32. /**
  33. * This is the core of the XML_Feed_Parser package. It identifies feed types
  34. * and abstracts access to them. It is an iterator, allowing for easy access
  35. * to the entire feed.
  36. *
  37. * @author James Stewart <james@jystewart.net>
  38. * @version Release: @package_version@
  39. * @package XML_Feed_Parser
  40. */
  41. class XML_Feed_Parser implements Iterator
  42. {
  43. /**
  44. * This is where we hold the feed object
  45. * @var Object
  46. */
  47. private $feed;
  48. /**
  49. * To allow for extensions, we make a public reference to the feed model
  50. * @var DOMDocument
  51. */
  52. public $model;
  53. /**
  54. * A map between entry ID and offset
  55. * @var array
  56. */
  57. protected $idMappings = array();
  58. /**
  59. * A storage space for Namespace URIs.
  60. * @var array
  61. */
  62. private $feedNamespaces = array(
  63. 'rss2' => array(
  64. 'http://backend.userland.com/rss',
  65. 'http://backend.userland.com/rss2',
  66. 'http://blogs.law.harvard.edu/tech/rss'));
  67. /**
  68. * Detects feed types and instantiate appropriate objects.
  69. *
  70. * Our constructor takes care of detecting feed types and instantiating
  71. * appropriate classes. For now we're going to treat Atom 0.3 as Atom 1.0
  72. * but raise a warning. I do not intend to introduce full support for
  73. * Atom 0.3 as it has been deprecated, but others are welcome to.
  74. *
  75. * @param string $feed XML serialization of the feed
  76. * @param bool $strict Whether or not to validate the feed
  77. * @param bool $suppressWarnings Trigger errors for deprecated feed types?
  78. * @param bool $tidy Whether or not to try and use the tidy library on input
  79. */
  80. function __construct($feed, $strict = false, $suppressWarnings = false, $tidy = false)
  81. {
  82. $this->model = new DOMDocument;
  83. if (! @$this->model->loadXML($feed)) { /* PrestaShop - no error display */
  84. if (extension_loaded('tidy') && $tidy) {
  85. $tidy = new tidy;
  86. $tidy->parseString($feed,
  87. array('input-xml' => true, 'output-xml' => true));
  88. $tidy->cleanRepair();
  89. if (! @$this->model->loadXML((string) $tidy)) { /* PrestaShop - no error display */
  90. throw new XML_Feed_Parser_Exception('Invalid input: this is not ' .
  91. 'valid XML');
  92. }
  93. } else {
  94. throw new XML_Feed_Parser_Exception('Invalid input: this is not valid XML');
  95. }
  96. }
  97. /* detect feed type */
  98. $doc_element = $this->model->documentElement;
  99. $error = false;
  100. switch (true) {
  101. case ($doc_element->namespaceURI == 'http://www.w3.org/2005/Atom'):
  102. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/Atom.php');
  103. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/AtomElement.php');
  104. $class = 'XML_Feed_Parser_Atom';
  105. break;
  106. case ($doc_element->namespaceURI == 'http://purl.org/atom/ns#'):
  107. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/Atom.php');
  108. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/AtomElement.php');
  109. $class = 'XML_Feed_Parser_Atom';
  110. $error = 'Atom 0.3 deprecated, using 1.0 parser which won\'t provide ' .
  111. 'all options';
  112. break;
  113. case ($doc_element->namespaceURI == 'http://purl.org/rss/1.0/' ||
  114. ($doc_element->hasChildNodes() && $doc_element->childNodes->length > 1
  115. && $doc_element->childNodes->item(1)->namespaceURI ==
  116. 'http://purl.org/rss/1.0/')):
  117. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS1.php');
  118. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS1Element.php');
  119. $class = 'XML_Feed_Parser_RSS1';
  120. break;
  121. case ($doc_element->namespaceURI == 'http://purl.org/rss/1.1/' ||
  122. ($doc_element->hasChildNodes() && $doc_element->childNodes->length > 1
  123. && $doc_element->childNodes->item(1)->namespaceURI ==
  124. 'http://purl.org/rss/1.1/')):
  125. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS11.php');
  126. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS11Element.php');
  127. $class = 'XML_Feed_Parser_RSS11';
  128. break;
  129. case (($doc_element->hasChildNodes() && $doc_element->childNodes->length > 1
  130. && $doc_element->childNodes->item(1)->namespaceURI ==
  131. 'http://my.netscape.com/rdf/simple/0.9/') ||
  132. $doc_element->namespaceURI == 'http://my.netscape.com/rdf/simple/0.9/'):
  133. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS09.php');
  134. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS09Element.php');
  135. $class = 'XML_Feed_Parser_RSS09';
  136. break;
  137. case ($doc_element->tagName == 'rss' and
  138. $doc_element->hasAttribute('version') &&
  139. $doc_element->getAttribute('version') == 0.91):
  140. $error = 'RSS 0.91 has been superceded by RSS2.0. Using RSS2.0 parser.';
  141. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS2.php');
  142. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS2Element.php');
  143. $class = 'XML_Feed_Parser_RSS2';
  144. break;
  145. case ($doc_element->tagName == 'rss' and
  146. $doc_element->hasAttribute('version') &&
  147. $doc_element->getAttribute('version') == 0.92):
  148. $error = 'RSS 0.92 has been superceded by RSS2.0. Using RSS2.0 parser.';
  149. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS2.php');
  150. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS2Element.php');
  151. $class = 'XML_Feed_Parser_RSS2';
  152. break;
  153. case (in_array($doc_element->namespaceURI, $this->feedNamespaces['rss2'])
  154. || $doc_element->tagName == 'rss'):
  155. if (! $doc_element->hasAttribute('version') ||
  156. $doc_element->getAttribute('version') != 2) {
  157. $error = 'RSS version not specified. Parsing as RSS2.0';
  158. }
  159. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS2.php');
  160. require_once(_PS_PEAR_XML_PARSER_PATH_.'Parser/RSS2Element.php');
  161. $class = 'XML_Feed_Parser_RSS2';
  162. break;
  163. default:
  164. throw new XML_Feed_Parser_Exception('Feed type unknown');
  165. break;
  166. }
  167. if (! $suppressWarnings && ! empty($error)) {
  168. trigger_error($error, E_USER_WARNING);
  169. }
  170. /* Instantiate feed object */
  171. $this->feed = new $class($this->model, $strict);
  172. }
  173. /**
  174. * Proxy to allow feed element names to be used as method names
  175. *
  176. * For top-level feed elements we will provide access using methods or
  177. * attributes. This function simply passes on a request to the appropriate
  178. * feed type object.
  179. *
  180. * @param string $call - the method being called
  181. * @param array $attributes
  182. */
  183. function __call($call, $attributes)
  184. {
  185. $attributes = array_pad($attributes, 5, false);
  186. list($a, $b, $c, $d, $e) = $attributes;
  187. return $this->feed->$call($a, $b, $c, $d, $e);
  188. }
  189. /**
  190. * Proxy to allow feed element names to be used as attribute names
  191. *
  192. * To allow variable-like access to feed-level data we use this
  193. * method. It simply passes along to __call() which in turn passes
  194. * along to the relevant object.
  195. *
  196. * @param string $val - the name of the variable required
  197. */
  198. function __get($val)
  199. {
  200. return $this->feed->$val;
  201. }
  202. /**
  203. * Provides iteration functionality.
  204. *
  205. * Of course we must be able to iterate... This function simply increases
  206. * our internal counter.
  207. */
  208. function next()
  209. {
  210. if (isset($this->current_item) &&
  211. $this->current_item <= $this->feed->numberEntries - 1) {
  212. ++$this->current_item;
  213. } elseif (! isset($this->current_item)) {
  214. $this->current_item = 0;
  215. } else {
  216. return false;
  217. }
  218. }
  219. /**
  220. * Return XML_Feed_Type object for current element
  221. *
  222. * @return XML_Feed_Parser_Type Object
  223. */
  224. function current()
  225. {
  226. return $this->getEntryByOffset($this->current_item);
  227. }
  228. /**
  229. * For iteration -- returns the key for the current stage in the array.
  230. *
  231. * @return int
  232. */
  233. function key()
  234. {
  235. return $this->current_item;
  236. }
  237. /**
  238. * For iteration -- tells whether we have reached the
  239. * end.
  240. *
  241. * @return bool
  242. */
  243. function valid()
  244. {
  245. return $this->current_item < $this->feed->numberEntries;
  246. }
  247. /**
  248. * For iteration -- resets the internal counter to the beginning.
  249. */
  250. function rewind()
  251. {
  252. $this->current_item = 0;
  253. }
  254. /**
  255. * Provides access to entries by ID if one is specified in the source feed.
  256. *
  257. * As well as allowing the items to be iterated over we want to allow
  258. * users to be able to access a specific entry. This is one of two ways of
  259. * doing that, the other being by offset. This method can be quite slow
  260. * if dealing with a large feed that hasn't yet been processed as it
  261. * instantiates objects for every entry until it finds the one needed.
  262. *
  263. * @param string $id Valid ID for the given feed format
  264. * @return XML_Feed_Parser_Type|false
  265. */
  266. function getEntryById($id)
  267. {
  268. if (isset($this->idMappings[$id])) {
  269. return $this->getEntryByOffset($this->idMappings[$id]);
  270. }
  271. /*
  272. * Since we have not yet encountered that ID, let's go through all the
  273. * remaining entries in order till we find it.
  274. * This is a fairly slow implementation, but it should work.
  275. */
  276. return $this->feed->getEntryById($id);
  277. }
  278. /**
  279. * Retrieve entry by numeric offset, starting from zero.
  280. *
  281. * As well as allowing the items to be iterated over we want to allow
  282. * users to be able to access a specific entry. This is one of two ways of
  283. * doing that, the other being by ID.
  284. *
  285. * @param int $offset The position of the entry within the feed, starting from 0
  286. * @return XML_Feed_Parser_Type|false
  287. */
  288. function getEntryByOffset($offset)
  289. {
  290. if ($offset < $this->feed->numberEntries) {
  291. if (isset($this->feed->entries[$offset])) {
  292. return $this->feed->entries[$offset];
  293. } else {
  294. try {
  295. $this->feed->getEntryByOffset($offset);
  296. } catch (Exception $e) {
  297. return false;
  298. }
  299. $id = $this->feed->entries[$offset]->getID();
  300. $this->idMappings[$id] = $offset;
  301. return $this->feed->entries[$offset];
  302. }
  303. } else {
  304. return false;
  305. }
  306. }
  307. /**
  308. * Retrieve version details from feed type class.
  309. *
  310. * @return void
  311. * @author James Stewart
  312. */
  313. function version()
  314. {
  315. return $this->feed->version;
  316. }
  317. /**
  318. * Returns a string representation of the feed.
  319. *
  320. * @return String
  321. **/
  322. function __toString()
  323. {
  324. return $this->feed->__toString();
  325. }
  326. }
  327. ?>