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