summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/MediaWiki/ParserHooks/DisplayMapRenderer.php
blob: 25a80683e451e953bd8e6f5a7a045049a78f6ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

namespace Maps\MediaWiki\ParserHooks;

use FormatJson;
use Html;
use Maps\DataAccess\MediaWikiFileUrlFinder;
use Maps\Elements\Location;
use Maps\MappingService;
use Maps\Presentation\ElementJsonSerializer;
use Maps\Presentation\WikitextParser;
use Maps\Presentation\WikitextParsers\LocationParser;
use Parser;

/**
 * Class handling the #display_map rendering.
 *
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 * @author Kim Eik
 */
class DisplayMapRenderer {

	public $service;

	/**
	 * @var LocationParser
	 */
	private $locationParser;

	/**
	 * @var MediaWikiFileUrlFinder
	 */
	private $fileUrlFinder;

	/**
	 * @var WikitextParser
	 */
	private $wikitextParser;
	/**
	 * @var ElementJsonSerializer
	 */
	private $elementSerializer;

	public function __construct( MappingService $service = null ) {
		$this->service = $service;
	}

	/**
	 * Handles the request from the parser hook by doing the work that's common for all
	 * mapping services, calling the specific methods and finally returning the resulting output.
	 *
	 * @param array $params
	 * @param Parser $parser
	 *
	 * @return string
	 */
	public final function renderMap( array $params, Parser $parser ) {
		$factory = \Maps\MapsFactory::newDefault();

		$this->locationParser = $factory->newLocationParser();
		$this->fileUrlFinder = $factory->getFileUrlFinder();

		$this->wikitextParser = new WikitextParser( clone $parser );
		$this->elementSerializer = new ElementJsonSerializer( $this->wikitextParser );

		$this->handleMarkerData( $params );

		$output = $this->getMapHTML(
			$params,
			$this->service->newMapId()
		);

		$dependencies = $this->service->getDependencyHtml( $params );

		// Only add a head item when there are dependencies.
		if ( $dependencies ) {
			$parser->getOutput()->addHeadItem( $dependencies );
		}

		$parser->getOutput()->addModules( $this->service->getResourceModules() );

		return $output;
	}

	/**
	 * Converts the data in the coordinates parameter to JSON-ready objects.
	 * These get stored in the locations parameter, and the coordinates on gets deleted.
	 */
	private function handleMarkerData( array &$params ) {
		$params['centre'] = $this->getCenter( $params['centre'] );

		if ( is_object( $params['wmsoverlay'] ) ) {
			$params['wmsoverlay'] = $params['wmsoverlay']->getJSONObject();
		}

		$params['locations'] = $this->getLocationJson( $params );

		unset( $params['coordinates'] );

		$this->handleShapeData( $params );
	}

	private function getCenter( $coordinatesOrAddress ) {
		if ( $coordinatesOrAddress === false ) {
			return false;
		}

		try {
			// FIXME: a Location makes no sense here, since the non-coordinate data is not used
			$location = $this->locationParser->parse( $coordinatesOrAddress );
		}
		catch ( \Exception $ex ) {
			// TODO: somehow report this to the user
			return false;
		}

		return $location->getJSONObject();
	}

	private function getLocationJson( array $params ) {
		$iconUrl = $this->fileUrlFinder->getUrlForFileName( $params['icon'] );
		$visitedIconUrl = $this->fileUrlFinder->getUrlForFileName( $params['visitedicon'] );

		$locationJsonObjects = [];

		foreach ( $params['coordinates'] as $coordinatesOrAddress ) {
			try {
				$location = $this->locationParser->parse( $coordinatesOrAddress );
			}
			catch ( \Exception $ex ) {
				// TODO: somehow report this to the user
				continue;
			}

			$locationJsonObjects[] = $this->getLocationJsonObject(
				$location,
				$params,
				$iconUrl,
				$visitedIconUrl
			);
		}

		return $locationJsonObjects;
	}

	private function getLocationJsonObject( Location $location, array $params, $iconUrl, $visitedIconUrl ) {
		$jsonObj = $location->getJSONObject( $params['title'], $params['label'], $iconUrl, '', '', $visitedIconUrl );

		$this->elementSerializer->titleAndText( $jsonObj );

		if ( isset( $jsonObj['inlineLabel'] ) ) {
			$jsonObj['inlineLabel'] = strip_tags(
				$this->wikitextParser->wikitextToHtml( $jsonObj['inlineLabel'] ),
				'<a><img>'
			);
		}

		return $jsonObj;
	}

	private function handleShapeData( array &$params ) {
		$textContainers = [
			&$params['lines'],
			&$params['polygons'],
			&$params['circles'],
			&$params['rectangles'],
			&$params['imageoverlays'], // FIXME: this is Google Maps specific!!
		];

		foreach ( $textContainers as &$textContainer ) {
			if ( is_array( $textContainer ) ) {
				foreach ( $textContainer as &$obj ) {
					$obj = $this->elementSerializer->elementToJson( $obj );
				}
			}
		}
	}

	/**
	 * Returns the HTML to display the map.
	 *
	 * @param array $params
	 * @param string $mapName
	 *
	 * @return string
	 */
	protected function getMapHTML( array $params, $mapName ) {
		return Html::rawElement(
			'div',
			[
				'id' => $mapName,
				'style' => "width: {$params['width']}; height: {$params['height']}; background-color: #cccccc; overflow: hidden;",
				'class' => 'maps-map maps-' . $this->service->getName()
			],
			wfMessage( 'maps-loading-map' )->inContentLanguage()->escaped() .
			Html::element(
				'div',
				[ 'style' => 'display:none', 'class' => 'mapdata' ],
				FormatJson::encode( $params )
			)
		);
	}

}