1 | <?php |
||
27 | class MapPrinter extends SMW\ResultPrinter { |
||
28 | |||
29 | private static $services = []; |
||
30 | |||
31 | /** |
||
32 | * @var LocationParser |
||
33 | */ |
||
34 | private $locationParser; |
||
35 | |||
36 | /** |
||
37 | * @var MappingService |
||
38 | */ |
||
39 | private $service; |
||
40 | |||
41 | /** |
||
42 | * @var string|boolean |
||
43 | */ |
||
44 | private $fatalErrorMsg = false; |
||
45 | |||
46 | /** |
||
47 | * @param string $format |
||
48 | * @param bool $inline |
||
49 | */ |
||
50 | public function __construct( $format, $inline = true ) { |
||
51 | $this->service = self::$services[$format]; |
||
52 | |||
53 | parent::__construct( $format, $inline ); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @since 3.4 |
||
58 | * FIXME: this is a temporary hack that should be replaced when SMW allows for dependency |
||
59 | * injection in query printers. |
||
60 | * |
||
61 | * @param MappingService $service |
||
62 | */ |
||
63 | public static function registerService( MappingService $service ) { |
||
64 | self::$services[$service->getName()] = $service; |
||
65 | } |
||
66 | |||
67 | public static function registerDefaultService( $serviceName ) { |
||
68 | self::$services['map'] = self::$services[$serviceName]; |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Builds up and returns the HTML for the map, with the queried coordinate data on it. |
||
73 | * |
||
74 | * @param SMWQueryResult $res |
||
75 | * @param int $outputMode |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | public final function getResultText( SMWQueryResult $res, $outputMode ) { |
||
80 | if ( $this->fatalErrorMsg !== false ) { |
||
81 | return $this->fatalErrorMsg; |
||
|
|||
82 | } |
||
83 | |||
84 | $this->addTrackingCategoryIfNeeded(); |
||
85 | |||
86 | $params = $this->params; |
||
87 | |||
88 | $this->initializeLocationParser(); |
||
89 | |||
90 | $queryHandler = new QueryHandler( $res, $outputMode ); |
||
91 | $queryHandler->setLinkStyle( $params['link'] ); |
||
92 | $queryHandler->setHeaderStyle( $params['headers'] ); |
||
93 | $queryHandler->setShowSubject( $params['showtitle'] ); |
||
94 | $queryHandler->setTemplate( $params['template'] ); |
||
95 | $queryHandler->setUserParam( $params['userparam'] ); |
||
96 | $queryHandler->setHideNamespace( $params['hidenamespace'] ); |
||
97 | $queryHandler->setActiveIcon( $params['activeicon'] ); |
||
98 | |||
99 | $this->handleMarkerData( $params, $queryHandler ); |
||
100 | |||
101 | $params['ajaxquery'] = urlencode( $params['ajaxquery'] ); |
||
102 | |||
103 | $this->service->addHtmlDependencies( |
||
104 | DisplayMapRenderer::getLayerDependencies( $params['format'], $params ) |
||
105 | ); |
||
106 | |||
107 | $locationAmount = count( $params['locations'] ); |
||
108 | |||
109 | if ( $locationAmount > 0 ) { |
||
110 | // We can only take care of the zoom defaulting here, |
||
111 | // as not all locations are available in whats passed to Validator. |
||
112 | if ( $this->fullParams['zoom']->wasSetToDefault() && $locationAmount > 1 ) { |
||
113 | $params['zoom'] = false; |
||
114 | } |
||
115 | |||
116 | $mapName = $this->service->getMapId(); |
||
117 | |||
118 | SMWOutputs::requireHeadItem( |
||
119 | $mapName, |
||
120 | $this->service->getDependencyHtml() |
||
121 | ); |
||
122 | |||
123 | foreach ( $this->service->getResourceModules() as $resourceModule ) { |
||
124 | SMWOutputs::requireResource( $resourceModule ); |
||
125 | } |
||
126 | |||
127 | if ( array_key_exists( 'source', $params ) ) { |
||
128 | unset( $params['source'] ); |
||
129 | } |
||
130 | |||
131 | return $this->getMapHTML( $params, $mapName ); |
||
132 | } else { |
||
133 | return $params['default']; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | private function addTrackingCategoryIfNeeded() { |
||
138 | /** |
||
139 | * @var Parser $wgParser |
||
140 | */ |
||
141 | global $wgParser; |
||
142 | |||
143 | if ( $GLOBALS['egMapsEnableCategory'] && $wgParser->getOutput() !== null ) { |
||
144 | $wgParser->addTrackingCategory( 'maps-tracking-category' ); |
||
145 | } |
||
146 | } |
||
147 | |||
148 | private function initializeLocationParser() { |
||
149 | $this->locationParser = \Maps\MapsFactory::newDefault()->newLocationParser(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Converts the data in the coordinates parameter to JSON-ready objects. |
||
154 | * These get stored in the locations parameter, and the coordinates on gets deleted. |
||
155 | * |
||
156 | * @param array &$params |
||
157 | * @param QueryHandler $queryHandler |
||
158 | */ |
||
159 | private function handleMarkerData( array &$params, QueryHandler $queryHandler ) { |
||
160 | $params['centre'] = $this->getCenter( $params['centre'] ); |
||
161 | |||
162 | $iconUrl = MapsFunctions::getFileUrl( $params['icon'] ); |
||
163 | $visitedIconUrl = MapsFunctions::getFileUrl( $params['visitedicon'] ); |
||
164 | |||
165 | $params['locations'] = $this->getJsonForStaticLocations( |
||
166 | $params['staticlocations'], |
||
167 | $params, |
||
168 | $iconUrl, |
||
169 | $visitedIconUrl |
||
170 | ); |
||
171 | |||
172 | unset( $params['staticlocations'] ); |
||
173 | |||
174 | $params['locations'] = array_merge( |
||
175 | $params['locations'], |
||
176 | $this->getJsonForLocations( |
||
177 | $queryHandler->getLocations(), |
||
178 | $params, |
||
179 | $iconUrl, |
||
180 | $visitedIconUrl |
||
181 | ) |
||
182 | ); |
||
183 | |||
184 | if ( $params['format'] === 'openlayers' ) { |
||
185 | $params['layers'] = DisplayMapRenderer::evilOpenLayersHack( $params['layers'] ); |
||
186 | } |
||
187 | |||
188 | |||
189 | } |
||
190 | |||
191 | private function getCenter( $coordinatesOrAddress ) { |
||
192 | if ( $coordinatesOrAddress === false ) { |
||
193 | return false; |
||
194 | } |
||
195 | |||
196 | try { |
||
197 | // FIXME: a Location makes no sense here, since the non-coordinate data is not used |
||
198 | $location = $this->locationParser->parse( $coordinatesOrAddress ); |
||
199 | } |
||
200 | catch ( \Exception $ex ) { |
||
201 | // TODO: somehow report this to the user |
||
202 | return false; |
||
203 | } |
||
204 | |||
205 | return $location->getJSONObject(); |
||
206 | } |
||
207 | |||
208 | private function getJsonForStaticLocations( array $staticLocations, array $params, $iconUrl, $visitedIconUrl ) { |
||
209 | $locationsJson = []; |
||
210 | |||
211 | foreach ( $staticLocations as $location ) { |
||
212 | $locationsJson[] = $this->getJsonForStaticLocation( |
||
213 | $location, |
||
214 | $params, |
||
215 | $iconUrl, |
||
216 | $visitedIconUrl, |
||
217 | clone $GLOBALS['wgParser'] |
||
218 | ); |
||
219 | } |
||
220 | |||
221 | return $locationsJson; |
||
222 | } |
||
223 | |||
224 | private function getJsonForStaticLocation( Location $location, array $params, $iconUrl, $visitedIconUrl, Parser $parser ) { |
||
240 | |||
241 | /** |
||
242 | * @param Location[] $locations |
||
243 | * @param array $params |
||
244 | * @param string $iconUrl |
||
245 | * @param string $visitedIconUrl |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | private function getJsonForLocations( iterable $locations, array $params, string $iconUrl, string $visitedIconUrl ): array { |
||
269 | |||
270 | /** |
||
271 | * @param BaseElement[] $elements |
||
272 | * @param array $params |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | private function getElementJsonArray( array $elements, array $params ): array { |
||
286 | |||
287 | /** |
||
288 | * Returns the HTML to display the map. |
||
289 | * |
||
290 | * @param array $params |
||
291 | * @param string $mapName |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | private function getMapHTML( array $params, string $mapName ): string { |
||
311 | |||
312 | /** |
||
313 | * Returns the internationalized name of the mapping service. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public final function getName() { |
||
320 | |||
321 | /** |
||
322 | * Returns a list of parameter information, for usage by Special:Ask and others. |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | public function getParameters() { |
||
338 | |||
339 | /** |
||
340 | * Returns an array containing the parameter info. |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | private function getParameterInfo() { |
||
411 | } |
||
412 |