Completed
Push — master ( 7880aa...3f359c )
by Thomas
05:14
created

newEntityOntology()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
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
	}
76
77
	/**
78
	 * @return ValueFormatter
79
	 */
80 8
	public function newWikibaseResourceNodeFormatter() {
81
		$options = $this->newFormatterOptions();
82
83
		$dispatchingFormatter = $this->newExtendedDispatchingJsonLdDataValueFormatter($options);
84
		return new JsonLdResourceFormatter(
85
			$dispatchingFormatter,
86 8
			$this->newSnakFormatter($dispatchingFormatter, $options),
87
			$options
88
		);
89
	}
90
91 8
	private function newFormatterOptions() {
92
		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
		));
97
	}
98
99
	private function newJsonLdGlobeCoordinateFormatter(FormatterOptions $options) {
100
		return new JsonLdGlobeCoordinateFormatter(new \DataValues\Geo\Formatters\GlobeCoordinateFormatter($options), $options);
101
	}
102
103
	private function newJsonLdQuantityFormatter(FormatterOptions $options) {
104
		return new JsonLdQuantityFormatter(
105
			new QuantityFormatter(
106
				$options,
107
				new DecimalFormatter($options),
108
				$this->newUnitSymbolFormatter($options),
109
				'$1 $2'
110
			),
111
			new JsonLdDecimalFormatter($options),
112
			$options
113
		);
114
	}
115
116 8
	private function newSimpleJsonLdEntityIdFormatter(FormatterOptions $options) {
117
		$entityFormatter = new JsonLdEntityFormatter($options);
118
119
		return new CachedJsonLdDataValueFormatter(
120
			new JsonLdEntityIdFormatter(
121 8
				$this->entityStore->getItemLookup(),
122
				new JsonLdItemFormatter($entityFormatter, $options),
123 8
				$this->entityStore->getPropertyLookup(),
124
				new JsonLdPropertyFormatter($entityFormatter, $options),
125
				$options
126
			),
127
			new JsonLdDataValueFormatterCache(
128 8
				$this->cache,
129 8
				'simpleentityid'
130
			)
131
		);
132 8
	}
133
134 8
	private function newExtendedJsonLdEntityIdFormatter(FormatterOptions $options) {
135
		$entityFormatter = new ExtendedJsonLdEntityFormatter(
136
			new JsonLdEntityFormatter($options),
137
			$this->newSnakFormatter($this->newSimpleDispatchingJsonLdDataValueFormatter($options), $options),
138
			$options
139
		);
140
141
		return new CachedJsonLdDataValueFormatter(
142
			new JsonLdEntityIdFormatter(
143 8
				$this->entityStore->getItemLookup(),
144
				new ExtendedJsonLdItemFormatter(
145
					new JsonLdItemFormatter($entityFormatter, $options),
146 8
					$this->newMediawikiArticleProvider(),
147
					$options
148
				),
149 8
				$this->entityStore->getPropertyLookup(),
150
				new JsonLdPropertyFormatter($entityFormatter, $options),
151
				$options
152
			),
153
			new JsonLdDataValueFormatterCache(
154 8
				$this->cache,
155 8
				'extendedentityid'
156
			)
157
		);
158 8
	}
159
160
	private function newSnakFormatter(JsonLdDataValueFormatter $dataValueFormatter, FormatterOptions $options) {
161
		return new JsonLdSnakFormatter(
162
			$this->entityStore->getPropertyLookup(),
163
			$this->newEntityOntology(),
164
			$dataValueFormatter,
165
			$options
166
		);
167
	}
168
169 8
	private function newSimpleDispatchingJsonLdDataValueFormatter(FormatterOptions $options) {
170
		return new DispatchingJsonLdDataValueFormatter(array(
171 8
			'globecoordinate' => $this->newJsonLdGlobeCoordinateFormatter($options),
172
			'monolingualtext' => new JsonLdMonolingualTextFormatter($options),
173 8
			'quantity' => $this->newJsonLdQuantityFormatter($options),
174
			'string' => new JsonLdStringFormatter($options),
175
			'time' => new JsonLdTimeFormatter(new IsoTimeFormatter($options), $options),
176
			'unknown' => new JsonLdUnknownFormatter($options),
177 8
			'wikibase-entityid' => $this->newSimpleJsonLdEntityIdFormatter($options)
178
		), $options);
179 8
	}
180
181 8
	private function newExtendedDispatchingJsonLdDataValueFormatter(FormatterOptions $options) {
182
		return new DispatchingJsonLdDataValueFormatter(array(
183 8
			'globecoordinate' => $this->newJsonLdGlobeCoordinateFormatter($options),
184
			'monolingualtext' => new JsonLdMonolingualTextFormatter($options),
185 8
			'quantity' => $this->newJsonLdQuantityFormatter($options),
186
			'string' => new JsonLdStringFormatter($options),
187
			'time' => new JsonLdTimeFormatter(new IsoTimeFormatter($options), $options),
188
			'unknown' => new JsonLdUnknownFormatter($options),
189 8
			'wikibase-entityid' => $this->newExtendedJsonLdEntityIdFormatter($options)
190
		), $options);
191 8
	}
192
193
	private function newUnitSymbolFormatter(FormatterOptions $options) {
194
		return new UnitSymbolFormatter(
195
			new EntityIriParser(new BasicEntityIdParser()),
196
			$this->newEntityOntology(),
197
			$this->entityStore->getItemLookup(),
198
			$options
199
		);
200
	}
201
202
	private function newEntityOntology() {
203
		return new EntityOntology(array(
204
			EntityOntology::OWL_EQUIVALENT_PROPERTY => new PropertyId('P1628'),
205
			EntityOntology::QUDT_SYMBOL => new PropertyId('P558')
206
		));
207
	}
208
209 8
	private function newMediawikiArticleProvider() {
210
		return new MediawikiArticleProvider(
211 8
			$this->sitesApi,
212
			new PerSiteLinkCache($this->cache, 'wparticle')
213
		);
214
	}
215
}
216