]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/RssWriter.php
Use __construct
[SourceForge/phpwiki.git] / lib / RssWriter.php
1 <?php
2 /*
3  * Code for creating RSS 1.0.
4  */
5
6 /**
7  * A class for writing RSS 1.0.
8  *
9  * @see http://purl.org/rss/1.0/spec,
10  *      http://www.usemod.com/cgi-bin/mb.pl?ModWiki
11  */
12 class RssWriter extends XmlElement
13 {
14     function __construct()
15     {
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#'));
19
20         $this->_modules = array(
21             //Standards
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/",
25             //Proposed
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/"
35         );
36
37         $this->_uris_seen = array();
38         $this->_items = array();
39     }
40
41     function registerModule($alias, $uri)
42     {
43         assert(!isset($this->_modules[$alias]));
44         $this->_modules[$alias] = $uri;
45     }
46
47     // Args should include:
48     //  'title', 'link', 'description'
49     // and can include:
50     //  'URI'
51     function channel($properties, $uri = false)
52     {
53         $this->_channel = $this->__node('channel', $properties, $uri);
54     }
55
56     // Args should include:
57     //  'title', 'link'
58     // and can include:
59     //  'description', 'URI'
60     function addItem($properties, $uri = false)
61     {
62         $this->_items[] = $this->__node('item', $properties, $uri);
63     }
64
65     // Args should include:
66     //  'url', 'title', 'link'
67     // and can include:
68     //  'URI'
69     function image($properties, $uri = false)
70     {
71         $this->_image = $this->__node('image', $properties, $uri);
72     }
73
74     // Args should include:
75     //  'title', 'description', 'name', and 'link'
76     // and can include:
77     //  'URI'
78     function textinput($properties, $uri = false)
79     {
80         $this->_textinput = $this->__node('textinput', $properties, $uri);
81     }
82
83     /**
84      * Finish construction of RSS.
85      */
86     function finish()
87     {
88         if (isset($this->_finished))
89             return;
90
91         $channel = &$this->_channel;
92         $items = &$this->_items;
93
94         $seq = new XmlElement('rdf:Seq');
95         if ($items) {
96             foreach ($items as $item)
97                 $seq->pushContent($this->__ref('rdf:li', $item));
98         }
99         $channel->pushContent(new XmlElement('items', false, $seq));
100
101         if (isset($this->_image)) {
102             $channel->pushContent($this->__ref('image', $this->_image));
103             $items[] = $this->_image;
104         }
105         if (isset($this->_textinput)) {
106             $channel->pushContent($this->__ref('textinput', $this->_textinput));
107             $items[] = $this->_textinput;
108         }
109
110         $this->pushContent($channel);
111         if ($items)
112             $this->pushContent($items);
113
114         $this->__spew();
115         $this->_finished = true;
116     }
117
118     /**
119      * Write output to HTTP client.
120      */
121     function __spew()
122     {
123         header("Content-Type: application/xml; charset=" . RSS_ENCODING);
124         echo('<' . '?xml version="1.0" encoding="' . RSS_ENCODING . '"?' . ">\n");
125         $this->printXML();
126     }
127
128     /**
129      * Create a new RDF <em>typedNode</em>.
130      */
131     function __node($type, $properties, $uri = false)
132     {
133         if (!$uri)
134             $uri = $properties['link'];
135         $attr['rdf:about'] = $this->__uniquify_uri($uri);
136         return new XmlElement($type, $attr,
137             $this->__elementize($properties));
138     }
139
140     /**
141      * Check object URI for uniqueness, create a unique URI if needed.
142      */
143     function __uniquify_uri($uri)
144     {
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]));
149         }
150         $this->_uris_seen[$uri] = true;
151         return $uri;
152     }
153
154     /**
155      * Convert hash of RDF properties to <em>propertyElt</em>s.
156      */
157     function __elementize($elements)
158     {
159         $out = array();
160         foreach ($elements as $prop => $val) {
161             $this->__check_predicate($prop);
162             if (is_array($val))
163                 $out[] = new XmlElement($prop, $val);
164             elseif (is_object($val))
165                 $out[] = $val; else
166                 $out[] = new XmlElement($prop, false, $val);
167         }
168         return $out;
169     }
170
171     /**
172      * Check property predicates for XMLNS sanity.
173      */
174     function __check_predicate($name)
175     {
176         if (preg_match('/^([^:]+):[^:]/', $name, $m)) {
177             $ns = $m[1];
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]);
182             }
183         }
184     }
185
186     /**
187      * Create a <em>propertyElt</em> which references another node in the RSS.
188      */
189     function __ref($predicate, $reference)
190     {
191         $attr['rdf:resource'] = $reference->getAttr('rdf:about');
192         return new XmlElement($predicate, $attr);
193     }
194 }
195
196 /* Taken from mediawiki.
197  * See http://www.atomenabled.org/developers/syndication/
198  */
199 class AtomFeed extends RssWriter
200 {
201
202     // Args should include:
203     //  'title', 'link', 'description'
204     // and can include:
205     //  'URI'
206     function feed($properties, $uri = false)
207     {
208         global $LANG;
209         $attr = array('xmlns' => 'http://www.w3.org/2005/Atom',
210             'version' => '0.3', // or 1.0
211             'lang' => $LANG);
212         $this->_channel = $this->__node('feed', $attr, $properties, $uri);
213     }
214
215     /**
216      * Write output to HTTP client.
217      */
218     function __spew()
219     {
220         header("Content-Type: application/atom+xml; charset=" . RSS_ENCODING);
221         echo('<' . '?xml version="1.0" encoding="' . RSS_ENCODING . '"?' . ">\n");
222         $this->printXML();
223     }
224
225     /**
226      * Create a new entry
227      */
228     function __node($type, $attr, $properties, $uri = false)
229     {
230         if (!$uri)
231             $uri = $properties['link'];
232         //$attr['rdf:about'] = $this->__uniquify_uri($uri);
233         return new XmlElement($type, $attr,
234             $this->__elementize($properties));
235     }
236
237     // Args should include:
238     //  'title', 'link', author, modified, issued, created, summary,
239     // and can include:
240     //  comment
241     function addItem($properties, $attr = false, $uri = false)
242     {
243         $this->_items[] = $this->__node('entry', $attr, $properties, $uri);
244     }
245
246     /**
247      * Print it.
248      */
249     function finish()
250     {
251         if (isset($this->_finished))
252             return;
253
254         $channel = &$this->_channel;
255         $items = &$this->_items;
256         if ($items)
257             $channel->pushContent($items);
258         $this->pushContent($channel);
259
260         $this->__spew();
261         $this->_finished = true;
262     }
263 }
264
265 // Local Variables:
266 // mode: php
267 // tab-width: 8
268 // c-basic-offset: 4
269 // c-hanging-comment-ender-p: nil
270 // indent-tabs-mode: nil
271 // End: