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.

98 lines
3.1 KiB

  1. <?php
  2. namespace p3k\XRay\Formats;
  3. trait Mf2Feed {
  4. private static function parseAsHFeed($mf2, $http, $url) {
  5. $data = [
  6. 'type' => 'feed',
  7. 'items' => [],
  8. ];
  9. // Given an mf2 data structure from a web page, assume it is a feed of entries
  10. // and return the XRay data structure for the feed.
  11. // Look for the first (BFS) h-feed if present, otherwise use the list of items.
  12. // Normalize this into a simpler mf2 structure, (h-feed -> h-* children)
  13. $feed = self::_findFirstOfType($mf2, 'h-feed');
  14. if(!$feed) {
  15. // There was no h-feed.
  16. // Check for a top-level h-card with children
  17. if(isset($mf2['items'][0]) && in_array('h-card', $mf2['items'][0]['type'])) {
  18. $feed = $mf2['items'][0];
  19. // If the h-card has children, use them, otherwise look for siblings
  20. if(!isset($feed['children'])) {
  21. $items = self::_findAllObjectsExcept($mf2, ['h-card']);
  22. $feed['children'] = $items;
  23. }
  24. } else {
  25. $children = self::_findAllObjectsExcept($mf2, ['h-card','h-feed']);
  26. $feed = [
  27. 'type' => ['h-feed'],
  28. 'properties' => [],
  29. 'children' => $children
  30. ];
  31. }
  32. }
  33. if(!isset($feed['children']))
  34. $feed['children'] = [];
  35. // Now that the feed has been normalized so all the items are under "children", we
  36. // can transform each entry into the XRay format, including finding the author, etc
  37. foreach($feed['children'] as $item) {
  38. $parsed = false;
  39. if(in_array('h-entry', $item['type']) || in_array('h-cite', $item['type'])) {
  40. $parsed = self::parseAsHEntry($mf2, $item, false, $url);
  41. }
  42. elseif(in_array('h-event', $item['type'])) {
  43. $parsed = self::parseAsHEvent($mf2, $item, false, $url);
  44. }
  45. elseif(in_array('h-review', $item['type'])) {
  46. $parsed = self::parseAsHReview($mf2, $item, false, $url);
  47. }
  48. elseif(in_array('h-recipe', $item['type'])) {
  49. $parsed = self::parseAsHRecipe($mf2, $item, false, $url);
  50. }
  51. elseif(in_array('h-product', $item['type'])) {
  52. $parsed = self::parseAsHProduct($mf2, $item, false, $url);
  53. }
  54. elseif(in_array('h-item', $item['type'])) {
  55. $parsed = self::parseAsHItem($mf2, $item, false, $url);
  56. }
  57. elseif(in_array('h-card', $item['type'])) {
  58. $parsed = self::parseAsHCard($item, false, $url);
  59. }
  60. if($parsed) {
  61. $data['items'][] = $parsed['data'];
  62. }
  63. }
  64. return [
  65. 'data' => $data,
  66. 'source-format' => 'mf2+html',
  67. ];
  68. }
  69. private static function _findFirstOfType($mf2, $type) {
  70. foreach($mf2['items'] as $item) {
  71. if(in_array($type, $item['type'])) {
  72. return $item;
  73. } else {
  74. if(isset($item['children'])) {
  75. $items = $item['children'];
  76. return self::_findFirstOfType(['items'=>$items], $type);
  77. }
  78. }
  79. }
  80. }
  81. private static function _findAllObjectsExcept($mf2, $types) {
  82. $items = [];
  83. foreach($mf2['items'] as $item) {
  84. if(count(array_intersect($item['type'], $types)) == 0) {
  85. $items[] = $item;
  86. }
  87. }
  88. return $items;
  89. }
  90. }