summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/src/MapsFactory.php
blob: 6ab23d5d909000c938cd674b3ea51d1ff1b01122 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

declare( strict_types = 1 );

namespace Maps;

use FileFetcher\Cache\Factory as CacheFactory;
use FileFetcher\FileFetcher;
use Jeroen\SimpleGeocoder\Geocoder;
use Jeroen\SimpleGeocoder\Geocoders\Decorators\CoordinateFriendlyGeocoder;
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GeoNamesGeocoder;
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\GoogleGeocoder;
use Jeroen\SimpleGeocoder\Geocoders\FileFetchers\NominatimGeocoder;
use Jeroen\SimpleGeocoder\Geocoders\NullGeocoder;
use Maps\DataAccess\CachingGeocoder;
use Maps\DataAccess\MapsFileFetcher;
use Maps\DataAccess\MediaWikiFileUrlFinder;
use Maps\DataAccess\PageContentFetcher;
use Maps\MediaWiki\ParserHooks\DisplayMapFunction;
use Maps\Presentation\CoordinateFormatter;
use Maps\Presentation\WikitextParsers\LocationParser;
use MediaWiki\MediaWikiServices;
use SimpleCache\Cache\Cache;
use SimpleCache\Cache\MediaWikiCache;

/**
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class MapsFactory {

	private $settings;
	private $mediaWikiServices;

	private function __construct( array $settings, MediaWikiServices $mediaWikiServices ) {
		$this->settings = $settings;
		$this->mediaWikiServices = $mediaWikiServices;
	}

	public static function newDefault(): self {
		return new self( $GLOBALS, MediaWikiServices::getInstance() );
	}

	/**
	 * Only for legacy code where dependency injection is not possible
	 */
	public static function globalInstance(): self {
		static $instance = null;

		if ( $instance === null ) {
			$instance = self::newDefault();
		}

		return $instance;
	}

	public function newLocationParser(): LocationParser {
		return LocationParser::newInstance(
			$this->getGeocoder(),
			$this->getFileUrlFinder()
		);
	}

	public function getGeocoder(): Geocoder {
		$geocoder = new CoordinateFriendlyGeocoder( $this->newCoreGeocoder() );

		if ( $this->settings['egMapsEnableGeoCache'] ) {
			return new CachingGeocoder(
				$geocoder,
				$this->getMediaWikiCache(),
				$this->settings['egMapsGeoCacheTtl']
			);
		}

		return $geocoder;
	}

	private function newCoreGeocoder(): Geocoder {
		switch ( $this->settings['egMapsDefaultGeoService'] ) {
			case 'geonames':
				if ( $this->settings['egMapsGeoNamesUser'] === '' ) {
					return $this->newGoogleGeocoder();
				}

				return new GeoNamesGeocoder(
					$this->newFileFetcher(),
					$this->settings['egMapsGeoNamesUser']
				);
			case 'google':
				return $this->newGoogleGeocoder();
			case 'nominatim':
				return new NominatimGeocoder(
					$this->newFileFetcher()
				);
			default:
				return new NullGeocoder();
		}
	}

	private function newGoogleGeocoder(): Geocoder {
		return new GoogleGeocoder(
			$this->newFileFetcher(),
			$this->settings['egMapsGMaps3ApiKey'],
			$this->settings['egMapsGMaps3ApiVersion']
		);
	}

	public function getFileFetcher(): FileFetcher {
		return $this->newFileFetcher();
	}

	private function newFileFetcher(): FileFetcher {
		return new MapsFileFetcher();
	}

	public function getGeoJsonFileFetcher(): FileFetcher {
		if ( $this->settings['egMapsGeoJsonCacheTtl'] === 0 ) {
			return $this->getFileFetcher();
		}

		return ( new CacheFactory() )->newJeroenSimpleCacheFetcher(
			$this->getFileFetcher(),
			$this->getMediaWikiSimpleCache( $this->settings['egMapsGeoJsonCacheTtl'] )
		);
	}

	private function getMediaWikiSimpleCache( int $ttlInSeconds ): Cache {
		return new MediaWikiCache(
			$this->getMediaWikiCache(),
			$ttlInSeconds
		);
	}

	private function getMediaWikiCache(): \BagOStuff {
		return wfGetCache( CACHE_ANYTHING );
	}

	public function getPageContentFetcher(): PageContentFetcher {
		return new PageContentFetcher(
			$this->mediaWikiServices->getTitleParser(),
			$this->mediaWikiServices->getRevisionLookup()
		);
	}

	public function getCoordinateFormatter(): CoordinateFormatter {
		return new CoordinateFormatter();
	}

	public function getFileUrlFinder(): FileUrlFinder {
		return new MediaWikiFileUrlFinder();
	}

	public function getMappingServices(): MappingServices {
		return new MappingServices(
			$this->settings['egMapsAvailableServices'],
			$this->settings['egMapsDefaultService'],
			new GoogleMapsService(),
			new LeafletService()
		);
	}

	public function getDisplayMapFunction(): DisplayMapFunction {
		return new DisplayMapFunction(
			$this->getMappingServices()
		);
	}

}