xref: /dokuwiki/inc/Action/Redirect.php (revision d868eb89f182718a31113373a6272670bd7f8012)
158528803SAndreas Gohr<?php
258528803SAndreas Gohr
358528803SAndreas Gohrnamespace dokuwiki\Action;
458528803SAndreas Gohr
558528803SAndreas Gohruse dokuwiki\Action\Exception\ActionAbort;
6cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
758528803SAndreas Gohr
858528803SAndreas Gohr/**
958528803SAndreas Gohr * Class Redirect
1058528803SAndreas Gohr *
1158528803SAndreas Gohr * Used to redirect to the current page with the last edited section as a target if found
1258528803SAndreas Gohr *
1358528803SAndreas Gohr * @package dokuwiki\Action
1458528803SAndreas Gohr */
158c7c53b0SAndreas Gohrclass Redirect extends AbstractAliasAction
168c7c53b0SAndreas Gohr{
1758528803SAndreas Gohr
1858528803SAndreas Gohr    /**
1958528803SAndreas Gohr     * Redirect to the show action, trying to jump to the previously edited section
2058528803SAndreas Gohr     *
2158528803SAndreas Gohr     * @triggers ACTION_SHOW_REDIRECT
2258528803SAndreas Gohr     * @throws ActionAbort
2358528803SAndreas Gohr     */
24*d868eb89SAndreas Gohr    public function preProcess()
25*d868eb89SAndreas Gohr    {
2658528803SAndreas Gohr        global $PRE;
2758528803SAndreas Gohr        global $TEXT;
2858528803SAndreas Gohr        global $INPUT;
2958528803SAndreas Gohr        global $ID;
3058528803SAndreas Gohr        global $ACT;
3158528803SAndreas Gohr
326723156fSAndreas Gohr        $opts = ['id' => $ID, 'preact' => $ACT];
3358528803SAndreas Gohr        //get section name when coming from section edit
3458528803SAndreas Gohr        if ($INPUT->has('hid')) {
3558528803SAndreas Gohr            // Use explicitly transmitted header id
3658528803SAndreas Gohr            $opts['fragment'] = $INPUT->str('hid');
3758528803SAndreas Gohr        } elseif ($PRE && preg_match('/^\s*==+([^=\n]+)/', $TEXT, $match)) {
3858528803SAndreas Gohr            // Fallback to old mechanism
3958528803SAndreas Gohr            $check = false; //Byref
4058528803SAndreas Gohr            $opts['fragment'] = sectionID($match[0], $check);
4158528803SAndreas Gohr        }
4258528803SAndreas Gohr
4358528803SAndreas Gohr        // execute the redirect
446723156fSAndreas Gohr        Event::createAndTrigger('ACTION_SHOW_REDIRECT', $opts, [$this, 'redirect']);
4558528803SAndreas Gohr
4658528803SAndreas Gohr        // should never be reached
47d3d3f39aSAndreas Gohr        throw new ActionAbort('show');
4858528803SAndreas Gohr    }
4958528803SAndreas Gohr
5058528803SAndreas Gohr    /**
5158528803SAndreas Gohr     * Execute the redirect
5258528803SAndreas Gohr     *
5358528803SAndreas Gohr     * Default action for ACTION_SHOW_REDIRECT
5458528803SAndreas Gohr     *
5558528803SAndreas Gohr     * @param array $opts id and fragment for the redirect and the preact
5658528803SAndreas Gohr     */
57*d868eb89SAndreas Gohr    public function redirect($opts)
58*d868eb89SAndreas Gohr    {
59e9fede20SPhy        $go = wl($opts['id'], '', true, '&');
6058528803SAndreas Gohr        if(isset($opts['fragment'])) $go .= '#' . $opts['fragment'];
6158528803SAndreas Gohr
6258528803SAndreas Gohr        //show it
6358528803SAndreas Gohr        send_redirect($go);
6458528803SAndreas Gohr    }
6558528803SAndreas Gohr}
66