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 ( is_null( $value ) ) {
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 ( !is_null( $value ) ) {
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 }
Wrapper for Html::namespaceSelector to use in HTMLForm.
msg( $key,... $params)
Get a translated interface message.
Implements a tag multiselect input field for namespaces.