You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

113 lines
4.1 KiB

  1. <?php
  2. /**
  3. * Move Plugin Page Rewrite Functionality
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Andreas Gohr <gohr@cosmocode.de>
  7. */
  8. // must be run within Dokuwiki
  9. if(!defined('DOKU_INC')) die();
  10. /**
  11. * Class action_plugin_move_rewrite
  12. */
  13. class action_plugin_move_rewrite extends DokuWiki_Action_Plugin {
  14. /**
  15. * Register event handlers.
  16. *
  17. * @param Doku_Event_Handler $controller The plugin controller
  18. */
  19. public function register(Doku_Event_Handler $controller) {
  20. $controller->register_hook('IO_WIKIPAGE_READ', 'AFTER', $this, 'handle_read', array());
  21. $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache', array());
  22. }
  23. /**
  24. * Rewrite pages when they are read and they need to be updated.
  25. *
  26. * @param Doku_Event $event The event object
  27. * @param mixed $param Optional parameters (not used)
  28. */
  29. function handle_read(Doku_Event $event, $param) {
  30. global $ACT, $conf;
  31. static $stack = array();
  32. // handle only reads of the current revision
  33. if($event->data[3]) return;
  34. // only rewrite if not in move already
  35. $save = true;
  36. if(helper_plugin_move_rewrite::isLocked()) {
  37. $save = false;
  38. }
  39. $id = $event->data[2];
  40. if($event->data[1]) $id = $event->data[1] . ':' . $id;
  41. if(!$id) {
  42. // try to reconstruct the id from the filename
  43. $path = $event->data[0][0];
  44. if(strpos($path, $conf['datadir']) === 0) {
  45. $path = substr($path, strlen($conf['datadir']) + 1);
  46. $id = pathID($path);
  47. }
  48. }
  49. if(isset($stack[$id])) return;
  50. // Don't change the page when the user is currently changing the page content or the page is locked
  51. $forbidden_actions = array('save', 'preview', 'recover', 'revert');
  52. if((isset($ACT) && (
  53. in_array($ACT, $forbidden_actions) || (is_array($ACT) && in_array(key($ACT), $forbidden_actions)
  54. )))
  55. // checklock checks if the page lock hasn't expired and the page hasn't been locked by another user
  56. // the file exists check checks if the page is reported unlocked if a lock exists which means that
  57. // the page is locked by the current user
  58. || checklock($id) !== false || @file_exists(wikiLockFN($id))
  59. ) return;
  60. /** @var helper_plugin_move_rewrite $helper */
  61. $helper = plugin_load('helper', 'move_rewrite', true);
  62. if(!is_null($helper)) {
  63. $stack[$id] = true;
  64. $event->result = $helper->rewritePage($id, $event->result, $save);
  65. unset($stack[$id]);
  66. }
  67. }
  68. /**
  69. * Handle the cache events, it looks if a page needs to be rewritten so it can expire the cache of the page
  70. *
  71. * @param Doku_Event $event The even object
  72. * @param mixed $param Optional parameters (not used)
  73. */
  74. function handle_cache(Doku_Event $event, $param) {
  75. global $conf;
  76. /** @var $cache cache_parser */
  77. $cache = $event->data;
  78. $id = $cache->page;
  79. if(!$id) {
  80. // try to reconstruct the id from the filename
  81. $path = $cache->file;
  82. if(strpos($path, $conf['datadir']) === 0) {
  83. $path = substr($path, strlen($conf['datadir']) + 1);
  84. $id = pathID($path);
  85. }
  86. }
  87. if($id) {
  88. /** @var helper_plugin_move_rewrite $helper */
  89. $helper = $this->loadHelper('move_rewrite');
  90. if(!is_null($helper)) {
  91. $meta = $helper->getMoveMeta($id);
  92. if($meta && ($meta['pages'] || $meta['media'])) {
  93. $file = wikiFN($id, '', false);
  94. if(is_writable($file))
  95. $cache->depends['purge'] = true;
  96. else // FIXME: print error here or fail silently?
  97. msg('Error: Page ' . hsc($id) . ' needs to be rewritten because of page renames but is not writable.', -1);
  98. }
  99. }
  100. }
  101. }
  102. }