1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Maps\Map\DisplayMap; |
6
|
|
|
|
7
|
|
|
use Maps\DataAccess\MediaWikiFileUrlFinder; |
8
|
|
|
use Maps\Elements\Location; |
9
|
|
|
use Maps\Map\MapData; |
10
|
|
|
use Maps\Map\MapOutput; |
11
|
|
|
use Maps\Map\MapOutputBuilder; |
12
|
|
|
use Maps\MappingService; |
13
|
|
|
use Maps\Presentation\ElementJsonSerializer; |
14
|
|
|
use Maps\Map\MapHtmlBuilder; |
15
|
|
|
use Maps\Presentation\WikitextParser; |
16
|
|
|
use Maps\Presentation\WikitextParsers\LocationParser; |
17
|
|
|
use Parser; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class handling the #display_map rendering. |
21
|
|
|
* |
22
|
|
|
* @licence GNU GPL v2+ |
23
|
|
|
* @author Jeroen De Dauw < [email protected] > |
24
|
|
|
* @author Kim Eik |
25
|
|
|
*/ |
26
|
|
|
class DisplayMapRenderer { |
27
|
|
|
|
28
|
|
|
public $service; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LocationParser |
32
|
|
|
*/ |
33
|
|
|
private $locationParser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MediaWikiFileUrlFinder |
37
|
|
|
*/ |
38
|
|
|
private $fileUrlFinder; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var WikitextParser |
42
|
|
|
*/ |
43
|
|
|
private $wikitextParser; |
44
|
|
|
/** |
45
|
|
|
* @var ElementJsonSerializer |
46
|
|
|
*/ |
47
|
|
|
private $elementSerializer; |
48
|
|
|
|
49
|
27 |
|
public function __construct( MappingService $service = null ) { |
50
|
27 |
|
$this->service = $service; |
51
|
27 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Handles the request from the parser hook by doing the work that's common for all |
55
|
|
|
* mapping services, calling the specific methods and finally returning the resulting output. |
56
|
|
|
* |
57
|
|
|
* @param MapData $mapData |
58
|
|
|
* @param Parser $parser |
59
|
|
|
* |
60
|
|
|
* @return MapOutput |
61
|
|
|
*/ |
62
|
27 |
|
public final function renderMap( MapData $mapData, Parser $parser ): MapOutput { |
63
|
27 |
|
$factory = \Maps\MapsFactory::globalInstance(); |
64
|
|
|
|
65
|
27 |
|
$this->locationParser = $factory->newLocationParser(); |
66
|
27 |
|
$this->fileUrlFinder = $factory->getFileUrlFinder(); |
67
|
|
|
|
68
|
27 |
|
$this->wikitextParser = new WikitextParser( clone $parser ); |
69
|
27 |
|
$this->elementSerializer = new ElementJsonSerializer( $this->wikitextParser ); |
70
|
|
|
|
71
|
27 |
|
$mapData->setParameters( $this->handleMarkerData( $mapData->getParameters() ) ); |
72
|
|
|
|
73
|
|
|
// TODO: inject |
74
|
27 |
|
$outputBuilder = new MapOutputBuilder(); |
75
|
|
|
|
76
|
27 |
|
return $outputBuilder->buildOutput( $this->service, $mapData ); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Converts the data in the coordinates parameter to JSON-ready objects. |
81
|
|
|
* These get stored in the locations parameter, and the coordinates on gets deleted. |
82
|
|
|
*/ |
83
|
27 |
|
private function handleMarkerData( array $params ) { |
84
|
27 |
|
$params['centre'] = $this->getCenter( $params['centre'] ); |
85
|
|
|
|
86
|
|
|
// FIXME: this parameter is google maps service specific |
87
|
27 |
|
if ( array_key_exists( 'wmsoverlay', $params ) && is_object( $params['wmsoverlay'] ) ) { |
88
|
|
|
$params['wmsoverlay'] = $params['wmsoverlay']->getJSONObject(); |
89
|
|
|
} |
90
|
|
|
|
91
|
27 |
|
$params['locations'] = $this->getLocationJson( $params ); |
92
|
|
|
|
93
|
27 |
|
unset( $params['coordinates'] ); |
94
|
|
|
|
95
|
27 |
|
$this->handleShapeData( $params ); |
96
|
|
|
|
97
|
27 |
|
return $params; |
98
|
|
|
} |
99
|
|
|
|
100
|
27 |
|
private function getCenter( $coordinatesOrAddress ) { |
101
|
27 |
|
if ( $coordinatesOrAddress === false ) { |
102
|
27 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
// FIXME: a Location makes no sense here, since the non-coordinate data is not used |
107
|
|
|
$location = $this->locationParser->parse( $coordinatesOrAddress ); |
108
|
|
|
} |
109
|
|
|
catch ( \Exception $ex ) { |
110
|
|
|
// TODO: somehow report this to the user |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $location->getJSONObject(); |
115
|
|
|
} |
116
|
|
|
|
117
|
27 |
|
private function getLocationJson( array $params ) { |
118
|
27 |
|
$iconUrl = $this->fileUrlFinder->getUrlForFileName( $params['icon'] ); |
119
|
27 |
|
$visitedIconUrl = $this->fileUrlFinder->getUrlForFileName( $params['visitedicon'] ?? '' ); |
120
|
|
|
|
121
|
27 |
|
$locationJsonObjects = []; |
122
|
|
|
|
123
|
27 |
|
foreach ( $params['coordinates'] as $coordinatesOrAddress ) { |
124
|
|
|
try { |
125
|
16 |
|
$location = $this->locationParser->parse( $coordinatesOrAddress ); |
126
|
|
|
} |
127
|
1 |
|
catch ( \Exception $ex ) { |
128
|
|
|
// TODO: somehow report this to the user |
129
|
1 |
|
continue; |
130
|
|
|
} |
131
|
|
|
|
132
|
15 |
|
$locationJsonObjects[] = $this->getLocationJsonObject( |
133
|
15 |
|
$location, |
134
|
|
|
$params, |
135
|
|
|
$iconUrl, |
136
|
|
|
$visitedIconUrl |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
27 |
|
return $locationJsonObjects; |
141
|
|
|
} |
142
|
|
|
|
143
|
15 |
|
private function getLocationJsonObject( Location $location, array $params, $iconUrl, $visitedIconUrl ) { |
144
|
15 |
|
$jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl, '', '', $visitedIconUrl ); |
145
|
|
|
|
146
|
15 |
|
$this->elementSerializer->titleAndText( $jsonObj ); |
147
|
|
|
|
148
|
15 |
|
if ( isset( $jsonObj['inlineLabel'] ) ) { |
149
|
1 |
|
$jsonObj['inlineLabel'] = strip_tags( |
150
|
1 |
|
$this->wikitextParser->wikitextToHtml( $jsonObj['inlineLabel'] ), |
151
|
1 |
|
'<a><img>' |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
15 |
|
return $jsonObj; |
156
|
|
|
} |
157
|
|
|
|
158
|
27 |
|
private function handleShapeData( array &$params ) { |
159
|
|
|
$textContainers = [ |
160
|
27 |
|
&$params['lines'], |
161
|
27 |
|
&$params['polygons'], |
162
|
27 |
|
&$params['circles'], |
163
|
27 |
|
&$params['rectangles'], |
164
|
27 |
|
&$params['imageoverlays'], // FIXME: this is Google Maps specific!! |
165
|
|
|
]; |
166
|
|
|
|
167
|
27 |
|
foreach ( $textContainers as &$textContainer ) { |
168
|
27 |
|
if ( is_array( $textContainer ) ) { |
169
|
27 |
|
foreach ( $textContainer as &$obj ) { |
170
|
5 |
|
$obj = $this->elementSerializer->elementToJson( $obj ); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
27 |
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: