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