1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
4
|
|
|
|
5
|
|
|
namespace Maps; |
6
|
|
|
|
7
|
|
|
use FileFetcher\CachingFileFetcher; |
8
|
|
|
use FileFetcher\FileFetcher; |
9
|
|
|
use Jeroen\SimpleGeocoder\Geocoder; |
10
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder; |
11
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder; |
12
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder; |
13
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder; |
14
|
|
|
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder; |
15
|
|
|
use Maps\DataAccess\CachingGeocoder; |
16
|
|
|
use Maps\DataAccess\MapsFileFetcher; |
17
|
|
|
use Maps\DataAccess\MediaWikiFileUrlFinder; |
18
|
|
|
use Maps\DataAccess\PageContentFetcher; |
19
|
|
|
use Maps\Presentation\CoordinateFormatter; |
20
|
|
|
use Maps\Presentation\WikitextParsers\LocationParser; |
21
|
|
|
use MediaWiki\MediaWikiServices; |
22
|
|
|
use SimpleCache\Cache\Cache; |
23
|
|
|
use SimpleCache\Cache\MediaWikiCache; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @licence GNU GPL v2+ |
27
|
|
|
* @author Jeroen De Dauw < [email protected] > |
28
|
|
|
*/ |
29
|
|
|
class MapsFactory { |
30
|
|
|
|
31
|
|
|
private $settings; |
32
|
|
|
private $mediaWikiServices; |
33
|
|
|
|
34
|
49 |
|
private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) { |
35
|
49 |
|
$this->settings = $settings; |
36
|
49 |
|
$this->mediaWikiServices = $mediaWikiServices; |
37
|
49 |
|
} |
38
|
|
|
|
39
|
49 |
|
public static function newDefault(): self { |
40
|
49 |
|
return new self( $GLOBALS, MediaWikiServices::getInstance() ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Only for legacy code where dependency injection is not possible |
45
|
|
|
*/ |
46
|
75 |
|
public static function globalInstance(): self { |
47
|
75 |
|
static $instance = null; |
48
|
|
|
|
49
|
75 |
|
if ( $instance === null ) { |
50
|
|
|
$instance = self::newDefault(); |
51
|
|
|
} |
52
|
|
|
|
53
|
75 |
|
return $instance; |
54
|
|
|
} |
55
|
|
|
|
56
|
20 |
|
public function newLocationParser(): LocationParser { |
57
|
20 |
|
return LocationParser::newInstance( |
58
|
20 |
|
$this->getGeocoder(), |
59
|
20 |
|
$this->getFileUrlFinder() |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
101 |
|
public function getGeocoder(): Geocoder { |
64
|
101 |
|
$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() ); |
65
|
|
|
|
66
|
101 |
|
if ( $this->settings['egMapsEnableGeoCache'] ) { |
67
|
101 |
|
return new CachingGeocoder( |
68
|
101 |
|
$geocoder, |
69
|
101 |
|
$this->getMediaWikiCache(), |
70
|
101 |
|
$this->settings['egMapsGeoCacheTtl'] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $geocoder; |
75
|
|
|
} |
76
|
|
|
|
77
|
101 |
|
private function newCoreGeocoder(): Geocoder { |
78
|
101 |
|
switch ( $this->settings['egMapsDefaultGeoService'] ) { |
79
|
101 |
|
case 'geonames': |
80
|
|
|
if ( $this->settings['egMapsGeoNamesUser'] === '' ) { |
81
|
|
|
return $this->newGoogleGeocoder(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new GeoNamesGeocoder( |
85
|
|
|
$this->newFileFetcher(), |
86
|
|
|
$this->settings['egMapsGeoNamesUser'] |
87
|
|
|
); |
88
|
101 |
|
case 'google': |
89
|
|
|
return $this->newGoogleGeocoder(); |
90
|
101 |
|
case 'nominatim': |
91
|
101 |
|
return new NominatimGeocoder( |
92
|
101 |
|
$this->newFileFetcher() |
93
|
|
|
); |
94
|
|
|
default: |
95
|
|
|
return new NullGeocoder(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function newGoogleGeocoder(): Geocoder { |
100
|
|
|
return new GoogleGeocoder( |
101
|
|
|
$this->newFileFetcher(), |
102
|
|
|
$this->settings['egMapsGMaps3ApiKey'], |
103
|
|
|
$this->settings['egMapsGMaps3ApiVersion'] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getFileFetcher(): FileFetcher { |
108
|
|
|
return $this->newFileFetcher(); |
109
|
|
|
} |
110
|
|
|
|
111
|
101 |
|
private function newFileFetcher(): FileFetcher { |
112
|
101 |
|
return new MapsFileFetcher(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getGeoJsonFileFetcher(): FileFetcher { |
116
|
|
|
return new CachingFileFetcher( |
117
|
|
|
$this->getFileFetcher(), |
118
|
|
|
$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] ) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache { |
123
|
|
|
return new MediaWikiCache( |
124
|
|
|
$this->getMediaWikiCache(), |
125
|
|
|
$ttlInSeconds |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
101 |
|
private function getMediaWikiCache(): \BagOStuff { |
130
|
101 |
|
return wfGetCache( CACHE_ANYTHING ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getPageContentFetcher(): PageContentFetcher { |
134
|
|
|
return new PageContentFetcher( |
135
|
|
|
$this->mediaWikiServices->getTitleParser(), |
136
|
|
|
$this->mediaWikiServices->getRevisionLookup() |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
17 |
|
public function getCoordinateFormatter(): CoordinateFormatter { |
141
|
17 |
|
return new CoordinateFormatter(); |
142
|
|
|
} |
143
|
|
|
|
144
|
79 |
|
public function getFileUrlFinder(): FileUrlFinder { |
145
|
79 |
|
return new MediaWikiFileUrlFinder(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|