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