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