| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\HTMLForm\Field; |
| 4 |
|
| 5 |
use MediaWiki\MediaWikiServices; |
| 6 |
use MediaWiki\Widget\NamespacesMultiselectWidget; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class HTMLNamespacesMultiselectField extends HTMLSelectNamespace { |
| 21 |
public function loadDataFromRequest( $request ) { |
| 22 |
$value = $request->getText( $this->mName, $this->getDefault() ?? '' ); |
| 23 |
|
| 24 |
$namespaces = explode( "\n", $value ); |
| 25 |
|
| 26 |
$namespaces = array_values( array_filter( $namespaces, static function ( $namespace ) { |
| 27 |
return trim( $namespace ) !== ''; |
| 28 |
} ) ); |
| 29 |
|
| 30 |
return implode( "\n", $namespaces ); |
| 31 |
} |
| 32 |
|
| 33 |
public function validate( $value, $alldata ) { |
| 34 |
if ( !$this->mParams['exists'] || $value === '' ) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $value === null ) { |
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
$namespaces = explode( "\n", $value ); |
| 44 |
|
| 45 |
if ( isset( $this->mParams['max'] ) && ( count( $namespaces ) > $this->mParams['max'] ) ) { |
| 46 |
return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] ); |
| 47 |
} |
| 48 |
|
| 49 |
foreach ( $namespaces as $namespace ) { |
| 50 |
if ( |
| 51 |
$namespace < 0 || |
| 52 |
!MediaWikiServices::getInstance()->getNamespaceInfo()->exists( (int)$namespace ) |
| 53 |
) { |
| 54 |
return $this->msg( 'htmlform-select-badoption' ); |
| 55 |
} |
| 56 |
|
| 57 |
$result = parent::validate( $namespace, $alldata ); |
| 58 |
if ( $result !== true ) { |
| 59 |
return $result; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
public function getInputHTML( $value ) { |
| 67 |
$this->mParent->getOutput()->enableOOUI(); |
| 68 |
return $this->getInputOOUI( $value ); |
| 69 |
} |
| 70 |
|
| 71 |
public function getInputOOUI( $value ) { |
| 72 |
$this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' ); |
| 73 |
|
| 74 |
$params = [ |
| 75 |
'id' => $this->mID, |
| 76 |
'name' => $this->mName, |
| 77 |
'dir' => $this->mDir, |
| 78 |
]; |
| 79 |
|
| 80 |
if ( isset( $this->mParams['disabled'] ) ) { |
| 81 |
$params['disabled'] = $this->mParams['disabled']; |
| 82 |
} |
| 83 |
|
| 84 |
if ( isset( $this->mParams['default'] ) ) { |
| 85 |
$params['default'] = $this->mParams['default']; |
| 86 |
} |
| 87 |
|
| 88 |
$params['placeholder'] = $this->mParams['placeholder'] ?? |
| 89 |
$this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain(); |
| 90 |
|
| 91 |
if ( isset( $this->mParams['max'] ) ) { |
| 92 |
$params['tagLimit'] = $this->mParams['max']; |
| 93 |
} |
| 94 |
|
| 95 |
if ( isset( $this->mParams['input'] ) ) { |
| 96 |
$params['input'] = $this->mParams['input']; |
| 97 |
} |
| 98 |
|
| 99 |
if ( isset( $this->mParams['allowEditTags'] ) ) { |
| 100 |
$params['allowEditTags'] = $this->mParams['allowEditTags']; |
| 101 |
} |
| 102 |
|
| 103 |
if ( $value !== null ) { |
| 104 |
|
| 105 |
$params['default'] = $value === '' ? [] : explode( "\n", $value ); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
$params['infusable'] = true; |
| 110 |
$params['classes'] = [ 'mw-htmlform-autoinfuse' ]; |
| 111 |
$widget = new NamespacesMultiselectWidget( $params ); |
| 112 |
$widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] ); |
| 113 |
|
| 114 |
return $widget; |
| 115 |
} |
| 116 |
|
| 117 |
protected function shouldInfuseOOUI() { |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
protected function getOOUIModules() { |
| 122 |
return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ]; |
| 123 |
} |
| 124 |
|
| 125 |
public function getInputCodex( $value, $hasErrors ) { |
| 126 |
|
| 127 |
|
| 128 |
$textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] ); |
| 129 |
return $textAreaField->getInputCodex( $value, $hasErrors ); |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
class_alias( HTMLNamespacesMultiselectField::class, 'HTMLNamespacesMultiselectField' ); |