wiki.techinc.nl/includes/specialpage/SpecialRedirectWithAction.php
DannyS712 f3e2b140c8 Create abstract special page SpecialRedirectWithAction, and use it.
Special:Edit, Special:History, Special:Purge, and Special:Info all
redirect to index.php?title=$1&action=, where the action is edit,
history, purge, or info. Thus, they can each extend a common class.

Bug: T13456
Change-Id: I64aca28075f5d79ec841b6868e1760f2dac4ec90
2019-11-28 01:00:28 +00:00

87 lines
2.5 KiB
PHP

<?php
/**
* Abstract to simplify creation of redirect special pages
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup SpecialPage
*/
abstract class SpecialRedirectWithAction extends RedirectSpecialPage {
protected $action, $msgPrefix;
function __construct( $name, $action, $msgPrefix ) {
parent::__construct( $name );
$this->action = $action;
$this->msgPrefix = $msgPrefix;
}
/**
* @inheritDoc
*/
public function getRedirect( $subpage ) {
if ( $subpage === null || $subpage === '' ) {
return false;
}
$this->mAddedRedirectParams['title'] = $subpage;
$this->mAddedRedirectParams['action'] = $this->action;
return true;
}
protected function showNoRedirectPage() {
$this->setHeaders();
$this->outputHeader();
$this->showForm();
}
private function showForm() {
// Dynamic messages used:
// 'special' . $this->msgPrefix . '-page'
// 'special' . $this->msgPrefix . '-submit'
// Each special page that extends this should include those as comments for grep
$form = HTMLForm::factory( 'ooui', [
'page' => [
'type' => 'text',
'name' => 'page',
'label-message' => 'special' . $this->msgPrefix . '-page',
'required' => true,
],
], $this->getContext(), $this->msgPrefix );
$form->setSubmitTextMsg( 'special' . $this->msgPrefix . '-submit' );
$form->setSubmitCallback( [ $this, 'onFormSubmit' ] );
$form->show();
}
public function onFormSubmit( $formData ) {
$title = $formData['page'];
try {
$page = Title::newFromTextThrow( $title );
} catch ( MalformedTitleException $e ) {
return Status::newFatal( $e->getMessageObject() );
}
$query = [ 'action' => $this->action ];
$url = $page->getFullUrlForRedirect( $query );
$this->getOutput()->redirect( $url );
}
public function isListed() {
return true;
}
protected function getGroupName() {
return 'redirects';
}
}