1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Maps\ParserHooks; |
6
|
|
|
|
7
|
|
|
use Maps\MapsFactory; |
8
|
|
|
use ParamProcessor\ParamDefinition; |
9
|
|
|
use ParserHook; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class for the 'mapsdoc' parser hooks, |
13
|
|
|
* which displays documentation for a specified mapping service. |
14
|
|
|
* |
15
|
|
|
* @licence GNU GPL v2+ |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class MapsDocFunction extends ParserHook { |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Field to store the value of the language parameter. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $language; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Renders and returns the output. |
29
|
|
|
* |
30
|
|
|
* @see ParserHook::render |
31
|
|
|
* |
32
|
|
|
* @param array $parameters |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function render( array $parameters ) { |
37
|
|
|
$this->language = $parameters['language']; |
38
|
|
|
|
39
|
|
|
$factory = MapsFactory::globalInstance(); |
40
|
|
|
|
41
|
|
|
$params = $this->getServiceParameters( $factory, $parameters['service'] ); |
42
|
|
|
|
43
|
|
|
return $this->getParameterTable( $factory, $params ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function getServiceParameters( MapsFactory $factory, string $service ) { |
47
|
|
|
return array_merge( |
48
|
|
|
[ |
49
|
|
|
'zoom' => [ |
50
|
|
|
'type' => 'integer', |
51
|
|
|
'message' => 'maps-par-zoom', |
52
|
|
|
] |
53
|
|
|
], |
54
|
|
|
$factory->getMappingServices()->getService( $service )->getParameterInfo() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the wikitext for a table listing the provided parameters. |
60
|
|
|
*/ |
61
|
|
|
private function getParameterTable( MapsFactory $factory, array $parameters ): string { |
62
|
|
|
$tableRows = []; |
63
|
|
|
|
64
|
|
|
$parameters = $factory->getParamDefinitionFactory()->newDefinitionsFromArrays( $parameters ); |
65
|
|
|
|
66
|
|
|
foreach ( $parameters as $parameter ) { |
67
|
|
|
$tableRows[] = $this->getDescriptionRow( $parameter ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$table = ''; |
71
|
|
|
|
72
|
|
|
if ( count( $tableRows ) > 0 ) { |
73
|
|
|
$tableRows = array_merge( |
74
|
|
|
[ |
75
|
|
|
'!' . $this->msg( 'validator-describe-header-parameter' ) . "\n" . |
76
|
|
|
//'!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" . |
77
|
|
|
'!' . $this->msg( 'validator-describe-header-type' ) . "\n" . |
78
|
|
|
'!' . $this->msg( 'validator-describe-header-default' ) . "\n" . |
79
|
|
|
'!' . $this->msg( 'validator-describe-header-description' ) |
80
|
|
|
], |
81
|
|
|
$tableRows |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$table = implode( "\n|-\n", $tableRows ); |
85
|
|
|
|
86
|
|
|
$table = |
87
|
|
|
'{| class="wikitable sortable"' . "\n" . |
88
|
|
|
$table . |
89
|
|
|
"\n|}"; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $table; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Returns the wikitext for a table row describing a single parameter. |
97
|
|
|
* |
98
|
|
|
* @param ParamDefinition $parameter |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
private function getDescriptionRow( ParamDefinition $parameter ) { |
103
|
|
|
$description = $this->msg( $parameter->getMessage() ); |
104
|
|
|
|
105
|
|
|
$type = $this->msg( $parameter->getTypeMessage() ); |
106
|
|
|
|
107
|
|
|
$default = $parameter->isRequired() ? "''" . $this->msg( |
108
|
|
|
'validator-describe-required' |
109
|
|
|
) . "''" : $parameter->getDefault(); |
110
|
|
|
if ( is_array( $default ) ) { |
111
|
|
|
$default = implode( ', ', $default ); |
112
|
|
|
} elseif ( is_bool( $default ) ) { |
113
|
|
|
$default = $default ? 'yes' : 'no'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ( $default === '' ) { |
117
|
|
|
$default = "''" . $this->msg( 'validator-describe-empty' ) . "''"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return <<<EOT |
121
|
|
|
| {$parameter->getName()} |
122
|
|
|
| {$type} |
123
|
|
|
| {$default} |
124
|
|
|
| {$description} |
125
|
|
|
EOT; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Message function that takes into account the language parameter. |
130
|
|
|
* |
131
|
|
|
* @param string $key |
|
|
|
|
132
|
|
|
* @param ... $args |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
private function msg() { |
137
|
|
|
$args = func_get_args(); |
138
|
|
|
$key = array_shift( $args ); |
139
|
|
|
return wfMessage( $key, $args )->inLanguage( $this->language )->text(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @see ParserHook::getDescription() |
144
|
|
|
*/ |
145
|
|
|
public function getMessage() { |
146
|
|
|
return 'maps-mapsdoc-description'; |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Gets the name of the parser hook. |
151
|
|
|
* |
152
|
|
|
* @see ParserHook::getName |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
2 |
|
protected function getName() { |
157
|
2 |
|
return 'mapsdoc'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns an array containing the parameter info. |
162
|
|
|
* |
163
|
|
|
* @see ParserHook::getParameterInfo |
164
|
|
|
* |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
3 |
|
protected function getParameterInfo( $type ) { |
168
|
3 |
|
$params = []; |
169
|
|
|
|
170
|
3 |
|
$params['service'] = [ |
171
|
3 |
|
'values' => $GLOBALS['egMapsAvailableServices'], |
172
|
|
|
'tolower' => true, |
173
|
|
|
]; |
174
|
|
|
|
175
|
3 |
|
$params['language'] = [ |
176
|
3 |
|
'default' => $GLOBALS['wgLanguageCode'], |
177
|
|
|
]; |
178
|
|
|
|
179
|
|
|
// Give grep a chance to find the usages: |
180
|
|
|
// maps-geocode-par-service, maps-geocode-par-language |
181
|
3 |
|
foreach ( $params as $name => &$param ) { |
182
|
3 |
|
$param['message'] = 'maps-geocode-par-' . $name; |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
return $params; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Returns the list of default parameters. |
190
|
|
|
* |
191
|
|
|
* @see ParserHook::getDefaultParameters |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
*/ |
195
|
1 |
|
protected function getDefaultParameters( $type ) { |
196
|
1 |
|
return [ 'service', 'language' ]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.