1 | <?php |
||
18 | class SMMapPrinter extends SMW\ResultPrinter { |
||
19 | |||
20 | /** |
||
21 | * @since 0.6 |
||
22 | * |
||
23 | * @var iMappingService |
||
24 | */ |
||
25 | protected $service; |
||
26 | |||
27 | /** |
||
28 | * @since 1.0 |
||
29 | * |
||
30 | * @var string|boolean false |
||
31 | */ |
||
32 | protected $fatalErrorMsg = false; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param $format String |
||
38 | * @param $inline |
||
39 | */ |
||
40 | 2 | public function __construct( $format, $inline = true ) { |
|
45 | |||
46 | /** |
||
47 | * Returns an array containing the parameter info. |
||
48 | * |
||
49 | * @since 1.0 |
||
50 | * |
||
51 | * @return array |
||
52 | */ |
||
53 | protected function getParameterInfo() { |
||
54 | global $smgQPShowTitle, $smgQPTemplate, $smgQPHideNamespace; |
||
55 | |||
56 | $params = ParamDefinition::getCleanDefinitions( MapsMapper::getCommonParameters() ); |
||
57 | |||
58 | $this->service->addParameterInfo( $params ); |
||
59 | |||
60 | $params['staticlocations'] = [ |
||
61 | 'type' => 'mapslocation', |
||
62 | 'aliases' => [ 'locations', 'points' ], |
||
63 | 'default' => [], |
||
64 | 'islist' => true, |
||
65 | 'delimiter' => ';', |
||
66 | 'message' => 'semanticmaps-par-staticlocations', |
||
67 | ]; |
||
68 | |||
69 | $params['showtitle'] = [ |
||
70 | 'type' => 'boolean', |
||
71 | 'aliases' => 'show title', |
||
72 | 'default' => $smgQPShowTitle, |
||
73 | ]; |
||
74 | |||
75 | $params['hidenamespace'] = [ |
||
76 | 'type' => 'boolean', |
||
77 | 'aliases' => 'hide namespace', |
||
78 | 'default' => $smgQPHideNamespace, |
||
79 | ]; |
||
80 | |||
81 | $params['template'] = [ |
||
82 | 'default' => $smgQPTemplate, |
||
83 | ]; |
||
84 | |||
85 | $params['userparam'] = [ |
||
86 | 'default' => '', |
||
87 | ]; |
||
88 | |||
89 | $params['activeicon'] = [ |
||
90 | 'type' => 'string', |
||
91 | 'default' => '', |
||
92 | ]; |
||
93 | |||
94 | $params['pagelabel'] = [ |
||
95 | 'type' => 'boolean', |
||
96 | 'default' => false, |
||
97 | ]; |
||
98 | |||
99 | // Messages: |
||
100 | // semanticmaps-par-staticlocations, semanticmaps-par-forceshow, semanticmaps-par-showtitle, |
||
101 | // semanticmaps-par-hidenamespace, semanticmaps-par-centre, semanticmaps-par-template, |
||
102 | // semanticmaps-par-geocodecontrol, semanticmaps-par-activeicon semanticmaps-par-markerlabel |
||
103 | foreach ( $params as $name => &$data ) { |
||
104 | if ( is_array( $data ) && !array_key_exists( 'message', $data ) ) { |
||
105 | $data['message'] = 'semanticmaps-par-' . $name; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | $params = array_merge( $params, MapsDisplayMap::getCommonMapParams() ); |
||
110 | |||
111 | return $params; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Builds up and returns the HTML for the map, with the queried coordinate data on it. |
||
116 | * |
||
117 | * @param SMWQueryResult $res |
||
118 | * @param $outputmode |
||
119 | * |
||
120 | * @return array or string |
||
121 | */ |
||
122 | public final function getResultText( SMWQueryResult $res, $outputmode ) { |
||
123 | if ( $this->fatalErrorMsg !== false ) { |
||
124 | return $this->fatalErrorMsg; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @var Parser $wgParser |
||
129 | */ |
||
130 | global $wgParser; |
||
131 | |||
132 | $params = $this->params; |
||
133 | |||
134 | $queryHandler = new SMQueryHandler( $res, $outputmode ); |
||
135 | $queryHandler->setLinkStyle($params['link']); |
||
136 | $queryHandler->setHeaderStyle($params['headers']); |
||
137 | $queryHandler->setShowSubject( $params['showtitle'] ); |
||
138 | $queryHandler->setTemplate( $params['template'] ); |
||
139 | $queryHandler->setUserParam( $params['userparam'] ); |
||
140 | $queryHandler->setHideNamespace( $params['hidenamespace'] ); |
||
141 | $queryHandler->setActiveIcon( $params['activeicon'] ); |
||
142 | |||
143 | $this->handleMarkerData( $params, $queryHandler ); |
||
144 | $locationAmount = count( $params['locations'] ); |
||
145 | |||
146 | if ( $locationAmount > 0 ) { |
||
147 | // We can only take care of the zoom defaulting here, |
||
148 | // as not all locations are available in whats passed to Validator. |
||
149 | if ( $this->fullParams['zoom']->wasSetToDefault() && $locationAmount > 1 ) { |
||
150 | $params['zoom'] = false; |
||
151 | } |
||
152 | |||
153 | $mapName = $this->service->getMapId(); |
||
154 | |||
155 | SMWOutputs::requireHeadItem( |
||
156 | $mapName, |
||
157 | $this->service->getDependencyHtml() . |
||
158 | $configVars = Skin::makeVariablesScript( $this->service->getConfigVariables() ) |
||
159 | ); |
||
160 | |||
161 | foreach ( $this->service->getResourceModules() as $resourceModule ) { |
||
162 | SMWOutputs::requireResource( $resourceModule ); |
||
163 | } |
||
164 | |||
165 | if ( array_key_exists( 'source', $params ) ) { |
||
166 | unset( $params['source'] ); |
||
167 | } |
||
168 | |||
169 | return $this->getMapHTML( $params, $wgParser, $mapName ); |
||
170 | } |
||
171 | else { |
||
172 | return $params['default']; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Returns the HTML to display the map. |
||
178 | * |
||
179 | * @since 1.1 |
||
180 | * |
||
181 | * @param array $params |
||
182 | * @param Parser $parser |
||
183 | * @param string $mapName |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | protected function getMapHTML( array $params, Parser $parser, $mapName ) { |
||
188 | return Html::rawElement( |
||
189 | 'div', |
||
190 | [ |
||
191 | 'id' => $mapName, |
||
192 | 'style' => "width: {$params['width']}; height: {$params['height']}; background-color: #cccccc; overflow: hidden;", |
||
193 | 'class' => 'maps-map maps-' . $this->service->getName() |
||
194 | ], |
||
195 | wfMessage( 'maps-loading-map' )->inContentLanguage()->escaped() . |
||
196 | Html::element( |
||
197 | 'div', |
||
198 | [ 'style' => 'display:none', 'class' => 'mapdata' ], |
||
199 | FormatJson::encode( $this->getJSONObject( $params, $parser ) ) |
||
200 | ) |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Returns a PHP object to encode to JSON with the map data. |
||
206 | * |
||
207 | * @since 1.1 |
||
208 | * |
||
209 | * @param array $params |
||
210 | * @param Parser $parser |
||
211 | * |
||
212 | * @return mixed |
||
213 | */ |
||
214 | protected function getJSONObject( array $params, Parser $parser ) { |
||
217 | |||
218 | /** |
||
219 | * Converts the data in the coordinates parameter to JSON-ready objects. |
||
220 | * These get stored in the locations parameter, and the coordinates on gets deleted. |
||
221 | * |
||
222 | * @since 1.0 |
||
223 | * |
||
224 | * @param array &$params |
||
225 | * @param SMQueryHandler $queryHandler |
||
226 | */ |
||
227 | protected function handleMarkerData( array &$params, SMQueryHandler $queryHandler ) { |
||
228 | if ( is_object( $params['centre'] ) ) { |
||
229 | $params['centre'] = $params['centre']->getJSONObject(); |
||
230 | } |
||
231 | |||
232 | $iconUrl = MapsMapper::getFileUrl( $params['icon'] ); |
||
233 | $visitedIconUrl = MapsMapper::getFileUrl( $params['visitedicon'] ); |
||
234 | |||
235 | $params['locations'] = $this->getJsonForStaticLocations( |
||
236 | $params['staticlocations'], |
||
237 | $params, |
||
238 | $iconUrl, |
||
239 | $visitedIconUrl |
||
240 | ); |
||
241 | |||
242 | unset( $params['staticlocations'] ); |
||
243 | |||
244 | $this->addShapeData( $queryHandler->getShapes(), $params, $iconUrl, $visitedIconUrl ); |
||
245 | |||
246 | if ( $params['format'] === 'openlayers' ) { |
||
247 | $params['layers'] = MapsDisplayMapRenderer::evilOpenLayersHack( $params['layers'] ); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | protected function getJsonForStaticLocations( array $staticLocations, array $params, $iconUrl, $visitedIconUrl ) { |
||
252 | /** |
||
253 | * @var Parser $wgParser |
||
254 | */ |
||
255 | global $wgParser; |
||
256 | |||
257 | $parser = version_compare( $GLOBALS['wgVersion'], '1.18', '<' ) ? $wgParser : clone $wgParser; |
||
258 | |||
259 | $locationsJson = []; |
||
260 | |||
261 | foreach ( $staticLocations as $location ) { |
||
262 | $locationsJson[] = $this->getJsonForStaticLocation( |
||
263 | $location, |
||
264 | $params, |
||
265 | $iconUrl, |
||
266 | $visitedIconUrl, |
||
267 | $parser |
||
268 | ); |
||
269 | } |
||
270 | |||
271 | return $locationsJson; |
||
272 | } |
||
273 | |||
274 | protected function getJsonForStaticLocation( Location $location, array $params, $iconUrl, $visitedIconUrl, Parser $parser ) { |
||
275 | $jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl, '', '', $visitedIconUrl ); |
||
276 | |||
277 | $jsonObj['title'] = $parser->parse( $jsonObj['title'], $parser->getTitle(), new ParserOptions() )->getText(); |
||
278 | $jsonObj['text'] = $parser->parse( $jsonObj['text'], $parser->getTitle(), new ParserOptions() )->getText(); |
||
279 | |||
280 | $hasTitleAndtext = $jsonObj['title'] !== '' && $jsonObj['text'] !== ''; |
||
281 | $jsonObj['text'] = ( $hasTitleAndtext ? '<b>' . $jsonObj['title'] . '</b><hr />' : $jsonObj['title'] ) . $jsonObj['text']; |
||
282 | $jsonObj['title'] = strip_tags( $jsonObj['title'] ); |
||
283 | |||
284 | if ( $params['pagelabel'] ) { |
||
285 | $jsonObj['inlineLabel'] = Linker::link( Title::newFromText( $jsonObj['title'] ) ); |
||
286 | } |
||
287 | |||
288 | return $jsonObj; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param Element[] $queryShapes |
||
293 | * @param array $params |
||
294 | * @param string $iconUrl |
||
295 | * @param string $visitedIconUrl |
||
296 | */ |
||
297 | protected function addShapeData( array $queryShapes, array &$params, $iconUrl, $visitedIconUrl ) { |
||
298 | $params['locations'] = array_merge( |
||
299 | $params['locations'], |
||
300 | $this->getJsonForLocations( |
||
301 | $queryShapes['locations'], |
||
302 | $params, |
||
303 | $iconUrl, |
||
304 | $visitedIconUrl |
||
305 | ) |
||
306 | ); |
||
307 | |||
308 | $params['lines'] = $this->getElementJsonArray( $queryShapes['lines'], $params ); |
||
309 | $params['polygons'] = $this->getElementJsonArray( $queryShapes['polygons'], $params ); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param Location[] $locations |
||
314 | * @param array $params |
||
315 | * @param string $iconUrl |
||
316 | * @param string $visitedIconUrl |
||
317 | * |
||
318 | * @return array |
||
319 | */ |
||
320 | protected function getJsonForLocations( array $locations, array $params, $iconUrl, $visitedIconUrl ) { |
||
321 | $locationsJson = []; |
||
322 | |||
323 | foreach ( $locations as $location ) { |
||
324 | $jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl, '', '', $visitedIconUrl ); |
||
325 | |||
326 | $jsonObj['title'] = strip_tags( $jsonObj['title'] ); |
||
327 | |||
328 | $locationsJson[] = $jsonObj; |
||
329 | } |
||
330 | |||
331 | return $locationsJson; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param BaseElement[] $elements |
||
336 | * @param array $params |
||
337 | * |
||
338 | * @return array |
||
339 | */ |
||
340 | protected function getElementJsonArray( array $elements, array $params ) { |
||
341 | $elementsJson = []; |
||
342 | |||
343 | foreach ( $elements as $element ) { |
||
344 | $jsonObj = $element->getJSONObject( $params['title'], $params['label'] ); |
||
345 | $elementsJson[] = $jsonObj; |
||
346 | } |
||
347 | |||
348 | return $elementsJson; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * Returns the internationalized name of the mapping service. |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public final function getName() { |
||
359 | |||
360 | /** |
||
361 | * Returns a list of parameter information, for usage by Special:Ask and others. |
||
362 | * |
||
363 | * @return array |
||
364 | */ |
||
365 | public function getParameters() { |
||
366 | $params = parent::getParameters(); |
||
377 | } |
||
378 |
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: