common.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_author() {
  20. $author = $this->elem->getElementsByTagName("author")->item(0);
  21. if ($author) {
  22. $name = $author->getElementsByTagName("name")->item(0);
  23. if ($name) return $name->nodeValue;
  24. $email = $author->getElementsByTagName("email")->item(0);
  25. if ($email) return $email->nodeValue;
  26. if ($author->nodeValue)
  27. return $author->nodeValue;
  28. }
  29. $author = $this->xpath->query("dc:creator", $this->elem)->item(0);
  30. if ($author) {
  31. return $author->nodeValue;
  32. }
  33. }
  34. function get_comments_url() {
  35. //RSS only. Use a query here to avoid namespace clashes (e.g. with slash).
  36. //might give a wrong result if a default namespace was declared (possible with XPath 2.0)
  37. $com_url = $this->xpath->query("comments", $this->elem)->item(0);
  38. if($com_url)
  39. return $com_url->nodeValue;
  40. //Atom Threading Extension (RFC 4685) stuff. Could be used in RSS feeds, so it's in common.
  41. //'text/html' for type is too restrictive?
  42. $com_url = $this->xpath->query("atom:link[@rel='replies' and contains(@type,'text/html')]/@href", $this->elem)->item(0);
  43. if($com_url)
  44. return $com_url->nodeValue;
  45. }
  46. function get_comments_count() {
  47. //also query for ATE stuff here
  48. $query = "slash:comments|thread:total|atom:link[@rel='replies']/@thread:count";
  49. $comments = $this->xpath->query($query, $this->elem)->item(0);
  50. if ($comments) {
  51. return $comments->nodeValue;
  52. }
  53. }
  54. function count_children($node) {
  55. return $node->getElementsByTagName("*")->length;
  56. }
  57. function subtree_or_text($node) {
  58. if ($this->count_children($node) == 0) {
  59. return $node->nodeValue;
  60. } else {
  61. return $node->c14n();
  62. }
  63. }
  64. }