changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / action / diff.php
1 <?php
2
3 /*
4   this is the "stupid diff", which shows up changes between two
5   saved versions of a WikiPage; even if working very unclean it
6   allows to see what has changed
7   it is accessible through the "info about page" action
8 */
9
10
11
12  $ewiki_plugins["action"]["diff"] = "ewiki_page_stupid_diff";
13  $ewiki_config["action_links"]["info"]["diff"] = "diff";
14
15
16
17  function ewiki_page_stupid_diff($id, $data, $action) {
18
19
20     if ($uu=$GLOBALS["ewiki_diff_versions"]) {
21        list($new_ver, $old_ver) = $uu;
22        $data = ewiki_db::GET($id, $new_ver);
23     }
24     else {
25        $new_ver = $data["version"];
26        $old_ver = $new_ver - 1;
27     }
28     if ($old_ver > 0) {
29        $data0 = ewiki_db::GET($id, $old_ver);
30     }
31
32     $o = ewiki_make_title($id, "Differences between version $new_ver and $old_ver of »{$id}«");
33
34     $o .= ewiki_stupid_diff($data["content"], $data0["content"]);
35
36     return($o);
37  }
38
39
40  function ewiki_stupid_diff($new, $old, $show_unchanged=1, $informational=0) {
41
42     $old = preg_split("/\s*\n/", trim($old));
43     $new = preg_split("/\s*\n/", trim($new));
44
45     $diff_rm = array_diff($old, $new);
46     $diff_add = array_diff($new, $old);
47     if ($informational) {
48        $i = array_intersect($new, $old);
49        if (empty($i)) {
50           if (count($diff_add) >= (6.5 * count(array_unique($diff_rm)))) {
51              $o .= '<div class="note"><b>(overwritten with previous[?] content)</b></div>' . "\n";
52              $diff_add = array();
53           }
54           elseif ($diff_rm) {
55              $o .= '<div class="note"><b>(previous content completely removed)</b></div>' . "\n";
56              $diff_rm = array();
57           }
58        }
59     }
60
61     foreach ($new as $i=>$line) {
62     
63        $i2 = $i;
64        while ($rm = $diff_rm[$i2++]) {
65           $o .= '<div class="del"><b>-</b><font color="#990000"> <tt>' . htmlentities($rm) . "</tt></font></div>\n";
66           unset($diff_rm[$i2-1]);
67        }
68
69        if (in_array($line, $diff_add)) {
70           $o .= '<div class="add"><b>+</b><font color="#009900"> <tt>' . htmlentities($line) . "</tt></font></div>\n";
71        }
72        elseif ($show_unchanged) {
73           $o .= "<div><b>&nbsp;</b> " . htmlentities($line) . "</div>\n";
74        }
75
76     }
77
78     foreach ($diff_rm as $rm) {
79        $o .= '<div class="del"><b>-</b><font color="#990000"> <tt>' . htmlentities($rm) . "</tt></font></div>\n";
80     }
81     
82     return('<div class="diff">' . $o . '</div>');
83  }
84
85
86 ?>