1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maps\MediaWiki\Content; |
4
|
|
|
|
5
|
|
|
use FormatJson; |
6
|
|
|
use Maps\Presentation\GeoJsonMapPageUi; |
7
|
|
|
use Maps\Presentation\OutputFacade; |
8
|
|
|
use ParserOptions; |
9
|
|
|
use ParserOutput; |
10
|
|
|
use Status; |
11
|
|
|
use Title; |
12
|
|
|
|
13
|
|
|
class GeoJsonContent extends \JsonContent { |
14
|
|
|
|
15
|
|
|
public const CONTENT_MODEL_ID = 'GeoJSON'; |
16
|
|
|
|
17
|
|
|
public static function newEmptyContentString(): string { |
18
|
|
|
$text = '{"type": "FeatureCollection", "features": []}'; |
19
|
|
|
return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK ); |
20
|
|
|
} |
21
|
|
|
|
22
|
3 |
|
public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) { |
23
|
3 |
|
parent::__construct( |
24
|
3 |
|
$text, |
25
|
|
|
$modelId |
26
|
|
|
); |
27
|
3 |
|
} |
28
|
|
|
|
29
|
3 |
|
public function getData(): Status { |
30
|
3 |
|
$status = parent::getData(); |
31
|
|
|
|
32
|
3 |
|
if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) { |
33
|
1 |
|
return Status::newFatal( 'Invalid GeoJson' ); |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
return $status; |
37
|
|
|
} |
38
|
|
|
|
39
|
3 |
|
private function isGeoJson( $json ): bool { |
40
|
3 |
|
return property_exists( $json, 'type' ) |
41
|
3 |
|
&& $json->type === 'FeatureCollection' |
42
|
3 |
|
&& property_exists( $json, 'features' ) |
43
|
3 |
|
&& is_array( $json->features ); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
protected function fillParserOutput( Title $title, $revId, ParserOptions $options, |
47
|
|
|
$generateHtml, ParserOutput &$output ) { |
48
|
|
|
|
49
|
1 |
|
if ( $generateHtml && $this->isValid() ) { |
50
|
1 |
|
( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) ); |
51
|
|
|
} else { |
52
|
|
|
$output->setText( '' ); |
53
|
|
|
} |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
} |
57
|
|
|
|