]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/atom10feed.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[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 $categories;
55     private $contributors;
56     private $generator;
57     private $icon;
58     private $links;
59     private $logo;
60     private $rights;
61     private $subtitle;
62     private $title;
63     private $published;
64     private $updated;
65     private $entries;
66
67     /**
68      * Constructor
69      *
70      * @param boolean $indent  flag to turn indenting on or off
71      *
72      * @return void
73      */
74     function __construct($indent = true) {
75         parent::__construct($indent);
76         $this->namespaces = array();
77         $this->links      = array();
78         $this->entries    = array();
79         $this->addNamespace('xmlns', 'http://www.w3.org/2005/Atom');
80     }
81
82     /**
83      * Add another namespace to the feed
84      *
85      * @param string $namespace the namespace
86      * @param string $uri       namspace uri
87      *
88      * @return void
89      */
90     function addNamespace($namespace, $uri)
91     {
92         $ns = array($namespace => $uri);
93         $this->namespaces = array_merge($this->namespaces, $ns);
94     }
95
96     function getNamespaces()
97     {
98         return $this->namespaces;
99     }
100
101     function initFeed()
102     {
103         $this->xw->startDocument('1.0', 'UTF-8');
104         $commonAttrs = array('xml:lang' => 'en-US');
105         $commonAttrs = array_merge($commonAttrs, $this->namespaces);
106         $this->elementStart('feed', $commonAttrs);
107
108         $this->element('id', null, $this->id);
109         $this->element('title', null, $this->title);
110         $this->element('subtitle', null, $this->subtitle);
111
112         if (!empty($this->logo)) {
113             $this->element('logo', null, $this->logo);
114         }
115
116         $this->element('updated', null, $this->updated);
117
118         $this->renderLinks();
119     }
120
121     /**
122      * Check that all required elements have been set, etc.
123      * Throws an Atom10FeedException if something's missing.
124      *
125      * @return void
126      */
127     function validate()
128     {
129     }
130
131     function renderLinks()
132     {
133         foreach ($this->links as $attrs)
134         {
135             $this->element('link', $attrs, null);
136         }
137     }
138
139     function addEntryRaw($entry)
140     {
141         array_push($this->entries, $entry);
142     }
143
144     function addEntry($entry)
145     {
146         array_push($this->entries, $entry->getString());
147     }
148
149     function renderEntries()
150     {
151         foreach ($this->entries as $entry) {
152             $this->raw($entry);
153         }
154     }
155
156     function endFeed()
157     {
158         $this->elementEnd('feed');
159         $this->xw->endDocument();
160     }
161
162     function getString()
163     {
164         $this->validate();
165
166         $this->initFeed();
167         $this->renderEntries();
168         $this->endFeed();
169
170         return $this->xw->outputMemory();
171     }
172
173     function setId($id)
174     {
175         $this->id = $id;
176     }
177
178     function setTitle($title)
179     {
180         $this->title = $title;
181     }
182
183     function setSubtitle($subtitle)
184     {
185         $this->subtitle = $subtitle;
186     }
187
188     function setLogo($logo)
189     {
190         $this->logo = $logo;
191     }
192
193     function setUpdated($dt)
194     {
195         $this->updated = common_date_iso8601($dt);
196     }
197
198     function setPublished($dt)
199     {
200         $this->published = common_date_iso8601($dt);
201     }
202
203     /**
204      * Adds a link element into the Atom document
205      *
206      * Assumes you want rel="alternate" and type="text/html" unless
207      * you send in $otherAttrs.
208      *
209      * @param string $uri            the uri the href needs to point to
210      * @param array  $otherAttrs     other attributes to stick in
211      *
212      * @return void
213      */
214     function addLink($uri, $otherAttrs = null) {
215         $attrs = array('href' => $uri);
216
217         if (is_null($otherAttrs)) {
218             $attrs['rel']  = 'alternate';
219             $attrs['type'] = 'text/html';
220         } else {
221             $attrs = array_merge($attrs, $otherAttrs);
222         }
223
224         array_push($this->links, $attrs);
225     }
226
227 }