Completed
Push — SemanticGeoJson ( e5361a )
by Jeroen De
03:37
created

MapsFactory::getMediaWikiCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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