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