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