]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | abstract class FeedItem_Common extends FeedItem { | |
3 | protected $elem; | |
4 | protected $xpath; | |
5 | protected $doc; | |
6 | ||
7 | function __construct($elem, $doc, $xpath) { | |
8 | $this->elem = $elem; | |
9 | $this->xpath = $xpath; | |
10 | $this->doc = $doc; | |
11 | ||
12 | try { | |
13 | ||
14 | $source = $elem->getElementsByTagName("source")->item(0); | |
15 | ||
16 | // we don't need <source> element | |
17 | if ($source) | |
18 | $elem->removeChild($source); | |
19 | } catch (DOMException $e) { | |
20 | // | |
21 | } | |
22 | } | |
23 | ||
24 | function get_element() { | |
25 | return $this->elem; | |
26 | } | |
27 | ||
28 | function get_author() { | |
29 | $author = $this->elem->getElementsByTagName("author")->item(0); | |
30 | ||
31 | if ($author) { | |
32 | $name = $author->getElementsByTagName("name")->item(0); | |
33 | ||
34 | if ($name) return $name->nodeValue; | |
35 | ||
36 | $email = $author->getElementsByTagName("email")->item(0); | |
37 | ||
38 | if ($email) return $email->nodeValue; | |
39 | ||
40 | if ($author->nodeValue) | |
41 | return $author->nodeValue; | |
42 | } | |
43 | ||
44 | $author = $this->xpath->query("dc:creator", $this->elem)->item(0); | |
45 | ||
46 | if ($author) { | |
47 | return $author->nodeValue; | |
48 | } | |
49 | } | |
50 | ||
51 | function get_comments_url() { | |
52 | //RSS only. Use a query here to avoid namespace clashes (e.g. with slash). | |
53 | //might give a wrong result if a default namespace was declared (possible with XPath 2.0) | |
54 | $com_url = $this->xpath->query("comments", $this->elem)->item(0); | |
55 | ||
56 | if($com_url) | |
57 | return $com_url->nodeValue; | |
58 | ||
59 | //Atom Threading Extension (RFC 4685) stuff. Could be used in RSS feeds, so it's in common. | |
60 | //'text/html' for type is too restrictive? | |
61 | $com_url = $this->xpath->query("atom:link[@rel='replies' and contains(@type,'text/html')]/@href", $this->elem)->item(0); | |
62 | ||
63 | if($com_url) | |
64 | return $com_url->nodeValue; | |
65 | } | |
66 | ||
67 | function get_comments_count() { | |
68 | //also query for ATE stuff here | |
69 | $query = "slash:comments|thread:total|atom:link[@rel='replies']/@thread:count"; | |
70 | $comments = $this->xpath->query($query, $this->elem)->item(0); | |
71 | ||
72 | if ($comments) { | |
73 | return $comments->nodeValue; | |
74 | } | |
75 | } | |
76 | ||
77 | // this is common for both Atom and RSS types and deals with various media: elements | |
78 | function get_enclosures() { | |
79 | $encs = []; | |
80 | ||
81 | $enclosures = $this->xpath->query("media:content", $this->elem); | |
82 | ||
83 | foreach ($enclosures as $enclosure) { | |
84 | $enc = new FeedEnclosure(); | |
85 | ||
86 | $enc->type = $enclosure->getAttribute("type"); | |
87 | $enc->link = $enclosure->getAttribute("url"); | |
88 | $enc->length = $enclosure->getAttribute("length"); | |
89 | $enc->height = $enclosure->getAttribute("height"); | |
90 | $enc->width = $enclosure->getAttribute("width"); | |
91 | ||
92 | $medium = $enclosure->getAttribute("medium"); | |
93 | if (!$enc->type && $medium) { | |
94 | $enc->type = strtolower("$medium/generic"); | |
95 | } | |
96 | ||
97 | $desc = $this->xpath->query("media:description", $enclosure)->item(0); | |
98 | if ($desc) $enc->title = strip_tags($desc->nodeValue); | |
99 | ||
100 | array_push($encs, $enc); | |
101 | } | |
102 | ||
103 | $enclosures = $this->xpath->query("media:group", $this->elem); | |
104 | ||
105 | foreach ($enclosures as $enclosure) { | |
106 | $enc = new FeedEnclosure(); | |
107 | ||
108 | $content = $this->xpath->query("media:content", $enclosure)->item(0); | |
109 | ||
110 | if ($content) { | |
111 | $enc->type = $content->getAttribute("type"); | |
112 | $enc->link = $content->getAttribute("url"); | |
113 | $enc->length = $content->getAttribute("length"); | |
114 | $enc->height = $content->getAttribute("height"); | |
115 | $enc->width = $content->getAttribute("width"); | |
116 | ||
117 | $medium = $content->getAttribute("medium"); | |
118 | if (!$enc->type && $medium) { | |
119 | $enc->type = strtolower("$medium/generic"); | |
120 | } | |
121 | ||
122 | $desc = $this->xpath->query("media:description", $content)->item(0); | |
123 | if ($desc) { | |
124 | $enc->title = strip_tags($desc->nodeValue); | |
125 | } else { | |
126 | $desc = $this->xpath->query("media:description", $enclosure)->item(0); | |
127 | if ($desc) $enc->title = strip_tags($desc->nodeValue); | |
128 | } | |
129 | ||
130 | array_push($encs, $enc); | |
131 | } | |
132 | } | |
133 | ||
134 | $enclosures = $this->xpath->query("media:thumbnail", $this->elem); | |
135 | ||
136 | foreach ($enclosures as $enclosure) { | |
137 | $enc = new FeedEnclosure(); | |
138 | ||
139 | $enc->type = "image/generic"; | |
140 | $enc->link = $enclosure->getAttribute("url"); | |
141 | $enc->height = $enclosure->getAttribute("height"); | |
142 | $enc->width = $enclosure->getAttribute("width"); | |
143 | ||
144 | array_push($encs, $enc); | |
145 | } | |
146 | ||
147 | return $encs; | |
148 | } | |
149 | ||
150 | function count_children($node) { | |
151 | return $node->getElementsByTagName("*")->length; | |
152 | } | |
153 | ||
154 | function subtree_or_text($node) { | |
155 | if ($this->count_children($node) == 0) { | |
156 | return $node->nodeValue; | |
157 | } else { | |
158 | return $node->c14n(); | |
159 | } | |
160 | } | |
161 | ||
162 | } |