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.
 
 
 
 
 

190 lines
5.3 KiB

  1. <?php
  2. namespace dokuwiki\Feed;
  3. use dokuwiki\ChangeLog\MediaChangeLog;
  4. use dokuwiki\File\MediaFile;
  5. use dokuwiki\Ui\Media\Display;
  6. class FeedMediaProcessor extends FeedItemProcessor
  7. {
  8. /** @inheritdoc */
  9. public function getURL($linkto)
  10. {
  11. switch ($linkto) {
  12. case 'page':
  13. $opt = [
  14. 'image' => $this->getId(),
  15. 'ns' => getNS($this->getId()),
  16. 'rev' => $this->getRev()
  17. ];
  18. break;
  19. case 'rev':
  20. $opt = [
  21. 'image' => $this->getId(),
  22. 'ns' => getNS($this->getId()),
  23. 'rev' => $this->getRev(),
  24. 'tab_details' => 'history'
  25. ];
  26. break;
  27. case 'current':
  28. $opt = [
  29. 'image' => $this->getId(),
  30. 'ns' => getNS($this->getId())
  31. ];
  32. break;
  33. case 'diff':
  34. default:
  35. $opt = [
  36. 'image' => $this->getId(),
  37. 'ns' => getNS($this->getId()),
  38. 'rev' => $this->getRev(),
  39. 'tab_details' => 'history',
  40. 'media_do' => 'diff'
  41. ];
  42. }
  43. return media_managerURL($opt, '&', true);
  44. }
  45. public function getBody($content)
  46. {
  47. switch ($content) {
  48. case 'diff':
  49. case 'htmldiff':
  50. $prev = $this->getPrev();
  51. if ($prev) {
  52. if ($this->isExisting()) {
  53. $src1 = new MediaFile($this->getId(), $prev);
  54. $src2 = new MediaFile($this->getId());
  55. } else {
  56. $src1 = new MediaFile($this->getId(), $prev);
  57. $src2 = null;
  58. }
  59. } else {
  60. $src1 = null;
  61. $src2 = new MediaFile($this->getId());
  62. }
  63. return $this->createDiffTable($src1, $src2);
  64. case 'abstract':
  65. case 'html':
  66. default:
  67. $src = new Display(new MediaFile($this->getId()));
  68. return $this->cleanHTML($src->getPreviewHtml(500, 500));
  69. }
  70. }
  71. /**
  72. * @inheritdoc
  73. * @todo read exif keywords
  74. */
  75. public function getCategory()
  76. {
  77. return (array)getNS($this->getId());
  78. }
  79. /**
  80. * Get the revision timestamp of this page
  81. *
  82. * Note: we only handle most current revisions in feeds, so the revision is usually just the
  83. * lastmodifed timestamp of the page file. However, if the page does not exist, we need to
  84. * determine the revision from the changelog.
  85. * @return int
  86. */
  87. public function getRev()
  88. {
  89. $rev = parent::getRev();
  90. if ($rev) return $rev;
  91. if (media_exists($this->id)) {
  92. $this->data['rev'] = filemtime(mediaFN($this->id));
  93. $this->data['exists'] = true;
  94. } else {
  95. $this->loadRevisions();
  96. }
  97. return $this->data['rev'];
  98. }
  99. /**
  100. * Get the previous revision timestamp of this page
  101. *
  102. * @return int|null The previous revision or null if there is none
  103. */
  104. public function getPrev()
  105. {
  106. if ($this->data['prev'] ?? 0) return $this->data['prev'];
  107. $this->loadRevisions();
  108. return $this->data['prev'];
  109. }
  110. /**
  111. * Does this page exist?
  112. *
  113. * @return bool
  114. */
  115. public function isExisting()
  116. {
  117. if (!isset($this->data['exists'])) {
  118. $this->data['exists'] = media_exists($this->id);
  119. }
  120. return $this->data['exists'];
  121. }
  122. /**
  123. * Load the current and previous revision from the changelog
  124. * @return void
  125. */
  126. protected function loadRevisions()
  127. {
  128. $changelog = new MediaChangeLog($this->id);
  129. $revs = $changelog->getRevisions(0, 2); // FIXME check that this returns the current one correctly
  130. if (!isset($this->data['rev'])) {
  131. // prefer an already set date, only set if missing
  132. // it should usally not happen that neither is available
  133. $this->data['rev'] = $revs[0] ?? 0;
  134. }
  135. // a previous revision might not exist
  136. $this->data['prev'] = $revs[1] ?? null;
  137. }
  138. /**
  139. * Create a table showing the two media files
  140. *
  141. * @param MediaFile|null $src1
  142. * @param MediaFile|null $src2
  143. * @return string
  144. */
  145. protected function createDiffTable($src1, $src2)
  146. {
  147. global $lang;
  148. $content = '<table>';
  149. $content .= '<tr>';
  150. $content .= '<th width="50%">' . ($src1 ? $src1->getRev() : '') . '</th>';
  151. $content .= '<th width="50%">' . $lang['current'] . '</th>';
  152. $content .= '</tr>';
  153. $content .= '<tr>';
  154. $content .= '<td align="center">';
  155. if ($src1) {
  156. $display = new Display($src1);
  157. $display->getPreviewHtml(300, 300);
  158. }
  159. $content .= '</td>';
  160. $content .= '<td align="center">';
  161. if ($src2) {
  162. $display = new Display($src2);
  163. $display->getPreviewHtml(300, 300);
  164. }
  165. $content .= '</td>';
  166. $content .= '</tr>';
  167. $content .= '</table>';
  168. return $this->cleanHTML($content);
  169. }
  170. }