]> git.wh0rd.org - tt-rss.git/blame - classes/feeditem/common.php
parser: properly support tag subtrees instead of text content for article content
[tt-rss.git] / classes / feeditem / common.php
CommitLineData
b4d16900
AD
1<?php
2abstract 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;
b5844603 11
01561287 12 try {
b5844603 13
01561287
AD
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 }
b4d16900
AD
22 }
23
24 function get_author() {
25 $author = $this->elem->getElementsByTagName("author")->item(0);
26
27 if ($author) {
28 $name = $author->getElementsByTagName("name")->item(0);
29
30 if ($name) return $name->nodeValue;
31
32 $email = $author->getElementsByTagName("email")->item(0);
33
34 if ($email) return $email->nodeValue;
602fe534
AD
35
36 if ($author->nodeValue)
37 return $author->nodeValue;
b4d16900
AD
38 }
39
40 $author = $this->xpath->query("dc:creator", $this->elem)->item(0);
41
42 if ($author) {
43 return $author->nodeValue;
44 }
45 }
46
b4d16900 47 function get_comments_url() {
d71ac5d3 48 //RSS only. Use a query here to avoid namespace clashes (e.g. with slash).
49 //might give a wrong result if a default namespace was declared (possible with XPath 2.0)
50 $com_url = $this->xpath->query("comments", $this->elem)->item(0);
b4d16900 51
d71ac5d3 52 if($com_url)
53 return $com_url->nodeValue;
54
55 //Atom Threading Extension (RFC 4685) stuff. Could be used in RSS feeds, so it's in common.
56 //'text/html' for type is too restrictive?
57 $com_url = $this->xpath->query("atom:link[@rel='replies' and contains(@type,'text/html')]/@href", $this->elem)->item(0);
58
59 if($com_url)
60 return $com_url->nodeValue;
b4d16900
AD
61 }
62
63 function get_comments_count() {
d71ac5d3 64 //also query for ATE stuff here
65 $query = "slash:comments|thread:total|atom:link[@rel='replies']/@thread:count";
66 $comments = $this->xpath->query($query, $this->elem)->item(0);
b4d16900
AD
67
68 if ($comments) {
69 return $comments->nodeValue;
70 }
71 }
72
7d1e15c3
AD
73 function count_children($node) {
74 return $node->getElementsByTagName("*")->length;
75 }
76
77 function subtree_or_text($node) {
78 if ($this->count_children($node) == 0) {
79 return $node->nodeValue;
80 } else {
81 return $node->c14n();
82 }
83 }
b4d16900
AD
84
85}
86?>