simuledit.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2006 Patrick R. Michaud (pmichaud@pobox.com)
  3. This file is part of PmWiki; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published
  5. by the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version. See pmwiki.php for full details.
  7. This file enables merging of concurrent edits, using the "diff3"
  8. program available on most Unix systems to merge the edits. If
  9. diff3 is not available or you'd like to use a different command,
  10. then set $SysMergeCmd accordingly.
  11. */
  12. array_unshift($EditFunctions,'MergeSimulEdits');
  13. $HTMLStylesFmt['simuledit'] = ".editconflict { color:green;
  14. font-style:italic; margin-top:1.33em; margin-bottom:1.33em; }\n";
  15. function Merge($newtext,$oldtext,$pagetext) {
  16. global $WorkDir,$SysMergeCmd;
  17. SDV($SysMergeCmd,"/usr/bin/diff3 -L '' -L '' -L '' -m -E");
  18. if (substr($newtext,-1,1)!="\n") $newtext.="\n";
  19. if (substr($oldtext,-1,1)!="\n") $oldtext.="\n";
  20. if (substr($pagetext,-1,1)!="\n") $pagetext.="\n";
  21. $tempnew = tempnam($WorkDir,"new");
  22. $tempold = tempnam($WorkDir,"old");
  23. $temppag = tempnam($WorkDir,"page");
  24. if ($newfp=fopen($tempnew,'w')) { fputs($newfp,$newtext); fclose($newfp); }
  25. if ($oldfp=fopen($tempold,'w')) { fputs($oldfp,$oldtext); fclose($oldfp); }
  26. if ($pagfp=fopen($temppag,'w')) { fputs($pagfp,$pagetext); fclose($pagfp); }
  27. $mergetext = '';
  28. $merge_handle = popen("$SysMergeCmd $tempnew $tempold $temppag",'r');
  29. if ($merge_handle) {
  30. while (!feof($merge_handle)) $mergetext .= fread($merge_handle,4096);
  31. pclose($merge_handle);
  32. }
  33. @unlink($tempnew); @unlink($tempold); @unlink($temppag);
  34. return $mergetext;
  35. }
  36. function MergeSimulEdits($pagename,&$page,&$new) {
  37. global $Now, $EnablePost, $MessagesFmt, $WorkDir;
  38. if (@!$_POST['basetime'] || !PageExists($pagename)
  39. || $page['time'] >= $Now
  40. || $_POST['basetime']>=$page['time']
  41. || $page['text'] == $new['text']) return;
  42. $EnablePost = 0;
  43. $old = array();
  44. RestorePage($pagename,$page,$old,"diff:{$_POST['basetime']}");
  45. $text = Merge($new['text'],$old['text'],$page['text']);
  46. if ($text > '') { $new['text'] = $text; $ec = '$[EditConflict]'; }
  47. else $ec = '$[EditWarning]';
  48. XLSDV('en', array(
  49. 'EditConflict' => "The page you are
  50. editing has been modified since you started editing it.
  51. The modifications have been merged into the text below,
  52. you may want to verify the results of the merge before
  53. pressing save. Conflicts the system couldn't resolve are
  54. bracketed by &lt;&lt;&lt;&lt;&lt;&lt;&lt; and
  55. &gt;&gt;&gt;&gt;&gt;&gt;&gt;.",
  56. 'EditWarning' => "The page you are editing has been modified
  57. since you started editing it. If you continue, your
  58. changes will overwrite any changes that others have made."));
  59. $MessagesFmt[] = "<p class='editconflict'>$ec
  60. (<a target='_blank' href='\$PageUrl?action=diff'>$[View changes]</a>)
  61. </p>\n";
  62. }