-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRSSGenerator.php
211 lines (169 loc) · 5.82 KB
/
RSSGenerator.php
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
namespace RSSGenerator_Inst;
/*
* This is a quick & dirty RSS feed generator.
* RSS elements will be supported at (my) need.
*
* Copyright 2014 by wltb
*
* @license LGPL-3.0+ <http://spdx.org/licenses/LGPL-3.0+>
*/
/*
Doesn't check if the final XML confirms to RSS specifications,
e.g. format of the element texts or required elements.
Throws a DOMException if a parameter text can't be stored in a DOMText node.
*/
/*
#Example Usage
include 'RSSGenerator.php'
$f = new RSSGenerator\Feed();
$f->link('http://www.example.com');
$f->set_feed_elements(array('description' => 'Nice Feed', 'title' => 'Feed Title'));
$f->new_item(array('description' => 'Nice item', 'author' => 'me'));
$it = $f->new_item(array('description' => 'Another nice item'));
$it->content = 'Content'; #content currently overrides description
$f->save('php://stdout');
*/
class Feed {
private $feed;
private $channel;
private $stored_elems = array();
static private $supported_tags = array('description', 'title', 'link');
static private $alias_tags = array('url' => 'link');
public function __set($name, $value) {
if(isset(self::$alias_tags[$name])) $name = self::$alias_tags[$name];
if(in_array($name, self::$supported_tags)) {
$type = 'DOMText';
$this->factory_singleton($value, $name, $type);
}
}
public function __get($name) {
if(isset(self::$alias_tags[$name])) $name = self::$alias_tags[$name];
if(isset($this->stored_elems[$name])) return $this->stored_elems[$name];
else return NULL;
}
function __construct($ar=array(), $doc=NULL) {
if($doc) $this->feed = $doc;
else {
$this->feed = new \DOMDocument('1.0', 'utf-8');
$this->feed->loadXML('<rss version="2.0"><channel/></rss>');
$this->feed->formatOutput = true;
}
$this->channel = $this->feed->getElementsByTagName('channel')->item(0);
$this->set_feed_elements($ar);
}
function set_feed_elements($ar) {
foreach($ar as $key => $val) $this->$key = $val;
}
function get_xpath() {
return new \DOMXPath($this->feed);
}
/*
Creates or replaces feed channel elements
@param string $tag Tag name of the node
@param string $text Text to be stored in the node $tag
@param DOMText/DOMCdataSection $type Type of the text node that stores $text & is appended to node $tag
*/
private function factory_singleton($text, $tag, $type='DOMText') {
$text_node = create_text_node($text, $type);
$parent = $this->channel;
$node = $this->stored_elems[$tag]; //caching for easier access/check
if($node) $parent->removeChild($node);
$node = $parent->insertBefore(new \DOMElement($tag), $parent->firstChild);
$node->appendChild($text_node);
$this->stored_elems[$tag] = $node;
}
function new_item($ar=array()) {
$item = $this->feed->createElement('item');
$this->channel->appendChild($item);
$item_obj = new FeedItem($item, $ar);
return $item_obj;
}
function saveXML() {
$this->feed->encoding = 'utf-8';
return $this->feed->saveXML() . "\n";
}
function save($filename) {
$this->feed->encoding = 'utf-8';
$this->feed->save($filename);
}
}
function sanitize_text($text) {
return trim(preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}\x{10000}-\x{10FFFF}]+/u', ' ', $text));
}
# Throws a DOMException if $text is not convertable.
# Catching is only meaningful in Feed or FeedItem, where more context can be added.
# Seperate function because $text may have to be filtered.
function create_text_node($text, $type) {
return new $type(sanitize_text($text));
}
class FeedItem {
private $item;
static private $supported_tags = array('description', 'guid', 'pubDate',
'title', 'link', 'author');
static private $alias_tags = array('content' => 'description',
'date' => 'pubDate', 'url' => 'link');
private $stored_elems = array();
function __construct($item, $ar = array()) {
$this->item = $item;
//calls __set
foreach($ar as $key => $val) $this->$key = $val;
}
function get_item() {
return $this->item;
}
//see function in Feed class
private function factory_singleton($text, $tag, $type='DOMText') {
$text_node = create_text_node($text, $type);
$parent = $this->item;
$node = $this->stored_elems[$tag];
if($node) $parent->removeChild($node);
$node = $parent->appendChild(new \DOMElement($tag));
$node->appendChild($text_node);
$this->stored_elems[$tag] = $node;
}
public function __set($name, $value) {
if(isset(self::$alias_tags[$name])) $name = self::$alias_tags[$name];
if(in_array($name, self::$supported_tags)) {
if($name == 'description') $type = 'DOMCdataSection';
else $type = 'DOMText';
$this->factory_singleton($value, $name, $type);
} elseif($name == 'category') {
$this->category($value);
} elseif($name == 'slash_comments') {
$this->slash_comments($value);
}
}
public function __get($name) {
if(isset(self::$alias_tags[$name])) $name = self::$alias_tags[$name];
if(isset($this->stored_elems[$name])) return $this->stored_elems[$name]->textContent;
else return NULL;
}
function slash_comments($num_comments) {
$this->item;
$node = $this->stored_elems['slash_comments'];
if($node) {
$node->nodeValue = $num_comments;
} else {
$this->item->parentNode->parentNode->setAttributeNS('http://www.w3.org/2000/xmlns/',
'xmlns:slash', 'http://purl.org/rss/1.0/modules/slash/');
$com = new \DOMElement('comments', $num_comments,
'http://purl.org/rss/1.0/modules/slash/');
$this->item->appendChild($com);
$this->stored_elems['slash_comments'] = $com;
}
}
/*
Adds category nodes.
@param string/array $arg category names
*/
function category($arg) {
if(!(is_string($arg) || is_array($arg)) ) return; //could bark here a little
elseif(is_string($arg)) $arg = array($arg);
foreach($arg as $cat) {
$text_node = create_text_node($cat, 'DOMText');
$node = $this->item->appendChild(new \DOMElement('category'));
$node->appendChild($text_node);
}
}
}