xref: /dokuwiki/inc/Action/Redirect.php (revision a19c9aa0217112e3ab7ebc160354c7e9fbabe8eb)
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    /**
19     * Redirect to the show action, trying to jump to the previously edited section
20     *
21     * @triggers ACTION_SHOW_REDIRECT
22     * @throws ActionAbort
23     */
24    public function preProcess()
25    {
26        global $PRE;
27        global $TEXT;
28        global $INPUT;
29        global $ID;
30        global $ACT;
31
32        $opts = ['id' => $ID, 'preact' => $ACT];
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        } elseif ($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        Event::createAndTrigger('ACTION_SHOW_REDIRECT', $opts, [$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    {
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