action.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * DokuWiki Plugin pageredirect (Action Component)
  4. *
  5. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  6. * @author Elan Ruusamäe <glen@delfi.ee>
  7. * @author David Lorentsen <zyberdog@quakenet.org>
  8. */
  9. // must be run within Dokuwiki
  10. if(!defined('DOKU_INC')) die();
  11. class action_plugin_pageredirect extends DokuWiki_Action_Plugin {
  12. /**
  13. * Registers a callback function for a given event
  14. *
  15. * @param Doku_Event_Handler $controller DokuWiki's event controller object
  16. */
  17. public function register(Doku_Event_Handler $controller) {
  18. /* @see action_plugin_pageredirect::handle_dokuwiki_started() */
  19. $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'handle_dokuwiki_started');
  20. /* @see action_plugin_pageredirect::handle_parser_metadata_render() */
  21. $controller->register_hook('PARSER_METADATA_RENDER', 'BEFORE', $this, 'handle_parser_metadata_render');
  22. // This plugin goes first, PR#555, requires dokuwiki 2014-05-05 (Ponder Stibbons)
  23. /* @see action_plugin_pageredirect::handle_tpl_act_render() */
  24. $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_tpl_act_render', null, PHP_INT_MIN);
  25. $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'handle_indexer');
  26. // Handle move plugin
  27. $controller->register_hook('PLUGIN_MOVE_HANDLERS_REGISTER', 'BEFORE', $this, 'handle_move_register');
  28. }
  29. public function handle_dokuwiki_started(&$event, $param) {
  30. global $ID, $ACT, $REV;
  31. // skip when looking page history or action is not 'show'
  32. if(($ACT != 'show' && $ACT != '') || $REV) {
  33. return;
  34. }
  35. $metadata = $this->get_metadata($ID);
  36. // return if no redirection data
  37. if(!$metadata) {
  38. return;
  39. }
  40. list($page, $is_external) = $metadata;
  41. // return if external redirect is not allowed
  42. if($is_external && !$this->getConf('allow_external')) {
  43. return;
  44. }
  45. global $INPUT;
  46. $redirect = $INPUT->get->str('redirect', '0');
  47. // return if redirection is temporarily disabled,
  48. // or we have been redirected 5 times in a row
  49. if($redirect == 'no' || $redirect > 4) {
  50. return;
  51. }
  52. $redirect = (int)$redirect+1;
  53. // verify metadata currency
  54. // FIXME: why
  55. if(@filemtime(metaFN($ID, '.meta')) < @filemtime(wikiFN($ID))) {
  56. throw new Exception('should not get here');
  57. return;
  58. }
  59. // preserve #section from $page
  60. list($page, $section) = explode('#', $page, 2);
  61. if(isset($section)) {
  62. $section = '#' . $section;
  63. } else {
  64. $section = '';
  65. }
  66. // prepare link for internal redirects, keep external targets
  67. if(!$is_external) {
  68. $page = wl($page, array('redirect' => $redirect), true, '&');
  69. if($this->getConf('show_note')) {
  70. $this->flash_message($ID);
  71. }
  72. // add anchor if not external redirect
  73. $page .= $section;
  74. }
  75. $this->redirect($page);
  76. }
  77. public function handle_tpl_act_render(&$event, $param) {
  78. global $ACT;
  79. // handle on do=show
  80. if($ACT != 'show' && $ACT != '') {
  81. return true;
  82. }
  83. if($this->getConf('show_note')) {
  84. $this->render_flash();
  85. }
  86. return true;
  87. }
  88. public function handle_parser_metadata_render(&$event, $param) {
  89. if(isset($event->data->meta['relation'])) {
  90. // FIXME: why is this needed here?!
  91. unset($event->data->meta['relation']['isreplacedby']);
  92. }
  93. }
  94. public function handle_indexer(Doku_Event $event, $param) {
  95. $new_references = array();
  96. foreach ($event->data['metadata']['relation_references'] as $target) {
  97. $redirect_target = $this->get_metadata($target);
  98. if ($redirect_target) {
  99. list($page, $is_external) = $redirect_target;
  100. if (!$is_external) {
  101. $new_references[] = $page;
  102. }
  103. }
  104. }
  105. if (count($new_references) > 0) {
  106. $event->data['metadata']['relation_references'] = array_unique(array_merge($new_references, $event->data['metadata']['relation_references']));
  107. }
  108. // FIXME: if the currently indexed page contains a redirect, all pages pointing to it need a new backlink entry!
  109. // Note that these entries need to be added for every source page separately.
  110. // An alternative could be to force re-indexing of all source pages by removing their ".indexed" file but this will only happen when they are visited.
  111. }
  112. /**
  113. * remember to show note about being redirected from another page
  114. * @param string $ID page id from where the redirect originated
  115. */
  116. private function flash_message($ID) {
  117. if(headers_sent()) {
  118. // too late to do start session
  119. // and following code requires session
  120. return;
  121. }
  122. session_start();
  123. $_SESSION[DOKU_COOKIE]['redirect'] = $ID;
  124. }
  125. /**
  126. * show note about being redirected from another page
  127. */
  128. private function render_flash() {
  129. global $INPUT;
  130. $redirect = $INPUT->get->str('redirect');
  131. // loop counter
  132. if($redirect <= 0 || $redirect > 5) {
  133. return;
  134. }
  135. $ID = isset($_SESSION[DOKU_COOKIE]['redirect']) ? $_SESSION[DOKU_COOKIE]['redirect'] : null;
  136. if(!$ID) {
  137. return;
  138. }
  139. unset($_SESSION[DOKU_COOKIE]['redirect']);
  140. $page = cleanID($ID);
  141. $use_heading = useHeading('navigation') && p_get_first_heading($page);
  142. $title = hsc($use_heading ? p_get_first_heading($page) : $page);
  143. $url = wl($page, array('redirect' => 'no'), true, '&');
  144. $link = '<a href="' . $url . '" class="wikilink1" title="' . $page . '">' . $title . '</a>';
  145. echo '<div class="noteredirect">' . sprintf($this->getLang('redirected_from'), $link) . '</div><br/>';
  146. }
  147. private function get_metadata($ID) {
  148. // make sure we always get current metadata, but simple cache logic (i.e. render when page is newer than metadata) is enough
  149. $metadata = p_get_metadata($ID, 'relation isreplacedby', METADATA_RENDER_USING_SIMPLE_CACHE|METADATA_RENDER_UNLIMITED);
  150. // legacy compat
  151. if(is_string($metadata)) {
  152. $metadata = array($metadata);
  153. }
  154. return $metadata;
  155. }
  156. /**
  157. * Redirect to url.
  158. * @param string $url
  159. */
  160. private function redirect($url) {
  161. header("HTTP/1.1 301 Moved Permanently");
  162. send_redirect($url);
  163. }
  164. public function handle_move_register(Doku_Event $event, $params) {
  165. $event->data['handlers']['pageredirect'] = array($this, 'rewrite_redirect');
  166. }
  167. public function rewrite_redirect($match, $state, $pos, $plugin, helper_plugin_move_handler $handler) {
  168. $metadata = $this->get_metadata($ID);
  169. if ($metadata[1]) return $match; // Fail-safe for external redirection (Do not rewrite)
  170. $match = trim($match);
  171. if (substr($match, 0, 1) == "~") {
  172. // "~~REDIRECT>pagename#anchor~~" pattern
  173. // Strip syntax
  174. $match = substr($match, 2, strlen($match) - 4);
  175. list($syntax, $src, $anchor) = array_pad(preg_split("/>|#/", $match), 3, "");
  176. // Resolve new source.
  177. if (method_exists($handler, 'adaptRelativeId')) {
  178. $new_src = $handler->adaptRelativeId($src);
  179. } else {
  180. $new_src = $handler->resolveMoves($src, 'page');
  181. $new_src = $handler->relativeLink($src, $new_src, 'page');
  182. }
  183. $result = "~~".$syntax.">".$new_src;
  184. if (!empty($anchor)) $result .= "#".$anchor;
  185. $result .= "~~";
  186. return $result;
  187. } else if (substr($match, 0, 1) == "#") {
  188. // "#REDIRECT pagename#anchor" pattern
  189. // Strip syntax
  190. $match = substr($match, 1);
  191. list($syntax, $src, $anchor) = array_pad(preg_split("/ |#/", $match), 3, "");
  192. // Resolve new source.
  193. if (method_exists($handler, 'adaptRelativeId')) {
  194. $new_src = $handler->adaptRelativeId($src);
  195. } else {
  196. $new_src = $handler->resolveMoves($src, 'page');
  197. $new_src = $handler->relativeLink($src, $new_src, 'page');
  198. }
  199. $result = "\n#".$syntax." ".$new_src;
  200. if (!empty($anchor)) $result .= "#".$anchor;
  201. return $result;
  202. }
  203. // Fail-safe
  204. return $match;
  205. }
  206. }