3 * Code for creating RSS 1.0.
6 // Encoding for RSS output.
7 if (!defined('RSS_ENCODING'))
8 define('RSS_ENCODING', $GLOBALS['charset']);
11 * A class for writing RSS 1.0.
13 * @see http://purl.org/rss/1.0/spec,
14 * http://www.usemod.com/cgi-bin/mb.pl?ModWiki
16 class RssWriter extends XmlElement
20 $this->XmlElement('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=" . RSS_ENCODING);
128 echo('<' . '?xml version="1.0" encoding="' . RSS_ENCODING . '"?' . ">\n");
129 //printf("<!-- generator=\"PhpWiki-%s\" -->\n", PHPWIKI_VERSION);
134 * Create a new RDF <em>typedNode</em>.
136 function __node($type, $properties, $uri = false)
139 $uri = $properties['link'];
140 $attr['rdf:about'] = $this->__uniquify_uri($uri);
141 return new XmlElement($type, $attr,
142 $this->__elementize($properties));
146 * Check object URI for uniqueness, create a unique URI if needed.
148 function __uniquify_uri($uri)
150 if (!$uri || isset($this->_uris_seen[$uri])) {
151 $n = count($this->_uris_seen);
152 $uri = $this->_channel->getAttr('rdf:about') . "#uri$n";
153 assert(!isset($this->_uris_seen[$uri]));
155 $this->_uris_seen[$uri] = true;
160 * Convert hash of RDF properties to <em>propertyElt</em>s.
162 function __elementize($elements)
165 foreach ($elements as $prop => $val) {
166 $this->__check_predicate($prop);
168 $out[] = new XmlElement($prop, $val);
169 elseif (is_object($val))
171 $out[] = new XmlElement($prop, false, $val);
177 * Check property predicates for XMLNS sanity.
179 function __check_predicate($name)
181 if (preg_match('/^([^:]+):[^:]/', $name, $m)) {
183 if (!$this->getAttr("xmlns:$ns")) {
184 if (!isset($this->_modules[$ns]))
185 die("$name: unknown namespace ($ns)");
186 $this->setAttr("xmlns:$ns", $this->_modules[$ns]);
192 * Create a <em>propertyElt</em> which references another node in the RSS.
194 function __ref($predicate, $reference)
196 $attr['rdf:resource'] = $reference->getAttr('rdf:about');
197 return new XmlElement($predicate, $attr);
201 /* Taken from mediawiki.
202 * See http://www.atomenabled.org/developers/syndication/
204 class AtomFeed extends RssWriter
207 // Args should include:
208 // 'title', 'link', 'description'
211 function feed($properties, $uri = false)
214 $attr = array('xmlns' => 'http://www.w3.org/2005/Atom',
215 'version' => '0.3', // or 1.0
217 $this->_channel = $this->__node('feed', $attr, $properties, $uri);
221 * Write output to HTTP client.
225 header("Content-Type: application/atom+xml; charset=" . RSS_ENCODING);
226 echo('<' . '?xml version="1.0" encoding="' . RSS_ENCODING . '"?' . ">\n");
227 //printf("<!-- generator=\"PhpWiki-%s\" -->\n", PHPWIKI_VERSION);
234 function __node($type, $attr, $properties, $uri = false)
237 $uri = $properties['link'];
238 //$attr['rdf:about'] = $this->__uniquify_uri($uri);
239 return new XmlElement($type, $attr,
240 $this->__elementize($properties));
243 // Args should include:
244 // 'title', 'link', author, modified, issued, created, summary,
247 function addItem($properties, $attr = false, $uri = false)
249 $this->_items[] = $this->__node('entry', $attr, $properties, $uri);
257 if (isset($this->_finished))
260 $channel = &$this->_channel;
261 $items = &$this->_items;
263 $channel->pushContent($items);
264 $this->pushContent($channel);
267 $this->_finished = true;
275 // c-hanging-comment-ender-p: nil
276 // indent-tabs-mode: nil