X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fatom10feed.php;h=2bba6d0a91474bf5ac7271c3cc35aa83b2574e2c;hb=a0b9aeb43ef1b08e8dd7fe25101c515a0df53e7f;hp=ccca76a09e2a76cac4e20c08e91c17d01273543c;hpb=c465f675d9dbcf9f808bc31a1d01e753df4ddf58;p=quix0rs-gnu-social.git diff --git a/lib/atom10feed.php b/lib/atom10feed.php index ccca76a09e..2bba6d0a91 100644 --- a/lib/atom10feed.php +++ b/lib/atom10feed.php @@ -49,17 +49,22 @@ class Atom10FeedException extends Exception class Atom10Feed extends XMLStringer { public $xw; + + // @fixme most of these should probably be read-only properties private $namespaces; private $authors; + private $subject; private $categories; private $contributors; private $generator; private $icon; private $links; - private $logo; + private $selfLink; + private $selfLinkType; + public $logo; private $rights; - private $subtitle; - private $title; + public $subtitle; + public $title; private $published; private $updated; private $entries; @@ -74,9 +79,10 @@ class Atom10Feed extends XMLStringer function __construct($indent = true) { parent::__construct($indent); $this->namespaces = array(); + $this->authors = array(); $this->links = array(); $this->entries = array(); - $this->addNamespace('xmlns', 'http://www.w3.org/2005/Atom'); + $this->addNamespace('', 'http://www.w3.org/2005/Atom'); } /** @@ -93,6 +99,53 @@ class Atom10Feed extends XMLStringer $this->namespaces = array_merge($this->namespaces, $ns); } + function addAuthor($name, $uri = null, $email = null) + { + $xs = new XMLStringer(true); + + $xs->elementStart('author'); + + if (!empty($name)) { + $xs->element('name', null, $name); + } else { + throw new Atom10FeedException( + // TRANS: Atom feed exception thrown when an author element does not contain a name element. + _('Author element must contain a name element.') + ); + } + + if (isset($uri)) { + $xs->element('uri', null, $uri); + } + + if (isset($email)) { + $xs->element('email', null, $email); + } + + $xs->elementEnd('author'); + + array_push($this->authors, $xs->getString()); + } + + /** + * Add an Author to the feed via raw XML string + * + * @param string $xmlAuthor An XML string representation author + * + * @return void + */ + function addAuthorRaw($xmlAuthor) + { + array_push($this->authors, $xmlAuthor); + } + + function renderAuthors() + { + foreach ($this->authors as $author) { + $this->raw($author); + } + } + function getNamespaces() { return $this->namespaces; @@ -100,11 +153,26 @@ class Atom10Feed extends XMLStringer function initFeed() { - $this->xw->startDocument('1.0', 'UTF-8'); + $this->startXML(); $commonAttrs = array('xml:lang' => 'en-US'); - $commonAttrs = array_merge($commonAttrs, $this->namespaces); + foreach ($this->namespaces as $prefix => $uri) { + if ($prefix == '') { + $attr = 'xmlns'; + } else { + $attr = 'xmlns:' . $prefix; + } + $commonAttrs[$attr] = $uri; + } $this->elementStart('feed', $commonAttrs); + $this->element( + 'generator', array( + 'uri' => 'https://gnu.io/social', + 'version' => GNUSOCIAL_VERSION + ), + 'GNU social' + ); + $this->element('id', null, $this->id); $this->element('title', null, $this->title); $this->element('subtitle', null, $this->subtitle); @@ -115,6 +183,12 @@ class Atom10Feed extends XMLStringer $this->element('updated', null, $this->updated); + $this->renderAuthors(); + + if ($this->selfLink) { + $this->addLink($this->selfLink, array('rel' => 'self', + 'type' => $this->selfLinkType)); + } $this->renderLinks(); } @@ -136,9 +210,9 @@ class Atom10Feed extends XMLStringer } } - function addEntryRaw($entry) + function addEntryRaw($xmlEntry) { - array_push($this->entries, $entry); + array_push($this->entries, $xmlEntry); } function addEntry($entry) @@ -161,11 +235,20 @@ class Atom10Feed extends XMLStringer function getString() { - $this->validate(); + if (Event::handle('StartApiAtom', array($this))) { + + $this->validate(); + $this->initFeed(); + + if (!empty($this->subject)) { + $this->raw($this->subject); + } - $this->initFeed(); - $this->renderEntries(); - $this->endFeed(); + $this->renderEntries(); + $this->endFeed(); + + Event::handle('EndApiAtom', array($this)); + } return $this->xw->outputMemory(); } @@ -175,6 +258,12 @@ class Atom10Feed extends XMLStringer $this->id = $id; } + function setSelfLink($url, $type='application/atom+xml') + { + $this->selfLink = $url; + $this->selfLinkType = $type; + } + function setTitle($title) { $this->title = $title; @@ -223,5 +312,4 @@ class Atom10Feed extends XMLStringer array_push($this->links, $attrs); } - }