xref: /dokuwiki/inc/Ui/PageDiff.php (revision 63ab9afea47c7d80a8ede45def7caa27e9d03276)
1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\ChangeLog\PageChangeLog;
6use dokuwiki\Form\Form;
7
8/**
9 * DokuWiki PageDiff Interface
10 *
11 * @package dokuwiki\Ui
12 */
13class PageDiff extends Diff
14{
15    protected $text;
16    protected $showIntro;
17    protected $difftype;
18
19    /**
20     * PageDiff Ui constructor
21     *
22     * @param  string $text  when non-empty: compare with this text with most current version
23     * @param  bool   $showIntro display the intro text
24     * @param  string $difftype  diff view type (inline or sidebyside)
25     */
26    public function __construct($text = '', $showIntro = true, $difftype = null)
27    {
28        $this->text      = $text;
29        $this->showIntro = $showIntro;
30
31        // determine diff view type
32        if (isset($difftype)) {
33            $this->difftype  = $difftype;
34        } else {
35            global $INPUT;
36            global $INFO;
37            $this->difftype = $INPUT->str('difftype') ?: get_doku_pref('difftype', $difftype);
38            if (empty($this->difftype) && $INFO['ismobile']) {
39                $this->difftype = 'inline';
40            }
41        }
42        if ($this->difftype !== 'inline') $this->difftype = 'sidebyside';
43    }
44
45    /**
46     * Show diff
47     * between current page version and provided $text
48     * or between the revisions provided via GET or POST
49     *
50     * @author Andreas Gohr <andi@splitbrain.org>
51     *
52     * @return void
53     */
54    public function show()
55    {
56        global $ID;
57        global $REV;
58        global $lang;
59        global $INPUT;
60        global $INFO;
61        $pagelog = new PageChangeLog($ID);
62
63        /*
64         * Determine requested revision(s)
65         */
66        // we're trying to be clever here, revisions to compare can be either
67        // given as rev and rev2 parameters, with rev2 being optional. Or in an
68        // array in rev2.
69        $rev1 = $REV;
70
71        $rev2 = $INPUT->ref('rev2');
72        if (is_array($rev2)) {
73            $rev1 = (int) $rev2[0];
74            $rev2 = (int) $rev2[1];
75
76            if (!$rev1) {
77                $rev1 = $rev2;
78                unset($rev2);
79            }
80        } else {
81            $rev2 = $INPUT->int('rev2');
82        }
83
84        /*
85         * Determine left and right revision, its texts and the header
86         */
87        $r_minor = '';
88        $l_minor = '';
89
90        if ($this->text) { // compare text to the most current revision
91            $l_rev = '';
92            $l_text = rawWiki($ID, '');
93            $l_head = '<a class="wikilink1" href="'. wl($ID) .'">'
94                . $ID .' '. dformat((int) @filemtime(wikiFN($ID))) .'</a> '
95                . $lang['current'];
96
97            $r_rev = '';
98            $r_text = cleanText($this->text);
99            $r_head = $lang['yours'];
100        } else {
101            if ($rev1 && isset($rev2) && $rev2) { // two specific revisions wanted
102                // make sure order is correct (older on the left)
103                if ($rev1 < $rev2) {
104                    $l_rev = $rev1;
105                    $r_rev = $rev2;
106                } else {
107                    $l_rev = $rev2;
108                    $r_rev = $rev1;
109                }
110            } elseif ($rev1) { // single revision given, compare to current
111                $r_rev = '';
112                $l_rev = $rev1;
113            } else { // no revision was given, compare previous to current
114                $r_rev = '';
115                $revs = $pagelog->getRevisions(0, 1);
116                $l_rev = $revs[0];
117                $REV = $l_rev; // store revision back in $REV
118            }
119
120            // when both revisions are empty then the page was created just now
121            if (!$l_rev && !$r_rev) {
122                $l_text = '';
123            } else {
124                $l_text = rawWiki($ID, $l_rev);
125            }
126            $r_text = rawWiki($ID, $r_rev);
127
128            // get header of diff HTML
129            list($l_head, $r_head, $l_minor, $r_minor) = $this->diffHead(
130                $l_rev, $r_rev, null, false, ($this->difftype == 'inline')
131            );
132        }
133
134        /*
135         * Build navigation
136         */
137        $l_nav = '';
138        $r_nav = '';
139        if (!$this->text) {
140            list($l_nav, $r_nav) = $this->diffNavigation($pagelog, $l_rev, $r_rev);
141        }
142        /*
143         * Create diff object and the formatter
144         */
145        $diff = new \Diff(explode("\n", $l_text), explode("\n", $r_text));
146
147        if ($this->difftype == 'inline') {
148            $diffformatter = new \InlineDiffFormatter();
149        } else {
150            $diffformatter = new \TableDiffFormatter();
151        }
152        /*
153         * Display intro
154         */
155        if ($this->showIntro) print p_locale_xhtml('diff');
156
157        /*
158         * Display type and exact reference
159         */
160        if (!$this->text) {
161            print '<div class="diffoptions group">';
162
163            // create the form to select difftype
164            $form = new Form(['action' => wl()]);
165            $form->setHiddenField('id', $ID);
166            $form->setHiddenField('rev2[0]', $l_rev);
167            $form->setHiddenField('rev2[1]', $r_rev);
168            $form->setHiddenField('do', 'diff');
169            $options = array(
170                         'sidebyside' => $lang['diff_side'],
171                         'inline' => $lang['diff_inline']
172            );
173            $input = $form->addDropdown('difftype', $options, $lang['diff_type'])
174                ->val($this->difftype)->addClass('quickselect');
175            $input->useInput(false); // inhibit prefillInput() during toHTML() process
176            $form->addButton('do[diff]', 'Go')->attr('type','submit');
177            print $form->toHTML();
178
179            print '<p>';
180            // link to exactly this view FS#2835
181            print $this->diffViewlink('difflink', $l_rev, ($r_rev ?: $INFO['currentrev']));
182            print '</p>';
183
184            print '</div>'; // .diffoptions
185        }
186
187        /*
188         * Display diff view table
189         */
190        print '<div class="table">';
191        print '<table class="diff diff_'. $this->difftype .'">';
192
193        //navigation and header
194        if ($this->difftype == 'inline') {
195            if (!$this->text) {
196                print '<tr>'
197                    . '<td class="diff-lineheader">-</td>'
198                    . '<td class="diffnav">'. $l_nav .'</td>'
199                    . '</tr>';
200                print '<tr>'
201                    . '<th class="diff-lineheader">-</th>'
202                    . '<th '. $l_minor .'>'. $l_head .'</th>'
203                    .'</tr>';
204            }
205            print '<tr>'
206                . '<td class="diff-lineheader">+</td>'
207                . '<td class="diffnav">'. $r_nav .'</td>'
208                .'</tr>';
209            print '<tr>'
210                . '<th class="diff-lineheader">+</th>'
211                . '<th '. $r_minor .'>'. $r_head .'</th>'
212                . '</tr>';
213        } else {
214            if (!$this->text) {
215                print '<tr>'
216                    . '<td colspan="2" class="diffnav">'. $l_nav .'</td>'
217                    . '<td colspan="2" class="diffnav">'. $r_nav .'</td>'
218                    . '</tr>';
219            }
220            print '<tr>'
221                . '<th colspan="2" '. $l_minor .'>'. $l_head .'</th>'
222                . '<th colspan="2" '. $r_minor .'>'. $r_head .'</th>'
223                . '</tr>';
224        }
225
226        //diff view
227        print $this->insertSoftbreaks($diffformatter->format($diff));
228
229        print '</table>';
230        print '</div>';
231    }
232
233
234    /**
235     * Create html for revision navigation
236     *
237     * @param PageChangeLog $pagelog changelog object of current page
238     * @param int           $l_rev   left revision timestamp
239     * @param int           $r_rev   right revision timestamp
240     * @return string[] html of left and right navigation elements
241     */
242    protected function diffNavigation($pagelog, $l_rev, $r_rev)
243    {
244        global $INFO, $ID;
245
246        // last timestamp is not in changelog, retrieve timestamp from metadata
247        // note: when page is removed, the metadata timestamp is zero
248        if (!$r_rev) {
249            if (isset($INFO['meta']['last_change']['date'])) {
250                $r_rev = $INFO['meta']['last_change']['date'];
251            } else {
252                $r_rev = 0;
253            }
254        }
255
256        //retrieve revisions with additional info
257        list($l_revs, $r_revs) = $pagelog->getRevisionsAround($l_rev, $r_rev);
258        $l_revisions = array();
259        if (!$l_rev) {
260            //no left revision given, add dummy
261            $l_revisions[0]= array('label' => '', 'attrs' => []);
262        }
263        foreach ($l_revs as $rev) {
264            $info = $pagelog->getRevisionInfo($rev);
265            $l_revisions[$rev] = array(
266                'label' => dformat($info['date']) .' '. editorinfo($info['user'], true) .' '. $info['sum'],
267                'attrs' => ['title' => $rev],
268            );
269            if ($r_rev ? $rev >= $r_rev : false) $l_revisions[$rev]['attrs']['disabled'] = 'disabled';
270        }
271        $r_revisions = array();
272        if (!$r_rev) {
273            //no right revision given, add dummy
274            $r_revisions[0] = array('label' => '', 'attrs' => []);
275        }
276        foreach ($r_revs as $rev) {
277            $info = $pagelog->getRevisionInfo($rev);
278            $r_revisions[$rev] = array(
279                'label' => dformat($info['date']) .' '. editorinfo($info['user'], true) .' '. $info['sum'],
280                'attrs' => ['title' => $rev],
281            );
282            if ($rev <= $l_rev) $r_revisions[$rev]['attrs']['disabled'] = 'disabled';
283        }
284
285        //determine previous/next revisions
286        $l_index = array_search($l_rev, $l_revs);
287        $l_prev = $l_revs[$l_index + 1];
288        $l_next = $l_revs[$l_index - 1];
289        if ($r_rev) {
290            $r_index = array_search($r_rev, $r_revs);
291            $r_prev = $r_revs[$r_index + 1];
292            $r_next = $r_revs[$r_index - 1];
293        } else {
294            //removed page
295            if ($l_next) {
296                $r_prev = $r_revs[0];
297            } else {
298                $r_prev = null;
299            }
300            $r_next = null;
301        }
302
303        /*
304         * Left side:
305         */
306        $l_nav = '';
307        //move back
308        if ($l_prev) {
309            $l_nav .= $this->diffViewlink('diffbothprevrev', $l_prev, $r_prev);
310            $l_nav .= $this->diffViewlink('diffprevrev', $l_prev, $r_rev);
311        }
312        //dropdown
313        $form = new Form(['action' => wl()]);
314        $form->setHiddenField('id', $ID);
315        $form->setHiddenField('difftype', $this->difftype);
316        $form->setHiddenField('rev2[1]', $r_rev);
317        $form->setHiddenField('do', 'diff');
318        $input = $form->addDropdown('rev2[0]', $l_revisions)->val($l_rev)->addClass('quickselect');
319        $input->useInput(false); // inhibit prefillInput() during toHTML() process
320        $form->addButton('do[diff]', 'Go')->attr('type','submit');
321        $l_nav .= $form->toHTML();
322        //move forward
323        if ($l_next && ($l_next < $r_rev || !$r_rev)) {
324            $l_nav .= $this->diffViewlink('diffnextrev', $l_next, $r_rev);
325        }
326
327        /*
328         * Right side:
329         */
330        $r_nav = '';
331        //move back
332        if ($l_rev < $r_prev) {
333            $r_nav .= $this->diffViewlink('diffprevrev', $l_rev, $r_prev);
334        }
335        //dropdown
336        $form = new Form(['action' => wl()]);
337        $form->setHiddenField('id', $ID);
338        $form->setHiddenField('rev2[0]', $l_rev);
339        $form->setHiddenField('difftype', $this->difftype);
340        $form->setHiddenField('do', 'diff');
341        $input = $form->addDropdown('rev2[1]', $r_revisions)->val($r_rev)->addClass('quickselect');
342        $input->useInput(false); // inhibit prefillInput() during toHTML() process
343        $form->addButton('do[diff]', 'Go')->attr('type','submit');
344        $r_nav .= $form->toHTML();
345        //move forward
346        if ($r_next) {
347            if ($pagelog->isCurrentRevision($r_next)) {
348                //last revision is diff with current page
349                $r_nav .= $this->diffViewlink('difflastrev', $l_rev);
350            } else {
351                $r_nav .= $this->diffViewlink('diffnextrev', $l_rev, $r_next);
352            }
353        } else {
354            $r_nav .= $this->diffViewlink('diffbothnextrev', $l_next, $r_next);
355        }
356        return array($l_nav, $r_nav);
357    }
358
359    /**
360     * Create html link to a diff view defined by two revisions
361     *
362     * @param string $linktype
363     * @param int $lrev oldest revision
364     * @param int $rrev newest revision or null for diff with current revision
365     * @return string html of link to a diff view
366     */
367    protected function diffViewlink($linktype, $lrev, $rrev = null)
368    {
369        global $ID, $lang;
370        if ($rrev === null) {
371            $urlparam = array(
372                'do' => 'diff',
373                'rev' => $lrev,
374                'difftype' => $this->difftype,
375            );
376        } else {
377            $urlparam = array(
378                'do' => 'diff',
379                'rev2[0]' => $lrev,
380                'rev2[1]' => $rrev,
381                'difftype' => $this->difftype,
382            );
383        }
384        return  '<a class="'. $linktype .'" href="'. wl($ID, $urlparam) .'" title="'. $lang[$linktype] .'">'
385              . '<span>'. $lang[$linktype] .'</span>'
386              . '</a>';
387    }
388
389
390    /**
391     * Insert soft breaks in diff html
392     *
393     * @param string $diffhtml
394     * @return string
395     */
396    public function insertSoftbreaks($diffhtml)
397    {
398        // search the diff html string for both:
399        // - html tags, so these can be ignored
400        // - long strings of characters without breaking characters
401        return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/', function ($match) {
402            // if match is an html tag, return it intact
403            if ($match[0][0] == '<') return $match[0];
404            // its a long string without a breaking character,
405            // make certain characters into breaking characters by inserting a
406            // word break opportunity (<wbr> tag) in front of them.
407            $regex = <<< REGEX
408(?(?=              # start a conditional expression with a positive look ahead ...
409&\#?\\w{1,6};)     # ... for html entities - we don't want to split them (ok to catch some invalid combinations)
410&\#?\\w{1,6};      # yes pattern - a quicker match for the html entity, since we know we have one
411|
412[?/,&\#;:]         # no pattern - any other group of 'special' characters to insert a breaking character after
413)+                 # end conditional expression
414REGEX;
415            return preg_replace('<'.$regex.'>xu', '\0<wbr>', $match[0]);
416        }, $diffhtml);
417    }
418
419}
420