Merge "docs: Fix typo"
[mediawiki.git] / includes / specialpage / SpecialRedirectWithAction.php
blob5578bf4738e593de0ab2b4558ef63ea6c4ea07a0
1 <?php
3 namespace MediaWiki\SpecialPage;
5 use MediaWiki\HTMLForm\HTMLForm;
6 use MediaWiki\MediaWikiServices;
7 use MediaWiki\Status\Status;
8 use MediaWiki\Title\MalformedTitleException;
9 use MediaWiki\Title\Title;
10 use SearchEngineFactory;
12 /**
13 * Abstract to simplify creation of redirect special pages
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.
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.
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
30 * @stable to extend
32 * @file
33 * @ingroup SpecialPage
34 * @author DannyS712
36 abstract class SpecialRedirectWithAction extends RedirectSpecialPage {
37 /** @var string */
38 protected $action;
40 /** @var string */
41 protected $msgPrefix;
43 /** @var SearchEngineFactory */
44 private $searchEngineFactory;
46 /**
47 * @stable to call
48 * @since 1.39 SearchEngineFactory added
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
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();
69 $this->searchEngineFactory = $searchEngineFactory;
72 /**
73 * @inheritDoc
75 public function getRedirect( $subpage ) {
76 if ( $subpage === null || $subpage === '' ) {
77 return false;
79 $this->mAddedRedirectParams['title'] = $subpage;
80 $this->mAddedRedirectParams['action'] = $this->action;
81 return true;
84 /**
85 * @stable to override
87 protected function showNoRedirectPage() {
88 $this->setHeaders();
89 $this->outputHeader();
90 $this->showForm();
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,
105 ], $this->getContext(), $this->msgPrefix );
106 $form->setSubmitTextMsg( 'special' . $this->msgPrefix . '-submit' );
107 $form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
108 $form->show();
112 * @stable to override
114 * @param array $formData
116 * @return Status|null
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() );
125 $query = [ 'action' => $this->action ];
126 $url = $page->getFullUrlForRedirect( $query );
127 $this->getOutput()->redirect( $url );
131 * @stable to override
132 * @return bool
134 public function isListed() {
135 return true;
139 * Return an array of subpages beginning with $search that this special page will accept.
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
146 public function prefixSearchSubpages( $search, $limit, $offset ) {
147 return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
151 * @stable to override
152 * @return string
154 protected function getGroupName() {
155 return 'redirects';
159 /** @deprecated class alias since 1.41 */
160 class_alias( SpecialRedirectWithAction::class, 'SpecialRedirectWithAction' );