You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

173 lines
4.3 KiB

  1. <?php
  2. namespace dokuwiki\Feed;
  3. use dokuwiki\Extension\AuthPlugin;
  4. use RuntimeException;
  5. /**
  6. * Accept more or less arbitrary data to represent data to later construct a feed item from.
  7. * Provide lazy loading accessors to all the data we need for feed generation.
  8. */
  9. abstract class FeedItemProcessor
  10. {
  11. /** @var string This page's ID */
  12. protected $id;
  13. /** @var array bag of holding */
  14. protected $data;
  15. /**
  16. * Constructor
  17. *
  18. * @param array $data Needs to have at least an 'id' key
  19. */
  20. public function __construct($data)
  21. {
  22. if (!isset($data['id'])) throw new RuntimeException('Missing ID');
  23. $this->id = cleanID($data['id']);
  24. $this->data = $data;
  25. }
  26. /**
  27. * Get the page ID
  28. *
  29. * @return string
  30. */
  31. public function getId()
  32. {
  33. return $this->id;
  34. }
  35. /**
  36. * Get the revision timestamp of this page
  37. *
  38. * If the input gave us a revision, date or lastmodified already, we trust that it is correct.
  39. *
  40. * Note: we only handle most current revisions in feeds, so the revision is usually just the
  41. * lastmodifed timestamp of the page file. However, if the item does not exist, we need to
  42. * determine the revision from the changelog.
  43. *
  44. * @return int
  45. */
  46. public function getRev()
  47. {
  48. if ($this->data['rev'] ?? 0) return $this->data['rev'];
  49. if (isset($this->data['date'])) {
  50. $this->data['rev'] = (int)$this->data['date'];
  51. }
  52. if (isset($this->data['lastmodified'])) {
  53. $this->data['rev'] = (int)$this->data['lastmodified'];
  54. }
  55. return $this->data['rev'] ?? 0;
  56. }
  57. /**
  58. * Construct the URL for the feed item based on the link_to option
  59. *
  60. * @param string $linkto The link_to option
  61. * @return string URL
  62. */
  63. abstract public function getURL($linkto);
  64. /**
  65. * @return string
  66. */
  67. public function getTitle()
  68. {
  69. return $this->data['title'] ?? noNS($this->getId());
  70. }
  71. /**
  72. * Construct the body of the feed item based on the item_content option
  73. *
  74. * @param string $content The item_content option
  75. * @return string
  76. */
  77. abstract public function getBody($content);
  78. /**
  79. * Get the change summary for this item if any
  80. *
  81. * @return string
  82. */
  83. public function getSummary()
  84. {
  85. return (string)($this->data['sum'] ?? '');
  86. }
  87. /**
  88. * Get the author info for this item
  89. *
  90. * @return string[] [email, author]
  91. */
  92. public function getAuthor()
  93. {
  94. global $conf;
  95. global $auth;
  96. $user = $this->data['user'] ?? '';
  97. $author = 'Anonymous';
  98. $email = 'anonymous@undisclosed.example.com';
  99. if (!$user) return [$email, $author];
  100. $author = $user;
  101. $email = $user . '@undisclosed.example.com';
  102. if ($conf['useacl'] && $auth instanceof AuthPlugin) {
  103. $userInfo = $auth->getUserData($user);
  104. if ($userInfo) {
  105. switch ($conf['showuseras']) {
  106. case 'username':
  107. case 'username_link':
  108. $author = $userInfo['name'];
  109. break;
  110. }
  111. }
  112. }
  113. return [$email, $author];
  114. }
  115. /**
  116. * Get the categories for this item
  117. *
  118. * @return string[]
  119. */
  120. abstract public function getCategory();
  121. /**
  122. * Clean HTML for the use in feeds
  123. *
  124. * @param string $html
  125. * @return string
  126. */
  127. protected function cleanHTML($html)
  128. {
  129. global $conf;
  130. // no TOC in feeds
  131. $html = preg_replace('/(<!-- TOC START -->).*(<!-- TOC END -->)/s', '', $html);
  132. // add alignment for images
  133. $html = preg_replace('/(<img .*?class="medialeft")/s', '\\1 align="left"', $html);
  134. $html = preg_replace('/(<img .*?class="mediaright")/s', '\\1 align="right"', $html);
  135. // make URLs work when canonical is not set, regexp instead of rerendering!
  136. if (!$conf['canonical']) {
  137. $base = preg_quote(DOKU_REL, '/');
  138. $html = preg_replace(
  139. '/(<a href|<img src)="(' . $base . ')/s',
  140. '$1="' . DOKU_URL,
  141. $html
  142. );
  143. }
  144. return $html;
  145. }
  146. }