Browse Source

Use non-preferred media types as last resort

master
J. King 4 years ago
parent
commit
8bbd5c43b9
  1. 14
      lib/Parser/XML/Construct.php
  2. 2
      tests/cases/XML/feed-atom.yaml

14
lib/Parser/XML/Construct.php

@ -232,7 +232,7 @@ abstract class Construct {
return $out;
}
/** Returns the first Atom link URL which matches the desired relation, with nearest desired media type, or no media type if none match */
/** Returns the first Atom link URL which matches the desired relation, with nearest desired media type */
protected function fetchAtomRelation(string $rel = "", array $mediaTypes = [], \DOMNode $context = null): ?Url {
// tidy ther list of media types; this orders them worst (0)to best (highest index) and then creates a hashtable
$mediaTypes = array_flip(array_reverse(array_values(array_unique(array_map(function(string $t) {
@ -245,16 +245,14 @@ abstract class Construct {
$result = array_reduce($rels, function($best, $cur) use ($mediaTypes) {
$t = trim($cur->getAttribute("type"));
// absence of media type is acceptable if no better match yet exists
if (!strlen($t)) {
if (!$best) {
return [$cur, -1]; // any match will rank higher than -1
}
if (!strlen($t) && (!$best || $best[1] < -1)) {
return [$cur, -1]; // any preferred type will rank higher than -1
}
$t = $this->parseMediaType($t);
if ($t) {
$rank = $mediaTypes[$t] ?? null;
if (!is_null($rank) && (!$best || $rank > $best[1])) {
// if the media type is acceptable and there is currently no candidate or the candidate ranks lower, use the current link
$rank = $mediaTypes[$t] ?? -2; // even no type will rank higher than a non-preferred type
if (!$best || $rank > $best[1]) {
// if there is currently no candidate or the candidate ranks lower, use the current link
return [$cur, $rank];
}
}

2
tests/cases/XML/feed-atom.yaml

@ -129,7 +129,7 @@ Feed link 6: # HTML is even more preferred
version: '1.0'
link: 'http://example.com/'
Feed link 7: # No type is better than an unacceptable type
Feed link 7: # No type is better than a non-preferred type
input: >
<feed xmlns="http://www.w3.org/2005/Atom">
<link href="http://example.net/" type="image/png"/>

Loading…
Cancel
Save