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
14 function __construct()
16 $this->XmlElement('rdf:RDF',
17 array('xmlns' => "http://purl.org/rss/1.0/",
18 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'));
20 $this->_modules = array(
22 'content' => "http://purl.org/rss/1.0/modules/content/",
23 'dc' => "http://purl.org/dc/elements/1.1/",
24 'sy' => "http://purl.org/rss/1.0/modules/syndication/",
26 'wiki' => "http://purl.org/rss/1.0/modules/wiki/",
27 'ag' => "http://purl.org/rss/1.0/modules/aggregation/",
28 'annotate' => "http://purl.org/rss/1.0/modules/annotate/",
29 'audio' => "http://media.tangent.org/rss/1.0/",
30 'cp' => "http://my.theinfo.org/changed/1.0/rss/",
31 'rss091' => "http://purl.org/rss/1.0/modules/rss091/",
32 'slash' => "http://purl.org/rss/1.0/modules/slash/",
33 'taxo' => "http://purl.org/rss/1.0/modules/taxonomy/",
34 'thr' => "http://purl.org/rss/1.0/modules/threading/"
37 $this->_uris_seen = array();
38 $this->_items = array();
41 function registerModule($alias, $uri)
43 assert(!isset($this->_modules[$alias]));
44 $this->_modules[$alias] = $uri;
47 // Args should include:
48 // 'title', 'link', 'description'
51 function channel($properties, $uri = false)
53 $this->_channel = $this->__node('channel', $properties, $uri);
56 // Args should include:
59 // 'description', 'URI'
60 function addItem($properties, $uri = false)
62 $this->_items[] = $this->__node('item', $properties, $uri);
65 // Args should include:
66 // 'url', 'title', 'link'
69 function image($properties, $uri = false)
71 $this->_image = $this->__node('image', $properties, $uri);
74 // Args should include:
75 // 'title', 'description', 'name', and 'link'
78 function textinput($properties, $uri = false)
80 $this->_textinput = $this->__node('textinput', $properties, $uri);
84 * Finish construction of RSS.
88 if (isset($this->_finished))
91 $channel = &$this->_channel;
92 $items = &$this->_items;
94 $seq = new XmlElement('rdf:Seq');
96 foreach ($items as $item)
97 $seq->pushContent($this->__ref('rdf:li', $item));
99 $channel->pushContent(new XmlElement('items', false, $seq));
101 if (isset($this->_image)) {
102 $channel->pushContent($this->__ref('image', $this->_image));
103 $items[] = $this->_image;
105 if (isset($this->_textinput)) {
106 $channel->pushContent($this->__ref('textinput', $this->_textinput));
107 $items[] = $this->_textinput;
110 $this->pushContent($channel);
112 $this->pushContent($items);
115 $this->_finished = true;
119 * Write output to HTTP client.
123 header("Content-Type: application/xml; charset=" . RSS_ENCODING);
124 echo('<' . '?xml version="1.0" encoding="' . RSS_ENCODING . '"?' . ">\n");
129 * Create a new RDF <em>typedNode</em>.
131 function __node($type, $properties, $uri = false)
134 $uri = $properties['link'];
135 $attr['rdf:about'] = $this->__uniquify_uri($uri);
136 return new XmlElement($type, $attr,
137 $this->__elementize($properties));
141 * Check object URI for uniqueness, create a unique URI if needed.
143 function __uniquify_uri($uri)
145 if (!$uri || isset($this->_uris_seen[$uri])) {
146 $n = count($this->_uris_seen);
147 $uri = $this->_channel->getAttr('rdf:about') . "#uri$n";
148 assert(!isset($this->_uris_seen[$uri]));
150 $this->_uris_seen[$uri] = true;
155 * Convert hash of RDF properties to <em>propertyElt</em>s.
157 function __elementize($elements)
160 foreach ($elements as $prop => $val) {
161 $this->__check_predicate($prop);
163 $out[] = new XmlElement($prop, $val);
164 elseif (is_object($val))
166 $out[] = new XmlElement($prop, false, $val);
172 * Check property predicates for XMLNS sanity.
174 function __check_predicate($name)
176 if (preg_match('/^([^:]+):[^:]/', $name, $m)) {
178 if (!$this->getAttr("xmlns:$ns")) {
179 if (!isset($this->_modules[$ns]))
180 die("$name: unknown namespace ($ns)");
181 $this->setAttr("xmlns:$ns", $this->_modules[$ns]);
187 * Create a <em>propertyElt</em> which references another node in the RSS.
189 function __ref($predicate, $reference)
191 $attr['rdf:resource'] = $reference->getAttr('rdf:about');
192 return new XmlElement($predicate, $attr);
196 /* Taken from mediawiki.
197 * See http://www.atomenabled.org/developers/syndication/
199 class AtomFeed extends RssWriter
202 // Args should include:
203 // 'title', 'link', 'description'
206 function feed($properties, $uri = false)
209 $attr = array('xmlns' => 'http://www.w3.org/2005/Atom',
210 'version' => '0.3', // or 1.0
212 $this->_channel = $this->__node('feed', $attr, $properties, $uri);
216 * Write output to HTTP client.
220 header("Content-Type: application/atom+xml; charset=" . RSS_ENCODING);
221 echo('<' . '?xml version="1.0" encoding="' . RSS_ENCODING . '"?' . ">\n");
228 function __node($type, $attr, $properties, $uri = false)
231 $uri = $properties['link'];
232 //$attr['rdf:about'] = $this->__uniquify_uri($uri);
233 return new XmlElement($type, $attr,
234 $this->__elementize($properties));
237 // Args should include:
238 // 'title', 'link', author, modified, issued, created, summary,
241 function addItem($properties, $attr = false, $uri = false)
243 $this->_items[] = $this->__node('entry', $attr, $properties, $uri);
251 if (isset($this->_finished))
254 $channel = &$this->_channel;
255 $items = &$this->_items;
257 $channel->pushContent($items);
258 $this->pushContent($channel);
261 $this->_finished = true;
269 // c-hanging-comment-ender-p: nil
270 // indent-tabs-mode: nil