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
53 // @fixme most of these should probably be read-only properties
58 private $contributors;
63 private $selfLinkType;
75 * @param boolean $indent flag to turn indenting on or off
79 function __construct($indent = true) {
80 parent::__construct($indent);
81 $this->namespaces = array();
82 $this->authors = array();
83 $this->links = array();
84 $this->entries = array();
85 $this->addNamespace('', 'http://www.w3.org/2005/Atom');
89 * Add another namespace to the feed
91 * @param string $namespace the namespace
92 * @param string $uri namspace uri
96 function addNamespace($namespace, $uri)
98 $ns = array($namespace => $uri);
99 $this->namespaces = array_merge($this->namespaces, $ns);
102 function addAuthor($name, $uri = null, $email = null)
104 $xs = new XMLStringer(true);
106 $xs->elementStart('author');
109 $xs->element('name', null, $name);
111 throw new Atom10FeedException(
112 _('author element must contain a name element.')
117 $xs->element('uri', null, $uri);
121 $xs->element('email', null, $email);
124 $xs->elementEnd('author');
126 array_push($this->authors, $xs->getString());
130 * Add an Author to the feed via raw XML string
132 * @param string $xmlAuthor An XML string representation author
136 function addAuthorRaw($xmlAuthor)
138 array_push($this->authors, $xmlAuthor);
141 function renderAuthors()
143 foreach ($this->authors as $author) {
149 * Deprecated <activity:subject>; ignored
151 * @param string $xmlSubject An XML string representation of the subject
156 function setActivitySubject($xmlSubject)
158 throw new ServerException(_('Don\'t use this method!'));
161 function getNamespaces()
163 return $this->namespaces;
168 $this->xw->startDocument('1.0', 'UTF-8');
169 $commonAttrs = array('xml:lang' => 'en-US');
170 foreach ($this->namespaces as $prefix => $uri) {
174 $attr = 'xmlns:' . $prefix;
176 $commonAttrs[$attr] = $uri;
178 $this->elementStart('feed', $commonAttrs);
182 'uri' => 'http://status.net',
183 'version' => STATUSNET_VERSION
188 $this->element('id', null, $this->id);
189 $this->element('title', null, $this->title);
190 $this->element('subtitle', null, $this->subtitle);
192 if (!empty($this->logo)) {
193 $this->element('logo', null, $this->logo);
196 $this->element('updated', null, $this->updated);
198 $this->renderAuthors();
200 if ($this->selfLink) {
201 $this->addLink($this->selfLink, array('rel' => 'self',
202 'type' => $this->selfLinkType));
204 $this->renderLinks();
208 * Check that all required elements have been set, etc.
209 * Throws an Atom10FeedException if something's missing.
217 function renderLinks()
219 foreach ($this->links as $attrs)
221 $this->element('link', $attrs, null);
225 function addEntryRaw($xmlEntry)
227 array_push($this->entries, $xmlEntry);
230 function addEntry($entry)
232 array_push($this->entries, $entry->getString());
235 function renderEntries()
237 foreach ($this->entries as $entry) {
244 $this->elementEnd('feed');
245 $this->xw->endDocument();
250 if (Event::handle('StartApiAtom', array($this))) {
255 if (!empty($this->subject)) {
256 $this->raw($this->subject);
259 $this->renderEntries();
262 Event::handle('EndApiAtom', array($this));
265 return $this->xw->outputMemory();
273 function setSelfLink($url, $type='application/atom+xml')
275 $this->selfLink = $url;
276 $this->selfLinkType = $type;
279 function setTitle($title)
281 $this->title = $title;
284 function setSubtitle($subtitle)
286 $this->subtitle = $subtitle;
289 function setLogo($logo)
294 function setUpdated($dt)
296 $this->updated = common_date_iso8601($dt);
299 function setPublished($dt)
301 $this->published = common_date_iso8601($dt);
305 * Adds a link element into the Atom document
307 * Assumes you want rel="alternate" and type="text/html" unless
308 * you send in $otherAttrs.
310 * @param string $uri the uri the href needs to point to
311 * @param array $otherAttrs other attributes to stick in
315 function addLink($uri, $otherAttrs = null) {
316 $attrs = array('href' => $uri);
318 if (is_null($otherAttrs)) {
319 $attrs['rel'] = 'alternate';
320 $attrs['type'] = 'text/html';
322 $attrs = array_merge($attrs, $otherAttrs);
325 array_push($this->links, $attrs);