| 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 | 2 |  | 	public function __construct( string $text, string $modelId = self::CONTENT_MODEL_ID ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 2 |  | 		parent::__construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 2 |  | 			$text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 			$modelId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 2 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 2 |  | 	public function getData(): Status { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 2 |  | 		$status = parent::getData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  | 		if ( $status->isGood() && !$this->isGeoJson( $status->getValue() ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  | 			return Status::newFatal( 'Invalid GeoJson' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  | 		return $status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 2 |  | 	private function isGeoJson( $json ): bool { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 2 |  | 		return property_exists( $json, 'type' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 2 |  | 			&& $json->type === 'FeatureCollection' | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 2 |  | 			&& property_exists( $json, 'features' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 2 |  | 			&& is_array( $json->features ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	protected function fillParserOutput( Title $title, $revId, ParserOptions $options, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		$generateHtml, ParserOutput &$output ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		if ( $generateHtml && $this->isValid() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			( GeoJsonMapPageUi::forExistingPage( $this->beautifyJSON() ) )->addToOutput( OutputFacade::newFromParserOutput( $output ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			$output->setText( '' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  |