]> git.wh0rd.org - tt-rss.git/blob - classes/feeditem/common.php
add a workaround to support numeric tags
[tt-rss.git] / classes / feeditem / common.php
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 function count_children($node) {
78 return $node->getElementsByTagName("*")->length;
79 }
80
81 function subtree_or_text($node) {
82 if ($this->count_children($node) == 0) {
83 return $node->nodeValue;
84 } else {
85 return $node->c14n();
86 }
87 }
88
89 }