Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
HTMLFileField | |
0.00% |
0 / 58 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
loadDataFromRequest | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getInputHTML | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
getInputOOUI | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
30 | |||
getInputWidget | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
shouldInfuseOOUI | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\HTMLForm\Field; |
4 | |
5 | use MediaWiki\Html\Html; |
6 | use MediaWiki\HTMLForm\HTMLFormField; |
7 | use OOUI\Widget; |
8 | |
9 | /** |
10 | * File <input> field. |
11 | * |
12 | * Besides the parameters recognized by HTMLFormField, the following are |
13 | * recognized: |
14 | * placeholder/placeholder-message - HTML placeholder attribute |
15 | * accept - Array of acceptable MIME types/extensions to show in file chooser, |
16 | * null to accept all files. |
17 | * multiple - Allow multiple files to be selected |
18 | * |
19 | * @stable to extend |
20 | */ |
21 | class HTMLFileField extends HTMLFormField { |
22 | /** @var string */ |
23 | protected $mPlaceholder = ''; |
24 | /** @var string[]|null */ |
25 | protected $mAccept = null; |
26 | |
27 | /** @var bool */ |
28 | protected $mMultiple; |
29 | |
30 | /** |
31 | * @stable to call |
32 | * |
33 | * @param array $params |
34 | * - placeholder/placeholder-message |
35 | * - accept |
36 | * - multiple |
37 | */ |
38 | public function __construct( $params ) { |
39 | if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) { |
40 | $params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off'; |
41 | } |
42 | |
43 | parent::__construct( $params ); |
44 | |
45 | if ( isset( $params['placeholder-message'] ) ) { |
46 | $this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text(); |
47 | } elseif ( isset( $params['placeholder'] ) ) { |
48 | $this->mPlaceholder = $params['placeholder']; |
49 | } |
50 | |
51 | $this->mAccept = $params['accept'] ?? null; |
52 | $this->mMultiple = !empty( $params['multiple'] ); |
53 | } |
54 | |
55 | /** |
56 | * @inheritDoc |
57 | */ |
58 | public function loadDataFromRequest( $request ) { |
59 | return $request->getUpload( $this->mName )->getName(); |
60 | } |
61 | |
62 | /** |
63 | * @inheritDoc |
64 | * @stable to override |
65 | */ |
66 | public function getInputHTML( $value ) { |
67 | $attribs = [ |
68 | 'id' => $this->mID, |
69 | 'name' => $this->mName, |
70 | 'dir' => $this->mDir, |
71 | ] + $this->getTooltipAndAccessKey(); |
72 | |
73 | if ( $this->mClass !== '' ) { |
74 | $attribs['class'] = $this->mClass; |
75 | } |
76 | if ( $this->mAccept ) { |
77 | $attribs['accept'] = implode( ',', $this->mAccept ); |
78 | } |
79 | if ( $this->mMultiple ) { |
80 | $attribs['multiple'] = ''; |
81 | } |
82 | // Note: Placeholders are not supported by native file inputs |
83 | |
84 | $allowedParams = [ |
85 | 'title', |
86 | 'tabindex', |
87 | 'disabled', |
88 | 'required', |
89 | 'autofocus', |
90 | 'readonly', |
91 | ]; |
92 | |
93 | $attribs += $this->getAttributes( $allowedParams ); |
94 | |
95 | return Html::input( $this->mName, $value ?? '', 'file', $attribs ); |
96 | } |
97 | |
98 | /** |
99 | * @inheritDoc |
100 | * @stable to override |
101 | */ |
102 | public function getInputOOUI( $value ) { |
103 | $attribs = $this->getTooltipAndAccessKeyOOUI(); |
104 | |
105 | if ( $this->mClass !== '' ) { |
106 | $attribs['classes'] = [ $this->mClass ]; |
107 | } |
108 | if ( $this->mPlaceholder !== '' ) { |
109 | $attribs['placeholder'] = $this->mPlaceholder; |
110 | } |
111 | if ( $this->mAccept ) { |
112 | $attribs['accept'] = $this->mAccept; |
113 | } |
114 | if ( $this->mMultiple ) { |
115 | $attribs['multiple'] = true; |
116 | } |
117 | |
118 | # @todo Enforce pattern, step, required, readonly on the server side as |
119 | # well |
120 | $allowedParams = [ |
121 | 'title', |
122 | 'tabindex', |
123 | 'disabled', |
124 | 'required', |
125 | 'autofocus', |
126 | 'readonly', |
127 | ]; |
128 | |
129 | $attribs += \OOUI\Element::configFromHtmlAttributes( |
130 | $this->getAttributes( $allowedParams ) |
131 | ); |
132 | |
133 | return $this->getInputWidget( [ |
134 | 'id' => $this->mID, |
135 | 'name' => $this->mName, |
136 | 'dir' => $this->mDir, |
137 | ] + $attribs ); |
138 | } |
139 | |
140 | /** |
141 | * @stable to override |
142 | * |
143 | * @param array $params |
144 | * |
145 | * @return Widget |
146 | */ |
147 | protected function getInputWidget( $params ) { |
148 | return new \OOUI\SelectFileInputWidget( $params ); |
149 | } |
150 | |
151 | /** |
152 | * @inheritDoc |
153 | * @stable to override |
154 | */ |
155 | protected function shouldInfuseOOUI() { |
156 | return true; |
157 | } |
158 | } |
159 | |
160 | /** @deprecated class alias since 1.42 */ |
161 | class_alias( HTMLFileField::class, 'HTMLFileField' ); |