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