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