File: HTMLFileField.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (161 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php

namespace MediaWiki\HTMLForm\Field;

use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLFormField;
use OOUI\Widget;

/**
 * File <input> field.
 *
 * Besides the parameters recognized by HTMLFormField, the following are
 * recognized:
 *   placeholder/placeholder-message - HTML placeholder attribute
 *   accept - Array of acceptable MIME types/extensions to show in file chooser,
 *            null to accept all files.
 *   multiple - Allow multiple files to be selected
 *
 * @stable to extend
 */
class HTMLFileField extends HTMLFormField {
	/** @var string */
	protected $mPlaceholder = '';
	/** @var string[]|null */
	protected $mAccept = null;

	/** @var bool */
	protected $mMultiple;

	/**
	 * @stable to call
	 *
	 * @param array $params
	 *   - placeholder/placeholder-message
	 *   - accept
	 *   - multiple
	 */
	public function __construct( $params ) {
		if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
			$params['autocomplete'] = $params['autocomplete'] ? 'on' : 'off';
		}

		parent::__construct( $params );

		if ( isset( $params['placeholder-message'] ) ) {
			$this->mPlaceholder = $this->getMessage( $params['placeholder-message'] )->text();
		} elseif ( isset( $params['placeholder'] ) ) {
			$this->mPlaceholder = $params['placeholder'];
		}

		$this->mAccept = $params['accept'] ?? null;
		$this->mMultiple = !empty( $params['multiple'] );
	}

	/**
	 * @inheritDoc
	 */
	public function loadDataFromRequest( $request ) {
		return $request->getUpload( $this->mName )->getName();
	}

	/**
	 * @inheritDoc
	 * @stable to override
	 */
	public function getInputHTML( $value ) {
		$attribs = [
			'id' => $this->mID,
			'name' => $this->mName,
			'dir' => $this->mDir,
		] + $this->getTooltipAndAccessKey();

		if ( $this->mClass !== '' ) {
			$attribs['class'] = $this->mClass;
		}
		if ( $this->mAccept ) {
			$attribs['accept'] = implode( ',', $this->mAccept );
		}
		if ( $this->mMultiple ) {
			$attribs['multiple'] = '';
		}
		// Note: Placeholders are not supported by native file inputs

		$allowedParams = [
			'title',
			'tabindex',
			'disabled',
			'required',
			'autofocus',
			'readonly',
		];

		$attribs += $this->getAttributes( $allowedParams );

		return Html::input( $this->mName, $value ?? '', 'file', $attribs );
	}

	/**
	 * @inheritDoc
	 * @stable to override
	 */
	public function getInputOOUI( $value ) {
		$attribs = $this->getTooltipAndAccessKeyOOUI();

		if ( $this->mClass !== '' ) {
			$attribs['classes'] = [ $this->mClass ];
		}
		if ( $this->mPlaceholder !== '' ) {
			$attribs['placeholder'] = $this->mPlaceholder;
		}
		if ( $this->mAccept ) {
			$attribs['accept'] = $this->mAccept;
		}
		if ( $this->mMultiple ) {
			$attribs['multiple'] = true;
		}

		# @todo Enforce pattern, step, required, readonly on the server side as
		# well
		$allowedParams = [
			'title',
			'tabindex',
			'disabled',
			'required',
			'autofocus',
			'readonly',
		];

		$attribs += \OOUI\Element::configFromHtmlAttributes(
			$this->getAttributes( $allowedParams )
		);

		return $this->getInputWidget( [
			'id' => $this->mID,
			'name' => $this->mName,
			'dir' => $this->mDir,
		] + $attribs );
	}

	/**
	 * @stable to override
	 *
	 * @param array $params
	 *
	 * @return Widget
	 */
	protected function getInputWidget( $params ) {
		return new \OOUI\SelectFileInputWidget( $params );
	}

	/**
	 * @inheritDoc
	 * @stable to override
	 */
	protected function shouldInfuseOOUI() {
		return true;
	}
}

/** @deprecated class alias since 1.42 */
class_alias( HTMLFileField::class, 'HTMLFileField' );