1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\MediaWiki\Content; |
4
|
|
|
|
5
|
|
|
use Html; |
6
|
|
|
use ParserOptions; |
7
|
|
|
use ParserOutput; |
8
|
|
|
use Status; |
9
|
|
|
use Title; |
10
|
|
|
|
11
|
|
|
class GeoJsonContent extends \JsonContent { |
12
|
|
|
|
13
|
|
|
public const CONTENT_MODEL_ID = 'GeoJSON'; |
14
|
|
|
|
15
|
|
|
public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) { |
16
|
|
|
parent::__construct( $text, $modelId ); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getData(): Status { |
20
|
|
|
$status = parent::getData(); |
21
|
|
|
|
22
|
|
|
if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) { |
23
|
|
|
return Status::newFatal( 'Invalid GeoJson' ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $status; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function isGeoJson( $json ): bool { |
30
|
|
|
return property_exists( $json, 'type' ) |
31
|
|
|
&& $json->type === 'FeatureCollection' |
32
|
|
|
&& property_exists( $json, 'features' ) |
33
|
|
|
&& is_array( $json->features ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function fillParserOutput( Title $title, $revId, ParserOptions $options, |
37
|
|
|
$generateHtml, ParserOutput &$output ) { |
38
|
|
|
|
39
|
|
|
if ( $generateHtml && $this->isValid() ) { |
40
|
|
|
$output->setText( |
41
|
|
|
$this->getMapHtml() |
42
|
|
|
. |
43
|
|
|
Html::element( |
44
|
|
|
'script', |
45
|
|
|
[], |
46
|
|
|
'var GeoJson =' . $this->beautifyJSON() . ';' |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
$output->addModules( 'ext.maps.leaflet.editor' ); |
50
|
|
|
} else { |
51
|
|
|
$output->setText( '' ); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function getMapHtml(): string { |
56
|
|
|
return $this->wrapHtmlInThumbDivs( |
57
|
|
|
Html::rawElement( |
58
|
|
|
'div', |
59
|
|
|
[ |
60
|
|
|
'id' => 'GeoJsonMap', |
61
|
|
|
'style' => "width: 100%; height: 600px; background-color: #eeeeee; overflow: hidden;", |
62
|
|
|
'class' => 'maps-map maps-leaflet GeoJsonMap' |
63
|
|
|
], |
64
|
|
|
wfMessage( 'maps-loading-map' )->inContentLanguage()->escaped() |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function wrapHtmlInThumbDivs( string $html ): string { |
70
|
|
|
return Html::rawElement( |
71
|
|
|
'div', |
72
|
|
|
[ |
73
|
|
|
'class' => 'thumb' |
74
|
|
|
], |
75
|
|
|
Html::rawElement( |
76
|
|
|
'div', |
77
|
|
|
[ |
78
|
|
|
'class' => 'thumbinner' |
79
|
|
|
], |
80
|
|
|
$html |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|