1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
<?php rcs_id('$Id: RssWriter.php,v 1.12 2005/07/24 09:52:59 rurban Exp $');
/*
* Code for creating RSS 1.0.
*/
// Encoding for RSS output.
if (!defined('RSS_ENCODING'))
define('RSS_ENCODING', $GLOBALS['charset']);
/**
* A class for writing RSS 1.0.
*
* @see http://purl.org/rss/1.0/spec,
* http://www.usemod.com/cgi-bin/mb.pl?ModWiki
*/
class RssWriter extends XmlElement
{
function RssWriter () {
$this->XmlElement('rdf:RDF',
array('xmlns' => "http://purl.org/rss/1.0/",
'xmlns:rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'));
$this->_modules = array(
//Standards
'content' => "http://purl.org/rss/1.0/modules/content/",
'dc' => "http://purl.org/dc/elements/1.1/",
'sy' => "http://purl.org/rss/1.0/modules/syndication/",
//Proposed
'wiki' => "http://purl.org/rss/1.0/modules/wiki/",
'ag' => "http://purl.org/rss/1.0/modules/aggregation/",
'annotate' => "http://purl.org/rss/1.0/modules/annotate/",
'audio' => "http://media.tangent.org/rss/1.0/",
'cp' => "http://my.theinfo.org/changed/1.0/rss/",
'rss091' => "http://purl.org/rss/1.0/modules/rss091/",
'slash' => "http://purl.org/rss/1.0/modules/slash/",
'taxo' => "http://purl.org/rss/1.0/modules/taxonomy/",
'thr' => "http://purl.org/rss/1.0/modules/threading/"
);
$this->_uris_seen = array();
$this->_items = array();
}
function registerModule($alias, $uri) {
assert(!isset($this->_modules[$alias]));
$this->_modules[$alias] = $uri;
}
// Args should include:
// 'title', 'link', 'description'
// and can include:
// 'URI'
function channel($properties, $uri = false) {
$this->_channel = $this->__node('channel', $properties, $uri);
}
// Args should include:
// 'title', 'link'
// and can include:
// 'description', 'URI'
function addItem($properties, $uri = false) {
$this->_items[] = $this->__node('item', $properties, $uri);
}
// Args should include:
// 'url', 'title', 'link'
// and can include:
// 'URI'
function image($properties, $uri = false) {
$this->_image = $this->__node('image', $properties, $uri);
}
// Args should include:
// 'title', 'description', 'name', and 'link'
// and can include:
// 'URI'
function textinput($properties, $uri = false) {
$this->_textinput = $this->__node('textinput', $properties, $uri);
}
/**
* Finish construction of RSS.
*/
function finish() {
if (isset($this->_finished))
return;
$channel = &$this->_channel;
$items = &$this->_items;
$seq = new XmlElement('rdf:Seq');
if ($items) {
foreach ($items as $item)
$seq->pushContent($this->__ref('rdf:li', $item));
}
$channel->pushContent(new XmlElement('items', false, $seq));
if (isset($this->_image)) {
$channel->pushContent($this->__ref('image', $this->_image));
$items[] = $this->_image;
}
if (isset($this->_textinput)) {
$channel->pushContent($this->__ref('textinput', $this->_textinput));
$items[] = $this->_textinput;
}
$this->pushContent($channel);
if ($items)
$this->pushContent($items);
$this->__spew();
$this->_finished = true;
}
/**
* Write output to HTTP client.
*/
function __spew() {
header("Content-Type: application/xml; charset=" . RSS_ENCODING);
printf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", RSS_ENCODING);
printf("<!-- generator=\"PhpWiki-%s\" -->\n", PHPWIKI_VERSION);
$this->printXML();
}
/**
* Create a new RDF <em>typedNode</em>.
*/
function __node($type, $properties, $uri = false) {
if (! $uri)
$uri = $properties['link'];
$attr['rdf:about'] = $this->__uniquify_uri($uri);
return new XmlElement($type, $attr,
$this->__elementize($properties));
}
/**
* Check object URI for uniqueness, create a unique URI if needed.
*/
function __uniquify_uri ($uri) {
if (!$uri || isset($this->_uris_seen[$uri])) {
$n = count($this->_uris_seen);
$uri = $this->_channel->getAttr('rdf:about') . "#uri$n";
assert(!isset($this->_uris_seen[$uri]));
}
$this->_uris_seen[$uri] = true;
return $uri;
}
/**
* Convert hash of RDF properties to <em>propertyElt</em>s.
*/
function __elementize ($elements) {
$out = array();
foreach ($elements as $prop => $val) {
$this->__check_predicate($prop);
$out[] = new XmlElement($prop, false, $val);
}
return $out;
}
/**
* Check property predicates for XMLNS sanity.
*/
function __check_predicate ($name) {
if (preg_match('/^([^:]+):[^:]/', $name, $m)) {
$ns = $m[1];
if (! $this->getAttr("xmlns:$ns")) {
if (!isset($this->_modules[$ns]))
die("$name: unknown namespace ($ns)");
$this->setAttr("xmlns:$ns", $this->_modules[$ns]);
}
}
}
/**
* Create a <em>propertyElt</em> which references another node in the RSS.
*/
function __ref($predicate, $reference) {
$attr['rdf:resource'] = $reference->getAttr('rdf:about');
return new XmlElement($predicate, $attr);
}
};
// (c-file-style: "gnu")
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>
|