You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.0 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. use HTMLPurifier, HTMLPurifier_Config;
  4. use DOMDocument, DOMXPath;
  5. use p3k\XRay\Formats;
  6. use PicoFeed\Reader\Reader;
  7. use PicoFeed\PicoFeedException;
  8. class XML extends Format {
  9. public static function matches_host($url) { return true; }
  10. public static function matches($url) { return true; }
  11. public static function parse($xml, $url) {
  12. $result = [
  13. 'data' => [
  14. 'type' => 'unknown',
  15. ],
  16. 'url' => $url,
  17. 'source-format' => 'xml',
  18. ];
  19. try {
  20. $reader = new Reader();
  21. $parser = $reader->getParser($url, $xml, '');
  22. $feed = $parser->execute();
  23. $result['data']['type'] = 'feed';
  24. $result['data']['items'] = [];
  25. foreach($feed->getItems() as $item) {
  26. $result['data']['items'][] = self::_hEntryFromFeedItem($item, $feed);
  27. }
  28. } catch(PicoFeedException $e) {
  29. }
  30. return $result;
  31. }
  32. private static function _hEntryFromFeedItem($item, $feed) {
  33. $entry = [
  34. 'type' => 'entry',
  35. 'author' => [
  36. 'name' => null,
  37. 'url' => null,
  38. 'photo' => null
  39. ]
  40. ];
  41. if(is_array($guid=$item->getTag('guid')) && count($guid))
  42. $entry['uid'] = $guid[0];
  43. elseif(is_array($guid=$item->getTag('id')) && count($guid))
  44. $entry['uid'] = $guid[0];
  45. if($item->getUrl())
  46. $entry['url'] = $item->getUrl();
  47. if($item->getPublishedDate())
  48. $entry['published'] = $item->getPublishedDate()->format('c');
  49. if($item->getContent())
  50. $entry['content'] = [
  51. 'html' => self::sanitizeHTML($item->getContent()),
  52. 'text' => self::stripHTML($item->getContent())
  53. ];
  54. if($item->getTitle() && $item->getTitle() != $item->getUrl()) {
  55. $title = $item->getTitle();
  56. $entry['name'] = $title;
  57. // Check if the title is a prefix of the content and drop if so
  58. if(isset($entry['content'])) {
  59. if(substr($title, -3) == '...' || substr($title, -1) == '…') {
  60. if(substr($title, -3) == '...') {
  61. $trimmedTitle = substr($title, 0, -3);
  62. } else {
  63. $trimmedTitle = substr($title, 0, -1);
  64. }
  65. if(substr($entry['content']['text'], 0, strlen($trimmedTitle)) == $trimmedTitle) {
  66. unset($entry['name']);
  67. }
  68. }
  69. }
  70. }
  71. if($item->getAuthor()) {
  72. $entry['author']['name'] = $item->getAuthor();
  73. }
  74. if($item->getAuthorUrl()) {
  75. $entry['author']['url'] = $item->getAuthorUrl();
  76. } else if($feed->siteUrl) {
  77. $entry['author']['url'] = $feed->siteUrl;
  78. }
  79. if($item->getEnclosureType()) {
  80. $prop = false;
  81. switch($item->getEnclosureType()) {
  82. case 'audio/mpeg':
  83. $prop = 'audio'; break;
  84. case 'image/jpeg':
  85. case 'image/png':
  86. case 'image/gif':
  87. $prop = 'photo'; break;
  88. }
  89. if($prop)
  90. $entry[$prop] = [$item->getEnclosureUrl()];
  91. }
  92. $entry['post-type'] = \p3k\XRay\PostType::discover($entry);
  93. return $entry;
  94. }
  95. }