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