diff options
author | Yaco <franco@reevo.org> | 2021-10-19 20:24:11 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2021-10-19 20:24:11 -0300 |
commit | e3880a1c86acaa3bbd05786ad2f5c586e6511a58 (patch) | |
tree | ec77bfc5b69f259a159c95188797bd0dade92357 /www/wiki/extensions/Maps/src/MediaWiki/Content/GeoJsonContent.php | |
parent | 20ca0685509f8010580d3b45036a64ab48616af1 (diff) |
updates Maps to 7.13.0
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 +} |