]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/diff.php
Rename private functions without underscores
[SourceForge/phpwiki.git] / lib / diff.php
1 <?php
2
3 // diff.php
4 //
5 // PhpWiki diff output code.
6 //
7 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
8 // You may copy this code freely under the conditions of the GPL.
9 //
10
11 require_once 'lib/difflib.php';
12
13 class _HWLDF_WordAccumulator
14 {
15     function __construct()
16     {
17         $this->_lines = array();
18         $this->_line = false;
19         $this->_group = false;
20         $this->_tag = '~begin';
21     }
22
23     function _flushGroup($new_tag)
24     {
25         if ($this->_group !== false) {
26             if (!$this->_line)
27                 $this->_line = HTML();
28             $this->_line->pushContent($this->_tag
29                 ? new HtmlElement($this->_tag,
30                     $this->_group)
31                 : $this->_group);
32         }
33         $this->_group = '';
34         $this->_tag = $new_tag;
35     }
36
37     function _flushLine($new_tag)
38     {
39         $this->_flushGroup($new_tag);
40         if ($this->_line)
41             $this->_lines[] = $this->_line;
42         $this->_line = HTML();
43     }
44
45     function addWords($words, $tag = '')
46     {
47         if ($tag != $this->_tag)
48             $this->_flushGroup($tag);
49
50         foreach ($words as $word) {
51             // new-line should only come as first char of word.
52             if ($word === "")
53                 continue;
54             if ($word[0] == "\n") {
55                 $this->_group .= " ";
56                 $this->_flushLine($tag);
57                 $word = substr($word, 1);
58             }
59             assert(!strstr($word, "\n"));
60             $this->_group .= $word;
61         }
62     }
63
64     function getLines()
65     {
66         $this->_flushLine('~done');
67         return $this->_lines;
68     }
69 }
70
71 class WordLevelDiff extends MappedDiff
72 {
73     function __construct($orig_lines, $final_lines)
74     {
75         list ($orig_words, $orig_stripped) = $this->_split($orig_lines);
76         list ($final_words, $final_stripped) = $this->_split($final_lines);
77
78         $this->MappedDiff($orig_words, $final_words,
79             $orig_stripped, $final_stripped);
80     }
81
82     function _split($lines)
83     {
84         // FIXME: fix POSIX char class.
85         if (!preg_match_all('/ ( [^\S\n]+ | [[:alnum:]]+ | . ) (?: (?!< \n) [^\S\n])? /xs',
86             implode("\n", $lines),
87             $m)
88         ) {
89             return array(array(''), array(''));
90         }
91         return array($m[0], $m[1]);
92     }
93
94     function orig()
95     {
96         $orig = new _HWLDF_WordAccumulator;
97
98         foreach ($this->edits as $edit) {
99             if ($edit->type == 'copy')
100                 $orig->addWords($edit->orig);
101             elseif ($edit->orig)
102                 $orig->addWords($edit->orig, 'del');
103         }
104         return $orig->getLines();
105     }
106
107     function _final()
108     {
109         $final = new _HWLDF_WordAccumulator;
110
111         foreach ($this->edits as $edit) {
112             if ($edit->type == 'copy')
113                 $final->addWords($edit->final);
114             elseif ($edit->final)
115                 $final->addWords($edit->final, 'ins');
116         }
117         return $final->getLines();
118     }
119 }
120
121 /**
122  * HTML unified diff formatter.
123  *
124  * This class formats a diff into a CSS-based
125  * unified diff format.
126  *
127  * Within groups of changed lines, diffs are highlit
128  * at the character-diff level.
129  */
130 class HtmlUnifiedDiffFormatter extends UnifiedDiffFormatter
131 {
132     /**
133      * @var HtmlElement
134      */
135     public $_top;
136     /**
137      * @var HtmlElement
138      */
139     public $_block;
140
141     function __construct($context_lines = 4)
142     {
143         parent::__construct($context_lines);
144     }
145
146     function _start_diff()
147     {
148         $this->_top = HTML::div(array('class' => 'diff'));
149     }
150
151     function _end_diff()
152     {
153         $val = $this->_top;
154         unset($this->_top);
155         return $val;
156     }
157
158     function _start_block($header)
159     {
160         $this->_block = HTML::div(array('class' => 'block'),
161             HTML::samp($header));
162     }
163
164     function _end_block()
165     {
166         $this->_top->pushContent($this->_block);
167         unset($this->_block);
168     }
169
170     function _lines($lines, $class, $prefix = false, $elem = false)
171     {
172         if (!$prefix)
173             $prefix = HTML::raw('&nbsp;');
174         $div = HTML::div(array('class' => 'difftext'));
175         foreach ($lines as $line) {
176             if ($elem)
177                 $line = new HtmlElement($elem, $line);
178             $div->pushContent(HTML::div(array('class' => $class),
179                 HTML::samp(array('class' => 'prefix'),
180                     $prefix),
181                 $line, HTML::raw('&nbsp;')));
182         }
183         $this->_block->pushContent($div);
184     }
185
186     function _context($lines)
187     {
188         $this->_lines($lines, 'context');
189     }
190
191     function _deleted($lines)
192     {
193         $this->_lines($lines, 'deleted', '-', 'del');
194     }
195
196     function _added($lines)
197     {
198         $this->_lines($lines, 'added', '+', 'ins');
199     }
200
201     function _changed($orig, $final)
202     {
203         $diff = new WordLevelDiff($orig, $final);
204         $this->_lines($diff->orig(), 'original', '-');
205         $this->_lines($diff->_final(), 'final', '+');
206     }
207 }
208
209 /////////////////////////////////////////////////////////////////
210
211 /**
212  * @param string $label
213  * @param WikiDB_PageRevision $rev
214  * @param WikiRequest $request
215  * @param bool $is_current
216  * @return $this|HtmlElement
217  */
218 function PageInfoRow($label, $rev, &$request, $is_current = false)
219 {
220     global $WikiTheme;
221
222     $row = HTML::tr(HTML::td(array('class' => 'align-right'), $label));
223     if ($rev) {
224         $author = $rev->get('author');
225         $dbi = $request->getDbh();
226
227         $iswikipage = (isWikiWord($author) && $dbi->isWikiPage($author));
228         $authorlink = $iswikipage ? WikiLink($author) : $author;
229         $version = $rev->getVersion();
230         $linked_version = WikiLink($rev, 'existing', $version);
231         if ($is_current)
232             $revertbutton = HTML();
233         else
234             $revertbutton = $WikiTheme->makeActionButton(array('action' => 'revert',
235                     'version' => $version),
236                 false, $rev);
237         $row->pushContent(HTML::td(fmt("version %s", $linked_version)),
238             HTML::td($WikiTheme->getLastModifiedMessage($rev,
239                 false)),
240             HTML::td(fmt("by %s", $authorlink)),
241             HTML::td($revertbutton)
242         );
243     } else {
244         $row->pushContent(HTML::td(array('colspan' => '4'), _("None")));
245     }
246     return $row;
247 }
248
249 /**
250  * @param WikiRequest $request
251  */
252 function showDiff(&$request)
253 {
254     $pagename = $request->getArg('pagename');
255     if (is_array($versions = $request->getArg('versions'))) {
256         // Version selection from pageinfo.php display:
257         rsort($versions);
258         list ($version, $previous) = $versions;
259     } else {
260         $version = $request->getArg('version');
261         $previous = $request->getArg('previous');
262     }
263
264     // abort if page doesn't exist
265     $page = $request->getPage();
266     $current = $page->getCurrentRevision(false);
267     if ($current->getVersion() < 1) {
268         $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
269             HTML::p(fmt("Page ā€œ%sā€ does not exist.", WikiLink($pagename, 'unknown'))));
270         require_once 'lib/Template.php';
271         GeneratePage($html, sprintf(_("Diff: %s"), $pagename), false);
272         return; //early return
273     }
274
275     if ($version) {
276         if (!($new = $page->getRevision($version)))
277             NoSuchRevision($request, $page, $version);
278         $new_version = fmt("version %d", $version);
279     } else {
280         $new = $current;
281         $new_version = _("current version");
282     }
283
284     if (preg_match('/^\d+$/', $previous)) {
285         if (!($old = $page->getRevision($previous)))
286             NoSuchRevision($request, $page, $previous);
287         $old_version = fmt("version %d", $previous);
288         $others = array('major', 'minor', 'author');
289     } else {
290         switch ($previous) {
291             case 'author':
292                 $old = $new;
293                 while ($old = $page->getRevisionBefore($old)) {
294                     if ($old->get('author') != $new->get('author'))
295                         break;
296                 }
297                 $old_version = _("revision by previous author");
298                 $others = array('major', 'minor');
299                 break;
300             case 'minor':
301                 $old = $page->getRevisionBefore($new);
302                 $old_version = _("previous revision");
303                 $others = array('major', 'author');
304                 break;
305             default:
306                 $old = $new;
307                 while ($old && $old->get('is_minor_edit'))
308                     $old = $page->getRevisionBefore($old);
309                 if ($old)
310                     $old = $page->getRevisionBefore($old);
311                 $old_version = _("predecessor to the previous major change");
312                 $others = array('minor', 'author');
313                 break;
314         }
315     }
316
317     $new_link = WikiLink($new, '', $new_version);
318     $old_link = $old ? WikiLink($old, '', $old_version) : $old_version;
319     $page_link = WikiLink($page);
320
321     $html = HTML::div(array('class' => 'wikitext', 'id' => 'difftext'),
322         HTML::p(fmt("Differences between %s and %s of %s.",
323             $new_link, $old_link, $page_link)));
324
325     $otherdiffs = HTML::p(_("Other diffs:"));
326     $label = array('major' => _("Previous Major Revision"),
327         'minor' => _("Previous Revision"),
328         'author' => _("Previous Author"));
329     foreach ($others as $other) {
330         $args = array('action' => 'diff', 'previous' => $other);
331         if ($version)
332             $args['version'] = $version;
333         if (count($otherdiffs->getContent()) > 1)
334             $otherdiffs->pushContent(", ");
335         else
336             $otherdiffs->pushContent(" ");
337         $otherdiffs->pushContent(Button($args, $label[$other]));
338     }
339     $html->pushContent($otherdiffs);
340
341     if ($old and $old->getVersion() == 0)
342         $old = false;
343
344     $html->pushContent(HTML::Table(PageInfoRow(_("Newer page:"), $new,
345             $request, empty($version)),
346         PageInfoRow(_("Older page:"), $old,
347             $request, false)));
348
349     if ($new && $old) {
350         $diff = new Diff($old->getContent(), $new->getContent());
351
352         if ($diff->isEmpty()) {
353             $html->pushContent(HTML::hr(),
354                 HTML::p(sprintf(_('Content of versions %1$s and %2$s is identical.'),
355                     $old->getVersion(),
356                     $new->getVersion())));
357             // If two consecutive versions have the same content, it is because the page was
358             // renamed, or metadata changed: ACL, owner, markup.
359             // We give the reason by printing the summary.
360             if (($new->getVersion() - $old->getVersion()) == 1) {
361                 $html->pushContent(HTML::p(sprintf(_('Version %1$s was created because: %2$s'),
362                     $new->getVersion(),
363                     $new->get('summary'))));
364             }
365         } else {
366             $fmt = new HtmlUnifiedDiffFormatter;
367             $html->pushContent($fmt->format($diff));
368         }
369
370         $html->pushContent(HTML::hr(), HTML::h2($new_version));
371         require_once 'lib/BlockParser.php';
372         $html->pushContent(TransformText($new, $pagename));
373     }
374
375     require_once 'lib/Template.php';
376     GeneratePage($html, sprintf(_("Diff: %s"), $pagename), $new);
377 }
378
379 // Local Variables:
380 // mode: php
381 // tab-width: 8
382 // c-basic-offset: 4
383 // c-hanging-comment-ender-p: nil
384 // indent-tabs-mode: nil
385 // End: