Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
SpecialRedirectWithAction
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
6 / 6
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getRedirect
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 showNoRedirectPage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 showForm
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 onFormSubmit
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 isListed
n/a
0 / 0
n/a
0 / 0
1
 prefixSearchSubpages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getGroupName
n/a
0 / 0
n/a
0 / 0
1
1<?php
2
3/**
4 * @license GPL-2.0-or-later
5 * @author DannyS712
6 */
7
8namespace MediaWiki\SpecialPage;
9
10use MediaWiki\HTMLForm\HTMLForm;
11use MediaWiki\Search\SearchEngineFactory;
12use MediaWiki\Title\Title;
13
14/**
15 * Abstract to simplify creation of redirect special pages
16 *
17 * @stable to extend
18 * @ingroup SpecialPage
19 */
20abstract class SpecialRedirectWithAction extends RedirectSpecialPage {
21    /** @var string */
22    protected $action;
23
24    /** @var string */
25    protected $msgPrefix;
26
27    /**
28     * @stable to call
29     * @since 1.39 SearchEngineFactory added
30     *
31     * @param string $name
32     * @param string $action
33     * @param string $msgPrefix
34     * @param SearchEngineFactory $searchEngineFactory
35     */
36    public function __construct(
37        $name,
38        $action,
39        $msgPrefix,
40        private readonly SearchEngineFactory $searchEngineFactory,
41    ) {
42        parent::__construct( $name );
43        $this->action = $action;
44        $this->msgPrefix = $msgPrefix;
45    }
46
47    /**
48     * @inheritDoc
49     */
50    public function getRedirect( $subpage ) {
51        if ( $subpage === null || $subpage === '' ) {
52            return false;
53        }
54        $this->mAddedRedirectParams['title'] = $subpage;
55        $this->mAddedRedirectParams['action'] = $this->action;
56        return true;
57    }
58
59    /**
60     * @stable to override
61     */
62    protected function showNoRedirectPage() {
63        $this->setHeaders();
64        $this->outputHeader();
65        $this->showForm();
66    }
67
68    private function showForm() {
69        // Dynamic messages used:
70        // 'special' . $this->msgPrefix . '-page'
71        // 'special' . $this->msgPrefix . '-submit'
72        // Each special page that extends this should include those as comments for grep
73        $form = HTMLForm::factory( 'ooui', [
74            'page' => [
75                'type' => 'title',
76                'name' => 'page',
77                'label-message' => 'special' . $this->msgPrefix . '-page',
78                'required' => true,
79                'creatable' => true,
80            ],
81        ], $this->getContext(), $this->msgPrefix );
82        $form->setSubmitTextMsg( 'special' . $this->msgPrefix . '-submit' );
83        $form->setSubmitCallback( $this->onFormSubmit( ... ) );
84        $form->show();
85    }
86
87    /**
88     * @stable to override
89     *
90     * @param array $formData
91     */
92    public function onFormSubmit( $formData ) {
93        $title = $formData['page'];
94        $page = Title::newFromText( $title );
95        $query = [ 'action' => $this->action ];
96        $url = $page->getFullUrlForRedirect( $query );
97        $this->getOutput()->redirect( $url );
98    }
99
100    /**
101     * @stable to override
102     * @return bool
103     * @codeCoverageIgnore Merely declarative
104     */
105    public function isListed() {
106        return true;
107    }
108
109    /**
110     * Return an array of subpages beginning with $search that this special page will accept.
111     *
112     * @param string $search Prefix to search for
113     * @param int $limit Maximum number of results to return (usually 10)
114     * @param int $offset Number of results to skip (usually 0)
115     * @return string[] Matching subpages
116     */
117    public function prefixSearchSubpages( $search, $limit, $offset ) {
118        return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
119    }
120
121    /**
122     * @stable to override
123     * @return string
124     * @codeCoverageIgnore Merely declarative
125     */
126    protected function getGroupName() {
127        return 'redirects';
128    }
129}
130
131// @codeCoverageIgnoreStart
132/** @deprecated class alias since 1.41 */
133class_alias( SpecialRedirectWithAction::class, 'SpecialRedirectWithAction' );
134// @codeCoverageIgnoreEnd