6a1eeb5f5dd326a11117ead279fbc4a1602134bb
[atutor.git] / mods / wiki / plugins / action / diff_gnu.php
1 <?php
2
3  # This diff plugin utilizes the external GNU diff program to show up
4  # differences between two versions of a WikiPage.
5  # The diff utility is commonly not part of the graphical OS simulators
6  # from Redmond, but you could install the Cygwin environment to make
7  # it available.
8
9
10
11  $ewiki_plugins["action"]["diff"] = "ewiki_page_gnu_diff";
12  $ewiki_config["action_links"]["info"]["diff"] = "diff";
13
14
15
16  function ewiki_page_gnu_diff($id, &$data, $action) {
17
18     #-- different operation modes of GNU diff:
19     $OPTIONS = " -B -u -U 50 ";
20 #   $OPTIONS = " -B ";
21 #   $OPTIONS = " -c ";
22 #   $OPTIONS = " --side-by-side ";
23
24     #-- fetch old wiki source
25     if (($old_ver = ($new_ver = $data["version"]) - 1) > 0)
26     $data0 = ewiki_db::GET($id, $old_ver);
27
28     $o = ewiki_make_title($id, "Differences between version $new_ver and $old_ver of »{$id}«");
29
30     #-- create temporary files from wikipages
31     $file0 = tempnam(EWIKI_TMP, "ewiki.diff.gnu.");
32     $f = fopen($file0, "w");
33     fwrite($f, $data0["content"]);
34     fclose($f);
35     $file1 = tempnam(EWIKI_TMP, "ewiki.diff.gnu.");
36     $f = fopen($file1, "w");
37     fwrite($f, $data["content"]);
38     fclose($f);
39
40     #-- parse thru GNU diff util
41     $fn = addslashes($id);
42     $OPTIONS .= " --label='$fn (version $old_ver)' --label='$fn (version $new_ver)' ";
43     $diff = shell_exec("diff $OPTIONS $file0 $file1");
44
45     #-- remove temporary files
46     unlink($file0);
47     unlink($file1);
48
49     #-- encolor diff output
50     foreach (explode("\n", $diff) as $dl) {
51
52        $str = substr($dl, 1);
53
54        switch (substr($dl, 0, 1)) {
55           case "<":
56           case "-":
57              $o .= "<b>-</b><font color=\"#990000\"> <tt>$str</tt></font><br />\n";
58              break;
59           case ">":
60           case "+":
61              $o .= "<b>+</b><font color=\"#009900\"> <tt>$str</tt></font><br />\n";
62              break;
63           case "*":
64           case "-":
65              break;
66           default:
67              $o .= "<small><tt>$dl</tt></small><br />";
68        }
69
70     }
71
72     return($o);
73  }
74
75
76 ?>