Completed
Push — master ( ac7e0a...7880aa )
by Thomas
04:53
created

newWikibaseResourceNodeFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters;
4
5
use Doctrine\Common\Cache\Cache;
6
use Mediawiki\Api\MediawikiApi;
7
use PPP\Wikidata\Cache\JsonLdDataValueFormatterCache;
8
use PPP\Wikidata\Cache\PerSiteLinkCache;
9
use PPP\Wikidata\ValueFormatters\JsonLd\DispatchingJsonLdDataValueFormatter;
10
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityIriParser;
11
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\EntityOntology;
12
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\ExtendedJsonLdEntityFormatter;
13
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdEntityFormatter;
14
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdEntityIdFormatter;
15
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdItemFormatter;
16
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdPropertyFormatter;
17
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\JsonLdSnakFormatter;
18
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDataValueFormatter;
19
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDecimalFormatter;
20
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdGlobeCoordinateFormatter;
21
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdMonolingualTextFormatter;
22
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdQuantityFormatter;
23
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdStringFormatter;
24
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdTimeFormatter;
25
use PPP\Wikidata\ValueFormatters\JsonLd\JsonLdUnknownFormatter;
26
use PPP\Wikidata\Wikipedia\MediawikiArticleProvider;
27
use ValueFormatters\DecimalFormatter;
28
use ValueFormatters\FormatterOptions;
29
use PPP\Wikidata\ValueFormatters\JsonLd\Entity\UnitSymbolFormatter;
30
use ValueFormatters\QuantityFormatter;
31
use ValueFormatters\ValueFormatter;
32
use Wikibase\DataModel\Entity\BasicEntityIdParser;
33
use Wikibase\DataModel\Entity\PropertyId;
34
use Wikibase\EntityStore\EntityStore;
35
36
/**
37
 * Build a parser for Wikibase value
38
 *
39
 * @licence GPLv2+
40
 * @author Thomas Pellissier Tanon
41
 */
42
class WikibaseResourceNodeFormatterFactory {
43
44
	/**
45
	 * @var string language code
46
	 */
47
	private $languageCode;
48
49
	/**
50
	 * @var EntityStore
51
	 */
52
	private $entityStore;
53
54
	/**
55
	 * @var MediawikiApi[]
56
	 */
57
	private $sitesApi;
58
59
	/**
60
	 * @var Cache
61
	 */
62
	private $cache;
63
64
	/**
65
	 * @param $languageCode
66
	 * @param EntityStore $entityStore
67
	 * @param MediawikiApi[] $sitesApi
68
	 * @param Cache $cache
69
	 */
70 8
	public function __construct($languageCode, EntityStore $entityStore, array $sitesApi, Cache $cache) {
71 8
		$this->languageCode = $languageCode;
72 8
		$this->entityStore = $entityStore;
73 8
		$this->sitesApi = $sitesApi;
74 8
		$this->cache = $cache;
75 8
	}
76
77
	/**
78
	 * @return ValueFormatter
79
	 */
80 8
	public function newWikibaseResourceNodeFormatter() {
81 8
		$options = $this->newFormatterOptions();
82
83 8
		$dispatchingFormatter = $this->newExtendedDispatchingJsonLdDataValueFormatter($options);
84 8
		return new JsonLdResourceFormatter(
85 8
			$dispatchingFormatter,
86 8
			$this->newSnakFormatter($dispatchingFormatter, $options),
87
			$options
88 8
		);
89
	}
90
91 8
	private function newFormatterOptions() {
92 8
		return new FormatterOptions(array(
93 8
			ValueFormatter::OPT_LANG => $this->languageCode,
94 8
			JsonLdEntityFormatter::OPT_ENTITY_BASE_URI => 'http://www.wikidata.org/entity/',
95 8
			JsonLdSnakFormatter::OPT_ALLOWED_VOCABULARIES => array('http://schema.org/')
96 8
		));
97
	}
98
99 8
	private function newJsonLdGlobeCoordinateFormatter(FormatterOptions $options) {
100 8
		return new JsonLdGlobeCoordinateFormatter(new \DataValues\Geo\Formatters\GlobeCoordinateFormatter($options), $options);
101
	}
102
103 8
	private function newJsonLdQuantityFormatter(FormatterOptions $options) {
104 8
		return new JsonLdQuantityFormatter(
105 8
			new QuantityFormatter(
106 8
				$options,
107 8
				new DecimalFormatter($options),
108 8
				$this->newUnitSymbolFormatter($options),
109
				'$1 $2'
110 8
			),
111 8
			new JsonLdDecimalFormatter($options),
112
			$options
113 8
		);
114
	}
115
116 8
	private function newSimpleJsonLdEntityIdFormatter(FormatterOptions $options) {
117 8
		$entityFormatter = new JsonLdEntityFormatter($options);
118
119 8
		return new CachedJsonLdDataValueFormatter(
120 8
			new JsonLdEntityIdFormatter(
121 8
				$this->entityStore->getItemLookup(),
122 8
				new JsonLdItemFormatter($entityFormatter, $options),
123 8
				$this->entityStore->getPropertyLookup(),
124 8
				new JsonLdPropertyFormatter($entityFormatter, $options),
125
				$options
126 8
			),
127 8
			new JsonLdDataValueFormatterCache(
128 8
				$this->cache,
129
				'simpleentityid'
130 8
			)
131 8
		);
132
	}
133
134 8
	private function newExtendedJsonLdEntityIdFormatter(FormatterOptions $options) {
135 8
		$entityFormatter = new ExtendedJsonLdEntityFormatter(
136 8
			new JsonLdEntityFormatter($options),
137 8
			$this->newSnakFormatter($this->newSimpleDispatchingJsonLdDataValueFormatter($options), $options),
138
			$options
139 8
		);
140
141 8
		return new CachedJsonLdDataValueFormatter(
142 8
			new JsonLdEntityIdFormatter(
143 8
				$this->entityStore->getItemLookup(),
144 8
				new ExtendedJsonLdItemFormatter(
145 8
					new JsonLdItemFormatter($entityFormatter, $options),
146 8
					$this->newMediawikiArticleProvider(),
147
					$options
148 8
				),
149 8
				$this->entityStore->getPropertyLookup(),
150 8
				new JsonLdPropertyFormatter($entityFormatter, $options),
151
				$options
152 8
			),
153 8
			new JsonLdDataValueFormatterCache(
154 8
				$this->cache,
155
				'extendedentityid'
156 8
			)
157 8
		);
158
	}
159
160 8
	private function newSnakFormatter(JsonLdDataValueFormatter $dataValueFormatter, FormatterOptions $options) {
161 8
		return new JsonLdSnakFormatter(
162 8
			$this->entityStore->getPropertyLookup(),
163 8
			$this->newEntityOntology(),
164 8
			$dataValueFormatter,
165
			$options
166 8
		);
167
	}
168
169 8
	private function newSimpleDispatchingJsonLdDataValueFormatter(FormatterOptions $options) {
170 8
		return new DispatchingJsonLdDataValueFormatter(array(
171 8
			'globecoordinate' => $this->newJsonLdGlobeCoordinateFormatter($options),
172 8
			'monolingualtext' => new JsonLdMonolingualTextFormatter($options),
173 8
			'quantity' => $this->newJsonLdQuantityFormatter($options),
174 8
			'string' => new JsonLdStringFormatter($options),
175 8
			'time' => new JsonLdTimeFormatter(new IsoTimeFormatter($options), $options),
176 8
			'unknown' => new JsonLdUnknownFormatter($options),
177 8
			'wikibase-entityid' => $this->newSimpleJsonLdEntityIdFormatter($options)
178 8
		), $options);
179
	}
180
181 8
	private function newExtendedDispatchingJsonLdDataValueFormatter(FormatterOptions $options) {
182 8
		return new DispatchingJsonLdDataValueFormatter(array(
183 8
			'globecoordinate' => $this->newJsonLdGlobeCoordinateFormatter($options),
184 8
			'monolingualtext' => new JsonLdMonolingualTextFormatter($options),
185 8
			'quantity' => $this->newJsonLdQuantityFormatter($options),
186 8
			'string' => new JsonLdStringFormatter($options),
187 8
			'time' => new JsonLdTimeFormatter(new IsoTimeFormatter($options), $options),
188 8
			'unknown' => new JsonLdUnknownFormatter($options),
189 8
			'wikibase-entityid' => $this->newExtendedJsonLdEntityIdFormatter($options)
190 8
		), $options);
191
	}
192
193 8
	private function newUnitSymbolFormatter(FormatterOptions $options) {
194 8
		return new UnitSymbolFormatter(
195 8
			new EntityIriParser(new BasicEntityIdParser()),
196 8
			$this->newEntityOntology(),
197 8
			$this->entityStore->getItemLookup(),
198
			$options
199 8
		);
200
	}
201
202 8
	private function newEntityOntology() {
203 8
		return new EntityOntology(array(
204 8
			EntityOntology::OWL_EQUIVALENT_PROPERTY => new PropertyId('P1628'),
205 8
			EntityOntology::QUDT_SYMBOL => new PropertyId('P558')
206 8
		));
207
	}
208
209 8
	private function newMediawikiArticleProvider() {
210 8
		return new MediawikiArticleProvider(
211 8
			$this->sitesApi,
212 8
			new PerSiteLinkCache($this->cache, 'wparticle')
213 8
		);
214
	}
215
}
216