xref: /dokuwiki/inc/Action/Redirect.php (revision e3c3abf1ebade68a06abb03d098d58afda87b317)
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 = ['id' => $ID, 'preact' => $ACT];
31        //get section name when coming from section edit
32        if ($INPUT->has('hid')) {
33            // Use explicitly transmitted header id
34            $opts['fragment'] = $INPUT->str('hid');
35        } elseif ($PRE && preg_match('/^\s*==+([^=\n]+)/', $TEXT, $match)) {
36            // Fallback to old mechanism
37            $check = false; //Byref
38            $opts['fragment'] = sectionID($match[0], $check);
39        }
40
41        // execute the redirect
42        Event::createAndTrigger('ACTION_SHOW_REDIRECT', $opts, [$this, 'redirect']);
43
44        // should never be reached
45        throw new ActionAbort('show');
46    }
47
48    /**
49     * Execute the redirect
50     *
51     * Default action for ACTION_SHOW_REDIRECT
52     *
53     * @param array $opts id and fragment for the redirect and the preact
54     */
55    public function redirect($opts) {
56        $go = wl($opts['id'], '', true, '&');
57        if(isset($opts['fragment'])) $go .= '#' . $opts['fragment'];
58
59        //show it
60        send_redirect($go);
61    }
62}
63