MediaWiki  1.28.1
HTMLSelectAndOtherField.php
Go to the documentation of this file.
1 <?php
2 
14  function __construct( $params ) {
15  if ( array_key_exists( 'other', $params ) ) {
16  // Do nothing
17  } elseif ( array_key_exists( 'other-message', $params ) ) {
18  $params['other'] = $this->getMessage( $params['other-message'] )->plain();
19  } else {
20  $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
21  }
22 
23  parent::__construct( $params );
24 
25  if ( $this->getOptions() === null ) {
26  // Sulk
27  throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28  }
29  if ( !in_array( 'other', $this->mOptions, true ) ) {
30  // Have 'other' always as first element
31  $this->mOptions = [ $params['other'] => 'other' ] + $this->mOptions;
32  }
33  $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
34 
35  }
36 
37  function getInputHTML( $value ) {
38  $select = parent::getInputHTML( $value[1] );
39 
40  $textAttribs = [
41  'id' => $this->mID . '-other',
42  'size' => $this->getSize(),
43  'class' => [ 'mw-htmlform-select-and-other-field' ],
44  'data-id-select' => $this->mID,
45  ];
46 
47  if ( $this->mClass !== '' ) {
48  $textAttribs['class'][] = $this->mClass;
49  }
50 
51  $allowedParams = [
52  'required',
53  'autofocus',
54  'multiple',
55  'disabled',
56  'tabindex',
57  'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
58  ];
59 
60  $textAttribs += $this->getAttributes( $allowedParams );
61 
62  $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
63 
64  return "$select<br />\n$textbox";
65  }
66 
67  function getInputOOUI( $value ) {
68  return false;
69  }
70 
77  if ( $request->getCheck( $this->mName ) ) {
78  $list = $request->getText( $this->mName );
79  $text = $request->getText( $this->mName . '-other' );
80 
81  // Should be built the same as in mediawiki.htmlform.js
82  if ( $list == 'other' ) {
83  $final = $text;
84  } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
85  # User has spoofed the select form to give an option which wasn't
86  # in the original offer. Sulk...
87  $final = $text;
88  } elseif ( $text == '' ) {
89  $final = $list;
90  } else {
91  $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
92  }
93  } else {
94  $final = $this->getDefault();
95 
96  $list = 'other';
97  $text = $final;
98  foreach ( $this->mFlatOptions as $option ) {
99  $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
100  if ( strpos( $text, $match ) === 0 ) {
101  $list = $option;
102  $text = substr( $text, strlen( $match ) );
103  break;
104  }
105  }
106  }
107 
108  return [ $final, $list, $text ];
109  }
110 
111  function getSize() {
112  return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
113  }
114 
115  function validate( $value, $alldata ) {
116  # HTMLSelectField forces $value to be one of the options in the select
117  # field, which is not useful here. But we do want the validation further up
118  # the chain
119  $p = parent::validate( $value[1], $alldata );
120 
121  if ( $p !== true ) {
122  return $p;
123  }
124 
125  if ( isset( $this->mParams['required'] )
126  && $this->mParams['required'] !== false
127  && $value[1] === ''
128  ) {
129  return $this->msg( 'htmlform-required' )->parse();
130  }
131 
132  return true;
133  }
134 }
Double field with a dropdown list constructed from a system message in the format.
getOptions()
Fetch the array of options from the field's parameters.
getMessage($value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a name + parameters array) into a Message.
msg()
Get a translated interface message.
A select dropdown field.
$value
$params
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
static input($name, $value= '', $type= 'text', array $attribs=[])
Convenience function to produce an "" element.
Definition: Html.php:675
getAttributes(array $list)
Returns the given attributes from the parameters.