]> git.wh0rd.org Git - tt-rss.git/blob - classes/feeditem/rss.php
Merge branch 'pullreq-store-language' of tkappe/tt-rss into master
[tt-rss.git] / classes / feeditem / rss.php
1 <?php
2 class FeedItem_RSS extends FeedItem_Common {
3         function get_id() {
4                 $id = $this->elem->getElementsByTagName("guid")->item(0);
5
6                 if ($id) {
7                         return $id->nodeValue;
8                 } else {
9                         return $this->get_link();
10                 }
11         }
12
13         function get_date() {
14                 $pubDate = $this->elem->getElementsByTagName("pubDate")->item(0);
15
16                 if ($pubDate) {
17                         return strtotime($pubDate->nodeValue);
18                 }
19
20                 $date = $this->xpath->query("dc:date", $this->elem)->item(0);
21
22                 if ($date) {
23                         return strtotime($date->nodeValue);
24                 }
25         }
26
27         function get_link() {
28                 $links = $this->xpath->query("atom:link", $this->elem);
29
30                 foreach ($links as $link) {
31                         if ($link && $link->hasAttribute("href") &&
32                                 (!$link->hasAttribute("rel")
33                                         || $link->getAttribute("rel") == "alternate"
34                                         || $link->getAttribute("rel") == "standout")) {
35
36                                 return trim($link->getAttribute("href"));
37                         }
38                 }
39
40                 $link = $this->elem->getElementsByTagName("guid")->item(0);
41
42                 if ($link && $link->hasAttributes() && $link->getAttribute("isPermaLink") == "true") {
43                         return trim($link->nodeValue);
44                 }
45
46                 $link = $this->elem->getElementsByTagName("link")->item(0);
47
48                 if ($link) {
49                         return trim($link->nodeValue);
50                 }
51         }
52
53         function get_title() {
54                 $title = $this->xpath->query("title", $this->elem)->item(0);
55
56                 if ($title) {
57                         return trim($title->nodeValue);
58                 }
59
60                 // if the document has a default namespace then querying for
61                 // title would fail because of reasons so let's try the old way
62                 $title = $this->elem->getElementsByTagName("title")->item(0);
63
64                 if ($title) {
65                         return trim($title->nodeValue);
66                 }
67         }
68
69         function get_content() {
70                 $contentA = $this->xpath->query("content:encoded", $this->elem)->item(0);
71                 $contentB = $this->elem->getElementsByTagName("description")->item(0);
72
73                 if ($contentA && !$contentB) {
74                         return $this->subtree_or_text($contentA);
75                 }
76
77
78                 if ($contentB && !$contentA) {
79                         return $this->subtree_or_text($contentB);
80                 }
81
82                 if ($contentA && $contentB) {
83                         $resultA = $this->subtree_or_text($contentA);
84                         $resultB = $this->subtree_or_text($contentB);
85
86                         return mb_strlen($resultA) > mb_strlen($resultB) ? $resultA : $resultB;
87                 }
88         }
89
90         function get_description() {
91                 $summary = $this->elem->getElementsByTagName("description")->item(0);
92
93                 if ($summary) {
94                         return $summary->nodeValue;
95                 }
96         }
97
98         function get_categories() {
99                 $categories = $this->elem->getElementsByTagName("category");
100                 $cats = array();
101
102                 foreach ($categories as $cat) {
103                         array_push($cats, trim($cat->nodeValue));
104                 }
105
106                 $categories = $this->xpath->query("dc:subject", $this->elem);
107
108                 foreach ($categories as $cat) {
109                         array_push($cats, trim($cat->nodeValue));
110                 }
111
112                 return $cats;
113         }
114
115         function get_enclosures() {
116                 $enclosures = $this->elem->getElementsByTagName("enclosure");
117
118                 $encs = array();
119
120                 foreach ($enclosures as $enclosure) {
121                         $enc = new FeedEnclosure();
122
123                         $enc->type = $enclosure->getAttribute("type");
124                         $enc->link = $enclosure->getAttribute("url");
125                         $enc->length = $enclosure->getAttribute("length");
126                         $enc->height = $enclosure->getAttribute("height");
127                         $enc->width = $enclosure->getAttribute("width");
128
129                         array_push($encs, $enc);
130                 }
131
132                 $enclosures = $this->xpath->query("media:content", $this->elem);
133
134                 foreach ($enclosures as $enclosure) {
135                         $enc = new FeedEnclosure();
136
137                         $enc->type = $enclosure->getAttribute("type");
138                         $enc->link = $enclosure->getAttribute("url");
139                         $enc->length = $enclosure->getAttribute("length");
140                         $enc->height = $enclosure->getAttribute("height");
141                         $enc->width = $enclosure->getAttribute("width");
142
143                         $desc = $this->xpath->query("media:description", $enclosure)->item(0);
144                         if ($desc) $enc->title = strip_tags($desc->nodeValue);
145
146                         array_push($encs, $enc);
147                 }
148
149
150                 $enclosures = $this->xpath->query("media:group", $this->elem);
151
152                 foreach ($enclosures as $enclosure) {
153                         $enc = new FeedEnclosure();
154
155                         $content = $this->xpath->query("media:content", $enclosure)->item(0);
156
157                         if ($content) {
158                                 $enc->type = $content->getAttribute("type");
159                                 $enc->link = $content->getAttribute("url");
160                                 $enc->length = $content->getAttribute("length");
161                                 $enc->height = $content->getAttribute("height");
162                                 $enc->width = $content->getAttribute("width");
163
164                                 $desc = $this->xpath->query("media:description", $content)->item(0);
165                                 if ($desc) {
166                                         $enc->title = strip_tags($desc->nodeValue);
167                                 } else {
168                                         $desc = $this->xpath->query("media:description", $enclosure)->item(0);
169                                         if ($desc) $enc->title = strip_tags($desc->nodeValue);
170                                 }
171
172                                 array_push($encs, $enc);
173                         }
174                 }
175
176                 $enclosures = $this->xpath->query("media:thumbnail", $this->elem);
177
178                 foreach ($enclosures as $enclosure) {
179                         $enc = new FeedEnclosure();
180
181                         $enc->type = "image/generic";
182                         $enc->link = $enclosure->getAttribute("url");
183                         $enc->height = $enclosure->getAttribute("height");
184                         $enc->width = $enclosure->getAttribute("width");
185
186                         array_push($encs, $enc);
187                 }
188
189                 return $encs;
190         }
191
192         function get_language() {
193                 $languages = $this->doc->getElementsByTagName('language');
194
195                 if (count($languages) == 0) {
196                         return "";
197                 }
198
199                 return $languages[0]->textContent;
200         }
201
202 }