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