Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RedirectSpecialPage
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
3 / 3
8
100.00% covered (success)
100.00%
1 / 1
 execute
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 getRedirect
n/a
0 / 0
n/a
0 / 0
0
 getRedirectQuery
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 personallyIdentifiableTarget
n/a
0 / 0
n/a
0 / 0
1
 showNoRedirectPage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\SpecialPage;
8
9use LogicException;
10use MediaWiki\Title\Title;
11
12/**
13 * Shortcut to construct a special page alias.
14 *
15 * @stable to extend
16 *
17 * @ingroup SpecialPage
18 */
19abstract class RedirectSpecialPage extends UnlistedSpecialPage {
20    /** @var array Query parameters that can be passed through redirects */
21    protected $mAllowedRedirectParams = [];
22
23    /** @var array Query parameters added by redirects */
24    protected $mAddedRedirectParams = [];
25
26    /**
27     * @stable to override
28     * @param string|null $subpage
29     */
30    public function execute( $subpage ) {
31        $redirect = $this->getRedirect( $subpage );
32        $query = $this->getRedirectQuery( $subpage );
33
34        if ( $redirect instanceof Title ) {
35            // Redirect to a page title with possible query parameters
36            $url = $redirect->getFullUrlForRedirect( $query );
37            $this->getOutput()->redirect( $url );
38        } elseif ( $redirect === true ) {
39            // Redirect to index.php with query parameters
40            $url = wfAppendQuery( wfScript( 'index' ), $query );
41            $this->getOutput()->redirect( $url );
42        } else {
43            $this->showNoRedirectPage();
44        }
45    }
46
47    /**
48     * If the special page is a redirect, then get the Title object it redirects to.
49     * False otherwise.
50     *
51     * @param string|null $subpage
52     * @return Title|bool
53     */
54    abstract public function getRedirect( $subpage );
55
56    /**
57     * Return part of the request string for a special redirect page
58     * This allows passing, e.g. action=history to Special:Mypage, etc.
59     *
60     * @stable to override
61     * @param string|null $subpage
62     * @return array|false
63     */
64    public function getRedirectQuery( $subpage ) {
65        $queryParams = $this->getRequest()->getQueryValues();
66        $keepParams = array_merge(
67            $this->mAllowedRedirectParams,
68            // parameters which can be passed to all pages
69            [ 'uselang', 'useskin', 'useformat', 'variant', 'debug', 'safemode' ]
70        );
71
72        $params = array_intersect_key( $queryParams, array_fill_keys( $keepParams, true ) );
73
74        foreach ( $this->mAddedRedirectParams as $arg => $val ) {
75            $params[$arg] = $val;
76        }
77
78        return count( $params )
79            ? $params
80            : false;
81    }
82
83    /**
84     * Indicate if the target of this redirect can be used to identify
85     * a particular user of this wiki (e.g., if the redirect is to the
86     * user page of a User). See T109724.
87     *
88     * @codeCoverageIgnore Merely declarative
89     * @stable to override
90     * @since 1.27
91     * @return bool
92     */
93    public function personallyIdentifiableTarget() {
94        return false;
95    }
96
97    /**
98     * @stable to override
99     */
100    protected function showNoRedirectPage() {
101        $class = static::class;
102        throw new LogicException( "RedirectSpecialPage $class doesn't redirect!" );
103    }
104}
105
106// @codeCoverageIgnoreStart
107/** @deprecated class alias since 1.41 */
108class_alias( RedirectSpecialPage::class, 'RedirectSpecialPage' );
109// @codeCoverageIgnoreEnd