| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare( strict_types = 1 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Maps\WikitextParsers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DataValues\Geo\Parsers\LatLongParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Jeroen\SimpleGeocoder\Geocoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Maps\LegacyModel\Location; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Maps\FileUrlFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Maps\MapsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Title; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ValueParsers\ParseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ValueParsers\StringValueParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use ValueParsers\ValueParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * ValueParser that parses the string representation of a location. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @since 3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @licence GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Jeroen De Dauw < [email protected] > | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class LocationParser implements ValueParser { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	private $geocoder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	private $fileUrlFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private $useAddressAsTitle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @deprecated Use newInstance instead | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function __construct( $enableLegacyCrud = true ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		if ( $enableLegacyCrud ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 			$this->geocoder = MapsFactory::globalInstance()->getGeocoder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 			$this->fileUrlFinder = MapsFactory::globalInstance()->getFileUrlFinder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			$this->useAddressAsTitle = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 	public static function newInstance( Geocoder $geocoder, FileUrlFinder $fileUrlFinder, bool $useAddressAsTitle = false ): self { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		$instance = new self( false ); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 		$instance->geocoder = $geocoder; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 		$instance->fileUrlFinder = $fileUrlFinder; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 		$instance->useAddressAsTitle = $useAddressAsTitle; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 		return $instance; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @see StringValueParser::stringParse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @since 3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @return Location | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @throws ParseException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	public function parse( $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$separator = '~'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$metaData = explode( $separator, $value ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		$coordinatesOrAddress = array_shift( $metaData ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$coordinates = $this->geocoder->geocode( $coordinatesOrAddress ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		if ( $coordinates === null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			throw new ParseException( 'Location is not a parsable coordinate and not a geocodable address' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$location = new Location( $coordinates ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		if ( $metaData !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			$this->setTitleOrLink( $location, array_shift( $metaData ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			if ( $this->useAddressAsTitle && $this->isAddress( $coordinatesOrAddress ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				$location->setTitle( $coordinatesOrAddress ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		if ( $metaData !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			$location->setText( array_shift( $metaData ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		if ( $metaData !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			$location->setIcon( $this->fileUrlFinder->getUrlForFileName( array_shift( $metaData ) ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		if ( $metaData !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 			$location->setGroup( array_shift( $metaData ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 		if ( $metaData !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 			$location->setInlineLabel( array_shift( $metaData ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		if ( $metaData !== [] ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			$location->setVisitedIcon( $this->fileUrlFinder->getUrlForFileName( array_shift( $metaData ) ) ) ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		return $location; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	private function setTitleOrLink( Location $location, $titleOrLink ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		if ( $this->isLink( $titleOrLink ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			$this->setLink( $location, $titleOrLink ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			$location->setTitle( $titleOrLink ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	private function isLink( $value ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		return strpos( $value, 'link:' ) === 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	private function setLink( Location $location, $link ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		$link = substr( $link, 5 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		$location->setLink( $this->getExpandedLink( $link ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	private function getExpandedLink( $link ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		if ( filter_var( $link, FILTER_VALIDATE_URL ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			return $link; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		$title = Title::newFromText( $link ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		if ( $title === null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 			return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		return $title->getFullURL(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	private function isAddress( string $coordsOrAddress ): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		$coordinateParser = new LatLongParser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			$coordinateParser->parse( $coordsOrAddress ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		catch ( ParseException $parseException ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |