MediaWiki master
HTMLNamespacesMultiselectField.php
Go to the documentation of this file.
1<?php
2
4
7
22 public function loadDataFromRequest( $request ) {
23 $value = $request->getText( $this->mName, $this->getDefault() ?? '' );
24
25 $namespaces = explode( "\n", $value );
26 // Remove empty lines
27 $namespaces = array_values( array_filter( $namespaces, static function ( $namespace ) {
28 return trim( $namespace ) !== '';
29 } ) );
30 // This function is expected to return a string
31 return implode( "\n", $namespaces );
32 }
33
35 public function validate( $value, $alldata ) {
36 if ( !$this->mParams['exists'] || $value === '' ) {
37 return true;
38 }
39
40 if ( $value === null ) {
41 return false;
42 }
43
44 // $value is a string, because HTMLForm fields store their values as strings
45 $namespaces = explode( "\n", $value );
46
47 if ( isset( $this->mParams['max'] ) && ( count( $namespaces ) > $this->mParams['max'] ) ) {
48 return $this->msg( 'htmlform-multiselect-toomany', $this->mParams['max'] );
49 }
50
51 foreach ( $namespaces as $namespace ) {
52 if (
53 $namespace < 0 ||
54 !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( (int)$namespace )
55 ) {
56 return $this->msg( 'htmlform-select-badoption' );
57 }
58
59 $result = parent::validate( $namespace, $alldata );
60 if ( $result !== true ) {
61 return $result;
62 }
63 }
64
65 return true;
66 }
67
69 public function getInputHTML( $value ) {
70 $this->mParent->getOutput()->enableOOUI();
71 return $this->getInputOOUI( $value )->toString();
72 }
73
75 public function getInputOOUI( $value ) {
76 $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.TagMultiselectWidget.styles' );
77
78 $params = [
79 'id' => $this->mID,
80 'name' => $this->mName,
81 'dir' => $this->mDir,
82 ];
83
84 $params['disabled'] = $this->isDisabledNoJs( $this->mParent->mFieldData );
85
86 if ( isset( $this->mParams['default'] ) ) {
87 $params['default'] = $this->mParams['default'];
88 }
89
90 $params['placeholder'] = $this->mParams['placeholder'] ??
91 $this->msg( 'mw-widgets-titlesmultiselect-placeholder' )->plain();
92
93 if ( isset( $this->mParams['max'] ) ) {
94 $params['tagLimit'] = $this->mParams['max'];
95 }
96
97 if ( isset( $this->mParams['input'] ) ) {
98 $params['input'] = $this->mParams['input'];
99 }
100
101 if ( isset( $this->mParams['allowEditTags'] ) ) {
102 $params['allowEditTags'] = $this->mParams['allowEditTags'];
103 }
104
105 if ( $value !== null ) {
106 // $value is a string, but the widget expects an array
107 $params['default'] = $value === '' ? [] : explode( "\n", $value );
108 }
109
110 // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm
111 $params['infusable'] = true;
112 $params['classes'] = [ 'mw-htmlform-autoinfuse' ];
113 $widget = new NamespacesMultiselectWidget( $params );
114 $widget->setAttributes( [ 'data-mw-modules' => implode( ',', $this->getOOUIModules() ) ] );
115
116 return $widget;
117 }
118
120 protected function shouldInfuseOOUI() {
121 return true;
122 }
123
125 protected function getOOUIModules() {
126 return [ 'mediawiki.widgets.NamespacesMultiselectWidget' ];
127 }
128
130 public function getInputCodex( $value, $hasErrors ) {
131 // HTMLTextAreaField defaults to 'rows' => 25, which is too big for this field
132 // Use 10 instead (but allow $this->mParams to override that value)
133 $textAreaField = new HTMLTextAreaField( $this->mParams + [ 'rows' => 10 ] );
134 return $textAreaField->getInputCodex( $value, $hasErrors );
135 }
136
137}
138
140class_alias( HTMLNamespacesMultiselectField::class, 'HTMLNamespacesMultiselectField' );
Implements a tag multiselect input field for namespaces.
shouldInfuseOOUI()
Whether the field should be automatically infused.Note that all OOUI HTMLForm fields are infusable (y...
getOOUIModules()
Get the list of extra ResourceLoader modules which must be loaded client-side before it's possible to...
getInputOOUI( $value)
Same as getInputHTML, but returns an OOUI object.Defaults to false, which getOOUI will interpret as "...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputCodex( $value, $hasErrors)
Same as getInputHTML, but for Codex.This is called by CodexHTMLForm.If not overridden,...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
Wrapper for Html::namespaceSelector to use in HTMLForm.
msg( $key,... $params)
Get a translated interface message.
isDisabledNoJs( $alldata)
Test whether this field is disabled in the generated HTML, either due to the 'disabled' parameter,...
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.