1 <?php rcs_id('$Id: RssWriter.php,v 1.10 2004-11-21 11:59:16 rurban Exp $');
3 * Code for creating RSS 1.0.
6 // Encoding for RSS output.
7 define('RSS_ENCODING', $GLOBALS['charset']);
10 * A class for writing RSS 1.0.
12 * @see http://purl.org/rss/1.0/spec,
13 * http://www.usemod.com/cgi-bin/mb.pl?ModWiki
15 class RssWriter extends XmlElement
17 function RssWriter () {
18 $this->XmlElement('rdf:RDF',
19 array('xmlns' => "http://purl.org/rss/1.0/",
20 'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'));
22 $this->_modules = array(
24 'content' => "http://purl.org/rss/1.0/modules/content/",
25 'dc' => "http://purl.org/dc/elements/1.1/",
26 'sy' => "http://purl.org/rss/1.0/modules/syndication/",
28 'wiki' => "http://purl.org/rss/1.0/modules/wiki/",
29 'ag' => "http://purl.org/rss/1.0/modules/aggregation/",
30 'annotate' => "http://purl.org/rss/1.0/modules/annotate/",
31 'audio' => "http://media.tangent.org/rss/1.0/",
32 'cp' => "http://my.theinfo.org/changed/1.0/rss/",
33 'rss091' => "http://purl.org/rss/1.0/modules/rss091/",
34 'slash' => "http://purl.org/rss/1.0/modules/slash/",
35 'taxo' => "http://purl.org/rss/1.0/modules/taxonomy/",
36 'thr' => "http://purl.org/rss/1.0/modules/threading/"
39 $this->_uris_seen = array();
40 $this->_items = array();
43 function registerModule($alias, $uri) {
44 assert(!isset($this->_modules[$alias]));
45 $this->_modules[$alias] = $uri;
48 // Args should include:
49 // 'title', 'link', 'description'
52 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) {
61 $this->_items[] = $this->__node('item', $properties, $uri);
64 // Args should include:
65 // 'url', 'title', 'link'
68 function image($properties, $uri = false) {
69 $this->_image = $this->__node('image', $properties, $uri);
72 // Args should include:
73 // 'title', 'description', 'name', and 'link'
76 function textinput($properties, $uri = false) {
77 $this->_textinput = $this->__node('textinput', $properties, $uri);
81 * Finish construction of RSS.
84 if (isset($this->_finished))
87 $channel = &$this->_channel;
88 $items = &$this->_items;
90 $seq = new XmlElement('rdf:Seq');
92 foreach ($items as $item)
93 $seq->pushContent($this->__ref('rdf:li', $item));
95 $channel->pushContent(new XmlElement('items', false, $seq));
97 if (isset($this->_image)) {
98 $channel->pushContent($this->__ref('image', $this->_image));
99 $items[] = $this->_image;
101 if (isset($this->_textinput)) {
102 $channel->pushContent($this->__ref('textinput', $this->_textinput));
103 $items[] = $this->_textinput;
106 $this->pushContent($channel);
108 $this->pushContent($items);
111 $this->_finished = true;
116 * Write output to HTTP client.
119 header("Content-Type: application/xml; charset=" . RSS_ENCODING);
120 printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", RSS_ENCODING);
126 * Create a new RDF <em>typedNode</em>.
128 function __node($type, $properties, $uri = false) {
130 $uri = $properties['link'];
131 $attr['rdf:about'] = $this->__uniquify_uri($uri);
132 return new XmlElement($type, $attr,
133 $this->__elementize($properties));
137 * Check object URI for uniqueness, create a unique URI if needed.
139 function __uniquify_uri ($uri) {
140 if (!$uri || isset($this->_uris_seen[$uri])) {
141 $n = count($this->_uris_seen);
142 $uri = $this->_channel->getAttr('rdf:about') . "#uri$n";
143 assert(!isset($this->_uris_seen[$uri]));
145 $this->_uris_seen[$uri] = true;
150 * Convert hash of RDF properties to <em>propertyElt</em>s.
152 function __elementize ($elements) {
154 foreach ($elements as $prop => $val) {
155 $this->__check_predicate($prop);
156 $out[] = new XmlElement($prop, false, $val);
162 * Check property predicates for XMLNS sanity.
164 function __check_predicate ($name) {
165 if (preg_match('/^([^:]+):[^:]/', $name, $m)) {
167 if (! $this->getAttr("xmlns:$ns")) {
168 if (!isset($this->_modules[$ns]))
169 die("$name: unknown namespace ($ns)");
170 $this->setAttr("xmlns:$ns", $this->_modules[$ns]);
176 * Create a <em>propertyElt</em> which references another node in the RSS.
178 function __ref($predicate, $reference) {
179 $attr['rdf:resource'] = $reference->getAttr('rdf:about');
180 return new XmlElement($predicate, $attr);
185 // (c-file-style: "gnu")
190 // c-hanging-comment-ender-p: nil
191 // indent-tabs-mode: nil