1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\MediaWiki\ParserHooks; |
4
|
|
|
|
5
|
|
|
use Maps; |
6
|
|
|
use Maps\MapsFunctions; |
7
|
|
|
use Maps\MappingServices; |
8
|
|
|
use Maps\Presentation\ParameterExtractor; |
9
|
|
|
use MWException; |
10
|
|
|
use ParamProcessor\ParamDefinition; |
11
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
12
|
|
|
use ParamProcessor\ProcessedParam; |
13
|
|
|
use ParamProcessor\Processor; |
14
|
|
|
use Parser; |
15
|
|
|
use ParserHooks\HookDefinition; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class for the 'display_map' parser hooks. |
19
|
|
|
* |
20
|
|
|
* @licence GNU GPL v2+ |
21
|
|
|
* @author Jeroen De Dauw < [email protected] > |
22
|
|
|
*/ |
23
|
|
|
class DisplayMapFunction { |
24
|
|
|
|
25
|
|
|
private $services; |
26
|
|
|
|
27
|
|
|
private $renderer; |
28
|
|
|
|
29
|
20 |
|
public function __construct( MappingServices $services ) { |
30
|
20 |
|
$this->services = $services; |
31
|
|
|
|
32
|
20 |
|
$this->renderer = new DisplayMapRenderer(); |
33
|
20 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Parser $parser |
37
|
|
|
* @param string[] $parameters Values of the array can be named parameters ("key=value") or unnamed. |
38
|
|
|
* They are not normalized, so can be "key = value " |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
* @throws MWException |
42
|
|
|
*/ |
43
|
18 |
|
public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters ): string { |
44
|
18 |
|
$processor = new Processor( new \ParamProcessor\Options() ); |
45
|
|
|
|
46
|
18 |
|
$service = $this->services->getServiceOrDefault( |
47
|
18 |
|
$this->extractServiceName( |
48
|
18 |
|
Maps\Presentation\ParameterExtractor::extractFromKeyValueStrings( $parameters ) |
49
|
|
|
) |
50
|
|
|
); |
51
|
|
|
|
52
|
18 |
|
$this->renderer->service = $service; |
53
|
|
|
|
54
|
18 |
|
$processor->setFunctionParams( |
55
|
18 |
|
$parameters, |
56
|
18 |
|
[], |
57
|
18 |
|
self::getHookDefinition( ';' )->getDefaultParameters() |
58
|
|
|
); |
59
|
|
|
|
60
|
18 |
|
$processor->setParameterDefinitions( |
61
|
|
|
// TODO use ParamDefinitionFactory |
62
|
18 |
|
ParamDefinition::getCleanDefinitions( |
63
|
18 |
|
array_merge( |
|
|
|
|
64
|
18 |
|
self::getHookDefinition( ';' )->getParameters(), |
65
|
18 |
|
$service->getParameterInfo() |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
18 |
|
return $this->getMapHtmlFromProcessor( $parser, $processor ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Parser $parser |
75
|
|
|
* @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys. |
76
|
|
|
* Both keys and values have not been normalized. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
* @throws MWException |
80
|
|
|
*/ |
81
|
2 |
|
public function getMapHtmlForParameterList( Parser $parser, array $parameters ) { |
82
|
2 |
|
$processor = new Processor( new \ParamProcessor\Options() ); |
83
|
|
|
|
84
|
2 |
|
$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) ); |
85
|
|
|
|
86
|
2 |
|
$this->renderer->service = $service; |
87
|
|
|
|
88
|
2 |
|
$processor->setParameters( $parameters ); |
89
|
2 |
|
$processor->setParameterDefinitions( |
90
|
|
|
// TODO use ParamDefinitionFactory |
91
|
2 |
|
ParamDefinition::getCleanDefinitions( |
92
|
2 |
|
array_merge( |
|
|
|
|
93
|
2 |
|
self::getHookDefinition( "\n" )->getParameters(), |
94
|
2 |
|
$service->getParameterInfo() |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
); |
98
|
|
|
|
99
|
2 |
|
return $this->getMapHtmlFromProcessor( $parser, $processor ); |
100
|
|
|
} |
101
|
|
|
|
102
|
20 |
|
private function getMapHtmlFromProcessor( Parser $parser, Processor $processor ) { |
103
|
20 |
|
$params = $processor->processParameters()->getParameters(); |
104
|
|
|
|
105
|
20 |
|
$this->defaultMapZoom( $params ); |
106
|
|
|
|
107
|
20 |
|
$this->trackMap( $parser ); |
108
|
|
|
|
109
|
20 |
|
return $this->renderer->renderMap( |
110
|
20 |
|
$this->processedParametersToKeyValueArray( $params ), |
111
|
20 |
|
$parser |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
20 |
|
private function extractServiceName( array $parameters ): string { |
116
|
20 |
|
$service = ( new ParameterExtractor() )->extract( |
117
|
20 |
|
[ 'mappingservice', 'service' ], |
118
|
20 |
|
$parameters |
119
|
|
|
); |
120
|
|
|
|
121
|
20 |
|
return $service ?? ''; |
122
|
|
|
} |
123
|
|
|
|
124
|
20 |
|
private function processedParametersToKeyValueArray( array $params ): array { |
125
|
20 |
|
$parameters = []; |
126
|
|
|
|
127
|
20 |
|
foreach ( $params as $parameter ) { |
128
|
20 |
|
$parameters[$parameter->getName()] = $parameter->getValue(); |
129
|
|
|
} |
130
|
|
|
|
131
|
20 |
|
return $parameters; |
132
|
|
|
} |
133
|
|
|
|
134
|
20 |
|
public static function getHookDefinition( string $locationDelimiter ): HookDefinition { |
135
|
20 |
|
return new HookDefinition( |
136
|
20 |
|
[ 'display_map', 'display_point', 'display_points', 'display_line' ], |
137
|
20 |
|
self::getParameterDefinitions( $locationDelimiter ), |
138
|
20 |
|
[ 'coordinates' ] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
20 |
|
private static function getParameterDefinitions( $locationDelimiter ): array { |
143
|
20 |
|
$params = MapsFunctions::getCommonParameters(); |
144
|
|
|
|
145
|
20 |
|
$params['coordinates'] = [ |
146
|
20 |
|
'type' => 'string', |
147
|
|
|
'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ], |
148
|
|
|
'default' => [], |
149
|
|
|
'islist' => true, |
150
|
20 |
|
'delimiter' => $locationDelimiter, |
151
|
20 |
|
'message' => 'maps-displaymap-par-coordinates', |
152
|
|
|
]; |
153
|
|
|
|
154
|
20 |
|
return $params; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param ProcessedParam[] $parameters |
159
|
|
|
*/ |
160
|
20 |
|
private function defaultMapZoom( array &$parameters ) { |
161
|
20 |
|
if ( array_key_exists( 'zoom', $parameters ) && $parameters['zoom']->wasSetToDefault() && count( |
162
|
19 |
|
$parameters['coordinates']->getValue() |
163
|
20 |
|
) > 1 ) { |
164
|
2 |
|
$parameters['zoom'] = $this->getParameterWithValue( $parameters['zoom'], false ); |
165
|
|
|
} |
166
|
20 |
|
} |
167
|
|
|
|
168
|
2 |
|
private function getParameterWithValue( ProcessedParam $param, $value ) { |
169
|
2 |
|
return new ProcessedParam( |
170
|
2 |
|
$param->getName(), |
171
|
2 |
|
$value, |
172
|
2 |
|
$param->wasSetToDefault(), |
173
|
2 |
|
$param->getOriginalName(), |
174
|
2 |
|
$param->getOriginalValue() |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
20 |
|
private function trackMap( Parser $parser ) { |
179
|
20 |
|
if ( $GLOBALS['egMapsEnableCategory'] ) { |
180
|
|
|
$parser->addTrackingCategory( 'maps-tracking-category' ); |
181
|
|
|
} |
182
|
20 |
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: