summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/MediaWiki/ParserHooks/MapsDocFunction.php
blob: e294736ba1fe97c2680344b7f3ec97cb014bb5fe (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php

namespace Maps\MediaWiki\ParserHooks;

use Maps\MapsFactory;
use ParamProcessor\ParamDefinition;
use ParserHook;

/**
 * Class for the 'mapsdoc' parser hooks,
 * which displays documentation for a specified mapping service.
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class MapsDocFunction extends ParserHook {

	/**
	 * Field to store the value of the language parameter.
	 *
	 * @var string
	 */
	protected $language;

	/**
	 * Renders and returns the output.
	 *
	 * @see ParserHook::render
	 *
	 * @param array $parameters
	 *
	 * @return string
	 */
	public function render( array $parameters ) {
		$this->language = $parameters['language'];

		$factory = MapsFactory::globalInstance();

		$params = $this->getServiceParameters( $factory, $parameters['service'] );

		return $this->getParameterTable( $factory, $params );
	}

	private function getServiceParameters( MapsFactory $factory, string $service ) {
		return array_merge(
			[
				'zoom' => [
					'type' => 'integer',
					'message' => 'maps-par-zoom',
				]
			],
			$factory->getMappingServices()->getService( $service )->getParameterInfo()
		);
	}

	/**
	 * Returns the wikitext for a table listing the provided parameters.
	 */
	private function getParameterTable( MapsFactory $factory, array $parameters ): string {
		$tableRows = [];

		$parameters = $factory->getParamDefinitionFactory()->newDefinitionsFromArrays( $parameters );

		foreach ( $parameters as $parameter ) {
			$tableRows[] = $this->getDescriptionRow( $parameter );
		}

		$table = '';

		if ( count( $tableRows ) > 0 ) {
			$tableRows = array_merge(
				[
					'!' . $this->msg( 'validator-describe-header-parameter' ) . "\n" .
					//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" .
					'!' . $this->msg( 'validator-describe-header-type' ) . "\n" .
					'!' . $this->msg( 'validator-describe-header-default' ) . "\n" .
					'!' . $this->msg( 'validator-describe-header-description' )
				],
				$tableRows
			);

			$table = implode( "\n|-\n", $tableRows );

			$table =
				'{| class="wikitable sortable"' . "\n" .
				$table .
				"\n|}";
		}

		return $table;
	}

	/**
	 * Returns the wikitext for a table row describing a single parameter.
	 *
	 * @param ParamDefinition $parameter
	 *
	 * @return string
	 */
	private function getDescriptionRow( ParamDefinition $parameter ) {
		$description = $this->msg( $parameter->getMessage() );

		$type = $this->msg( $parameter->getTypeMessage() );

		$default = $parameter->isRequired() ? "''" . $this->msg(
				'validator-describe-required'
			) . "''" : $parameter->getDefault();
		if ( is_array( $default ) ) {
			$default = implode( ', ', $default );
		} elseif ( is_bool( $default ) ) {
			$default = $default ? 'yes' : 'no';
		}

		if ( $default === '' ) {
			$default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
		}

		return <<<EOT
| {$parameter->getName()}
| {$type}
| {$default}
| {$description}
EOT;
	}

	/**
	 * Message function that takes into account the language parameter.
	 *
	 * @param string $key
	 * @param ... $args
	 *
	 * @return string
	 */
	private function msg() {
		$args = func_get_args();
		$key = array_shift( $args );
		return wfMessage( $key, $args )->inLanguage( $this->language )->text();
	}

	/**
	 * @see ParserHook::getDescription()
	 */
	public function getMessage() {
		return 'maps-mapsdoc-description';
	}

	/**
	 * Gets the name of the parser hook.
	 *
	 * @see ParserHook::getName
	 *
	 * @return string
	 */
	protected function getName() {
		return 'mapsdoc';
	}

	/**
	 * Returns an array containing the parameter info.
	 *
	 * @see ParserHook::getParameterInfo
	 *
	 * @return array
	 */
	protected function getParameterInfo( $type ) {
		$params = [];

		$params['service'] = [
			'values' => $GLOBALS['egMapsAvailableServices'],
			'tolower' => true,
		];

		$params['language'] = [
			'default' => $GLOBALS['wgLanguageCode'],
		];

		// Give grep a chance to find the usages:
		// maps-geocode-par-service, maps-geocode-par-language
		foreach ( $params as $name => &$param ) {
			$param['message'] = 'maps-geocode-par-' . $name;
		}

		return $params;
	}

	/**
	 * Returns the list of default parameters.
	 *
	 * @see ParserHook::getDefaultParameters
	 *
	 * @return array
	 */
	protected function getDefaultParameters( $type ) {
		return [ 'service', 'language' ];
	}

}