Browse Source

Fetch Atom people from entries, for later

master
J. King 6 years ago
parent
commit
c97ea75601
  1. 19
      lib/XMLCommon.php
  2. 28
      lib/XMLEntryPrimitives.php
  3. 16
      lib/XMLFeedPrimitives.php

19
lib/XMLCommon.php

@ -158,14 +158,14 @@ abstract class XMLCommon {
return $out;
} elseif (preg_match("/^([^@\s]+@\S+) \((.+?)\)$/", $person, $match)) { // tests "user@example.com (Full Name)" form
if ($this->validateMail($match[1])) {
$out->name = $match[2];
$out->name = trim($match[2]);
$out->mail = $match[1];
} else {
$out->name = $person;
}
} elseif (preg_match("/^((?:\S|\s(?!<))+) <([^>]+)>$/", $person, $match)) { // tests "Full Name <user@example.com>" form
if ($this->validateMail($match[2])) {
$out->name = $match[1];
$out->name = trim($match[1]);
$out->mail = $match[2];
} else {
$out->name = $person;
@ -179,6 +179,21 @@ abstract class XMLCommon {
return $out;
}
protected function parsePersonAtom(\DOMNode $node) {
$p = new Person;
$p->mail = $this->fetchText("./atom:email", $node) ?? "";
$p->name = $this->fetchText("./atom:name", $node) ?? $p->mail;
if (!strlen($p->name)) {
return null;
}
$url = $this->fetchElement("./atom:uri", $node);
if ($url) {
$p->url = $this->resolveNodeUrl($url);
}
$p->role = $node->localName;
return $p;
}
/** Tests whether a string is a valid e-mail address
*
* Accepts IDN hosts and Unicode localparts

28
lib/XMLEntryPrimitives.php

@ -25,4 +25,32 @@ trait XMLEntryPrimitives {
}
return $out;
}
/** Primitive to fetch a collection of people associated with an Atom entry */
protected function getPeopleAtom() {
$nodes = $this->fetchElements("./atom:author|./atom:contributor");
$out = new PersonCollection;
foreach ($nodes as $node) {
$p = $this->parsePersonAtom($node);
if ($p) {
$out[] = $p;
}
}
$primary = $out->primary();
// if the entry has no author, we retrieve the authors (and not contributors) from the entry's source element
if (!$primary || $primary->role != "author") {
$nodes = $this->fetchElements("./atom:source[1]/atom:author");
foreach ($nodes as $node) {
$p = $this->parsePersonAtom($node);
if ($p) {
$out[] = $p;
}
}
// if there are still no people, return null
if (!$out->primary()) {
return null;
}
}
return $out;
}
}

16
lib/XMLFeedPrimitives.php

@ -61,24 +61,16 @@ trait XMLFeedPrimitives {
/** Primitive to fetch a collection of people associated with an Atom feed */
protected function getPeopleAtom() {
$nodes = $this->fetchElements("././atom:author|./atom:contributor");
$nodes = $this->fetchElements("./atom:author|./atom:contributor");
if (!$nodes->length) {
return null;
}
$out = new PersonCollection;
foreach ($nodes as $node) {
$p = new Person;
$p->mail = $this->fetchText("./atom:email", $node) ?? "";
$p->name = $this->fetchText("./atom:name", $node) ?? $p->mail;
if (!strlen($p->name)) {
continue;
}
$url = $this->fetchElement("./atom:uri", $node);
if ($url) {
$p->url = $this->resolveNodeUrl($url);
$p = $this->parsePersonAtom($node);
if ($p) {
$out[] = $p;
}
$p->role = $node->localName;
$out[] = $p;
}
return $out;
}

Loading…
Cancel
Save