xref: /dokuwiki/inc/Action/Redirect.php (revision d4f83172d9533c4d84f450fe22ef630816b21d75)
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     * Redirect to the show action, trying to jump to the previously edited section
1958528803SAndreas Gohr     *
2058528803SAndreas Gohr     * @triggers ACTION_SHOW_REDIRECT
2158528803SAndreas Gohr     * @throws ActionAbort
2258528803SAndreas Gohr     */
23*d868eb89SAndreas Gohr    public function preProcess()
24*d868eb89SAndreas Gohr    {
2558528803SAndreas Gohr        global $PRE;
2658528803SAndreas Gohr        global $TEXT;
2758528803SAndreas Gohr        global $INPUT;
2858528803SAndreas Gohr        global $ID;
2958528803SAndreas Gohr        global $ACT;
3058528803SAndreas Gohr
316723156fSAndreas Gohr        $opts = ['id' => $ID, 'preact' => $ACT];
3258528803SAndreas Gohr        //get section name when coming from section edit
3358528803SAndreas Gohr        if ($INPUT->has('hid')) {
3458528803SAndreas Gohr            // Use explicitly transmitted header id
3558528803SAndreas Gohr            $opts['fragment'] = $INPUT->str('hid');
3658528803SAndreas Gohr        } elseif ($PRE && preg_match('/^\s*==+([^=\n]+)/', $TEXT, $match)) {
3758528803SAndreas Gohr            // Fallback to old mechanism
3858528803SAndreas Gohr            $check = false; //Byref
3958528803SAndreas Gohr            $opts['fragment'] = sectionID($match[0], $check);
4058528803SAndreas Gohr        }
4158528803SAndreas Gohr
4258528803SAndreas Gohr        // execute the redirect
436723156fSAndreas Gohr        Event::createAndTrigger('ACTION_SHOW_REDIRECT', $opts, [$this, 'redirect']);
4458528803SAndreas Gohr
4558528803SAndreas Gohr        // should never be reached
46d3d3f39aSAndreas Gohr        throw new ActionAbort('show');
4758528803SAndreas Gohr    }
4858528803SAndreas Gohr
4958528803SAndreas Gohr    /**
5058528803SAndreas Gohr     * Execute the redirect
5158528803SAndreas Gohr     *
5258528803SAndreas Gohr     * Default action for ACTION_SHOW_REDIRECT
5358528803SAndreas Gohr     *
5458528803SAndreas Gohr     * @param array $opts id and fragment for the redirect and the preact
5558528803SAndreas Gohr     */
56*d868eb89SAndreas Gohr    public function redirect($opts)
57*d868eb89SAndreas Gohr    {
58e9fede20SPhy        $go = wl($opts['id'], '', true, '&');
5958528803SAndreas Gohr        if (isset($opts['fragment'])) $go .= '#' . $opts['fragment'];
6058528803SAndreas Gohr
6158528803SAndreas Gohr        //show it
6258528803SAndreas Gohr        send_redirect($go);
6358528803SAndreas Gohr    }
6458528803SAndreas Gohr}
65