Browse Source

Add canonical feed URLs

master
J. King 6 years ago
parent
commit
0cec5a3132
  1. 8
      lib/Feed.php
  2. 17
      lib/JSON/Feed.php
  3. 5
      lib/XML/Construct.php
  4. 9
      lib/XML/Feed.php
  5. 6
      lib/XML/Primitives/Construct.php
  6. 12
      lib/XML/Primitives/Entry.php
  7. 26
      lib/XML/Primitives/Feed.php

8
lib/Feed.php

@ -9,7 +9,9 @@ namespace JKingWeb\Lax;
use JKingWeb\Lax\Person\Person;
use JKingWeb\Lax\Person\Collection as PersonCollection;
abstract class Feed {
abstract class Feed {
protected $reqUrl;
public $type;
public $version;
public $url;
@ -26,6 +28,7 @@ abstract class Feed {
/** Parses the feed to extract sundry metadata */
protected function parse() {
$this->id = $this->getId();
$this->url = $this->getUrl();
$this->link = $this->getLink();
$this->title = $this->getTitle();
$this->summary = $this->getSummary();
@ -36,6 +39,9 @@ abstract class Feed {
$this->title = strlen($this->title) ? $this->title : $this->link;
}
/** General function to fetch the canonical feed URL */
abstract public function getUrl(): string;
/** General function to fetch the feed title */
abstract public function getTitle(): string;

17
lib/JSON/Feed.php

@ -12,23 +12,29 @@ use JKingWeb\Lax\Person\Collection as PersonCollection;
class Feed extends \JKingWeb\Lax\Feed {
use Construct;
protected $reqUrl;
/** Constructs a parsed feed */
public function __construct(string $data, string $contentType = "", string $url = "") {
$this->reqUrl = $url;
$this->init($data, $contentType);
$this->parse();
}
/** Performs initialization of the instance */
protected function init(string $data, string $contentType = "") {
protected function init(string $data, string $contentType = "", string $url = "") {
$this->reqUrl = $url;
$this->json = json_decode($data);
$this->url = $this->reqUrl;
$this->type = "json";
$this->version = $this->fetchMember("version", "str") ?? "";
}
/** General function to fetch the canonical feed URL
*
* If the feed does not include a canonical URL, the request URL is returned instead
*/
public function getUrl(): string {
return $this->fetchUrl("feed_url") ?? $this->reqUrl;
}
/** General function to fetch the feed title */
public function getTitle(): string {
return $this->fetchMember("title", "str") ?? "";
@ -57,8 +63,7 @@ class Feed extends \JKingWeb\Lax\Feed {
* For JSON feeds this is always the feed URL specified in the feed
*/
public function getId(): string {
//return $this->getUrl();
return "";
return $this->fetchUrl("feed_url") ?? "";
}
/** General function to fetch a collection of people associated with a feed */

5
lib/XML/Construct.php

@ -21,7 +21,10 @@ trait Construct {
/** Retrieves an element node based on an XPath query */
protected function fetchElement(string $query, \DOMNode $context = null) {
$context = $context ?? $this->subject;
$node = $this->xpath->query("(".$query.")[1]", $context ?? $this->subject);
$node = @$this->xpath->query("(".$query.")[1]", $context ?? $this->subject);
if ($node===false) {
throw new \Exception("Invalid XPath query: $query");
}
return ($node->length) ? $node->item(0) : null;
}

9
lib/XML/Feed.php

@ -22,6 +22,7 @@ class Feed extends \JKingWeb\Lax\Feed {
/** Performs initialization of the instance */
protected function init(string $data, string $contentType = "", string $url = "") {
$this->reqUrl = $url;
$this->document = new \DOMDocument();
$this->document->loadXML($data, \LIBXML_BIGLINES | \LIBXML_COMPACT);
$this->document->documentURI = $url;
@ -54,6 +55,14 @@ class Feed extends \JKingWeb\Lax\Feed {
$this->url = $url;
}
/** General function to fetch the canonical feed URL
*
* If the feed does not include a canonical URL, the request URL is returned instead
*/
public function getUrl(): string {
return $this->getUrlAtom() ?? $this->getUrlRss1() ?? $this->getUrlPod() ?? $this->reqUrl;
}
/** General function to fetch the feed title */
public function getTitle(): string {
return $this->getTitleAtom() ?? $this->getTitleRss1() ?? $this->getTitleRss2() ?? $this->getTitleDC() ?? $this->getTitlePod() ?? "";

6
lib/XML/Primitives/Construct.php

@ -183,4 +183,10 @@ trait Construct {
$out[] = $p;
return $out;
}
/** Primitive to fetch an Atom feed or entry's canonical URL */
protected function getUrlAtom() {
$node = $this->fetchAtomRelations("self");
return $node->length ? $this->resolveNodeUrl($node->item(0), "href") : null;
}
}

12
lib/XML/Primitives/Entry.php

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace JKingWeb\Lax\XML\Primitives;
use JKingWeb\Lax\Person\Collection as PersonCollection;
use JKingWeb\Lax\XML\XPath;
trait Entry {
@ -55,4 +56,15 @@ trait Entry {
}
return $out;
}
/** Primitive to fetch an RDF entry's canonical URL */
protected function getUrlRss1() {
// XPath doesn't seem to like the query we'd need for this, so it must be done the hard way.
$node = $this->subject;
if ($node->localName=="item" && ($node->namespaceURI==XPath::NS['rss1'] || $node->namespaceURI==XPath::NS['rss0']) && $node->hasAttributeNS(XPath::NS['rdf'], "about")) {
return $this->resolveNodeUrl($node, "about", XPath::NS['rdf']);
} else {
return null;
}
}
}

26
lib/XML/Primitives/Feed.php

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace JKingWeb\Lax\XML\Primitives;
use JKingWeb\Lax\Person\Collection as PersonCollection;
use JKingWeb\Lax\XML\XPath;
trait Feed {
@ -76,4 +77,29 @@ trait Feed {
}
return $out;
}
/** Primitive to fetch an RDF feed's canonical URL */
protected function getUrlRss1() {
// XPath doesn't seem to like the query we'd need for this, so it must be done the hard way.
$node = $this->subject;
if ($node->hasAttributeNS(XPath::NS['rdf'], "about")) {
if (
($node->localName=="channel" && ($node->namespaceURI==XPath::NS['rss1'] || $node->namespaceURI==XPath::NS['rss0'])) ||
($node==$node->ownerDocument->documentElement && $node->localName=="RDF" && $node->namespaceURI==XPath::NS['rdf'])
) {
return $this->resolveNodeUrl($node, "about", XPath::NS['rdf']);
}
}
return null;
}
/** Primitive to fetch an podcast's canonical URL */
protected function getUrlPod() {
$node = $this->fetchElement("./apple:new-feed-url");
if ($node) {
return $this->resolveNodeUrl($node);
} else {
return null;
}
}
}

Loading…
Cancel
Save