MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / wiki / ewiki / plugins / patchsaving.php
blob9cec59c524436565e04547ba60010436b753e959
1 <?php
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.
12 define("EWIKI_BIN_DIFF", "/usr/bin/diff");
13 define("EWIKI_BIN_PATCH", "/usr/bin/patch");
15 if (function_exists("is_executable") && is_executable(EWIKI_BIN_PATCH) && is_executable(EWIKI_BIN_DIFF)) {
16 $ewiki_plugins["edit_patch"][] = "ewiki_edit_patch";
20 function ewiki_edit_patch($id, &$data) {
22 $r = false;
24 $base = ewiki_database(
25 "GET",
26 array("id"=>$id, "version"=>$_REQUEST["version"])
28 if (!$base) {
29 return(false);
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"]);
37 if ($f = fopen($fn_base, "w")) {
38 fwrite($f, $base["content"]);
39 fclose($f);
41 else {
42 return(false);
45 if ($f = fopen($fn_requ, "w")) {
46 fwrite($f, $_REQUEST["content"]);
47 fclose($f);
49 else {
50 unlink($fn_base);
51 return(false);
54 if ($f = fopen($fn_curr, "w")) {
55 fwrite($f, $data["content"]);
56 fclose($f);
58 else {
59 unlink($fn_base);
60 unlink($fn_requ);
61 return(false);
64 exec("diff -c $fn_base $fn_requ > $fn_patch", $output, $retval);
65 if ($retval) {
67 exec("patch $fn_curr $fn_patch", $output, $retval);
68 if (!$retval) {
70 $_REQUEST["version"] = $curr["version"];
71 $_REQUEST["content"] = implode("", file($fn_curr));
72 $r = true;
77 unlink($fn_base);
78 unlink($fn_requ);
79 unlink($fn_patch);
80 unlink($fn_curr);
82 return($r);