3 * Code for creating RSS 1.0.
7 * A class for writing RSS 1.0.
9 * @see http://purl.org/rss/1.0/spec,
10 * http://www.usemod.com/cgi-bin/mb.pl?ModWiki
12 class RssWriter extends XmlElement
18 function __construct()
20 parent::__construct('rdf:RDF',
21 array('xmlns' => "http://purl.org/rss/1.0/",
22 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'));
24 $this->_modules = array(
26 'content' => "http://purl.org/rss/1.0/modules/content/",
27 'dc' => "http://purl.org/dc/elements/1.1/",
28 'sy' => "http://purl.org/rss/1.0/modules/syndication/",
30 'wiki' => "http://purl.org/rss/1.0/modules/wiki/",
31 'ag' => "http://purl.org/rss/1.0/modules/aggregation/",
32 'annotate' => "http://purl.org/rss/1.0/modules/annotate/",
33 'audio' => "http://media.tangent.org/rss/1.0/",
34 'cp' => "http://my.theinfo.org/changed/1.0/rss/",
35 'rss091' => "http://purl.org/rss/1.0/modules/rss091/",
36 'slash' => "http://purl.org/rss/1.0/modules/slash/",
37 'taxo' => "http://purl.org/rss/1.0/modules/taxonomy/",
38 'thr' => "http://purl.org/rss/1.0/modules/threading/"
41 $this->_uris_seen = array();
42 $this->_items = array();
45 function registerModule($alias, $uri)
47 assert(!isset($this->_modules[$alias]));
48 $this->_modules[$alias] = $uri;
51 // Args should include:
52 // 'title', 'link', 'description'
55 function channel($properties, $uri = false)
57 $this->_channel = $this->__node('channel', $properties, $uri);
60 // Args should include:
63 // 'description', 'URI'
64 function addItem($properties, $uri = false)
66 $this->_items[] = $this->__node('item', $properties, $uri);
69 // Args should include:
70 // 'url', 'title', 'link'
73 function image($properties, $uri = false)
75 $this->_image = $this->__node('image', $properties, $uri);
78 // Args should include:
79 // 'title', 'description', 'name', and 'link'
82 function textinput($properties, $uri = false)
84 $this->_textinput = $this->__node('textinput', $properties, $uri);
88 * Finish construction of RSS.
92 if (isset($this->_finished))
95 $channel = &$this->_channel;
96 $items = &$this->_items;
98 $seq = new XmlElement('rdf:Seq');
100 foreach ($items as $item)
101 $seq->pushContent($this->__ref('rdf:li', $item));
103 $channel->pushContent(new XmlElement('items', false, $seq));
105 if (isset($this->_image)) {
106 $channel->pushContent($this->__ref('image', $this->_image));
107 $items[] = $this->_image;
109 if (isset($this->_textinput)) {
110 $channel->pushContent($this->__ref('textinput', $this->_textinput));
111 $items[] = $this->_textinput;
114 $this->pushContent($channel);
116 $this->pushContent($items);
119 $this->_finished = true;
123 * Write output to HTTP client.
127 header("Content-Type: application/xml; charset=UTF-8");
128 echo('<' . '?xml version="1.0" encoding="UTF-8" ?' . ">\n");
133 * Create a new RDF <em>typedNode</em>.
140 function __node($type, $properties, $uri = false)
143 $uri = $properties['link'];
144 $attr['rdf:about'] = $this->__uniquify_uri($uri);
145 return new XmlElement($type, $attr,
146 $this->__elementize($properties));
150 * Check object URI for uniqueness, create a unique URI if needed.
155 function __uniquify_uri($uri)
157 if (!$uri || isset($this->_uris_seen[$uri])) {
158 $n = count($this->_uris_seen);
159 $uri = $this->_channel->getAttr('rdf:about') . "#uri$n";
160 assert(!isset($this->_uris_seen[$uri]));
162 $this->_uris_seen[$uri] = true;
167 * Convert hash of RDF properties to <em>propertyElt</em>s.
172 function __elementize($elements)
175 foreach ($elements as $prop => $val) {
176 $this->__check_predicate($prop);
178 $out[] = new XmlElement($prop, $val);
179 elseif (is_object($val))
181 $out[] = new XmlElement($prop, false, $val);
187 * Check property predicates for XMLNS sanity.
189 * @param string $name
191 function __check_predicate($name)
193 if (preg_match('/^([^:]+):[^:]/', $name, $m)) {
195 if (!$this->getAttr("xmlns:$ns")) {
196 if (!isset($this->_modules[$ns]))
197 die("$name: unknown namespace ($ns)");
198 $this->setAttr("xmlns:$ns", $this->_modules[$ns]);
204 * Create a <em>propertyElt</em> which references another node in the RSS.
210 function __ref($predicate, $reference)
212 $attr['rdf:resource'] = $reference->getAttr('rdf:about');
213 return new XmlElement($predicate, $attr);
217 /* Taken from mediawiki.
218 * See http://www.atomenabled.org/developers/syndication/
220 class AtomFeed extends RssWriter
223 // Args should include:
224 // 'title', 'link', 'description'
227 function feed($properties, $uri = false)
230 $attr = array('xmlns' => 'http://www.w3.org/2005/Atom',
231 'version' => '0.3', // or 1.0
233 $this->_channel = $this->__node('feed', $attr, $properties, $uri);
237 * Write output to HTTP client.
241 header("Content-Type: application/atom+xml; charset=UTF-8");
242 echo('<' . '?xml version="1.0" encoding="UTF-8" ?' . ">\n");
251 * @param bool $properties
255 function __node($type, $attr, $properties, $uri = false)
258 $uri = $properties['link'];
259 //$attr['rdf:about'] = $this->__uniquify_uri($uri);
260 return new XmlElement($type, $attr,
261 $this->__elementize($properties));
264 // Args should include:
265 // 'title', 'link', author, modified, issued, created, summary,
268 function addItem($properties, $attr = false, $uri = false)
270 $this->_items[] = $this->__node('entry', $attr, $properties, $uri);
278 if (isset($this->_finished))
281 $channel = &$this->_channel;
282 $items = &$this->_items;
284 $channel->pushContent($items);
285 $this->pushContent($channel);
288 $this->_finished = true;
296 // c-hanging-comment-ender-p: nil
297 // indent-tabs-mode: nil