1<?php
2/**
3 * Abstract to simplify creation of redirect special pages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @stable to extend
21 *
22 * @file
23 * @ingroup SpecialPage
24 * @author DannyS712
25 */
26abstract class SpecialRedirectWithAction extends RedirectSpecialPage {
27	protected $action, $msgPrefix;
28
29	/**
30	 * @stable to call
31	 *
32	 * @param string $name
33	 * @param string $action
34	 * @param string $msgPrefix
35	 */
36	public function __construct( $name, $action, $msgPrefix ) {
37		parent::__construct( $name );
38		$this->action = $action;
39		$this->msgPrefix = $msgPrefix;
40	}
41
42	/**
43	 * @inheritDoc
44	 */
45	public function getRedirect( $subpage ) {
46		if ( $subpage === null || $subpage === '' ) {
47			return false;
48		}
49		$this->mAddedRedirectParams['title'] = $subpage;
50		$this->mAddedRedirectParams['action'] = $this->action;
51		return true;
52	}
53
54	/**
55	 * @stable to override
56	 */
57	protected function showNoRedirectPage() {
58		$this->setHeaders();
59		$this->outputHeader();
60		$this->showForm();
61	}
62
63	private function showForm() {
64		// Dynamic messages used:
65		// 'special' . $this->msgPrefix . '-page'
66		// 'special' . $this->msgPrefix . '-submit'
67		// Each special page that extends this should include those as comments for grep
68		$form = HTMLForm::factory( 'ooui', [
69			'page' => [
70				'type' => 'text',
71				'name' => 'page',
72				'label-message' => 'special' . $this->msgPrefix . '-page',
73				'required' => true,
74			],
75		], $this->getContext(), $this->msgPrefix );
76		$form->setSubmitTextMsg( 'special' . $this->msgPrefix . '-submit' );
77		$form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
78		$form->show();
79	}
80
81	/**
82	 * @stable to override
83	 *
84	 * @param array $formData
85	 *
86	 * @return Status|null
87	 */
88	public function onFormSubmit( $formData ) {
89		$title = $formData['page'];
90		try {
91			$page = Title::newFromTextThrow( $title );
92		} catch ( MalformedTitleException $e ) {
93			return Status::newFatal( $e->getMessageObject() );
94		}
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	 */
104	public function isListed() {
105		return true;
106	}
107
108	/**
109	 * @stable to override
110	 * @return string
111	 */
112	protected function getGroupName() {
113		return 'redirects';
114	}
115}
116