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