]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/atom10feed.php
Initial upgraded Atom output for group timelines
[quix0rs-gnu-social.git] / lib / atom10feed.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Class for building an Atom feed in memory
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Feed
23  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET'))
31 {
32     exit(1);
33 }
34
35 class Atom10FeedException extends Exception
36 {
37 }
38
39 /**
40  * Class for building an Atom feed in memory.  Get the finished doc
41  * as a string with Atom10Feed::getString().
42  *
43  * @category Feed
44  * @package  StatusNet
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/
48  */
49 class Atom10Feed extends XMLStringer
50 {
51     public  $xw;
52     private $namespaces;
53     private $authors;
54     private $subject;
55     private $categories;
56     private $contributors;
57     private $generator;
58     private $icon;
59     private $links;
60     private $logo;
61     private $rights;
62     private $subtitle;
63     private $title;
64     private $published;
65     private $updated;
66     private $entries;
67
68     /**
69      * Constructor
70      *
71      * @param boolean $indent  flag to turn indenting on or off
72      *
73      * @return void
74      */
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('xmlns', 'http://www.w3.org/2005/Atom');
82     }
83
84     /**
85      * Add another namespace to the feed
86      *
87      * @param string $namespace the namespace
88      * @param string $uri       namspace uri
89      *
90      * @return void
91      */
92     function addNamespace($namespace, $uri)
93     {
94         $ns = array($namespace => $uri);
95         $this->namespaces = array_merge($this->namespaces, $ns);
96     }
97
98     function addAuthor($name, $uri = null, $email = null)
99     {
100         $xs = new XMLStringer(true);
101
102         $xs->elementStart('author');
103
104         if (!empty($name)) {
105             $xs->element('name', null, $name);
106         } else {
107             throw new Atom10FeedException(
108                 'author element must contain a name element.'
109             );
110         }
111
112         if (!is_null($uri)) {
113             $xs->element('uri', null, $uri);
114         }
115
116         if (!is_null(email)) {
117             $xs->element('email', null, $email);
118         }
119
120         $xs->elementEnd('author');
121
122         array_push($this->authors, $xs->getString());
123     }
124
125     /**
126      * Add an Author to the feed via raw XML string
127      *
128      * @param string $xmlAuthor An XML string representation author
129      *
130      * @return void
131      */
132     function addAuthorRaw($xmlAuthor)
133     {
134         array_push($this->authors, $xmlAuthor);
135     }
136
137     function renderAuthors()
138     {
139         foreach ($this->authors as $author) {
140             $this->raw($author);
141         }
142     }
143
144     /**
145      * Add a activity feed subject via raw XML string
146      *
147      * @param string $xmlSubject An XML string representation of the subject
148      *
149      * @return void
150      */
151     function setActivitySubject($xmlSubject)
152     {
153         $this->subject = $xmlSubject;
154     }
155
156     function getNamespaces()
157     {
158         return $this->namespaces;
159     }
160
161     function initFeed()
162     {
163         $this->xw->startDocument('1.0', 'UTF-8');
164         $commonAttrs = array('xml:lang' => 'en-US');
165         $commonAttrs = array_merge($commonAttrs, $this->namespaces);
166         $this->elementStart('feed', $commonAttrs);
167
168         $this->element('id', null, $this->id);
169         $this->element('title', null, $this->title);
170         $this->element('subtitle', null, $this->subtitle);
171
172         if (!empty($this->logo)) {
173             $this->element('logo', null, $this->logo);
174         }
175
176         $this->element('updated', null, $this->updated);
177
178         $this->renderLinks();
179     }
180
181     /**
182      * Check that all required elements have been set, etc.
183      * Throws an Atom10FeedException if something's missing.
184      *
185      * @return void
186      */
187     function validate()
188     {
189     }
190
191     function renderLinks()
192     {
193         foreach ($this->links as $attrs)
194         {
195             $this->element('link', $attrs, null);
196         }
197     }
198
199     function addEntryRaw($xmlEntry)
200     {
201         array_push($this->entries, $xmlEntry);
202     }
203
204     function addEntry($entry)
205     {
206         array_push($this->entries, $entry->getString());
207     }
208
209     function renderEntries()
210     {
211         foreach ($this->entries as $entry) {
212             $this->raw($entry);
213         }
214     }
215
216     function endFeed()
217     {
218         $this->elementEnd('feed');
219         $this->xw->endDocument();
220     }
221
222     function getString()
223     {
224         $this->validate();
225
226         $this->initFeed();
227         $this->renderAuthors();
228
229         if (!empty($this->subject)) {
230             $this->raw($this->subject);
231         }
232
233         $this->renderEntries();
234         $this->endFeed();
235
236         return $this->xw->outputMemory();
237     }
238
239     function setId($id)
240     {
241         $this->id = $id;
242     }
243
244     function setTitle($title)
245     {
246         $this->title = $title;
247     }
248
249     function setSubtitle($subtitle)
250     {
251         $this->subtitle = $subtitle;
252     }
253
254     function setLogo($logo)
255     {
256         $this->logo = $logo;
257     }
258
259     function setUpdated($dt)
260     {
261         $this->updated = common_date_iso8601($dt);
262     }
263
264     function setPublished($dt)
265     {
266         $this->published = common_date_iso8601($dt);
267     }
268
269     /**
270      * Adds a link element into the Atom document
271      *
272      * Assumes you want rel="alternate" and type="text/html" unless
273      * you send in $otherAttrs.
274      *
275      * @param string $uri            the uri the href needs to point to
276      * @param array  $otherAttrs     other attributes to stick in
277      *
278      * @return void
279      */
280     function addLink($uri, $otherAttrs = null) {
281         $attrs = array('href' => $uri);
282
283         if (is_null($otherAttrs)) {
284             $attrs['rel']  = 'alternate';
285             $attrs['type'] = 'text/html';
286         } else {
287             $attrs = array_merge($attrs, $otherAttrs);
288         }
289
290         array_push($this->links, $attrs);
291     }
292
293 }