| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Maps\MediaWiki\ParserHooks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Maps; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Maps\MappingService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Maps\MappingServices; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Maps\MapsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Maps\Presentation\ParameterExtractor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use MWException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ParamProcessor\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Class for the 'display_map' parser hooks. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class DisplayMapFunction { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	private $services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	private $renderer; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 23 |  | 	public function __construct( MappingServices $services ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 23 |  | 		$this->services = $services; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 23 |  | 		$this->renderer = new DisplayMapRenderer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 23 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @param Parser $parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @param string[] $parameters Values of the array can be named parameters ("key=value") or unnamed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * They are not normalized, so can be "key =  value " | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @throws MWException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 21 |  | 	public function getMapHtmlForKeyValueStrings( Parser $parser, array $parameters ): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 21 |  | 		$processor = new Processor( new \ParamProcessor\Options() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 21 |  | 		$service = $this->services->getServiceOrDefault( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 21 |  | 			$this->extractServiceName( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 21 |  | 				Maps\Presentation\ParameterExtractor::extractFromKeyValueStrings( $parameters ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 21 |  | 		$this->renderer->service = $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 21 |  | 		$processor->setFunctionParams( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 21 |  | 			$parameters, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 21 |  | 			[], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 21 |  | 			self::getDefaultParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 21 |  | 		$processor->setParameterDefinitions( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 21 |  | 			$this->getAllParameterDefinitions( $service, ';' ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 21 |  | 		$this->trackMap( $parser ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 21 |  | 		return $this->renderer->renderMap( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 21 |  | 			$service->processingResultToMapParams( $processor->processParameters() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			$parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @param Parser $parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @param string[] $parameters Key value list of parameters. Unnamed parameters have numeric keys. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * Both keys and values have not been normalized. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @throws MWException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  | 	public function getMapHtmlForParameterList( Parser $parser, array $parameters ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  | 		$processor = new Processor( new \ParamProcessor\Options() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  | 		$service = $this->services->getServiceOrDefault( $this->extractServiceName( $parameters ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  | 		$this->renderer->service = $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  | 		$processor->setParameters( $parameters ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  | 		$processor->setParameterDefinitions( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  | 			$this->getAllParameterDefinitions( $service, "\n" ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  | 		$this->trackMap( $parser ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  | 		return $this->renderer->renderMap( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  | 			$service->processingResultToMapParams( $processor->processParameters() ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 			$parser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 23 |  | 	private function getAllParameterDefinitions( MappingService $service, string $locationDelimiter ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 23 |  | 		$params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 23 |  | 		$params['mappingservice'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 23 |  | 			'type' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 23 |  | 			'aliases' => 'service', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 23 |  | 			'default' => $GLOBALS['egMapsDefaultService'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 23 |  | 			'values' => MapsFactory::globalInstance()->getMappingServices()->getAllNames(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 23 |  | 			'message' => 'maps-par-mappingservice' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 23 |  | 		$params['coordinates'] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 23 |  | 			'type' => 'string', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 			'aliases' => [ 'coords', 'location', 'address', 'addresses', 'locations', 'points' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 			'default' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			'islist' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 23 |  | 			'delimiter' => $locationDelimiter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 23 |  | 			'message' => 'maps-displaymap-par-coordinates', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 23 |  | 		return MapsFactory::globalInstance()->getParamDefinitionFactory()->newDefinitionsFromArrays( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 23 |  | 			array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 23 |  | 				$params, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 23 |  | 				$service->getParameterInfo() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 23 |  | 	private function extractServiceName( array $parameters ): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 23 |  | 		$service = ( new ParameterExtractor() )->extract( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 23 |  | 			[ 'mappingservice', 'service' ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			$parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 23 |  | 		return $service ?? ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 23 |  | 	public static function getDefaultParameters(): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 23 |  | 		return [ 'coordinates' ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 23 |  | 	private function trackMap( Parser $parser ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 23 |  | 		if ( $GLOBALS['egMapsEnableCategory'] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			$parser->addTrackingCategory( 'maps-tracking-category' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 23 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  |