Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
RSSData | |
0.00% |
0 / 36 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 | |||
rssTokenToName | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\RSS; |
4 | |
5 | use DOMDocument; |
6 | use DOMXPath; |
7 | |
8 | class RSSData { |
9 | public $error; |
10 | public $items; |
11 | |
12 | /** |
13 | * Constructor, takes a DOMDocument and returns an array of parsed items. |
14 | * @param DOMDocument $xml the pre-parsed XML Document |
15 | */ |
16 | public function __construct( $xml ) { |
17 | if ( !( $xml instanceof DOMDocument ) ) { |
18 | $this->error = "Not passed DOMDocument object."; |
19 | return; |
20 | } |
21 | $xpath = new DOMXPath( $xml ); |
22 | |
23 | // namespace-safe method to find all elements |
24 | $items = $xpath->query( "//*[local-name() = 'item']" ); |
25 | |
26 | if ( $items->length === 0 ) { |
27 | $items = $xpath->query( "//*[local-name() = 'entry']" ); |
28 | } |
29 | |
30 | if ( $items->length === 0 ) { |
31 | $this->error = 'No RSS/ATOM items found.'; |
32 | return; |
33 | } |
34 | |
35 | foreach ( $items as $item ) { |
36 | $bit = []; |
37 | foreach ( $item->childNodes as $n ) { |
38 | $name = $this->rssTokenToName( $n->nodeName ); |
39 | if ( $name != null ) { |
40 | /** |
41 | * Because for DOMElements the nodeValue is just |
42 | * the text of the containing element, without any |
43 | * tags, it makes this a safe, if unattractive, |
44 | * value to use. If you want to allow people to |
45 | * mark up their RSS, some more precautions are |
46 | * needed. |
47 | */ |
48 | $bit[$name] = trim( $n->nodeValue ); |
49 | } |
50 | } |
51 | $this->items[] = $bit; |
52 | } |
53 | } |
54 | |
55 | /** |
56 | * Return a string that will be used to map RSS elements that |
57 | * contain similar data (e.g. dc:date, date, and pubDate) to the |
58 | * same array key. This works on WordPress feeds as-is, but it |
59 | * probably needs a way to concert dc:date format dates to be the |
60 | * same as pubDate. |
61 | * |
62 | * @param string $name name of the element we have |
63 | * @return string|null Name to map it to |
64 | */ |
65 | protected function rssTokenToName( $name ) { |
66 | $tokenNames = [ |
67 | 'dc:date' => 'date', |
68 | 'pubDate' => 'date', |
69 | 'updated' => 'date', |
70 | 'dc:creator' => 'author', |
71 | 'summary' => 'description', |
72 | 'content:encoded' => 'encodedContent', |
73 | 'category' => null, |
74 | 'comments' => null, |
75 | 'feedburner:origLink' => null, |
76 | 'slash:comments' => null, |
77 | 'slash:department' => null, |
78 | 'slash:hit_parade' => null, |
79 | 'slash:section' => null, |
80 | 'wfw:commentRss' => null, |
81 | ]; |
82 | |
83 | if ( array_key_exists( $name, $tokenNames ) ) { |
84 | return $tokenNames[ $name ]; |
85 | } |
86 | |
87 | return $name; |
88 | } |
89 | |
90 | } |