xref: /dokuwiki/inc/Action/Redirect.php (revision 6723156fd9886ff7204e21fb8bf9240b9223b40f)
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 */
1558528803SAndreas Gohrclass Redirect extends AbstractAliasAction {
1658528803SAndreas 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     */
2358528803SAndreas Gohr    public function preProcess() {
2458528803SAndreas Gohr        global $PRE;
2558528803SAndreas Gohr        global $TEXT;
2658528803SAndreas Gohr        global $INPUT;
2758528803SAndreas Gohr        global $ID;
2858528803SAndreas Gohr        global $ACT;
2958528803SAndreas Gohr
30*6723156fSAndreas Gohr        $opts = ['id' => $ID, 'preact' => $ACT];
3158528803SAndreas Gohr        //get section name when coming from section edit
3258528803SAndreas Gohr        if ($INPUT->has('hid')) {
3358528803SAndreas Gohr            // Use explicitly transmitted header id
3458528803SAndreas Gohr            $opts['fragment'] = $INPUT->str('hid');
3558528803SAndreas Gohr        } elseif ($PRE && preg_match('/^\s*==+([^=\n]+)/', $TEXT, $match)) {
3658528803SAndreas Gohr            // Fallback to old mechanism
3758528803SAndreas Gohr            $check = false; //Byref
3858528803SAndreas Gohr            $opts['fragment'] = sectionID($match[0], $check);
3958528803SAndreas Gohr        }
4058528803SAndreas Gohr
4158528803SAndreas Gohr        // execute the redirect
42*6723156fSAndreas Gohr        Event::createAndTrigger('ACTION_SHOW_REDIRECT', $opts, [$this, 'redirect']);
4358528803SAndreas Gohr
4458528803SAndreas Gohr        // should never be reached
45d3d3f39aSAndreas Gohr        throw new ActionAbort('show');
4658528803SAndreas Gohr    }
4758528803SAndreas Gohr
4858528803SAndreas Gohr    /**
4958528803SAndreas Gohr     * Execute the redirect
5058528803SAndreas Gohr     *
5158528803SAndreas Gohr     * Default action for ACTION_SHOW_REDIRECT
5258528803SAndreas Gohr     *
5358528803SAndreas Gohr     * @param array $opts id and fragment for the redirect and the preact
5458528803SAndreas Gohr     */
5558528803SAndreas Gohr    public function redirect($opts) {
56e9fede20SPhy        $go = wl($opts['id'], '', true, '&');
5758528803SAndreas Gohr        if(isset($opts['fragment'])) $go .= '#' . $opts['fragment'];
5858528803SAndreas Gohr
5958528803SAndreas Gohr        //show it
6058528803SAndreas Gohr        send_redirect($go);
6158528803SAndreas Gohr    }
6258528803SAndreas Gohr}
63