aboutsummaryrefslogtreecommitdiffstats
path: root/includes/installer/DatabaseForm.php
blob: a674b31fa212e2258a3b65691ce33921e116cf70 (plain) (blame)
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
<?php

namespace MediaWiki\Installer;

/**
 * @internal
 */
abstract class DatabaseForm {
	protected WebInstaller $webInstaller;
	protected DatabaseInstaller $dbInstaller;

	public function __construct( WebInstaller $webInstaller, DatabaseInstaller $dbInstaller ) {
		$this->webInstaller = $webInstaller;
		$this->dbInstaller = $dbInstaller;
	}

	/**
	 * Get a variable, taking local defaults into account.
	 * @param string $var
	 * @param mixed|null $default
	 * @return mixed
	 */
	protected function getVar( $var, $default = null ) {
		return $this->dbInstaller->getVar( $var, $default );
	}

	/**
	 * Set a variable
	 * @param string $name
	 * @param mixed $value
	 */
	protected function setVar( $name, $value ) {
		$this->dbInstaller->setVar( $name, $value );
	}

	/**
	 * Return the internal name, e.g. 'mysql', or 'sqlite'.
	 * @return string
	 */
	protected function getName() {
		return $this->dbInstaller->getName();
	}

	/**
	 * Get a labelled text box to configure a local variable.
	 *
	 * @param string $var
	 * @param string $label
	 * @param array $attribs
	 * @param string $helpData HTML
	 * @return string HTML
	 * @return-taint escaped
	 */
	protected function getTextBox( $var, $label, $attribs = [], $helpData = "" ) {
		$name = $this->getName() . '_' . $var;
		$value = $this->getVar( $var );
		if ( $attribs === null ) {
			$attribs = [];
		}

		return $this->webInstaller->getTextBox( [
			'var' => $var,
			'label' => $label,
			'attribs' => $attribs,
			'controlName' => $name,
			'value' => $value,
			'help' => $helpData
		] );
	}

	/**
	 * Get a labelled password box to configure a local variable.
	 * Implements password hiding.
	 *
	 * @param string $var
	 * @param string $label
	 * @param array $attribs
	 * @param string $helpData HTML
	 * @return string HTML
	 * @return-taint escaped
	 */
	protected function getPasswordBox( $var, $label, $attribs = [], $helpData = "" ) {
		$name = $this->getName() . '_' . $var;
		$value = $this->getVar( $var );
		if ( $attribs === null ) {
			$attribs = [];
		}

		return $this->webInstaller->getPasswordBox( [
			'var' => $var,
			'label' => $label,
			'attribs' => $attribs,
			'controlName' => $name,
			'value' => $value,
			'help' => $helpData
		] );
	}

	/**
	 * Get a labelled checkbox to configure a local boolean variable.
	 *
	 * @param string $var
	 * @param string $label
	 * @param array $attribs Optional.
	 * @param string $helpData Optional.
	 * @return string
	 */
	protected function getCheckBox( $var, $label, $attribs = [], $helpData = "" ) {
		$name = $this->getName() . '_' . $var;
		$value = $this->getVar( $var );

		return $this->webInstaller->getCheckBox( [
			'var' => $var,
			'label' => $label,
			'attribs' => $attribs,
			'controlName' => $name,
			'value' => $value,
			'help' => $helpData
		] );
	}

	/**
	 * Get a set of labelled radio buttons.
	 *
	 * @param array $params Parameters are:
	 *      var:            The variable to be configured (required)
	 *      label:          The message name for the label (required)
	 *      itemLabelPrefix: The message name prefix for the item labels (required)
	 *      values:         List of allowed values (required)
	 *      itemAttribs     Array of attribute arrays, outer key is the value name (optional)
	 *
	 * @return string
	 */
	protected function getRadioSet( $params ) {
		$params['controlName'] = $this->getName() . '_' . $params['var'];
		$params['value'] = $this->getVar( $params['var'] );

		return $this->webInstaller->getRadioSet( $params );
	}

	/**
	 * Convenience function to set variables based on form data.
	 * Assumes that variables containing "password" in the name are (potentially
	 * fake) passwords.
	 * @param array $varNames
	 * @return array
	 */
	protected function setVarsFromRequest( $varNames ) {
		return $this->webInstaller->setVarsFromRequest( $varNames, $this->getName() . '_' );
	}

}