Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialRedirectWithAction
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 8
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getRedirect
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 showNoRedirectPage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 showForm
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 onFormSubmit
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 isListed
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 prefixSearchSubpages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\SpecialPage;
4
5use MediaWiki\HTMLForm\HTMLForm;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Status\Status;
8use MediaWiki\Title\MalformedTitleException;
9use MediaWiki\Title\Title;
10use SearchEngineFactory;
11
12/**
13 * Abstract to simplify creation of redirect special pages
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
29 *
30 * @stable to extend
31 *
32 * @file
33 * @ingroup SpecialPage
34 * @author DannyS712
35 */
36abstract class SpecialRedirectWithAction extends RedirectSpecialPage {
37    /** @var string */
38    protected $action;
39
40    /** @var string */
41    protected $msgPrefix;
42
43    /** @var SearchEngineFactory */
44    private $searchEngineFactory;
45
46    /**
47     * @stable to call
48     * @since 1.39 SearchEngineFactory added
49     *
50     * @param string $name
51     * @param string $action
52     * @param string $msgPrefix
53     * @param SearchEngineFactory|null $searchEngineFactory Not providing this param is deprecated since 1.39
54     */
55    public function __construct(
56        $name,
57        $action,
58        $msgPrefix,
59        SearchEngineFactory $searchEngineFactory = null
60    ) {
61        parent::__construct( $name );
62        $this->action = $action;
63        $this->msgPrefix = $msgPrefix;
64        if ( !$searchEngineFactory ) {
65            // Fallback to global state if the new parameter was not provided
66            wfDeprecated( __METHOD__ . ' without providing SearchEngineFactory', '1.39' );
67            $searchEngineFactory = MediaWikiServices::getInstance()->getSearchEngineFactory();
68        }
69        $this->searchEngineFactory = $searchEngineFactory;
70    }
71
72    /**
73     * @inheritDoc
74     */
75    public function getRedirect( $subpage ) {
76        if ( $subpage === null || $subpage === '' ) {
77            return false;
78        }
79        $this->mAddedRedirectParams['title'] = $subpage;
80        $this->mAddedRedirectParams['action'] = $this->action;
81        return true;
82    }
83
84    /**
85     * @stable to override
86     */
87    protected function showNoRedirectPage() {
88        $this->setHeaders();
89        $this->outputHeader();
90        $this->showForm();
91    }
92
93    private function showForm() {
94        // Dynamic messages used:
95        // 'special' . $this->msgPrefix . '-page'
96        // 'special' . $this->msgPrefix . '-submit'
97        // Each special page that extends this should include those as comments for grep
98        $form = HTMLForm::factory( 'ooui', [
99            'page' => [
100                'type' => 'text',
101                'name' => 'page',
102                'label-message' => 'special' . $this->msgPrefix . '-page',
103                'required' => true,
104            ],
105        ], $this->getContext(), $this->msgPrefix );
106        $form->setSubmitTextMsg( 'special' . $this->msgPrefix . '-submit' );
107        $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
108        $form->show();
109    }
110
111    /**
112     * @stable to override
113     *
114     * @param array $formData
115     *
116     * @return Status|null
117     */
118    public function onFormSubmit( $formData ) {
119        $title = $formData['page'];
120        try {
121            $page = Title::newFromTextThrow( $title );
122        } catch ( MalformedTitleException $e ) {
123            return Status::newFatal( $e->getMessageObject() );
124        }
125        $query = [ 'action' => $this->action ];
126        $url = $page->getFullUrlForRedirect( $query );
127        $this->getOutput()->redirect( $url );
128    }
129
130    /**
131     * @stable to override
132     * @return bool
133     */
134    public function isListed() {
135        return true;
136    }
137
138    /**
139     * Return an array of subpages beginning with $search that this special page will accept.
140     *
141     * @param string $search Prefix to search for
142     * @param int $limit Maximum number of results to return (usually 10)
143     * @param int $offset Number of results to skip (usually 0)
144     * @return string[] Matching subpages
145     */
146    public function prefixSearchSubpages( $search, $limit, $offset ) {
147        return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
148    }
149
150    /**
151     * @stable to override
152     * @return string
153     */
154    protected function getGroupName() {
155        return 'redirects';
156    }
157}
158
159/** @deprecated class alias since 1.41 */
160class_alias( SpecialRedirectWithAction::class, 'SpecialRedirectWithAction' );