Rss10.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace PicoFeed\Parser;
  3. use SimpleXMLElement;
  4. use PicoFeed\Filter\Filter;
  5. /**
  6. * RSS 1.0 parser.
  7. *
  8. * @package PicoFeed\Parser
  9. * @author Frederic Guillot
  10. */
  11. class Rss10 extends Parser
  12. {
  13. /**
  14. * Supported namespaces.
  15. */
  16. protected $namespaces = array(
  17. 'rss' => 'http://purl.org/rss/1.0/',
  18. 'dc' => 'http://purl.org/dc/elements/1.1/',
  19. 'content' => 'http://purl.org/rss/1.0/modules/content/',
  20. 'feedburner' => 'http://rssnamespace.org/feedburner/ext/1.0',
  21. );
  22. /**
  23. * Get the path to the items XML tree.
  24. *
  25. * @param SimpleXMLElement $xml Feed xml
  26. * @return SimpleXMLElement[]
  27. */
  28. public function getItemsTree(SimpleXMLElement $xml)
  29. {
  30. return XmlParser::getXPathResult($xml, 'rss:item', $this->namespaces)
  31. ?: XmlParser::getXPathResult($xml, 'item')
  32. ?: $xml->item;
  33. }
  34. /**
  35. * Find the feed url.
  36. *
  37. * @param SimpleXMLElement $xml Feed xml
  38. * @param \PicoFeed\Parser\Feed $feed Feed object
  39. */
  40. public function findFeedUrl(SimpleXMLElement $xml, Feed $feed)
  41. {
  42. $feed->setFeedUrl('');
  43. }
  44. /**
  45. * Find the site url.
  46. *
  47. * @param SimpleXMLElement $xml Feed xml
  48. * @param \PicoFeed\Parser\Feed $feed Feed object
  49. */
  50. public function findSiteUrl(SimpleXMLElement $xml, Feed $feed)
  51. {
  52. $value = XmlParser::getXPathResult($xml, 'rss:channel/rss:link', $this->namespaces)
  53. ?: XmlParser::getXPathResult($xml, 'channel/link')
  54. ?: $xml->channel->link;
  55. $feed->setSiteUrl(XmlParser::getValue($value));
  56. }
  57. /**
  58. * Find the feed description.
  59. *
  60. * @param SimpleXMLElement $xml Feed xml
  61. * @param \PicoFeed\Parser\Feed $feed Feed object
  62. */
  63. public function findFeedDescription(SimpleXMLElement $xml, Feed $feed)
  64. {
  65. $description = XmlParser::getXPathResult($xml, 'rss:channel/rss:description', $this->namespaces)
  66. ?: XmlParser::getXPathResult($xml, 'channel/description')
  67. ?: $xml->channel->description;
  68. $feed->setDescription(XmlParser::getValue($description));
  69. }
  70. /**
  71. * Find the feed logo url.
  72. *
  73. * @param SimpleXMLElement $xml Feed xml
  74. * @param \PicoFeed\Parser\Feed $feed Feed object
  75. */
  76. public function findFeedLogo(SimpleXMLElement $xml, Feed $feed)
  77. {
  78. $logo = XmlParser::getXPathResult($xml, 'rss:image/rss:url', $this->namespaces)
  79. ?: XmlParser::getXPathResult($xml, 'image/url');
  80. $feed->setLogo(XmlParser::getValue($logo));
  81. }
  82. /**
  83. * Find the feed icon.
  84. *
  85. * @param SimpleXMLElement $xml Feed xml
  86. * @param \PicoFeed\Parser\Feed $feed Feed object
  87. */
  88. public function findFeedIcon(SimpleXMLElement $xml, Feed $feed)
  89. {
  90. $feed->setIcon('');
  91. }
  92. /**
  93. * Find the feed title.
  94. *
  95. * @param SimpleXMLElement $xml Feed xml
  96. * @param \PicoFeed\Parser\Feed $feed Feed object
  97. */
  98. public function findFeedTitle(SimpleXMLElement $xml, Feed $feed)
  99. {
  100. $title = XmlParser::getXPathResult($xml, 'rss:channel/rss:title', $this->namespaces)
  101. ?: XmlParser::getXPathResult($xml, 'channel/title')
  102. ?: $xml->channel->title;
  103. $feed->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($title)) ?: $feed->getSiteUrl());
  104. }
  105. /**
  106. * Find the feed language.
  107. *
  108. * @param SimpleXMLElement $xml Feed xml
  109. * @param \PicoFeed\Parser\Feed $feed Feed object
  110. */
  111. public function findFeedLanguage(SimpleXMLElement $xml, Feed $feed)
  112. {
  113. $language = XmlParser::getXPathResult($xml, 'rss:channel/dc:language', $this->namespaces)
  114. ?: XmlParser::getXPathResult($xml, 'channel/dc:language', $this->namespaces);
  115. $feed->setLanguage(XmlParser::getValue($language));
  116. }
  117. /**
  118. * Find the feed id.
  119. *
  120. * @param SimpleXMLElement $xml Feed xml
  121. * @param \PicoFeed\Parser\Feed $feed Feed object
  122. */
  123. public function findFeedId(SimpleXMLElement $xml, Feed $feed)
  124. {
  125. $feed->setId($feed->getFeedUrl() ?: $feed->getSiteUrl());
  126. }
  127. /**
  128. * Find the feed date.
  129. *
  130. * @param SimpleXMLElement $xml Feed xml
  131. * @param \PicoFeed\Parser\Feed $feed Feed object
  132. */
  133. public function findFeedDate(SimpleXMLElement $xml, Feed $feed)
  134. {
  135. $date = XmlParser::getXPathResult($xml, 'rss:channel/dc:date', $this->namespaces)
  136. ?: XmlParser::getXPathResult($xml, 'channel/dc:date', $this->namespaces);
  137. $feed->setDate($this->getDateParser()->getDateTime(XmlParser::getValue($date)));
  138. }
  139. /**
  140. * Find the item published date.
  141. *
  142. * @param SimpleXMLElement $entry Feed item
  143. * @param Item $item Item object
  144. * @param \PicoFeed\Parser\Feed $feed Feed object
  145. */
  146. public function findItemPublishedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
  147. {
  148. $date = XmlParser::getXPathResult($entry, 'dc:date', $this->namespaces);
  149. $item->setPublishedDate(!empty($date) ? $this->getDateParser()->getDateTime(XmlParser::getValue($date)) : null);
  150. }
  151. /**
  152. * Find the item updated date.
  153. *
  154. * @param SimpleXMLElement $entry Feed item
  155. * @param Item $item Item object
  156. * @param \PicoFeed\Parser\Feed $feed Feed object
  157. */
  158. public function findItemUpdatedDate(SimpleXMLElement $entry, Item $item, Feed $feed)
  159. {
  160. if ($item->publishedDate === null) {
  161. $this->findItemPublishedDate($entry, $item, $feed);
  162. }
  163. $item->setUpdatedDate($item->getPublishedDate()); // No updated date in RSS 1.0 specifications
  164. }
  165. /**
  166. * Find the item title.
  167. *
  168. * @param SimpleXMLElement $entry Feed item
  169. * @param \PicoFeed\Parser\Item $item Item object
  170. */
  171. public function findItemTitle(SimpleXMLElement $entry, Item $item)
  172. {
  173. $title = XmlParser::getXPathResult($entry, 'rss:title', $this->namespaces)
  174. ?: XmlParser::getXPathResult($entry, 'title')
  175. ?: $entry->title;
  176. $item->setTitle(Filter::stripWhiteSpace(XmlParser::getValue($title)) ?: $item->getUrl());
  177. }
  178. /**
  179. * Find the item author.
  180. *
  181. * @param SimpleXMLElement $xml Feed
  182. * @param SimpleXMLElement $entry Feed item
  183. * @param \PicoFeed\Parser\Item $item Item object
  184. */
  185. public function findItemAuthor(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item)
  186. {
  187. $author = XmlParser::getXPathResult($entry, 'dc:creator', $this->namespaces)
  188. ?: XmlParser::getXPathResult($xml, 'rss:channel/dc:creator', $this->namespaces)
  189. ?: XmlParser::getXPathResult($xml, 'channel/dc:creator', $this->namespaces);
  190. $item->setAuthor(XmlParser::getValue($author));
  191. }
  192. /**
  193. * Find the item author URL.
  194. *
  195. * @param SimpleXMLElement $xml Feed
  196. * @param SimpleXMLElement $entry Feed item
  197. * @param \PicoFeed\Parser\Item $item Item object
  198. */
  199. public function findItemAuthorUrl(SimpleXMLElement $xml, SimpleXMLElement $entry, Item $item)
  200. {
  201. // There appears to be no support for author URL in the dc: terms
  202. $item->setAuthorUrl('');
  203. }
  204. /**
  205. * Find the item content.
  206. *
  207. * @param SimpleXMLElement $entry Feed item
  208. * @param \PicoFeed\Parser\Item $item Item object
  209. */
  210. public function findItemContent(SimpleXMLElement $entry, Item $item)
  211. {
  212. $content = XmlParser::getXPathResult($entry, 'content:encoded', $this->namespaces);
  213. if (XmlParser::getValue($content) === '') {
  214. $content = XmlParser::getXPathResult($entry, 'rss:description', $this->namespaces)
  215. ?: XmlParser::getXPathResult($entry, 'description')
  216. ?: $entry->description;
  217. }
  218. $item->setContent(XmlParser::getValue($content));
  219. }
  220. /**
  221. * Find the item URL.
  222. *
  223. * @param SimpleXMLElement $entry Feed item
  224. * @param \PicoFeed\Parser\Item $item Item object
  225. */
  226. public function findItemUrl(SimpleXMLElement $entry, Item $item)
  227. {
  228. $link = XmlParser::getXPathResult($entry, 'feedburner:origLink', $this->namespaces)
  229. ?: XmlParser::getXPathResult($entry, 'rss:link', $this->namespaces)
  230. ?: XmlParser::getXPathResult($entry, 'link')
  231. ?: $entry->link;
  232. $item->setUrl(XmlParser::getValue($link));
  233. }
  234. /**
  235. * Genereate the item id.
  236. *
  237. * @param SimpleXMLElement $entry Feed item
  238. * @param \PicoFeed\Parser\Item $item Item object
  239. * @param \PicoFeed\Parser\Feed $feed Feed object
  240. */
  241. public function findItemId(SimpleXMLElement $entry, Item $item, Feed $feed)
  242. {
  243. $item->setId($this->generateId(
  244. $item->getTitle(), $item->getUrl(), $item->getContent()
  245. ));
  246. }
  247. /**
  248. * Find the item enclosure.
  249. *
  250. * @param SimpleXMLElement $entry Feed item
  251. * @param \PicoFeed\Parser\Item $item Item object
  252. * @param \PicoFeed\Parser\Feed $feed Feed object
  253. */
  254. public function findItemEnclosure(SimpleXMLElement $entry, Item $item, Feed $feed)
  255. {
  256. }
  257. /**
  258. * Find the item language.
  259. *
  260. * @param SimpleXMLElement $entry Feed item
  261. * @param \PicoFeed\Parser\Item $item Item object
  262. * @param \PicoFeed\Parser\Feed $feed Feed object
  263. */
  264. public function findItemLanguage(SimpleXMLElement $entry, Item $item, Feed $feed)
  265. {
  266. $language = XmlParser::getXPathResult($entry, 'dc:language', $this->namespaces);
  267. $item->setLanguage(XmlParser::getValue($language) ?: $feed->getLanguage());
  268. }
  269. /**
  270. * Find the item categories.
  271. *
  272. * @param SimpleXMLElement $entry Feed item
  273. * @param Item $item Item object
  274. * @param Feed $feed Feed object
  275. */
  276. public function findItemCategories(SimpleXMLElement $entry, Item $item, Feed $feed)
  277. {
  278. $categories = XmlParser::getXPathResult($entry, 'dc:subject', $this->namespaces);
  279. $item->setCategoriesFromXml($categories);
  280. }
  281. }