common.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. abstract class FeedItem_Common extends FeedItem {
  3. protected $elem;
  4. protected $xpath;
  5. protected $doc;
  6. function __construct($elem, $doc, $xpath) {
  7. $this->elem = $elem;
  8. $this->xpath = $xpath;
  9. $this->doc = $doc;
  10. try {
  11. $source = $elem->getElementsByTagName("source")->item(0);
  12. // we don't need <source> element
  13. if ($source)
  14. $elem->removeChild($source);
  15. } catch (DOMException $e) {
  16. //
  17. }
  18. }
  19. function get_element() {
  20. return $this->elem;
  21. }
  22. function get_author() {
  23. $author = $this->elem->getElementsByTagName("author")->item(0);
  24. if ($author) {
  25. $name = $author->getElementsByTagName("name")->item(0);
  26. if ($name) return $name->nodeValue;
  27. $email = $author->getElementsByTagName("email")->item(0);
  28. if ($email) return $email->nodeValue;
  29. if ($author->nodeValue)
  30. return $author->nodeValue;
  31. }
  32. $author = $this->xpath->query("dc:creator", $this->elem)->item(0);
  33. if ($author) {
  34. return $author->nodeValue;
  35. }
  36. }
  37. function get_comments_url() {
  38. //RSS only. Use a query here to avoid namespace clashes (e.g. with slash).
  39. //might give a wrong result if a default namespace was declared (possible with XPath 2.0)
  40. $com_url = $this->xpath->query("comments", $this->elem)->item(0);
  41. if($com_url)
  42. return $com_url->nodeValue;
  43. //Atom Threading Extension (RFC 4685) stuff. Could be used in RSS feeds, so it's in common.
  44. //'text/html' for type is too restrictive?
  45. $com_url = $this->xpath->query("atom:link[@rel='replies' and contains(@type,'text/html')]/@href", $this->elem)->item(0);
  46. if($com_url)
  47. return $com_url->nodeValue;
  48. }
  49. function get_comments_count() {
  50. //also query for ATE stuff here
  51. $query = "slash:comments|thread:total|atom:link[@rel='replies']/@thread:count";
  52. $comments = $this->xpath->query($query, $this->elem)->item(0);
  53. if ($comments) {
  54. return $comments->nodeValue;
  55. }
  56. }
  57. function count_children($node) {
  58. return $node->getElementsByTagName("*")->length;
  59. }
  60. function subtree_or_text($node) {
  61. if ($this->count_children($node) == 0) {
  62. return $node->nodeValue;
  63. } else {
  64. return $node->c14n();
  65. }
  66. }
  67. }