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.
 
 
 
 
 

327 lines
10 KiB

  1. <?php
  2. namespace dokuwiki\Ui;
  3. use dokuwiki\ChangeLog\MediaChangeLog;
  4. use dokuwiki\ChangeLog\RevisionInfo;
  5. use dokuwiki\Form\Form;
  6. use InvalidArgumentException;
  7. use JpegMeta;
  8. /**
  9. * DokuWiki MediaDiff Interface
  10. *
  11. * @package dokuwiki\Ui
  12. */
  13. class MediaDiff extends Diff
  14. {
  15. /* @var MediaChangeLog */
  16. protected $changelog;
  17. /* @var RevisionInfo older revision */
  18. protected $RevInfo1;
  19. /* @var RevisionInfo newer revision */
  20. protected $RevInfo2;
  21. /* @var bool */
  22. protected $is_img;
  23. /**
  24. * MediaDiff Ui constructor
  25. *
  26. * @param string $id media id
  27. */
  28. public function __construct($id)
  29. {
  30. if (!isset($id)) {
  31. throw new InvalidArgumentException('media id should not be empty!');
  32. }
  33. // init preference
  34. $this->preference['fromAjax'] = false; // see dokuwiki\Ajax::callMediadiff()
  35. $this->preference['showIntro'] = false;
  36. $this->preference['difftype'] = 'both'; // diff view type: both, opacity or portions
  37. parent::__construct($id);
  38. }
  39. /** @inheritdoc */
  40. protected function setChangeLog()
  41. {
  42. $this->changelog = new MediaChangeLog($this->id);
  43. }
  44. /**
  45. * Handle requested revision(s) and diff view preferences
  46. *
  47. * @return void
  48. */
  49. protected function handle()
  50. {
  51. global $INPUT;
  52. // retrieve requested rev or rev2
  53. parent::handle();
  54. // requested diff view type
  55. if ($INPUT->has('difftype')) {
  56. $this->preference['difftype'] = $INPUT->str('difftype');
  57. }
  58. }
  59. /**
  60. * Prepare revision info of comparison pair
  61. */
  62. protected function preProcess()
  63. {
  64. $changelog =& $this->changelog;
  65. // create revision info object for older and newer sides
  66. // RevInfo1 : older, left side
  67. // RevInfo2 : newer, right side
  68. $changelogRev1 = $changelog->getRevisionInfo($this->rev1);
  69. $changelogRev2 = $changelog->getRevisionInfo($this->rev2);
  70. $this->RevInfo1 = new RevisionInfo($changelogRev1);
  71. $this->RevInfo2 = new RevisionInfo($changelogRev2);
  72. $this->is_img = preg_match('/\.(jpe?g|gif|png)$/', $this->id);
  73. foreach ([$this->RevInfo1, $this->RevInfo2] as $RevInfo) {
  74. $isCurrent = $changelog->isCurrentRevision($RevInfo->val('date'));
  75. $RevInfo->isCurrent($isCurrent);
  76. if ($this->is_img) {
  77. $rev = $isCurrent ? '' : $RevInfo->val('date');
  78. $meta = new JpegMeta(mediaFN($this->id, $rev));
  79. // get image width and height for the media manager preview panel
  80. $RevInfo->append([
  81. 'previewSize' => media_image_preview_size($this->id, $rev, $meta)
  82. ]);
  83. }
  84. }
  85. // re-check image, ensure minimum image width for showImageDiff()
  86. $this->is_img = ($this->is_img
  87. && ($this->RevInfo1->val('previewSize')[0] ?? 0) >= 30
  88. && ($this->RevInfo2->val('previewSize')[0] ?? 0) >= 30
  89. );
  90. // adjust requested diff view type
  91. if (!$this->is_img) {
  92. $this->preference['difftype'] = 'both';
  93. }
  94. }
  95. /**
  96. * Shows difference between two revisions of media
  97. *
  98. * @author Kate Arzamastseva <pshns@ukr.net>
  99. */
  100. public function show()
  101. {
  102. global $conf;
  103. $ns = getNS($this->id);
  104. $auth = auth_quickaclcheck("$ns:*");
  105. if ($auth < AUTH_READ || !$this->id || !$conf['mediarevisions']) return;
  106. // retrieve form parameters: rev, rev2, difftype
  107. $this->handle();
  108. // prepare revision info of comparison pair
  109. $this->preProcess();
  110. // display intro
  111. if ($this->preference['showIntro']) echo p_locale_xhtml('diff');
  112. // print form to choose diff view type
  113. if ($this->is_img && !$this->preference['fromAjax']) {
  114. $this->showDiffViewSelector();
  115. echo '<div id="mediamanager__diff" >';
  116. }
  117. switch ($this->preference['difftype']) {
  118. case 'opacity':
  119. case 'portions':
  120. $this->showImageDiff();
  121. break;
  122. case 'both':
  123. default:
  124. $this->showFileDiff();
  125. break;
  126. }
  127. if ($this->is_img && !$this->preference['fromAjax']) {
  128. echo '</div>';
  129. }
  130. }
  131. /**
  132. * Print form to choose diff view type
  133. * the dropdown is to be added through JavaScript, see lib/scripts/media.js
  134. */
  135. protected function showDiffViewSelector()
  136. {
  137. // use timestamp for current revision, date may be false when revisions < 2
  138. [$rev1, $rev2] = [(int)$this->RevInfo1->val('date'), (int)$this->RevInfo2->val('date')];
  139. echo '<div class="diffoptions group">';
  140. $form = new Form([
  141. 'id' => 'mediamanager__form_diffview',
  142. 'action' => media_managerURL([], '&'),
  143. 'method' => 'get',
  144. 'class' => 'diffView',
  145. ]);
  146. $form->addTagOpen('div')->addClass('no');
  147. $form->setHiddenField('sectok', null);
  148. $form->setHiddenField('mediado', 'diff');
  149. $form->setHiddenField('rev2[0]', $rev1);
  150. $form->setHiddenField('rev2[1]', $rev2);
  151. $form->addTagClose('div');
  152. echo $form->toHTML();
  153. echo '</div>'; // .diffoptions
  154. }
  155. /**
  156. * Prints two images side by side
  157. * and slider
  158. *
  159. * @author Kate Arzamastseva <pshns@ukr.net>
  160. */
  161. protected function showImageDiff()
  162. {
  163. $rev1 = $this->RevInfo1->isCurrent() ? '' : $this->RevInfo1->val('date');
  164. $rev2 = $this->RevInfo2->isCurrent() ? '' : $this->RevInfo2->val('date');
  165. // diff view type: opacity or portions
  166. $type = $this->preference['difftype'];
  167. // adjust image width, right side (newer) has priority
  168. $rev1Size = $this->RevInfo1->val('previewSize');
  169. $rev2Size = $this->RevInfo2->val('previewSize');
  170. if ($rev1Size != $rev2Size) {
  171. if ($rev2Size[0] > $rev1Size[0]) {
  172. $rev1Size = $rev2Size;
  173. }
  174. }
  175. $rev1Src = ml($this->id, ['rev' => $rev1, 'h' => $rev1Size[1], 'w' => $rev1Size[0]]);
  176. $rev2Src = ml($this->id, ['rev' => $rev2, 'h' => $rev1Size[1], 'w' => $rev1Size[0]]);
  177. // slider
  178. echo '<div class="slider" style="max-width: ' . ($rev1Size[0] - 20) . 'px;" ></div>';
  179. // two images in divs
  180. echo '<div class="imageDiff ' . $type . '">';
  181. echo '<div class="image1" style="max-width: ' . $rev1Size[0] . 'px;">';
  182. echo '<img src="' . $rev1Src . '" alt="" />';
  183. echo '</div>';
  184. echo '<div class="image2" style="max-width: ' . $rev1Size[0] . 'px;">';
  185. echo '<img src="' . $rev2Src . '" alt="" />';
  186. echo '</div>';
  187. echo '</div>';
  188. }
  189. /**
  190. * Shows difference between two revisions of media file
  191. *
  192. * @author Kate Arzamastseva <pshns@ukr.net>
  193. */
  194. protected function showFileDiff()
  195. {
  196. global $lang;
  197. $ns = getNS($this->id);
  198. $auth = auth_quickaclcheck("$ns:*");
  199. $rev1 = $this->RevInfo1->isCurrent() ? '' : (int)$this->RevInfo1->val('date');
  200. $rev2 = $this->RevInfo2->isCurrent() ? '' : (int)$this->RevInfo2->val('date');
  201. // revision title
  202. $rev1Title = trim($this->RevInfo1->showRevisionTitle() . ' ' . $this->RevInfo1->showCurrentIndicator());
  203. $rev1Summary = ($this->RevInfo1->val('date'))
  204. ? $this->RevInfo1->showEditSummary() . ' ' . $this->RevInfo1->showEditor()
  205. : '';
  206. $rev2Title = trim($this->RevInfo2->showRevisionTitle() . ' ' . $this->RevInfo2->showCurrentIndicator());
  207. $rev2Summary = ($this->RevInfo2->val('date'))
  208. ? $this->RevInfo2->showEditSummary() . ' ' . $this->RevInfo2->showEditor()
  209. : '';
  210. $rev1Meta = new JpegMeta(mediaFN($this->id, $rev1));
  211. $rev2Meta = new JpegMeta(mediaFN($this->id, $rev2));
  212. // display diff view table
  213. echo '<div class="table">';
  214. echo '<table>';
  215. echo '<tr>';
  216. echo '<th>' . $rev1Title . ' ' . $rev1Summary . '</th>';
  217. echo '<th>' . $rev2Title . ' ' . $rev2Summary . '</th>';
  218. echo '</tr>';
  219. echo '<tr class="image">';
  220. echo '<td>';
  221. media_preview($this->id, $auth, $rev1, $rev1Meta); // $auth not used in media_preview()?
  222. echo '</td>';
  223. echo '<td>';
  224. media_preview($this->id, $auth, $rev2, $rev2Meta);
  225. echo '</td>';
  226. echo '</tr>';
  227. echo '<tr class="actions">';
  228. echo '<td>';
  229. media_preview_buttons($this->id, $auth, $rev1); // $auth used in media_preview_buttons()
  230. echo '</td>';
  231. echo '<td>';
  232. media_preview_buttons($this->id, $auth, $rev2);
  233. echo '</td>';
  234. echo '</tr>';
  235. $rev1Tags = media_file_tags($rev1Meta);
  236. $rev2Tags = media_file_tags($rev2Meta);
  237. // FIXME rev2Tags-only stuff ignored
  238. foreach ($rev1Tags as $key => $tag) {
  239. if ($tag['value'] != $rev2Tags[$key]['value']) {
  240. $rev2Tags[$key]['highlighted'] = true;
  241. $rev1Tags[$key]['highlighted'] = true;
  242. } elseif (!$tag['value'] || !$rev2Tags[$key]['value']) {
  243. unset($rev2Tags[$key]);
  244. unset($rev1Tags[$key]);
  245. }
  246. }
  247. echo '<tr>';
  248. foreach ([$rev1Tags, $rev2Tags] as $tags) {
  249. echo '<td>';
  250. echo '<dl class="img_tags">';
  251. foreach ($tags as $tag) {
  252. $value = cleanText($tag['value']);
  253. if (!$value) $value = '-';
  254. echo '<dt>' . $lang[$tag['tag'][1]] . '</dt>';
  255. echo '<dd>';
  256. if (!empty($tag['highlighted'])) echo '<strong>';
  257. if ($tag['tag'][2] == 'date') {
  258. echo dformat($value);
  259. } else {
  260. echo hsc($value);
  261. }
  262. if (!empty($tag['highlighted'])) echo '</strong>';
  263. echo '</dd>';
  264. }
  265. echo '</dl>';
  266. echo '</td>';
  267. }
  268. echo '</tr>';
  269. echo '</table>';
  270. echo '</div>';
  271. }
  272. }