Redirect.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace dokuwiki\Action;
  3. use dokuwiki\Action\Exception\ActionAbort;
  4. use dokuwiki\Extension\Event;
  5. /**
  6. * Class Redirect
  7. *
  8. * Used to redirect to the current page with the last edited section as a target if found
  9. *
  10. * @package dokuwiki\Action
  11. */
  12. class Redirect extends AbstractAliasAction {
  13. /**
  14. * Redirect to the show action, trying to jump to the previously edited section
  15. *
  16. * @triggers ACTION_SHOW_REDIRECT
  17. * @throws ActionAbort
  18. */
  19. public function preProcess() {
  20. global $PRE;
  21. global $TEXT;
  22. global $INPUT;
  23. global $ID;
  24. global $ACT;
  25. $opts = array(
  26. 'id' => $ID,
  27. 'preact' => $ACT
  28. );
  29. //get section name when coming from section edit
  30. if($INPUT->has('hid')) {
  31. // Use explicitly transmitted header id
  32. $opts['fragment'] = $INPUT->str('hid');
  33. } else if($PRE && preg_match('/^\s*==+([^=\n]+)/', $TEXT, $match)) {
  34. // Fallback to old mechanism
  35. $check = false; //Byref
  36. $opts['fragment'] = sectionID($match[0], $check);
  37. }
  38. // execute the redirect
  39. Event::createAndTrigger('ACTION_SHOW_REDIRECT', $opts, array($this, 'redirect'));
  40. // should never be reached
  41. throw new ActionAbort('show');
  42. }
  43. /**
  44. * Execute the redirect
  45. *
  46. * Default action for ACTION_SHOW_REDIRECT
  47. *
  48. * @param array $opts id and fragment for the redirect and the preact
  49. */
  50. public function redirect($opts) {
  51. $go = wl($opts['id'], '', true, '&');
  52. if(isset($opts['fragment'])) $go .= '#' . $opts['fragment'];
  53. //show it
  54. send_redirect($go);
  55. }
  56. }