changed git call from https to git readonly
[atutor.git] / mods / wiki / plugins / patchsaving.php
1 <?php
2
3 /*
4    This plugin catches concurrent edits of a page, and lets the 'patch'
5    and 'diff' utilities try to merge the different versions. This will
6    often prevent the "This page version was already saved by someone else"
7    failure message.
8    Please use the GNU diff and patch only. Sometimes the unified output
9    format may be superiour; but this depends on the subjects in your Wiki.
10 */
11
12 define("EWIKI_BIN_DIFF", "/usr/bin/diff");
13 define("EWIKI_BIN_PATCH", "/usr/bin/patch");
14
15 if (function_exists("is_executable") && is_executable(EWIKI_BIN_PATCH) && is_executable(EWIKI_BIN_DIFF)) {
16   $ewiki_plugins["edit_patch"][] = "ewiki_edit_patch";
17 }
18
19
20 function ewiki_edit_patch($id, &$data) {
21
22    $r = false;
23
24    $base = ewiki_database(
25       "GET",
26       array("id"=>$id, "version"=>$_REQUEST["version"])
27    );
28    if (!$base) { 
29      return(false);
30    }
31
32    $fn_base = EWIKI_TMP."/ewiki.base.".md5($base["content"]);
33    $fn_requ = EWIKI_TMP."/ewiki..requ.".md5($_REQUEST["content"]);
34    $fn_patch = EWIKI_TMP."/ewiki.patch.".md5($base["content"])."-".md5($_REQUEST["content"]);
35    $fn_curr = EWIKI_TMP."/ewiki.curr.".md5($data["content"]);
36
37    if ($f = fopen($fn_base, "w")) {
38      fwrite($f, $base["content"]);
39      fclose($f);
40    }
41    else { 
42      return(false);
43    }
44
45    if ($f = fopen($fn_requ, "w")) {
46      fwrite($f, $_REQUEST["content"]);
47      fclose($f);
48    }
49    else { 
50      unlink($fn_base);
51      return(false);
52    }
53
54    if ($f = fopen($fn_curr, "w")) {
55      fwrite($f, $data["content"]);
56      fclose($f);
57    }
58    else { 
59      unlink($fn_base);
60      unlink($fn_requ);
61      return(false);
62    }
63
64    exec("diff -c $fn_base $fn_requ > $fn_patch", $output, $retval);
65    if ($retval) {
66
67       exec("patch $fn_curr $fn_patch", $output, $retval);
68       if (!$retval) {
69
70          $_REQUEST["version"] = $curr["version"];
71          $_REQUEST["content"] = implode("", file($fn_curr));
72          $r = true;
73
74       }
75    }
76
77    unlink($fn_base);
78    unlink($fn_requ);
79    unlink($fn_patch);
80    unlink($fn_curr);
81
82    ewiki_log("patchsaving of {$id}[{$data[version]}] was ".($r?"":"un")."successful", 2);
83
84    return($r);
85 }
86
87
88 ?>