Completed
Push — master ( 38aa89...4dbba6 )
by Jeroen De
26s queued 10s
created

MapsFactory::getPageContentFetcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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