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\GeoJsonStore\GeoJsonStore; |
19
|
|
|
use Maps\DataAccess\GeoJsonStore\SemanticGeoJsonStore; |
20
|
|
|
use Maps\DataAccess\GeoJsonStore\SubObjectBuilder; |
21
|
|
|
use Maps\DataAccess\ImageRepository; |
22
|
|
|
use Maps\DataAccess\MapsFileFetcher; |
23
|
|
|
use Maps\DataAccess\MediaWikiFileUrlFinder; |
24
|
|
|
use Maps\DataAccess\MwImageRepository; |
25
|
|
|
use Maps\DataAccess\PageContentFetcher; |
26
|
|
|
use Maps\MediaWiki\ParserHooks\DisplayMapFunction; |
27
|
|
|
use Maps\Presentation\CoordinateFormatter; |
28
|
|
|
use Maps\Presentation\WikitextParsers\CircleParser; |
29
|
|
|
use Maps\Presentation\WikitextParsers\DistanceParser; |
30
|
|
|
use Maps\Presentation\WikitextParsers\ImageOverlayParser; |
31
|
|
|
use Maps\Presentation\WikitextParsers\LineParser; |
32
|
|
|
use Maps\Presentation\WikitextParsers\LocationParser; |
33
|
|
|
use Maps\Presentation\WikitextParsers\PolygonParser; |
34
|
|
|
use Maps\Presentation\WikitextParsers\RectangleParser; |
35
|
|
|
use Maps\Presentation\WikitextParsers\WmsOverlayParser; |
36
|
|
|
use MediaWiki\MediaWikiServices; |
37
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
38
|
|
|
use RepoGroup; |
39
|
|
|
use SimpleCache\Cache\Cache; |
40
|
|
|
use SimpleCache\Cache\MediaWikiCache; |
41
|
|
|
use SMW\ApplicationFactory; |
42
|
|
|
use Title; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @licence GNU GPL v2+ |
46
|
|
|
* @author Jeroen De Dauw < [email protected] > |
47
|
|
|
*/ |
48
|
|
|
class MapsFactory { |
49
|
|
|
|
50
|
|
|
protected static $globalInstance = null; |
51
|
|
|
|
52
|
|
|
private $settings; |
53
|
|
|
private $mediaWikiServices; |
54
|
|
|
|
55
|
|
|
private $leafletService; |
56
|
|
|
private $googleService; |
57
|
|
|
|
58
|
3 |
|
private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) { |
59
|
3 |
|
$this->settings = $settings; |
60
|
3 |
|
$this->mediaWikiServices = $mediaWikiServices; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Only for legacy code where dependency injection is not possible |
65
|
|
|
*/ |
66
|
129 |
|
public static function globalInstance(): self { |
67
|
129 |
|
if ( self::$globalInstance === null ) { |
68
|
|
|
self::$globalInstance = self::newDefault(); |
69
|
|
|
} |
70
|
|
|
|
71
|
129 |
|
return self::$globalInstance; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
protected static function newDefault(): self { |
75
|
3 |
|
return new static( $GLOBALS, MediaWikiServices::getInstance() ); |
76
|
|
|
} |
77
|
|
|
|
78
|
29 |
|
public function newLocationParser(): LocationParser { |
79
|
29 |
|
return LocationParser::newInstance( |
80
|
29 |
|
$this->getGeocoder(), |
81
|
29 |
|
$this->getFileUrlFinder() |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
110 |
|
public function getGeocoder(): Geocoder { |
86
|
110 |
|
$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() ); |
87
|
|
|
|
88
|
110 |
|
if ( $this->settings['egMapsEnableGeoCache'] ) { |
89
|
110 |
|
return new CachingGeocoder( |
90
|
110 |
|
$geocoder, |
91
|
110 |
|
$this->getMediaWikiCache(), |
92
|
110 |
|
$this->settings['egMapsGeoCacheTtl'] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $geocoder; |
97
|
|
|
} |
98
|
|
|
|
99
|
110 |
|
private function newCoreGeocoder(): Geocoder { |
100
|
110 |
|
switch ( $this->settings['egMapsDefaultGeoService'] ) { |
101
|
110 |
|
case 'geonames': |
102
|
|
|
if ( $this->settings['egMapsGeoNamesUser'] === '' ) { |
103
|
|
|
return $this->newGoogleGeocoder(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return new GeoNamesGeocoder( |
107
|
|
|
$this->newFileFetcher(), |
108
|
|
|
$this->settings['egMapsGeoNamesUser'] |
109
|
|
|
); |
110
|
110 |
|
case 'google': |
111
|
|
|
return $this->newGoogleGeocoder(); |
112
|
110 |
|
case 'nominatim': |
113
|
110 |
|
return new NominatimGeocoder( |
114
|
110 |
|
$this->newFileFetcher() |
115
|
|
|
); |
116
|
|
|
default: |
117
|
|
|
return new NullGeocoder(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function newGoogleGeocoder(): Geocoder { |
122
|
|
|
return new GoogleGeocoder( |
123
|
|
|
$this->newFileFetcher(), |
124
|
|
|
$this->settings['egMapsGMaps3ApiKey'], |
125
|
|
|
$this->settings['egMapsGMaps3ApiVersion'] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
public function getFileFetcher(): FileFetcher { |
130
|
3 |
|
return $this->newFileFetcher(); |
131
|
|
|
} |
132
|
|
|
|
133
|
110 |
|
private function newFileFetcher(): FileFetcher { |
134
|
110 |
|
return new MapsFileFetcher(); |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
public function getGeoJsonFileFetcher(): FileFetcher { |
138
|
3 |
|
if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) { |
139
|
3 |
|
return $this->getFileFetcher(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return ( new CacheFactory() )->newJeroenSimpleCacheFetcher( |
143
|
|
|
$this->getFileFetcher(), |
144
|
|
|
$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] ) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache { |
149
|
|
|
return new MediaWikiCache( |
150
|
|
|
$this->getMediaWikiCache(), |
151
|
|
|
$ttlInSeconds |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
110 |
|
private function getMediaWikiCache(): \BagOStuff { |
156
|
110 |
|
return wfGetCache( CACHE_ANYTHING ); |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
public function getPageContentFetcher(): PageContentFetcher { |
160
|
2 |
|
return new PageContentFetcher( |
161
|
2 |
|
$this->mediaWikiServices->getTitleParser(), |
162
|
2 |
|
$this->mediaWikiServices->getRevisionLookup() |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
20 |
|
public function getCoordinateFormatter(): CoordinateFormatter { |
167
|
20 |
|
return new CoordinateFormatter(); |
168
|
|
|
} |
169
|
|
|
|
170
|
88 |
|
public function getFileUrlFinder(): FileUrlFinder { |
171
|
88 |
|
return new MediaWikiFileUrlFinder(); |
172
|
|
|
} |
173
|
|
|
|
174
|
27 |
|
public function getMappingServices(): MappingServices { |
175
|
27 |
|
return new MappingServices( |
176
|
27 |
|
$this->settings['egMapsAvailableServices'], |
177
|
27 |
|
$this->settings['egMapsDefaultService'], |
178
|
27 |
|
$this->getGoogleMapsService(), |
179
|
27 |
|
$this->getLeafletService() |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
27 |
|
private function getGoogleMapsService(): GoogleMapsService { |
184
|
27 |
|
if ( $this->googleService === null ) { |
185
|
2 |
|
$this->googleService = new GoogleMapsService(); |
186
|
|
|
} |
187
|
|
|
|
188
|
27 |
|
return $this->googleService; |
189
|
|
|
} |
190
|
|
|
|
191
|
27 |
|
private function getLeafletService(): LeafletService { |
192
|
27 |
|
if ( $this->leafletService === null ) { |
193
|
2 |
|
$this->leafletService = new LeafletService( |
194
|
2 |
|
$this->getImageRepository() |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
27 |
|
return $this->leafletService; |
199
|
|
|
} |
200
|
|
|
|
201
|
27 |
|
public function getDisplayMapFunction(): DisplayMapFunction { |
202
|
27 |
|
return new DisplayMapFunction( |
203
|
27 |
|
$this->getMappingServices() |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
93 |
|
public function getParamDefinitionFactory(): ParamDefinitionFactory { |
208
|
93 |
|
$factory = ParamDefinitionFactory::newDefault(); |
209
|
|
|
|
210
|
93 |
|
$factory->registerType( 'coordinate', [ 'string-parser' => LatLongParser::class ] ); |
211
|
93 |
|
$factory->registerType( 'mapslocation', [ 'string-parser' => LocationParser::class ] ); |
212
|
93 |
|
$factory->registerType( 'mapsline', [ 'string-parser' => LineParser::class ] ); |
213
|
93 |
|
$factory->registerType( 'mapscircle', [ 'string-parser' => CircleParser::class ] ); |
214
|
93 |
|
$factory->registerType( 'mapsrectangle', [ 'string-parser' => RectangleParser::class ] ); |
215
|
93 |
|
$factory->registerType( 'mapspolygon', [ 'string-parser' => PolygonParser::class ] ); |
216
|
93 |
|
$factory->registerType( 'distance', [ 'string-parser' => DistanceParser::class ] ); |
217
|
93 |
|
$factory->registerType( 'wmsoverlay', [ 'string-parser' => WmsOverlayParser::class ] ); |
218
|
93 |
|
$factory->registerType( 'mapsimageoverlay', [ 'string-parser' => ImageOverlayParser::class ] ); |
219
|
|
|
|
220
|
93 |
|
return $factory; |
221
|
|
|
} |
222
|
|
|
|
223
|
3 |
|
public function newGeoJsonFetcher( FileFetcher $fileFetcher = null ): GeoJsonFetcher { |
224
|
3 |
|
return new GeoJsonFetcher( |
225
|
3 |
|
$fileFetcher ?? $this->getGeoJsonFileFetcher(), |
226
|
3 |
|
$this->mediaWikiServices->getTitleParser(), |
227
|
3 |
|
$this->mediaWikiServices->getRevisionLookup() |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
3 |
|
public function smwIntegrationIsEnabled(): bool { |
232
|
3 |
|
return !$this->settings['egMapsDisableSmwIntegration'] && defined( 'SMW_VERSION' ); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function newSemanticMapsSetup( &$mwGlobals ): SemanticMapsSetup { |
236
|
|
|
return SemanticMapsSetup::newFromMediaWikiGlobals( $mwGlobals, $this->getMappingServices() ); |
237
|
|
|
} |
238
|
|
|
|
239
|
3 |
|
public function newSemanticGeoJsonStore( \ParserOutput $parserOutput, Title $subjectPage ): GeoJsonStore { |
240
|
3 |
|
return new SemanticGeoJsonStore( |
241
|
3 |
|
$this->getSmwFactory()->newParserData( $subjectPage, $parserOutput ), |
242
|
|
|
$subjectPage, |
243
|
3 |
|
$this->getSmwFactory()->getEventDispatcher(), |
244
|
3 |
|
new SubObjectBuilder( $subjectPage ) |
245
|
|
|
); |
246
|
|
|
} |
247
|
|
|
|
248
|
3 |
|
private function getSmwFactory(): ApplicationFactory { |
249
|
3 |
|
return ApplicationFactory::getInstance(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getImageRepository(): ImageRepository { |
253
|
|
|
return new MwImageRepository( |
254
|
|
|
$this->getRepoGroup() |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
private function getRepoGroup(): RepoGroup { |
259
|
|
|
return RepoGroup::singleton(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|