simuledit.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php if (!defined('PmWiki')) exit();
  2. /* Copyright 2004-2015 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. Script maintained by Petko YOTOV www.pmwiki.org/petko
  12. */
  13. array_unshift($EditFunctions,'MergeSimulEdits');
  14. $HTMLStylesFmt['simuledit'] = ".editconflict { color:green;
  15. font-style:italic; margin-top:1.33em; margin-bottom:1.33em; }\n";
  16. function Merge($newtext,$oldtext,$pagetext) {
  17. global $WorkDir,$SysMergeCmd, $SysMergePassthru;
  18. SDV($SysMergeCmd,"/usr/bin/diff3 -L '' -L '' -L '' -m -E");
  19. if (substr($newtext,-1,1)!="\n") $newtext.="\n";
  20. if (substr($oldtext,-1,1)!="\n") $oldtext.="\n";
  21. if (substr($pagetext,-1,1)!="\n") $pagetext.="\n";
  22. $tempnew = tempnam($WorkDir,"new");
  23. $tempold = tempnam($WorkDir,"old");
  24. $temppag = tempnam($WorkDir,"page");
  25. if ($newfp=fopen($tempnew,'w')) { fputs($newfp,$newtext); fclose($newfp); }
  26. if ($oldfp=fopen($tempold,'w')) { fputs($oldfp,$oldtext); fclose($oldfp); }
  27. if ($pagfp=fopen($temppag,'w')) { fputs($pagfp,$pagetext); fclose($pagfp); }
  28. $mergetext = '';
  29. if (IsEnabled($SysMergePassthru, 0)) {
  30. ob_start();
  31. passthru("$SysMergeCmd $tempnew $tempold $temppag");
  32. $mergetext = ob_get_clean();
  33. }
  34. else {
  35. $merge_handle = popen("$SysMergeCmd $tempnew $tempold $temppag",'r');
  36. if ($merge_handle) {
  37. while (!feof($merge_handle)) $mergetext .= fread($merge_handle,4096);
  38. pclose($merge_handle);
  39. }
  40. }
  41. @unlink($tempnew); @unlink($tempold); @unlink($temppag);
  42. return $mergetext;
  43. }
  44. function MergeSimulEdits($pagename,&$page,&$new) {
  45. global $Now, $EnablePost, $MessagesFmt, $WorkDir;
  46. if (@!$_POST['basetime'] || !PageExists($pagename)
  47. || $page['time'] >= $Now
  48. || $_POST['basetime']>=$page['time']
  49. || $page['text'] == $new['text']) return;
  50. $EnablePost = 0;
  51. $old = array();
  52. RestorePage($pagename,$page,$old,"diff:{$_POST['basetime']}");
  53. $text = Merge($new['text'],$old['text'],$page['text']);
  54. if ($text > '') { $new['text'] = $text; $ec = '$[EditConflict]'; }
  55. else $ec = '$[EditWarning]';
  56. XLSDV('en', array(
  57. 'EditConflict' => "The page you are
  58. editing has been modified since you started editing it.
  59. The modifications have been merged into the text below,
  60. you may want to verify the results of the merge before
  61. pressing save. Conflicts the system couldn't resolve are
  62. bracketed by &lt;&lt;&lt;&lt;&lt;&lt;&lt; and
  63. &gt;&gt;&gt;&gt;&gt;&gt;&gt;.",
  64. 'EditWarning' => "The page you are editing has been modified
  65. since you started editing it. If you continue, your
  66. changes will overwrite any changes that others have made."));
  67. $MessagesFmt[] = "<p class='editconflict'>$ec
  68. (<a target='_blank' href='\$PageUrl?action=diff'>$[View changes]</a>)
  69. </p>\n";
  70. }