| 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\HTMLForm; |
| 25 |
|
| 26 |
use DomainException; |
| 27 |
use InvalidArgumentException; |
| 28 |
use LogicException; |
| 29 |
use MediaWiki\Context\ContextSource; |
| 30 |
use MediaWiki\Context\IContextSource; |
| 31 |
use MediaWiki\Debug\MWDebug; |
| 32 |
use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 33 |
use MediaWiki\Html\Html; |
| 34 |
use MediaWiki\HTMLForm\Field\HTMLApiField; |
| 35 |
use MediaWiki\HTMLForm\Field\HTMLAutoCompleteSelectField; |
| 36 |
use MediaWiki\HTMLForm\Field\HTMLButtonField; |
| 37 |
use MediaWiki\HTMLForm\Field\HTMLCheckField; |
| 38 |
use MediaWiki\HTMLForm\Field\HTMLCheckMatrix; |
| 39 |
use MediaWiki\HTMLForm\Field\HTMLComboboxField; |
| 40 |
use MediaWiki\HTMLForm\Field\HTMLDateTimeField; |
| 41 |
use MediaWiki\HTMLForm\Field\HTMLEditTools; |
| 42 |
use MediaWiki\HTMLForm\Field\HTMLExpiryField; |
| 43 |
use MediaWiki\HTMLForm\Field\HTMLFileField; |
| 44 |
use MediaWiki\HTMLForm\Field\HTMLFloatField; |
| 45 |
use MediaWiki\HTMLForm\Field\HTMLFormFieldCloner; |
| 46 |
use MediaWiki\HTMLForm\Field\HTMLHiddenField; |
| 47 |
use MediaWiki\HTMLForm\Field\HTMLInfoField; |
| 48 |
use MediaWiki\HTMLForm\Field\HTMLIntField; |
| 49 |
use MediaWiki\HTMLForm\Field\HTMLMultiSelectField; |
| 50 |
use MediaWiki\HTMLForm\Field\HTMLNamespacesMultiselectField; |
| 51 |
use MediaWiki\HTMLForm\Field\HTMLRadioField; |
| 52 |
use MediaWiki\HTMLForm\Field\HTMLSelectAndOtherField; |
| 53 |
use MediaWiki\HTMLForm\Field\HTMLSelectField; |
| 54 |
use MediaWiki\HTMLForm\Field\HTMLSelectLanguageField; |
| 55 |
use MediaWiki\HTMLForm\Field\HTMLSelectLimitField; |
| 56 |
use MediaWiki\HTMLForm\Field\HTMLSelectNamespace; |
| 57 |
use MediaWiki\HTMLForm\Field\HTMLSelectNamespaceWithButton; |
| 58 |
use MediaWiki\HTMLForm\Field\HTMLSelectOrOtherField; |
| 59 |
use MediaWiki\HTMLForm\Field\HTMLSizeFilterField; |
| 60 |
use MediaWiki\HTMLForm\Field\HTMLSubmitField; |
| 61 |
use MediaWiki\HTMLForm\Field\HTMLTagFilter; |
| 62 |
use MediaWiki\HTMLForm\Field\HTMLTagMultiselectField; |
| 63 |
use MediaWiki\HTMLForm\Field\HTMLTextAreaField; |
| 64 |
use MediaWiki\HTMLForm\Field\HTMLTextField; |
| 65 |
use MediaWiki\HTMLForm\Field\HTMLTextFieldWithButton; |
| 66 |
use MediaWiki\HTMLForm\Field\HTMLTimezoneField; |
| 67 |
use MediaWiki\HTMLForm\Field\HTMLTitlesMultiselectField; |
| 68 |
use MediaWiki\HTMLForm\Field\HTMLTitleTextField; |
| 69 |
use MediaWiki\HTMLForm\Field\HTMLUsersMultiselectField; |
| 70 |
use MediaWiki\HTMLForm\Field\HTMLUserTextField; |
| 71 |
use MediaWiki\Linker\Linker; |
| 72 |
use MediaWiki\Linker\LinkTarget; |
| 73 |
use MediaWiki\MainConfigNames; |
| 74 |
use MediaWiki\Message\Message; |
| 75 |
use MediaWiki\Page\PageReference; |
| 76 |
use MediaWiki\Parser\Sanitizer; |
| 77 |
use MediaWiki\Status\Status; |
| 78 |
use MediaWiki\Title\Title; |
| 79 |
use MediaWiki\Title\TitleValue; |
| 80 |
use MediaWiki\Xml\Xml; |
| 81 |
use StatusValue; |
| 82 |
use Stringable; |
| 83 |
use Wikimedia\Message\MessageParam; |
| 84 |
use Wikimedia\Message\MessageSpecifier; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
class HTMLForm extends ContextSource { |
| 211 |
use ProtectedHookAccessorTrait; |
| 212 |
|
| 213 |
|
| 214 |
public static $typeMappings = [ |
| 215 |
'api' => HTMLApiField::class, |
| 216 |
'text' => HTMLTextField::class, |
| 217 |
'textwithbutton' => HTMLTextFieldWithButton::class, |
| 218 |
'textarea' => HTMLTextAreaField::class, |
| 219 |
'select' => HTMLSelectField::class, |
| 220 |
'combobox' => HTMLComboboxField::class, |
| 221 |
'radio' => HTMLRadioField::class, |
| 222 |
'multiselect' => HTMLMultiSelectField::class, |
| 223 |
'limitselect' => HTMLSelectLimitField::class, |
| 224 |
'check' => HTMLCheckField::class, |
| 225 |
'toggle' => HTMLCheckField::class, |
| 226 |
'int' => HTMLIntField::class, |
| 227 |
'file' => HTMLFileField::class, |
| 228 |
'float' => HTMLFloatField::class, |
| 229 |
'info' => HTMLInfoField::class, |
| 230 |
'selectorother' => HTMLSelectOrOtherField::class, |
| 231 |
'selectandother' => HTMLSelectAndOtherField::class, |
| 232 |
'namespaceselect' => HTMLSelectNamespace::class, |
| 233 |
'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class, |
| 234 |
'tagfilter' => HTMLTagFilter::class, |
| 235 |
'sizefilter' => HTMLSizeFilterField::class, |
| 236 |
'button' => HTMLButtonField::class, |
| 237 |
'submit' => HTMLSubmitField::class, |
| 238 |
'hidden' => HTMLHiddenField::class, |
| 239 |
'edittools' => HTMLEditTools::class, |
| 240 |
'checkmatrix' => HTMLCheckMatrix::class, |
| 241 |
'cloner' => HTMLFormFieldCloner::class, |
| 242 |
'autocompleteselect' => HTMLAutoCompleteSelectField::class, |
| 243 |
'language' => HTMLSelectLanguageField::class, |
| 244 |
'date' => HTMLDateTimeField::class, |
| 245 |
'time' => HTMLDateTimeField::class, |
| 246 |
'datetime' => HTMLDateTimeField::class, |
| 247 |
'expiry' => HTMLExpiryField::class, |
| 248 |
'timezone' => HTMLTimezoneField::class, |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
'email' => HTMLTextField::class, |
| 253 |
'password' => HTMLTextField::class, |
| 254 |
'url' => HTMLTextField::class, |
| 255 |
'title' => HTMLTitleTextField::class, |
| 256 |
'user' => HTMLUserTextField::class, |
| 257 |
'tagmultiselect' => HTMLTagMultiselectField::class, |
| 258 |
'usersmultiselect' => HTMLUsersMultiselectField::class, |
| 259 |
'titlesmultiselect' => HTMLTitlesMultiselectField::class, |
| 260 |
'namespacesmultiselect' => HTMLNamespacesMultiselectField::class, |
| 261 |
]; |
| 262 |
|
| 263 |
|
| 264 |
public $mFieldData; |
| 265 |
|
| 266 |
|
| 267 |
protected $mMessagePrefix; |
| 268 |
|
| 269 |
|
| 270 |
protected $mFlatFields = []; |
| 271 |
|
| 272 |
protected $mFieldTree = []; |
| 273 |
|
| 274 |
protected $mShowSubmit = true; |
| 275 |
|
| 276 |
protected $mSubmitFlags = [ 'primary', 'progressive' ]; |
| 277 |
|
| 278 |
protected $mShowCancel = false; |
| 279 |
|
| 280 |
protected $mCancelTarget; |
| 281 |
|
| 282 |
|
| 283 |
protected $mSubmitCallback; |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
protected $mValidationErrorMessage; |
| 289 |
|
| 290 |
|
| 291 |
protected $mPre = ''; |
| 292 |
|
| 293 |
protected $mHeader = ''; |
| 294 |
|
| 295 |
protected $mFooter = ''; |
| 296 |
|
| 297 |
protected $mSectionHeaders = []; |
| 298 |
|
| 299 |
protected $mSectionFooters = []; |
| 300 |
|
| 301 |
protected $mPost = ''; |
| 302 |
|
| 303 |
protected $mId; |
| 304 |
|
| 305 |
protected $mName; |
| 306 |
|
| 307 |
protected $mTableId = ''; |
| 308 |
|
| 309 |
|
| 310 |
protected $mSubmitID; |
| 311 |
|
| 312 |
protected $mSubmitName; |
| 313 |
|
| 314 |
protected $mSubmitText; |
| 315 |
|
| 316 |
protected $mSubmitTooltip; |
| 317 |
|
| 318 |
|
| 319 |
protected $mFormIdentifier; |
| 320 |
|
| 321 |
protected $mSingleForm = false; |
| 322 |
|
| 323 |
|
| 324 |
protected $mTitle; |
| 325 |
|
| 326 |
protected $mMethod = 'post'; |
| 327 |
|
| 328 |
protected $mWasSubmitted = false; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
protected $mAction = false; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
protected $mCollapsible = false; |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
protected $mCollapsed = false; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
protected $mAutocomplete = null; |
| 357 |
|
| 358 |
|
| 359 |
protected $mUseMultipart = false; |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
protected $mHiddenFields = []; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
protected $mButtons = []; |
| 370 |
|
| 371 |
|
| 372 |
protected $mWrapperLegend = false; |
| 373 |
|
| 374 |
protected $mWrapperAttributes = []; |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
protected $mTokenSalt = ''; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
protected $mSections = []; |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
protected $mSubSectionBeforeFields = true; |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
protected $displayFormat = 'table'; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
protected $availableDisplayFormats = [ |
| 418 |
'table', |
| 419 |
'div', |
| 420 |
'raw', |
| 421 |
'inline', |
| 422 |
]; |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
protected $availableSubclassDisplayFormats = [ |
| 429 |
'vform', |
| 430 |
'codex', |
| 431 |
'ooui', |
| 432 |
]; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
private $hiddenTitleAddedToForm = false; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public static function factory( |
| 454 |
$displayFormat, $descriptor, IContextSource $context, $messagePrefix = '' |
| 455 |
) { |
| 456 |
switch ( $displayFormat ) { |
| 457 |
case 'codex': |
| 458 |
return new CodexHTMLForm( $descriptor, $context, $messagePrefix ); |
| 459 |
case 'vform': |
| 460 |
return new VFormHTMLForm( $descriptor, $context, $messagePrefix ); |
| 461 |
case 'ooui': |
| 462 |
return new OOUIHTMLForm( $descriptor, $context, $messagePrefix ); |
| 463 |
default: |
| 464 |
$form = new self( $descriptor, $context, $messagePrefix ); |
| 465 |
$form->setDisplayFormat( $displayFormat ); |
| 466 |
return $form; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
public function __construct( |
| 482 |
$descriptor, IContextSource $context, $messagePrefix = '' |
| 483 |
) { |
| 484 |
$this->setContext( $context ); |
| 485 |
$this->mMessagePrefix = $messagePrefix; |
| 486 |
$this->addFields( $descriptor ); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
public function addFields( $descriptor ) { |
| 499 |
$loadedDescriptor = []; |
| 500 |
|
| 501 |
foreach ( $descriptor as $fieldname => $info ) { |
| 502 |
$section = $info['section'] ?? ''; |
| 503 |
|
| 504 |
if ( isset( $info['type'] ) && $info['type'] === 'file' ) { |
| 505 |
$this->mUseMultipart = true; |
| 506 |
} |
| 507 |
|
| 508 |
$field = static::loadInputFromParameters( $fieldname, $info, $this ); |
| 509 |
|
| 510 |
$setSection =& $loadedDescriptor; |
| 511 |
if ( $section ) { |
| 512 |
foreach ( explode( '/', $section ) as $newName ) { |
| 513 |
$setSection[$newName] ??= []; |
| 514 |
$setSection =& $setSection[$newName]; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
$setSection[$fieldname] = $field; |
| 519 |
$this->mFlatFields[$fieldname] = $field; |
| 520 |
} |
| 521 |
|
| 522 |
$this->mFieldTree = array_merge_recursive( $this->mFieldTree, $loadedDescriptor ); |
| 523 |
|
| 524 |
return $this; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public function hasField( $fieldname ) { |
| 532 |
return isset( $this->mFlatFields[$fieldname] ); |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
public function getField( $fieldname ) { |
| 541 |
if ( !$this->hasField( $fieldname ) ) { |
| 542 |
throw new DomainException( __METHOD__ . ': no field named ' . $fieldname ); |
| 543 |
} |
| 544 |
return $this->mFlatFields[$fieldname]; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
public function setDisplayFormat( $format ) { |
| 557 |
if ( |
| 558 |
in_array( $format, $this->availableSubclassDisplayFormats, true ) || |
| 559 |
in_array( $this->displayFormat, $this->availableSubclassDisplayFormats, true ) |
| 560 |
) { |
| 561 |
throw new LogicException( 'Cannot change display format after creation, ' . |
| 562 |
'use HTMLForm::factory() instead' ); |
| 563 |
} |
| 564 |
|
| 565 |
if ( !in_array( $format, $this->availableDisplayFormats, true ) ) { |
| 566 |
throw new InvalidArgumentException( 'Display format must be one of ' . |
| 567 |
print_r( |
| 568 |
array_merge( |
| 569 |
$this->availableDisplayFormats, |
| 570 |
$this->availableSubclassDisplayFormats |
| 571 |
), |
| 572 |
true |
| 573 |
) ); |
| 574 |
} |
| 575 |
|
| 576 |
$this->displayFormat = $format; |
| 577 |
|
| 578 |
return $this; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
public function getDisplayFormat() { |
| 587 |
return $this->displayFormat; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
public static function getClassFromDescriptor( $fieldname, &$descriptor ) { |
| 607 |
if ( isset( $descriptor['class'] ) ) { |
| 608 |
$class = $descriptor['class']; |
| 609 |
} elseif ( isset( $descriptor['type'] ) ) { |
| 610 |
$class = static::$typeMappings[$descriptor['type']]; |
| 611 |
$descriptor['class'] = $class; |
| 612 |
} else { |
| 613 |
$class = null; |
| 614 |
} |
| 615 |
|
| 616 |
if ( !$class ) { |
| 617 |
throw new InvalidArgumentException( "Descriptor with no class for $fieldname: " |
| 618 |
. print_r( $descriptor, true ) ); |
| 619 |
} |
| 620 |
|
| 621 |
return $class; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public static function loadInputFromParameters( $fieldname, $descriptor, |
| 637 |
?HTMLForm $parent = null |
| 638 |
) { |
| 639 |
$class = static::getClassFromDescriptor( $fieldname, $descriptor ); |
| 640 |
|
| 641 |
$descriptor['fieldname'] = $fieldname; |
| 642 |
if ( $parent ) { |
| 643 |
$descriptor['parent'] = $parent; |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
return new $class( $descriptor ); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
public function prepareForm() { |
| 661 |
|
| 662 |
if ( |
| 663 |
$this->mFormIdentifier === null || |
| 664 |
$this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier || |
| 665 |
( $this->mSingleForm && $this->getMethod() === 'get' ) |
| 666 |
) { |
| 667 |
$this->loadFieldData(); |
| 668 |
} else { |
| 669 |
$this->mFieldData = []; |
| 670 |
} |
| 671 |
|
| 672 |
return $this; |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public function tryAuthorizedSubmit() { |
| 680 |
$result = false; |
| 681 |
|
| 682 |
if ( $this->mFormIdentifier === null ) { |
| 683 |
$identOkay = true; |
| 684 |
} else { |
| 685 |
$identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier; |
| 686 |
} |
| 687 |
|
| 688 |
$tokenOkay = false; |
| 689 |
if ( $this->getMethod() !== 'post' ) { |
| 690 |
$tokenOkay = true; |
| 691 |
} elseif ( $this->getRequest()->wasPosted() ) { |
| 692 |
$editToken = $this->getRequest()->getVal( 'wpEditToken' ); |
| 693 |
if ( $this->getUser()->isRegistered() || $editToken !== null ) { |
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
$tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt ); |
| 698 |
} else { |
| 699 |
$tokenOkay = true; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
if ( $tokenOkay && $identOkay ) { |
| 704 |
$this->mWasSubmitted = true; |
| 705 |
$result = $this->trySubmit(); |
| 706 |
} |
| 707 |
|
| 708 |
return $result; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
public function show() { |
| 719 |
$this->prepareForm(); |
| 720 |
|
| 721 |
$result = $this->tryAuthorizedSubmit(); |
| 722 |
if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) { |
| 723 |
return $result; |
| 724 |
} |
| 725 |
|
| 726 |
$this->displayForm( $result ); |
| 727 |
|
| 728 |
return false; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
public function showAlways() { |
| 737 |
$this->prepareForm(); |
| 738 |
|
| 739 |
$result = $this->tryAuthorizedSubmit(); |
| 740 |
|
| 741 |
$this->displayForm( $result ); |
| 742 |
|
| 743 |
return $result; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
public function trySubmit() { |
| 758 |
$valid = true; |
| 759 |
$hoistedErrors = Status::newGood(); |
| 760 |
if ( $this->mValidationErrorMessage ) { |
| 761 |
foreach ( $this->mValidationErrorMessage as $error ) { |
| 762 |
$hoistedErrors->fatal( ...$error ); |
| 763 |
} |
| 764 |
} else { |
| 765 |
$hoistedErrors->fatal( 'htmlform-invalid-input' ); |
| 766 |
} |
| 767 |
|
| 768 |
$this->mWasSubmitted = true; |
| 769 |
|
| 770 |
|
| 771 |
foreach ( $this->mFlatFields as $fieldname => $field ) { |
| 772 |
if ( !array_key_exists( $fieldname, $this->mFieldData ) ) { |
| 773 |
continue; |
| 774 |
} |
| 775 |
if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) { |
| 776 |
$this->mWasSubmitted = false; |
| 777 |
return false; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
$hasNonDefault = false; |
| 783 |
foreach ( $this->mFlatFields as $fieldname => $field ) { |
| 784 |
if ( !array_key_exists( $fieldname, $this->mFieldData ) ) { |
| 785 |
continue; |
| 786 |
} |
| 787 |
$hasNonDefault = $hasNonDefault || $this->mFieldData[$fieldname] !== $field->getDefault(); |
| 788 |
if ( $field->isDisabled( $this->mFieldData ) ) { |
| 789 |
continue; |
| 790 |
} |
| 791 |
$res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData ); |
| 792 |
if ( $res !== true ) { |
| 793 |
$valid = false; |
| 794 |
if ( $res !== false && !$field->canDisplayErrors() ) { |
| 795 |
if ( is_string( $res ) ) { |
| 796 |
$hoistedErrors->fatal( 'rawmessage', $res ); |
| 797 |
} else { |
| 798 |
$hoistedErrors->fatal( $res ); |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
if ( !$valid ) { |
| 805 |
|
| 806 |
if ( !$hasNonDefault && $this->getMethod() === 'get' && |
| 807 |
( $this->mFormIdentifier === null || |
| 808 |
$this->getRequest()->getCheck( 'wpFormIdentifier' ) ) |
| 809 |
) { |
| 810 |
$this->mWasSubmitted = false; |
| 811 |
return false; |
| 812 |
} |
| 813 |
return $hoistedErrors; |
| 814 |
} |
| 815 |
|
| 816 |
$callback = $this->mSubmitCallback; |
| 817 |
if ( !is_callable( $callback ) ) { |
| 818 |
throw new LogicException( 'HTMLForm: no submit callback provided. Use ' . |
| 819 |
'setSubmitCallback() to set one.' ); |
| 820 |
} |
| 821 |
|
| 822 |
$data = $this->filterDataForSubmit( $this->mFieldData ); |
| 823 |
|
| 824 |
$res = call_user_func( $callback, $data, $this ); |
| 825 |
if ( $res === false ) { |
| 826 |
$this->mWasSubmitted = false; |
| 827 |
} elseif ( $res instanceof StatusValue ) { |
| 828 |
|
| 829 |
$res = Status::wrap( $res ); |
| 830 |
} |
| 831 |
|
| 832 |
return $res; |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
public function wasSubmitted() { |
| 847 |
return $this->mWasSubmitted; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
public function setSubmitCallback( $cb ) { |
| 861 |
$this->mSubmitCallback = $cb; |
| 862 |
|
| 863 |
return $this; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
public function setValidationErrorMessage( $msg ) { |
| 876 |
$this->mValidationErrorMessage = $msg; |
| 877 |
|
| 878 |
return $this; |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
public function setIntro( $msg ) { |
| 890 |
wfDeprecated( __METHOD__, '1.38' ); |
| 891 |
return $this->setPreHtml( $msg ); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
public function setPreHtml( $html ) { |
| 903 |
$this->mPre = $html; |
| 904 |
|
| 905 |
return $this; |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
public function addPreHtml( $html ) { |
| 917 |
$this->mPre .= $html; |
| 918 |
|
| 919 |
return $this; |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
public function getPreHtml() { |
| 929 |
return $this->mPre; |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
public function setPreText( $msg ) { |
| 941 |
wfDeprecated( __METHOD__, '1.38' ); |
| 942 |
return $this->setPreHtml( $msg ); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
public function addPreText( $msg ) { |
| 954 |
wfDeprecated( __METHOD__, '1.38' ); |
| 955 |
return $this->addPreHtml( $msg ); |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
public function getPreText() { |
| 966 |
wfDeprecated( __METHOD__, '1.38' ); |
| 967 |
return $this->getPreHtml(); |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
public function addHeaderHtml( $html, $section = null ) { |
| 980 |
if ( $section === null ) { |
| 981 |
$this->mHeader .= $html; |
| 982 |
} else { |
| 983 |
$this->mSectionHeaders[$section] ??= ''; |
| 984 |
$this->mSectionHeaders[$section] .= $html; |
| 985 |
} |
| 986 |
|
| 987 |
return $this; |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
public function setHeaderHtml( $html, $section = null ) { |
| 1000 |
if ( $section === null ) { |
| 1001 |
$this->mHeader = $html; |
| 1002 |
} else { |
| 1003 |
$this->mSectionHeaders[$section] = $html; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return $this; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
public function getHeaderHtml( $section = null ) { |
| 1018 |
return $section ? $this->mSectionHeaders[$section] ?? '' : $this->mHeader; |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
public function addHeaderText( $msg, $section = null ) { |
| 1031 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1032 |
return $this->addHeaderHtml( $msg, $section ); |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
public function setHeaderText( $msg, $section = null ) { |
| 1046 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1047 |
return $this->setHeaderHtml( $msg, $section ); |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
public function getHeaderText( $section = null ) { |
| 1060 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1061 |
return $this->getHeaderHtml( $section ); |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
public function addFooterHtml( $html, $section = null ) { |
| 1074 |
if ( $section === null ) { |
| 1075 |
$this->mFooter .= $html; |
| 1076 |
} else { |
| 1077 |
$this->mSectionFooters[$section] ??= ''; |
| 1078 |
$this->mSectionFooters[$section] .= $html; |
| 1079 |
} |
| 1080 |
|
| 1081 |
return $this; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
public function setFooterHtml( $html, $section = null ) { |
| 1094 |
if ( $section === null ) { |
| 1095 |
$this->mFooter = $html; |
| 1096 |
} else { |
| 1097 |
$this->mSectionFooters[$section] = $html; |
| 1098 |
} |
| 1099 |
|
| 1100 |
return $this; |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
public function getFooterHtml( $section = null ) { |
| 1111 |
return $section ? $this->mSectionFooters[$section] ?? '' : $this->mFooter; |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
public function addFooterText( $msg, $section = null ) { |
| 1124 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1125 |
return $this->addFooterHtml( $msg, $section ); |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
public function setFooterText( $msg, $section = null ) { |
| 1139 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1140 |
return $this->setFooterHtml( $msg, $section ); |
| 1141 |
} |
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
public function getFooterText( $section = null ) { |
| 1152 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1153 |
return $this->getFooterHtml( $section ); |
| 1154 |
} |
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
public function addPostHtml( $html ) { |
| 1165 |
$this->mPost .= $html; |
| 1166 |
|
| 1167 |
return $this; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
public function setPostHtml( $html ) { |
| 1179 |
$this->mPost = $html; |
| 1180 |
|
| 1181 |
return $this; |
| 1182 |
} |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
public function getPostHtml() { |
| 1191 |
return $this->mPost; |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
public function addPostText( $msg ) { |
| 1203 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1204 |
return $this->addPostHtml( $msg ); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
public function setPostText( $msg ) { |
| 1216 |
wfDeprecated( __METHOD__, '1.38' ); |
| 1217 |
return $this->setPostHtml( $msg ); |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
public function setSections( $sections ) { |
| 1230 |
if ( $this->getDisplayFormat() !== 'codex' ) { |
| 1231 |
throw new \InvalidArgumentException( |
| 1232 |
"Non-Codex HTMLForms do not support additional section information." |
| 1233 |
); |
| 1234 |
} |
| 1235 |
|
| 1236 |
$this->mSections = $sections; |
| 1237 |
|
| 1238 |
return $this; |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
public function addHiddenField( $name, $value, array $attribs = [] ) { |
| 1252 |
if ( !is_array( $value ) ) { |
| 1253 |
|
| 1254 |
$attribs += [ 'name' => $name ]; |
| 1255 |
$this->mHiddenFields[] = [ $value, $attribs ]; |
| 1256 |
} |
| 1257 |
|
| 1258 |
return $this; |
| 1259 |
} |
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
public function addHiddenFields( array $fields ) { |
| 1273 |
foreach ( $fields as $name => $value ) { |
| 1274 |
if ( is_array( $value ) ) { |
| 1275 |
|
| 1276 |
continue; |
| 1277 |
} |
| 1278 |
$this->mHiddenFields[] = [ $value, [ 'name' => $name ] ]; |
| 1279 |
} |
| 1280 |
|
| 1281 |
return $this; |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
public function addButton( $data ) { |
| 1308 |
if ( !is_array( $data ) ) { |
| 1309 |
$args = func_get_args(); |
| 1310 |
if ( count( $args ) < 2 || count( $args ) > 4 ) { |
| 1311 |
throw new InvalidArgumentException( |
| 1312 |
'Incorrect number of arguments for deprecated calling style' |
| 1313 |
); |
| 1314 |
} |
| 1315 |
$data = [ |
| 1316 |
'name' => $args[0], |
| 1317 |
'value' => $args[1], |
| 1318 |
'id' => $args[2] ?? null, |
| 1319 |
'attribs' => $args[3] ?? null, |
| 1320 |
]; |
| 1321 |
} else { |
| 1322 |
if ( !isset( $data['name'] ) ) { |
| 1323 |
throw new InvalidArgumentException( 'A name is required' ); |
| 1324 |
} |
| 1325 |
if ( !isset( $data['value'] ) ) { |
| 1326 |
throw new InvalidArgumentException( 'A value is required' ); |
| 1327 |
} |
| 1328 |
} |
| 1329 |
$this->mButtons[] = $data + [ |
| 1330 |
'id' => null, |
| 1331 |
'attribs' => null, |
| 1332 |
'flags' => null, |
| 1333 |
'framed' => true, |
| 1334 |
]; |
| 1335 |
|
| 1336 |
return $this; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
public function setTokenSalt( $salt ) { |
| 1349 |
$this->mTokenSalt = $salt; |
| 1350 |
|
| 1351 |
return $this; |
| 1352 |
} |
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
public function displayForm( $submitResult ) { |
| 1369 |
$this->getOutput()->addHTML( $this->getHTML( $submitResult ) ); |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
private function getHiddenTitle(): string { |
| 1377 |
if ( $this->hiddenTitleAddedToForm ) { |
| 1378 |
return ''; |
| 1379 |
} |
| 1380 |
|
| 1381 |
$html = ''; |
| 1382 |
if ( $this->getMethod() === 'post' || |
| 1383 |
$this->getAction() === $this->getConfig()->get( MainConfigNames::Script ) |
| 1384 |
) { |
| 1385 |
$html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n"; |
| 1386 |
} |
| 1387 |
$this->hiddenTitleAddedToForm = true; |
| 1388 |
return $html; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
public function getHTML( $submitResult ) { |
| 1402 |
|
| 1403 |
$this->getOutput()->getMetadata()->setPreventClickjacking( true ); |
| 1404 |
$this->getOutput()->addModules( 'mediawiki.htmlform' ); |
| 1405 |
$this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' ); |
| 1406 |
|
| 1407 |
if ( $this->mCollapsible ) { |
| 1408 |
|
| 1409 |
$this->getOutput()->addModules( 'jquery.makeCollapsible' ); |
| 1410 |
} |
| 1411 |
|
| 1412 |
$headerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getHeaderText', '1.38' ) |
| 1413 |
? $this->getHeaderText() |
| 1414 |
: $this->getHeaderHtml(); |
| 1415 |
$footerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getFooterText', '1.38' ) |
| 1416 |
? $this->getFooterText() |
| 1417 |
: $this->getFooterHtml(); |
| 1418 |
$html = $this->getErrorsOrWarnings( $submitResult, 'error' ) |
| 1419 |
. $this->getErrorsOrWarnings( $submitResult, 'warning' ) |
| 1420 |
. $headerHtml |
| 1421 |
. $this->getHiddenTitle() |
| 1422 |
. $this->getBody() |
| 1423 |
. $this->getHiddenFields() |
| 1424 |
. $this->getButtons() |
| 1425 |
. $footerHtml; |
| 1426 |
|
| 1427 |
return $this->mPre . $this->wrapForm( $html ) . $this->mPost; |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
public function setCollapsibleOptions( $collapsedByDefault = false ) { |
| 1438 |
$this->mCollapsible = true; |
| 1439 |
$this->mCollapsed = $collapsedByDefault; |
| 1440 |
return $this; |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
protected function getFormAttributes() { |
| 1449 |
|
| 1450 |
$encType = $this->mUseMultipart |
| 1451 |
? 'multipart/form-data' |
| 1452 |
: 'application/x-www-form-urlencoded'; |
| 1453 |
|
| 1454 |
$attribs = [ |
| 1455 |
'class' => 'mw-htmlform', |
| 1456 |
'action' => $this->getAction(), |
| 1457 |
'method' => $this->getMethod(), |
| 1458 |
'enctype' => $encType, |
| 1459 |
]; |
| 1460 |
if ( $this->mId ) { |
| 1461 |
$attribs['id'] = $this->mId; |
| 1462 |
} |
| 1463 |
if ( is_string( $this->mAutocomplete ) ) { |
| 1464 |
$attribs['autocomplete'] = $this->mAutocomplete; |
| 1465 |
} |
| 1466 |
if ( $this->mName ) { |
| 1467 |
$attribs['name'] = $this->mName; |
| 1468 |
} |
| 1469 |
if ( $this->needsJSForHtml5FormValidation() ) { |
| 1470 |
$attribs['novalidate'] = true; |
| 1471 |
} |
| 1472 |
return $attribs; |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
public function wrapForm( $html ) { |
| 1483 |
|
| 1484 |
if ( $this->mWrapperLegend !== false ) { |
| 1485 |
$legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false; |
| 1486 |
$html = Html::rawElement( |
| 1487 |
'fieldset', |
| 1488 |
$this->mWrapperAttributes, |
| 1489 |
( $legend ? Html::element( 'legend', [], $legend ) : '' ) . $html |
| 1490 |
); |
| 1491 |
} |
| 1492 |
|
| 1493 |
return Html::rawElement( |
| 1494 |
'form', |
| 1495 |
$this->getFormAttributes(), |
| 1496 |
$html |
| 1497 |
); |
| 1498 |
} |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
public function getHiddenFields() { |
| 1505 |
$html = ''; |
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
$html .= $this->getHiddenTitle(); |
| 1510 |
|
| 1511 |
if ( $this->mFormIdentifier !== null ) { |
| 1512 |
$html .= Html::hidden( |
| 1513 |
'wpFormIdentifier', |
| 1514 |
$this->mFormIdentifier |
| 1515 |
) . "\n"; |
| 1516 |
} |
| 1517 |
if ( $this->getMethod() === 'post' ) { |
| 1518 |
$html .= Html::hidden( |
| 1519 |
'wpEditToken', |
| 1520 |
$this->getUser()->getEditToken( $this->mTokenSalt ), |
| 1521 |
[ 'id' => 'wpEditToken' ] |
| 1522 |
) . "\n"; |
| 1523 |
} |
| 1524 |
|
| 1525 |
foreach ( $this->mHiddenFields as [ $value, $attribs ] ) { |
| 1526 |
$html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n"; |
| 1527 |
} |
| 1528 |
|
| 1529 |
return $html; |
| 1530 |
} |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
public function getButtons() { |
| 1538 |
$buttons = ''; |
| 1539 |
|
| 1540 |
if ( $this->mShowSubmit ) { |
| 1541 |
$attribs = []; |
| 1542 |
|
| 1543 |
if ( isset( $this->mSubmitID ) ) { |
| 1544 |
$attribs['id'] = $this->mSubmitID; |
| 1545 |
} |
| 1546 |
|
| 1547 |
if ( isset( $this->mSubmitName ) ) { |
| 1548 |
$attribs['name'] = $this->mSubmitName; |
| 1549 |
} |
| 1550 |
|
| 1551 |
if ( isset( $this->mSubmitTooltip ) ) { |
| 1552 |
$attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip ); |
| 1553 |
} |
| 1554 |
|
| 1555 |
$attribs['class'] = [ 'mw-htmlform-submit' ]; |
| 1556 |
|
| 1557 |
$buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n"; |
| 1558 |
} |
| 1559 |
|
| 1560 |
if ( $this->mShowCancel ) { |
| 1561 |
$target = $this->getCancelTargetURL(); |
| 1562 |
$buttons .= Html::element( |
| 1563 |
'a', |
| 1564 |
[ |
| 1565 |
'href' => $target, |
| 1566 |
], |
| 1567 |
$this->msg( 'cancel' )->text() |
| 1568 |
) . "\n"; |
| 1569 |
} |
| 1570 |
|
| 1571 |
foreach ( $this->mButtons as $button ) { |
| 1572 |
$attrs = [ |
| 1573 |
'type' => 'submit', |
| 1574 |
'name' => $button['name'], |
| 1575 |
'value' => $button['value'] |
| 1576 |
]; |
| 1577 |
|
| 1578 |
if ( isset( $button['label-message'] ) ) { |
| 1579 |
$label = $this->getMessage( $button['label-message'] )->parse(); |
| 1580 |
} elseif ( isset( $button['label'] ) ) { |
| 1581 |
$label = htmlspecialchars( $button['label'] ); |
| 1582 |
} elseif ( isset( $button['label-raw'] ) ) { |
| 1583 |
$label = $button['label-raw']; |
| 1584 |
} else { |
| 1585 |
$label = htmlspecialchars( $button['value'] ); |
| 1586 |
} |
| 1587 |
|
| 1588 |
|
| 1589 |
if ( $button['attribs'] ) { |
| 1590 |
|
| 1591 |
$attrs += $button['attribs']; |
| 1592 |
} |
| 1593 |
|
| 1594 |
if ( isset( $button['id'] ) ) { |
| 1595 |
$attrs['id'] = $button['id']; |
| 1596 |
} |
| 1597 |
|
| 1598 |
$buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n"; |
| 1599 |
} |
| 1600 |
|
| 1601 |
if ( !$buttons ) { |
| 1602 |
return ''; |
| 1603 |
} |
| 1604 |
|
| 1605 |
return Html::rawElement( 'span', |
| 1606 |
[ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n"; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
public function getBody() { |
| 1615 |
return $this->displaySection( $this->mFieldTree, $this->mTableId ); |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
public function getErrorsOrWarnings( $elements, $elementsType ) { |
| 1628 |
if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) { |
| 1629 |
throw new DomainException( $elementsType . ' is not a valid type.' ); |
| 1630 |
} |
| 1631 |
$elementstr = false; |
| 1632 |
if ( $elements instanceof Status ) { |
| 1633 |
[ $errorStatus, $warningStatus ] = $elements->splitByErrorType(); |
| 1634 |
$status = $elementsType === 'error' ? $errorStatus : $warningStatus; |
| 1635 |
if ( $status->isGood() ) { |
| 1636 |
$elementstr = ''; |
| 1637 |
} else { |
| 1638 |
$elementstr = $status |
| 1639 |
->getMessage() |
| 1640 |
->setContext( $this ) |
| 1641 |
->setInterfaceMessageFlag( true ) |
| 1642 |
->parse(); |
| 1643 |
} |
| 1644 |
} elseif ( $elementsType === 'error' ) { |
| 1645 |
if ( is_array( $elements ) ) { |
| 1646 |
$elementstr = $this->formatErrors( $elements ); |
| 1647 |
} elseif ( $elements && $elements !== true ) { |
| 1648 |
$elementstr = (string)$elements; |
| 1649 |
} |
| 1650 |
} |
| 1651 |
|
| 1652 |
if ( !$elementstr ) { |
| 1653 |
return ''; |
| 1654 |
} elseif ( $elementsType === 'error' ) { |
| 1655 |
return Html::errorBox( $elementstr ); |
| 1656 |
} else { |
| 1657 |
return Html::warningBox( $elementstr ); |
| 1658 |
} |
| 1659 |
} |
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
public function formatErrors( $errors ) { |
| 1669 |
$errorstr = ''; |
| 1670 |
|
| 1671 |
foreach ( $errors as $error ) { |
| 1672 |
$errorstr .= Html::rawElement( |
| 1673 |
'li', |
| 1674 |
[], |
| 1675 |
$this->getMessage( $error )->parse() |
| 1676 |
); |
| 1677 |
} |
| 1678 |
|
| 1679 |
return Html::rawElement( 'ul', [], $errorstr ); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
public function setSubmitText( $t ) { |
| 1690 |
$this->mSubmitText = $t; |
| 1691 |
|
| 1692 |
return $this; |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
public function setSubmitDestructive() { |
| 1702 |
$this->mSubmitFlags = [ 'destructive', 'primary' ]; |
| 1703 |
|
| 1704 |
return $this; |
| 1705 |
} |
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
public function setSubmitTextMsg( $msg ) { |
| 1716 |
if ( !$msg instanceof Message ) { |
| 1717 |
$msg = $this->msg( $msg ); |
| 1718 |
} |
| 1719 |
$this->setSubmitText( $msg->text() ); |
| 1720 |
|
| 1721 |
return $this; |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
public function getSubmitText() { |
| 1729 |
return $this->mSubmitText ?: $this->msg( 'htmlform-submit' )->text(); |
| 1730 |
} |
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
public function setSubmitName( $name ) { |
| 1738 |
$this->mSubmitName = $name; |
| 1739 |
|
| 1740 |
return $this; |
| 1741 |
} |
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
public function setSubmitTooltip( $name ) { |
| 1749 |
$this->mSubmitTooltip = $name; |
| 1750 |
|
| 1751 |
return $this; |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
public function setSubmitID( $t ) { |
| 1763 |
$this->mSubmitID = $t; |
| 1764 |
|
| 1765 |
return $this; |
| 1766 |
} |
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
public function setFormIdentifier( string $ident, bool $single = false ) { |
| 1787 |
$this->mFormIdentifier = $ident; |
| 1788 |
$this->mSingleForm = $single; |
| 1789 |
|
| 1790 |
return $this; |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
public function suppressDefaultSubmit( $suppressSubmit = true ) { |
| 1804 |
$this->mShowSubmit = !$suppressSubmit; |
| 1805 |
|
| 1806 |
return $this; |
| 1807 |
} |
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
public function showCancel( $show = true ) { |
| 1816 |
$this->mShowCancel = $show; |
| 1817 |
return $this; |
| 1818 |
} |
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
public function setCancelTarget( $target ) { |
| 1827 |
if ( $target instanceof PageReference ) { |
| 1828 |
$target = TitleValue::castPageToLinkTarget( $target ); |
| 1829 |
} |
| 1830 |
|
| 1831 |
$this->mCancelTarget = $target; |
| 1832 |
return $this; |
| 1833 |
} |
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
protected function getCancelTargetURL() { |
| 1840 |
if ( is_string( $this->mCancelTarget ) ) { |
| 1841 |
return $this->mCancelTarget; |
| 1842 |
} else { |
| 1843 |
|
| 1844 |
$target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage(); |
| 1845 |
return $target->getLocalURL(); |
| 1846 |
} |
| 1847 |
} |
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
public function setTableId( $id ) { |
| 1859 |
$this->mTableId = $id; |
| 1860 |
|
| 1861 |
return $this; |
| 1862 |
} |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
public function setId( $id ) { |
| 1870 |
$this->mId = $id; |
| 1871 |
|
| 1872 |
return $this; |
| 1873 |
} |
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
public function setName( $name ) { |
| 1880 |
$this->mName = $name; |
| 1881 |
|
| 1882 |
return $this; |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
public function setWrapperLegend( $legend ) { |
| 1897 |
$this->mWrapperLegend = $legend; |
| 1898 |
|
| 1899 |
return $this; |
| 1900 |
} |
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
public function setWrapperAttributes( $attributes ) { |
| 1910 |
$this->mWrapperAttributes = $attributes; |
| 1911 |
|
| 1912 |
return $this; |
| 1913 |
} |
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
public function setWrapperLegendMsg( $msg ) { |
| 1925 |
if ( !$msg instanceof Message ) { |
| 1926 |
$msg = $this->msg( $msg ); |
| 1927 |
} |
| 1928 |
$this->setWrapperLegend( $msg->text() ); |
| 1929 |
|
| 1930 |
return $this; |
| 1931 |
} |
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
public function setMessagePrefix( $p ) { |
| 1943 |
$this->mMessagePrefix = $p; |
| 1944 |
|
| 1945 |
return $this; |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
public function setTitle( $t ) { |
| 1956 |
|
| 1957 |
$this->mTitle = Title::castFromPageReference( $t ); |
| 1958 |
|
| 1959 |
return $this; |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
public function getTitle() { |
| 1966 |
return $this->mTitle ?: $this->getContext()->getTitle(); |
| 1967 |
} |
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
|
| 1973 |
|
| 1974 |
|
| 1975 |
|
| 1976 |
public function setMethod( $method = 'post' ) { |
| 1977 |
$this->mMethod = strtolower( $method ); |
| 1978 |
|
| 1979 |
return $this; |
| 1980 |
} |
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
public function getMethod() { |
| 1986 |
return $this->mMethod; |
| 1987 |
} |
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) { |
| 2000 |
return Html::rawElement( |
| 2001 |
'fieldset', |
| 2002 |
$attributes, |
| 2003 |
Html::element( 'legend', [], $legend ) . $section |
| 2004 |
) . "\n"; |
| 2005 |
} |
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
public function displaySection( $fields, |
| 2026 |
$sectionName = '', |
| 2027 |
$fieldsetIDPrefix = '', |
| 2028 |
&$hasUserVisibleFields = false |
| 2029 |
) { |
| 2030 |
if ( $this->mFieldData === null ) { |
| 2031 |
throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. ' |
| 2032 |
. 'You probably called displayForm() without calling prepareForm() first.' ); |
| 2033 |
} |
| 2034 |
|
| 2035 |
$html = []; |
| 2036 |
$subsectionHtml = ''; |
| 2037 |
$hasLabel = false; |
| 2038 |
|
| 2039 |
foreach ( $fields as $key => $value ) { |
| 2040 |
if ( $value instanceof HTMLFormField ) { |
| 2041 |
$v = array_key_exists( $key, $this->mFieldData ) |
| 2042 |
? $this->mFieldData[$key] |
| 2043 |
: $value->getDefault(); |
| 2044 |
|
| 2045 |
$retval = $this->formatField( $value, $v ?? '' ); |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
if ( $value->hasVisibleOutput() ) { |
| 2050 |
$html[] = $retval; |
| 2051 |
|
| 2052 |
$labelValue = trim( $value->getLabel() ); |
| 2053 |
if ( $labelValue !== "\u{00A0}" && $labelValue !== ' ' && $labelValue !== '' ) { |
| 2054 |
$hasLabel = true; |
| 2055 |
} |
| 2056 |
|
| 2057 |
$hasUserVisibleFields = true; |
| 2058 |
} |
| 2059 |
} elseif ( is_array( $value ) ) { |
| 2060 |
$subsectionHasVisibleFields = false; |
| 2061 |
$section = |
| 2062 |
$this->displaySection( $value, |
| 2063 |
"mw-htmlform-$key", |
| 2064 |
"$fieldsetIDPrefix$key-", |
| 2065 |
$subsectionHasVisibleFields ); |
| 2066 |
|
| 2067 |
if ( $subsectionHasVisibleFields === true ) { |
| 2068 |
|
| 2069 |
$hasUserVisibleFields = true; |
| 2070 |
|
| 2071 |
$legend = $this->getLegend( $key ); |
| 2072 |
|
| 2073 |
$headerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getHeaderText', '1.38' ) |
| 2074 |
? $this->getHeaderText( $key ) |
| 2075 |
: $this->getHeaderHtml( $key ); |
| 2076 |
$footerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getFooterText', '1.38' ) |
| 2077 |
? $this->getFooterText( $key ) |
| 2078 |
: $this->getFooterHtml( $key ); |
| 2079 |
$section = $headerHtml . |
| 2080 |
$section . |
| 2081 |
$footerHtml; |
| 2082 |
|
| 2083 |
$attributes = []; |
| 2084 |
if ( $fieldsetIDPrefix ) { |
| 2085 |
$attributes['id'] = Sanitizer::escapeIdForAttribute( "$fieldsetIDPrefix$key" ); |
| 2086 |
} |
| 2087 |
$subsectionHtml .= $this->wrapFieldSetSection( |
| 2088 |
$legend, $section, $attributes, $fields === $this->mFieldTree |
| 2089 |
); |
| 2090 |
} else { |
| 2091 |
|
| 2092 |
$subsectionHtml .= $section; |
| 2093 |
} |
| 2094 |
} |
| 2095 |
} |
| 2096 |
|
| 2097 |
$html = $this->formatSection( $html, $sectionName, $hasLabel ); |
| 2098 |
|
| 2099 |
if ( $subsectionHtml ) { |
| 2100 |
if ( $this->mSubSectionBeforeFields ) { |
| 2101 |
return $subsectionHtml . "\n" . $html; |
| 2102 |
} else { |
| 2103 |
return $html . "\n" . $subsectionHtml; |
| 2104 |
} |
| 2105 |
} else { |
| 2106 |
return $html; |
| 2107 |
} |
| 2108 |
} |
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
protected function formatField( HTMLFormField $field, $value ) { |
| 2119 |
$displayFormat = $this->getDisplayFormat(); |
| 2120 |
switch ( $displayFormat ) { |
| 2121 |
case 'table': |
| 2122 |
return $field->getTableRow( $value ); |
| 2123 |
case 'div': |
| 2124 |
return $field->getDiv( $value ); |
| 2125 |
case 'raw': |
| 2126 |
return $field->getRaw( $value ); |
| 2127 |
case 'inline': |
| 2128 |
return $field->getInline( $value ); |
| 2129 |
default: |
| 2130 |
throw new LogicException( 'Not implemented' ); |
| 2131 |
} |
| 2132 |
} |
| 2133 |
|
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
|
| 2142 |
protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) { |
| 2143 |
if ( !$fieldsHtml ) { |
| 2144 |
|
| 2145 |
|
| 2146 |
return ''; |
| 2147 |
} |
| 2148 |
|
| 2149 |
$displayFormat = $this->getDisplayFormat(); |
| 2150 |
$html = implode( '', $fieldsHtml ); |
| 2151 |
|
| 2152 |
if ( $displayFormat === 'raw' ) { |
| 2153 |
return $html; |
| 2154 |
} |
| 2155 |
|
| 2156 |
|
| 2157 |
$attribs = $anyFieldHasLabel ? [] : [ 'class' => 'mw-htmlform-nolabel' ]; |
| 2158 |
|
| 2159 |
if ( $sectionName ) { |
| 2160 |
$attribs['id'] = Sanitizer::escapeIdForAttribute( $sectionName ); |
| 2161 |
} |
| 2162 |
|
| 2163 |
if ( $displayFormat === 'table' ) { |
| 2164 |
return Html::rawElement( 'table', |
| 2165 |
$attribs, |
| 2166 |
Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n"; |
| 2167 |
} elseif ( $displayFormat === 'inline' ) { |
| 2168 |
return Html::rawElement( 'span', $attribs, "\n$html\n" ); |
| 2169 |
} else { |
| 2170 |
return Html::rawElement( 'div', $attribs, "\n$html\n" ); |
| 2171 |
} |
| 2172 |
} |
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
public function loadData() { |
| 2178 |
$this->prepareForm(); |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
protected function loadFieldData() { |
| 2185 |
$fieldData = []; |
| 2186 |
$request = $this->getRequest(); |
| 2187 |
|
| 2188 |
foreach ( $this->mFlatFields as $fieldname => $field ) { |
| 2189 |
if ( $field->skipLoadData( $request ) ) { |
| 2190 |
continue; |
| 2191 |
} |
| 2192 |
if ( $field->mParams['disabled'] ?? false ) { |
| 2193 |
$fieldData[$fieldname] = $field->getDefault(); |
| 2194 |
} else { |
| 2195 |
$fieldData[$fieldname] = $field->loadDataFromRequest( $request ); |
| 2196 |
} |
| 2197 |
} |
| 2198 |
|
| 2199 |
|
| 2200 |
|
| 2201 |
foreach ( $fieldData as $name => &$value ) { |
| 2202 |
$field = $this->mFlatFields[$name]; |
| 2203 |
if ( $field->isDisabled( $fieldData ) ) { |
| 2204 |
$value = $field->getDefault(); |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
|
| 2209 |
foreach ( $fieldData as $name => &$value ) { |
| 2210 |
$field = $this->mFlatFields[$name]; |
| 2211 |
$value = $field->filter( $value, $fieldData ); |
| 2212 |
} |
| 2213 |
|
| 2214 |
$this->mFieldData = $fieldData; |
| 2215 |
} |
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
public function filterDataForSubmit( $data ) { |
| 2228 |
return $data; |
| 2229 |
} |
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
public function getLegend( $key ) { |
| 2241 |
return $this->msg( $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key )->text(); |
| 2242 |
} |
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
public function setAction( $action ) { |
| 2255 |
$this->mAction = $action; |
| 2256 |
|
| 2257 |
return $this; |
| 2258 |
} |
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
public function getAction() { |
| 2268 |
|
| 2269 |
if ( $this->mAction !== false ) { |
| 2270 |
return $this->mAction; |
| 2271 |
} |
| 2272 |
|
| 2273 |
$articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath ); |
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
if ( str_contains( $articlePath, '?' ) && $this->getMethod() === 'get' ) { |
| 2280 |
return $this->getConfig()->get( MainConfigNames::Script ); |
| 2281 |
} |
| 2282 |
|
| 2283 |
return $this->getTitle()->getLocalURL(); |
| 2284 |
} |
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
public function setAutocomplete( $autocomplete ) { |
| 2297 |
$this->mAutocomplete = $autocomplete; |
| 2298 |
|
| 2299 |
return $this; |
| 2300 |
} |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
protected function getMessage( $value ) { |
| 2309 |
return Message::newFromSpecifier( $value )->setContext( $this ); |
| 2310 |
} |
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
public function needsJSForHtml5FormValidation() { |
| 2322 |
foreach ( $this->mFlatFields as $field ) { |
| 2323 |
if ( $field->needsJSForHtml5FormValidation() ) { |
| 2324 |
return true; |
| 2325 |
} |
| 2326 |
} |
| 2327 |
return false; |
| 2328 |
} |
| 2329 |
} |
| 2330 |
|
| 2331 |
|
| 2332 |
class_alias( HTMLForm::class, 'HTMLForm' ); |