diff options
Diffstat (limited to 'www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php')
-rw-r--r-- | www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php | 57 |
1 files changed, 32 insertions, 25 deletions
diff --git a/www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php b/www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php index 72a89b04..c6eaae23 100644 --- a/www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php +++ b/www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php @@ -2,48 +2,55 @@ namespace Maps\MediaWiki\Content; -use Html; +use FormatJson; +use Maps\Presentation\GeoJsonMapPageUi; +use Maps\Presentation\OutputFacade; use ParserOptions; use ParserOutput; +use Status; use Title; class GeoJsonContent extends \JsonContent { public const CONTENT_MODEL_ID = 'GeoJSON'; + public static function newEmptyContentString(): string { + $text = '{"type": "FeatureCollection", "features": []}'; + return FormatJson::encode( FormatJson::parse( $text )->getValue(), true, FormatJson::UTF8_OK ); + } + public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) { - parent::__construct( $text, $modelId ); + parent::__construct( + $text, + $modelId + ); + } + + public function getData(): Status { + $status = parent::getData(); + + if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) { + return Status::newFatal( 'Invalid GeoJson' ); + } + + return $status; + } + + private function isGeoJson( $json ): bool { + return property_exists( $json, 'type' ) + && $json->type === 'FeatureCollection' + && property_exists( $json, 'features' ) + && is_array( $json->features ); } protected function fillParserOutput( Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output ) { if ( $generateHtml && $this->isValid() ) { - $output->setText( $this->getMapHtml( $this->beautifyJSON() ) ); - $output->addModules( 'ext.maps.leaflet.editor' ); + ( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) ); } else { $output->setText( '' ); } } - private function getMapHtml( string $jsonString ): string { - return - Html::element( - 'div', - [ - 'id' => 'GeoJsonMap', - 'class' => 'GeoJsonMap', - ] - ) - . '<style>' - . '.GeoJsonMap {width: "100%"; height: 600px; display: "inline-block"}' - . '</style>' - . - Html::element( - 'script', - [], - 'var GeoJson =' . $jsonString . ';' - ); - } - -}
\ No newline at end of file +} |