xref: /dokuwiki/inc/Action/Redirect.php (revision 8c7c53b0321a3cd3116b8d3b2ad27863a38dece7)
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        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        $go = wl($opts['id'], '', true, '&');
58        if(isset($opts['fragment'])) $go .= '#' . $opts['fragment'];
59
60        //show it
61        send_redirect($go);
62    }
63}
64