syspatch.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /* Copyright 2004 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 script contains the Patch function that existed in versions
  8. of PmWiki prior to 0.6.18. This function is used to restore
  9. previous versions of a page from a page's history, and makes use
  10. of an external call to patch(1) to recover the page.
  11. The use of the external patch(1) program has some potential
  12. disadvantages:
  13. 1. It requires a call to an external program
  14. 2. It sometimes leaves temporary files in the wiki.d/ directory
  15. 3. It doesn't always work right for normal diff output
  16. 4. The repeated reading/writing of files to disk may be
  17. slower than a PHP-based patch
  18. However, the use of external patch(1) may have some advantages in
  19. speed and robustness in handling a variety of diff output formats,
  20. therefore this module is included as an option.
  21. To enable the Patch functionality contained here, simply add the line
  22. include_once('scripts/syspatch.php');
  23. to the local/config.php file. This will return PmWiki to using
  24. the external patch utility and the algorithm in versions of PmWiki
  25. prior to 0.6.18.
  26. */
  27. SDV($PatchFunction,'SysPatch');
  28. SDV($SysPatchCmd,'/usr/bin/patch --silent');
  29. function SysPatch($page,$restore) {
  30. global $WikiDir,$SysPatchCmd;
  31. Lock(2);
  32. $txtfile = tempnam($WikiDir,"txt");
  33. $patfile = tempnam($WikiDir,"pat");
  34. if ($txtfp = fopen($txtfile,"w")) {
  35. fputs($txtfp,$page['text']);
  36. fclose($txtfp);
  37. }
  38. krsort($page); reset($page);
  39. foreach($page as $k=>$v) {
  40. if ($k < $restore) break;
  41. if (!preg_match('/^diff:/',$k)) continue;
  42. if ($patfp = fopen($patfile,"w")) {
  43. fputs($patfp,$v);
  44. fclose($patfp);
  45. }
  46. system("$SysPatchCmd $txtfile $patfile");
  47. }
  48. $text = implode('',file($txtfile));
  49. @unlink($txtfile); @unlink($patfile);
  50. return $text;
  51. }
  52. ?>