| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\SpecialPage; |
| 25 |
|
| 26 |
use MediaWiki\Context\DerivativeContext; |
| 27 |
use MediaWiki\Debug\MWDebug; |
| 28 |
use MediaWiki\HTMLForm\HTMLForm; |
| 29 |
use MediaWiki\Request\DerivativeRequest; |
| 30 |
use MediaWiki\Status\Status; |
| 31 |
use MediaWiki\User\User; |
| 32 |
use UserBlockedError; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
abstract class FormSpecialPage extends SpecialPage { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected $par = null; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
protected $reauthPostData = null; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
abstract protected function getFormFields(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected function preHtml() { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected function postHtml() { |
| 75 |
return ''; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected function preText() { |
| 84 |
wfDeprecated( __METHOD__, '1.38' ); |
| 85 |
return $this->preHtml(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
protected function postText() { |
| 94 |
wfDeprecated( __METHOD__, '1.38' ); |
| 95 |
return $this->postHtml(); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
protected function alterForm( HTMLForm $form ) { |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
protected function getMessagePrefix() { |
| 112 |
return strtolower( $this->getName() ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
protected function getDisplayFormat() { |
| 122 |
return 'table'; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
protected function getForm() { |
| 130 |
$context = $this->getContext(); |
| 131 |
$onSubmit = [ $this, 'onSubmit' ]; |
| 132 |
|
| 133 |
if ( $this->reauthPostData ) { |
| 134 |
|
| 135 |
$context = new DerivativeContext( $context ); |
| 136 |
$oldRequest = $this->getRequest(); |
| 137 |
$context->setRequest( new DerivativeRequest( |
| 138 |
$oldRequest, $this->reauthPostData + $oldRequest->getQueryValues(), true |
| 139 |
) ); |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
$onSubmit = static function () { |
| 144 |
return false; |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
$form = HTMLForm::factory( |
| 149 |
$this->getDisplayFormat(), |
| 150 |
$this->getFormFields(), |
| 151 |
$context, |
| 152 |
$this->getMessagePrefix() |
| 153 |
); |
| 154 |
if ( !$this->requiresPost() ) { |
| 155 |
$form->setMethod( 'get' ); |
| 156 |
} |
| 157 |
$form->setSubmitCallback( $onSubmit ); |
| 158 |
if ( $this->getDisplayFormat() !== 'ooui' ) { |
| 159 |
|
| 160 |
|
| 161 |
$form->setWrapperLegendMsg( $this->getMessagePrefix() . '-legend' ); |
| 162 |
} |
| 163 |
|
| 164 |
$headerMsg = $this->msg( $this->getMessagePrefix() . '-text' ); |
| 165 |
if ( !$headerMsg->isDisabled() ) { |
| 166 |
$form->addHeaderHtml( $headerMsg->parseAsBlock() ); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
$form->addPreHtml( MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'preText', '1.38' ) |
| 172 |
? $this->preText() |
| 173 |
: $this->preHtml() |
| 174 |
); |
| 175 |
$form->addPostHtml( MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'postText', '1.38' ) |
| 176 |
? $this->postText() |
| 177 |
: $this->postHtml() |
| 178 |
); |
| 179 |
|
| 180 |
|
| 181 |
$field = $this->getSubpageField(); |
| 182 |
|
| 183 |
if ( strval( $this->par ) !== '' && $field ) { |
| 184 |
$this->getRequest()->setVal( $form->getField( $field )->getName(), $this->par ); |
| 185 |
$form->setTitle( $this->getPageTitle() ); |
| 186 |
} |
| 187 |
$this->alterForm( $form ); |
| 188 |
if ( $form->getMethod() == 'post' ) { |
| 189 |
|
| 190 |
$params = array_diff_key( |
| 191 |
$this->getRequest()->getQueryValues(), [ 'title' => null ] ); |
| 192 |
$form->addHiddenField( 'redirectparams', wfArrayToCgi( $params ) ); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
$this->getHookRunner()->onSpecialPageBeforeFormDisplay( $this->getName(), $form ); |
| 197 |
|
| 198 |
return $form; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
abstract public function onSubmit( array $data ); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function onSuccess() { |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function execute( $par ) { |
| 226 |
$this->setParameter( $par ); |
| 227 |
$this->setHeaders(); |
| 228 |
$this->outputHeader(); |
| 229 |
|
| 230 |
|
| 231 |
$this->checkExecutePermissions( $this->getUser() ); |
| 232 |
|
| 233 |
$securityLevel = $this->getLoginSecurityLevel(); |
| 234 |
if ( $securityLevel !== false && !$this->checkLoginSecurityLevel( $securityLevel ) ) { |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
$form = $this->getForm(); |
| 239 |
|
| 240 |
if ( !$this->including() ) { |
| 241 |
$result = $this->getShowAlways() ? $form->showAlways() : $form->show(); |
| 242 |
} else { |
| 243 |
$result = $form->prepareForm()->tryAuthorizedSubmit(); |
| 244 |
} |
| 245 |
if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) { |
| 246 |
$this->onSuccess(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
protected function getShowAlways() { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
protected function setParameter( $par ) { |
| 264 |
$this->par = $par; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
protected function getSubpageField() { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
protected function checkExecutePermissions( User $user ) { |
| 283 |
$this->checkPermissions(); |
| 284 |
|
| 285 |
if ( $this->requiresUnblock() ) { |
| 286 |
$block = $user->getBlock(); |
| 287 |
if ( $block && $block->isSitewide() ) { |
| 288 |
throw new UserBlockedError( |
| 289 |
$block, |
| 290 |
$user, |
| 291 |
$this->getLanguage(), |
| 292 |
$this->getRequest()->getIP() |
| 293 |
); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if ( $this->requiresWrite() ) { |
| 298 |
$this->checkReadOnly(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function requiresPost() { |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function requiresWrite() { |
| 316 |
return $this->requiresPost(); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public function requiresUnblock() { |
| 324 |
return $this->requiresPost(); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
protected function setReauthPostData( array $data ) { |
| 334 |
$this->reauthPostData = $data; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
class_alias( FormSpecialPage::class, 'FormSpecialPage' ); |