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