StringUtils: Add a utility for checking if a string is a valid regex
[lhc/web/wiklou.git] / includes / actions / SpecialPageAction.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
16 *
17 * @file
18 * @ingroup Actions
19 */
20
21 use MediaWiki\MediaWikiServices;
22
23 /**
24 * An action that just passes the request to the relevant special page
25 *
26 * @ingroup Actions
27 * @since 1.25
28 */
29 class SpecialPageAction extends FormlessAction {
30 /**
31 * @var array A mapping of action names to special page names.
32 */
33 public static $actionToSpecialPageMapping = [
34 'revisiondelete' => 'Revisiondelete',
35 'editchangetags' => 'EditTags',
36 ];
37
38 public function getName() {
39 $request = $this->getRequest();
40 $actionName = $request->getVal( 'action', 'view' );
41 // TODO: Shouldn't need to copy-paste this code from Action::getActionName!
42 if ( $actionName === 'historysubmit' ) {
43 if ( $request->getBool( 'revisiondelete' ) ) {
44 $actionName = 'revisiondelete';
45 } elseif ( $request->getBool( 'editchangetags' ) ) {
46 $actionName = 'editchangetags';
47 }
48 }
49
50 if ( isset( self::$actionToSpecialPageMapping[$actionName] ) ) {
51 return $actionName;
52 }
53
54 return 'nosuchaction';
55 }
56
57 public function requiresUnblock() {
58 return false;
59 }
60
61 public function getDescription() {
62 return '';
63 }
64
65 public function onView() {
66 return '';
67 }
68
69 public function show() {
70 $special = $this->getSpecialPage();
71 if ( !$special ) {
72 throw new ErrorPageError(
73 $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
74 }
75
76 $special->setContext( $this->getContext() );
77 $special->getContext()->setTitle( $special->getPageTitle() );
78 $special->run( '' );
79 }
80
81 public function doesWrites() {
82 $special = $this->getSpecialPage();
83
84 return $special ? $special->doesWrites() : false;
85 }
86
87 /**
88 * @return SpecialPage|null
89 */
90 protected function getSpecialPage() {
91 $action = $this->getName();
92 if ( $action === 'nosuchaction' ) {
93 return null;
94 }
95
96 // map actions to (whitelisted) special pages
97 return MediaWikiServices::getInstance()->getSpecialPageFactory()->
98 getPage( self::$actionToSpecialPageMapping[$action] );
99 }
100 }