|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace Maps; |
|
6
|
|
|
|
|
7
|
|
|
use DataValues\Geo\Parsers\LatLongParser; |
|
8
|
|
|
use FileFetcher\Cache\Factory as CacheFactory; |
|
9
|
|
|
use FileFetcher\FileFetcher; |
|
10
|
|
|
use Jeroen\SimpleGeocoder\Geocoder; |
|
11
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder; |
|
12
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder; |
|
13
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder; |
|
14
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder; |
|
15
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder; |
|
16
|
|
|
use Maps\DataAccess\CachingGeocoder; |
|
17
|
|
|
use Maps\DataAccess\GeoJsonFetcher; |
|
18
|
|
|
use Maps\DataAccess\MapsFileFetcher; |
|
19
|
|
|
use Maps\DataAccess\MediaWikiFileUrlFinder; |
|
20
|
|
|
use Maps\DataAccess\PageContentFetcher; |
|
21
|
|
|
use Maps\MediaWiki\ParserHooks\DisplayMapFunction; |
|
22
|
|
|
use Maps\Presentation\CoordinateFormatter; |
|
23
|
|
|
use Maps\Presentation\WikitextParsers\CircleParser; |
|
24
|
|
|
use Maps\Presentation\WikitextParsers\DistanceParser; |
|
25
|
|
|
use Maps\Presentation\WikitextParsers\ImageOverlayParser; |
|
26
|
|
|
use Maps\Presentation\WikitextParsers\LineParser; |
|
27
|
|
|
use Maps\Presentation\WikitextParsers\LocationParser; |
|
28
|
|
|
use Maps\Presentation\WikitextParsers\PolygonParser; |
|
29
|
|
|
use Maps\Presentation\WikitextParsers\RectangleParser; |
|
30
|
|
|
use Maps\Presentation\WikitextParsers\WmsOverlayParser; |
|
31
|
|
|
use MediaWiki\MediaWikiServices; |
|
32
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
|
33
|
|
|
use SimpleCache\Cache\Cache; |
|
34
|
|
|
use SimpleCache\Cache\MediaWikiCache; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @licence GNU GPL v2+ |
|
38
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
39
|
|
|
*/ |
|
40
|
|
|
class MapsFactory { |
|
41
|
|
|
|
|
42
|
|
|
private $settings; |
|
43
|
|
|
private $mediaWikiServices; |
|
44
|
|
|
|
|
45
|
53 |
|
private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) { |
|
46
|
53 |
|
$this->settings = $settings; |
|
47
|
53 |
|
$this->mediaWikiServices = $mediaWikiServices; |
|
48
|
53 |
|
} |
|
49
|
|
|
|
|
50
|
53 |
|
public static function newDefault(): self { |
|
51
|
53 |
|
return new self( $GLOBALS, MediaWikiServices::getInstance() ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Only for legacy code where dependency injection is not possible |
|
56
|
|
|
*/ |
|
57
|
114 |
|
public static function globalInstance(): self { |
|
58
|
114 |
|
static $instance = null; |
|
59
|
|
|
|
|
60
|
114 |
|
if ( $instance === null ) { |
|
61
|
|
|
$instance = self::newDefault(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
114 |
|
return $instance; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
24 |
|
public function newLocationParser(): LocationParser { |
|
68
|
24 |
|
return LocationParser::newInstance( |
|
69
|
24 |
|
$this->getGeocoder(), |
|
70
|
24 |
|
$this->getFileUrlFinder() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
105 |
|
public function getGeocoder(): Geocoder { |
|
75
|
105 |
|
$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() ); |
|
76
|
|
|
|
|
77
|
105 |
|
if ( $this->settings['egMapsEnableGeoCache'] ) { |
|
78
|
105 |
|
return new CachingGeocoder( |
|
79
|
105 |
|
$geocoder, |
|
80
|
105 |
|
$this->getMediaWikiCache(), |
|
81
|
105 |
|
$this->settings['egMapsGeoCacheTtl'] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $geocoder; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
105 |
|
private function newCoreGeocoder(): Geocoder { |
|
89
|
105 |
|
switch ( $this->settings['egMapsDefaultGeoService'] ) { |
|
90
|
105 |
|
case 'geonames': |
|
91
|
|
|
if ( $this->settings['egMapsGeoNamesUser'] === '' ) { |
|
92
|
|
|
return $this->newGoogleGeocoder(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return new GeoNamesGeocoder( |
|
96
|
|
|
$this->newFileFetcher(), |
|
97
|
|
|
$this->settings['egMapsGeoNamesUser'] |
|
98
|
|
|
); |
|
99
|
105 |
|
case 'google': |
|
100
|
|
|
return $this->newGoogleGeocoder(); |
|
101
|
105 |
|
case 'nominatim': |
|
102
|
105 |
|
return new NominatimGeocoder( |
|
103
|
105 |
|
$this->newFileFetcher() |
|
104
|
|
|
); |
|
105
|
|
|
default: |
|
106
|
|
|
return new NullGeocoder(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function newGoogleGeocoder(): Geocoder { |
|
111
|
|
|
return new GoogleGeocoder( |
|
112
|
|
|
$this->newFileFetcher(), |
|
113
|
|
|
$this->settings['egMapsGMaps3ApiKey'], |
|
114
|
|
|
$this->settings['egMapsGMaps3ApiVersion'] |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function getFileFetcher(): FileFetcher { |
|
119
|
2 |
|
return $this->newFileFetcher(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
105 |
|
private function newFileFetcher(): FileFetcher { |
|
123
|
105 |
|
return new MapsFileFetcher(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
public function getGeoJsonFileFetcher(): FileFetcher { |
|
127
|
2 |
|
if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) { |
|
128
|
2 |
|
return $this->getFileFetcher(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return ( new CacheFactory() )->newJeroenSimpleCacheFetcher( |
|
132
|
|
|
$this->getFileFetcher(), |
|
133
|
|
|
$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] ) |
|
134
|
|
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache { |
|
138
|
|
|
return new MediaWikiCache( |
|
139
|
|
|
$this->getMediaWikiCache(), |
|
140
|
|
|
$ttlInSeconds |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
105 |
|
private function getMediaWikiCache(): \BagOStuff { |
|
145
|
105 |
|
return wfGetCache( CACHE_ANYTHING ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
public function getPageContentFetcher(): PageContentFetcher { |
|
149
|
1 |
|
return new PageContentFetcher( |
|
150
|
1 |
|
$this->mediaWikiServices->getTitleParser(), |
|
151
|
1 |
|
$this->mediaWikiServices->getRevisionLookup() |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
19 |
|
public function getCoordinateFormatter(): CoordinateFormatter { |
|
156
|
19 |
|
return new CoordinateFormatter(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
83 |
|
public function getFileUrlFinder(): FileUrlFinder { |
|
160
|
83 |
|
return new MediaWikiFileUrlFinder(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
23 |
|
public function getMappingServices(): MappingServices { |
|
164
|
23 |
|
return new MappingServices( |
|
165
|
23 |
|
$this->settings['egMapsAvailableServices'], |
|
166
|
23 |
|
$this->settings['egMapsDefaultService'], |
|
167
|
23 |
|
new GoogleMapsService(), |
|
168
|
23 |
|
new LeafletService() |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
23 |
|
public function getDisplayMapFunction(): DisplayMapFunction { |
|
173
|
23 |
|
return new DisplayMapFunction( |
|
174
|
23 |
|
$this->getMappingServices() |
|
175
|
|
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
89 |
|
public function getParamDefinitionFactory(): ParamDefinitionFactory { |
|
179
|
89 |
|
$factory = ParamDefinitionFactory::newDefault(); |
|
180
|
|
|
|
|
181
|
89 |
|
$factory->registerType( 'coordinate', [ 'string-parser' => LatLongParser::class ] ); |
|
182
|
89 |
|
$factory->registerType( 'mapslocation', [ 'string-parser' => LocationParser::class ] ); |
|
183
|
89 |
|
$factory->registerType( 'mapsline', [ 'string-parser' => LineParser::class ] ); |
|
184
|
89 |
|
$factory->registerType( 'mapscircle', [ 'string-parser' => CircleParser::class ] ); |
|
185
|
89 |
|
$factory->registerType( 'mapsrectangle', [ 'string-parser' => RectangleParser::class ] ); |
|
186
|
89 |
|
$factory->registerType( 'mapspolygon', [ 'string-parser' => PolygonParser::class ] ); |
|
187
|
89 |
|
$factory->registerType( 'distance', [ 'string-parser' => DistanceParser::class ] ); |
|
188
|
89 |
|
$factory->registerType( 'wmsoverlay', [ 'string-parser' => WmsOverlayParser::class ] ); |
|
189
|
89 |
|
$factory->registerType( 'mapsimageoverlay', [ 'string-parser' => ImageOverlayParser::class ] ); |
|
190
|
|
|
|
|
191
|
89 |
|
return $factory; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
2 |
|
public function newGeoJsonFetcher( FileFetcher $fileFetcher = null ): GeoJsonFetcher { |
|
195
|
2 |
|
return new GeoJsonFetcher( |
|
196
|
2 |
|
$fileFetcher ?? $this->getGeoJsonFileFetcher(), |
|
197
|
2 |
|
$this->mediaWikiServices->getTitleParser(), |
|
198
|
2 |
|
$this->mediaWikiServices->getRevisionLookup() |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
} |
|
203
|
|
|
|