-
Notifications
You must be signed in to change notification settings - Fork 9
/
RDF.php
395 lines (351 loc) · 10.7 KB
/
RDF.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**************************************************************************
* Copyright (C) 2011 Alex Dzyoba <[email protected]>
*
* This file is part of FeedParser.
*
* FeedParser is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FeedParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FeedParser. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
/**
* @file RDF.php
* @brief RSS RDF branch base classes.
*
* Contains FeedParserRDF and FeedParserRDFElement.
*/
/**
* @brief Base class for RDF branch feeds.
*
* This is the base class for RSS 0.90, RSS 1.0 and RSS 1.1. This feeds differs
* only by namespace, so to avoid code copy-pasting we create base class with
* all the methods
*
* This class implements IFeed interface.
*
* @author Alex Dzyoba <[email protected]>
*/
class FeedParserRDF extends FeedParser implements IFeed
{
/**
* We're likely to use XPath, so let's keep it global
*/
public $xpath;
/**
* When performing XPath queries we will use this prefix
*/
private $xpathPrefix = '//';
/**
* The feed type we are parsing.
*/
protected $feedType;
/**
* Contains array of items
*/
public $items;
/**
* We will be working with multiple namespaces and it is useful to
* keep them together
*/
protected $namespaces = array(
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'dc' => 'http://purl.org/rss/1.0/modules/dc/',
'content' => 'http://purl.org/rss/1.0/modules/content/',
'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
/**
* Our constructor does actually more than its parent. Besides creating
* xpath and DOMDocument objects it sets $feedType property to invoke proper
* subclass and adds correct RSS namespaces in $namespaces array
*
* @param $xml
* A DOM object representing the feed
* @param $type
* Type of RDF feed. Can be '0.90', '1.0' and '1.1'
* @param $strict
* (optional) Whether or not to validate this feed
*/
function __construct(DOMDocument $xml, $type, $strict = false)
{
$this->model = $xml;
if ($strict)
if (! $this->relaxNGValidate())
throw new Exception('Failed required validation');
switch($type)
{
case '0.90':
$this->feedType = 'RSS 0.90';
$this->namespaces['rss'] = 'http://my.netscape.com/rdf/simple/0.9/';
break;
case '1.0':
$this->feedType = 'RSS 1.0';
$this->namespaces['rss'] = 'http://purl.org/rss/1.0/';
break;
case '1.1':
$this->feedType = 'RSS 1.1';
$this->namespaces['rss'] = 'http://purl.org/net/rss1.1#';
break;
}
$this->xpath = new DOMXPath($this->model);
foreach ($this->namespaces as $key => $value)
$this->xpath->registerNamespace($key, $value);
}
/**
* Return link that holds link to website where feed came from
*
* @return
* String with link
*/
public function getLink()
{
// Evaluate XPath query over DOMDocument.
// We look for top level <link>
$items = $this->xpath->evaluate("/rdf:RDF/rss:channel/rss:link");
// If we have more than one top-level <title> then notify
if($items->length > 1)
echo 'Strange feed link';
// Even if we have more than 1 <link> we will return content of first
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
/**
* Return link that holds RDF feed itself.
*
* Unlike Atom feeds, RSS feeds don't have any special tag or anything else
* for that purpose.
*
* That's why we return empty string. In your application you should
* reimplement this method, for example, to return this value from database.
*
* @return
* String with feed link
*/
public function getFeedLink()
{
return '';
}
/**
* Return title of feed. (IFeed interface implementation)
*
* @return
* String with feed title
*/
public function getTitle()
{
// Evaluate XPath query over DOMDocument.
// We basicaly look for top-level <title>.
$items = $this->xpath->evaluate('/rdf:RDF/rss:channel/rss:title');
// If we have more than one top-level <title> then notify
if($items->length > 1)
echo 'Strange feed title';
// Even if we have more than 1 <title> we will return content of first
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
/**
* Return description for RDF feed. Description is containing in
* <description>. (IFeed interface implementation)
*
* @return
* String with feed description
*/
public function getDescription()
{
// Evaluate XPath query over DOMDocument.
// We basicaly look for top-level <description>.
$items = $this->xpath->evaluate('/rdf:RDF/rss:channel/rss:description');
// If we have more than one top-level <description> then notify
if($items->length > 1)
echo 'Strange feed description';
// Even if we have more than 1 <description> we will return content of first
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
/**
* Return type of feed. In this case we return $feedType constant
* string. (IFeed interface implementation)
*
* @return
* String with feed type
*/
public function getFeedType()
{
return $this->feedType;
}
/**
* Fetch items from feed as array of FeedParserRDFElement objects.
*
* The only reason why we don't keep it as property and fetch in
* constructor is performance, i.e. if we only want to build list of feeds
* then we do not need to fetch and parse all items of every feed. That's
* why we fetch it on demand
*
* @return
* Array of FeedParserRDFElement class instances
*/
public function getItems()
{
// Evaluate XPath query over DOMDocument.
// We look for top level link with 'rel=self' attribute
$entries = $this->xpath->evaluate("/rdf:RDF/rss:item");
$items = array();
foreach($entries as $entry)
{
// We should make DOMDocument object from DOMNode object to pass it
// to FeedParserRSS2Element constructor (otherwise there will be
// fatal error despite DOMDocument extends from DOMNode)
$doc = new DOMDocument;
$doc->appendChild($doc->importNode($entry, TRUE));
// Add to array new object representing entry
$items[] = new FeedParserRDFElement($doc, $this->feedType);
}
return $items;
}
}
/**
* @brief Handles RDF feed items.
*
* This class represents RDF <item> node, that holds items for RSS 0.90, RSS
* 1.0, RSS 1.1. We make array of this class instances in
* FeedParserRDF::getItems() .
*
* This class implements IItem interface.
*
* @author Alex Dzyoba <[email protected]>
*/
class FeedParserRDFElement implements IItem
{
/**
* Contains parsed XML document
*/
public $model;
/**
* We will be working with multiple namespaces and it is useful to
* keep them together
*/
protected $namespaces = array(
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'dc' => 'http://purl.org/rss/1.0/modules/dc/',
'content' => 'http://purl.org/rss/1.0/modules/content/',
'sy' => 'http://web.resource.org/rss/1.0/modules/syndication/');
/**
* Constructor. It takes DOMDocument representing tree under <item> tag
* and creates DOMXPath object for it based on send $type
*
* @param $xml
* A DOM object representing the entry
* @param $type
* Type of RDF feed. Can be '0.90', '1.0' and '1.1'
* @param $strict
* (optional) Whether or not to validate this feed
*/
function __construct(DOMDocument $xml, $type, $strict = false)
{
$this->model = $xml;
if ($strict)
if (! $this->relaxNGValidate())
throw new Exception('Failed required validation');
switch($type)
{
case 'RSS 0.90':
$this->namespaces['rss'] = 'http://my.netscape.com/rdf/simple/0.9/';
break;
case 'RSS 1.0':
$this->namespaces['rss'] = 'http://purl.org/rss/1.0/';
break;
case 'RSS 1.1':
$this->namespaces['rss'] = 'http://purl.org/net/rss1.1#';
break;
}
$this->xpath = new DOMXPath($this->model);
foreach ($this->namespaces as $key => $value)
$this->xpath->registerNamespace($key, $value);
}
/**
* Get title of entry. Query over tree for <title> and get content of tag
*
* @return
* String with title
*/
public function getTitle()
{
// Evaluate XPath query over DOMDocument.
// Because our DOMDocument root element is <entry> we have only one
// <title> element that we query.
$items = $this->xpath->evaluate('//rss:title');
// If we have more than one <title> then notify
if($items->length > 1)
echo 'Strange entry title';
// Even if we have more than 1 <title> we will return content of first
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
/**
* Get content of entry. Query over tree for <content> and get content of tag
*
* @return
* String with content
*/
public function getContent()
{
// Basically content of item located in <description>. But in some cases
// generators put only brief content in that tag, while real content is
// in <content:encoded>
//First, we look for <content:encoded>
$items = $this->xpath->evaluate('//content:encoded');
if(empty($items->item(0)->nodeValue)) // If it's empty
{
//We look for <dc:encoded>
$items = $this->xpath->evaluate('//dc:description');
if(empty($items->item(0)->nodeValue)) // If it's empty
{
//We look for <rss:description>
$items = $this->xpath->evaluate('//rss:description');
}
}
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
/**
* Get date of entry. Query over tree for <pubDate> and get content of tag
*
* @return
* String with publication date
*/
public function getPubDate()
{
// We get date from <pubDate>
$items = $this->xpath->evaluate('//rss:pubDate');
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
/**
* Get link of entry. Query over tree for <link> with rel=alternate and get
* content of tag
*
* @return
* String with item link
*/
public function getLink()
{
// We get date from <pubDate>
$items = $this->xpath->evaluate('//rss:link');
if($items->length === 0)
return '';
return $items->item(0)->nodeValue;
}
}