3 * StatusNet, the distributed open-source microblogging tool
5 * Class for building an Atom feed in memory
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET'))
35 class Atom10FeedException extends Exception
40 * Class for building an Atom feed in memory. Get the finished doc
41 * as a string with Atom10Feed::getString().
45 * @author Zach Copley <zach@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 class Atom10Feed extends XMLStringer
56 private $contributors;
71 * @param boolean $indent flag to turn indenting on or off
75 function __construct($indent = true) {
76 parent::__construct($indent);
77 $this->namespaces = array();
78 $this->authors = array();
79 $this->links = array();
80 $this->entries = array();
81 $this->addNamespace('', 'http://www.w3.org/2005/Atom');
85 * Add another namespace to the feed
87 * @param string $namespace the namespace
88 * @param string $uri namspace uri
92 function addNamespace($namespace, $uri)
94 $ns = array($namespace => $uri);
95 $this->namespaces = array_merge($this->namespaces, $ns);
98 function addAuthor($name, $uri = null, $email = null)
100 $xs = new XMLStringer(true);
102 $xs->elementStart('author');
105 $xs->element('name', null, $name);
107 throw new Atom10FeedException(
108 'author element must contain a name element.'
112 if (!is_null($uri)) {
113 $xs->element('uri', null, $uri);
116 if (!is_null(email)) {
117 $xs->element('email', null, $email);
120 $xs->elementEnd('author');
122 array_push($this->authors, $xs->getString());
126 * Add an Author to the feed via raw XML string
128 * @param string $xmlAuthor An XML string representation author
132 function addAuthorRaw($xmlAuthor)
134 array_push($this->authors, $xmlAuthor);
137 function renderAuthors()
139 foreach ($this->authors as $author) {
145 * Add a activity feed subject via raw XML string
147 * @param string $xmlSubject An XML string representation of the subject
151 function setActivitySubject($xmlSubject)
153 $this->subject = $xmlSubject;
156 function getNamespaces()
158 return $this->namespaces;
163 $this->xw->startDocument('1.0', 'UTF-8');
164 $commonAttrs = array('xml:lang' => 'en-US');
165 foreach ($this->namespaces as $prefix => $uri) {
169 $attr = 'xmlns:' . $prefix;
171 $commonAttrs[$attr] = $uri;
173 $this->elementStart('feed', $commonAttrs);
175 $this->element('id', null, $this->id);
176 $this->element('title', null, $this->title);
177 $this->element('subtitle', null, $this->subtitle);
179 if (!empty($this->logo)) {
180 $this->element('logo', null, $this->logo);
183 $this->element('updated', null, $this->updated);
185 $this->renderAuthors();
187 $this->renderLinks();
191 * Check that all required elements have been set, etc.
192 * Throws an Atom10FeedException if something's missing.
200 function renderLinks()
202 foreach ($this->links as $attrs)
204 $this->element('link', $attrs, null);
208 function addEntryRaw($xmlEntry)
210 array_push($this->entries, $xmlEntry);
213 function addEntry($entry)
215 array_push($this->entries, $entry->getString());
218 function renderEntries()
220 foreach ($this->entries as $entry) {
227 $this->elementEnd('feed');
228 $this->xw->endDocument();
233 if (Event::handle('StartApiAtom', array($this))) {
238 if (!empty($this->subject)) {
239 $this->raw($this->subject);
242 $this->renderEntries();
245 Event::handle('EndApiAtom', array($this));
248 return $this->xw->outputMemory();
256 function setTitle($title)
258 $this->title = $title;
261 function setSubtitle($subtitle)
263 $this->subtitle = $subtitle;
266 function setLogo($logo)
271 function setUpdated($dt)
273 $this->updated = common_date_iso8601($dt);
276 function setPublished($dt)
278 $this->published = common_date_iso8601($dt);
282 * Adds a link element into the Atom document
284 * Assumes you want rel="alternate" and type="text/html" unless
285 * you send in $otherAttrs.
287 * @param string $uri the uri the href needs to point to
288 * @param array $otherAttrs other attributes to stick in
292 function addLink($uri, $otherAttrs = null) {
293 $attrs = array('href' => $uri);
295 if (is_null($otherAttrs)) {
296 $attrs['rel'] = 'alternate';
297 $attrs['type'] = 'text/html';
299 $attrs = array_merge($attrs, $otherAttrs);
302 array_push($this->links, $attrs);