1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\ValueFormatters; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
use PPP\Wikidata\Wikipedia\MediawikiArticle; |
8
|
|
|
use PPP\Wikidata\Wikipedia\MediawikiArticleImage; |
9
|
|
|
use PPP\Wikidata\Wikipedia\MediawikiArticleProvider; |
10
|
|
|
use stdClass; |
11
|
|
|
use ValueFormatters\FormatterOptions; |
12
|
|
|
use ValueFormatters\ValueFormatter; |
13
|
|
|
use ValueFormatters\ValueFormatterBase; |
14
|
|
|
use Wikibase\DataModel\Entity\Item; |
15
|
|
|
use Wikibase\DataModel\SiteLinkList; |
16
|
|
|
use Wikibase\DataModel\Term\Term; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns the label of a given Wikibase entity id |
20
|
|
|
* |
21
|
|
|
* @licence GPLv2+ |
22
|
|
|
* @author Thomas Pellissier Tanon |
23
|
|
|
*/ |
24
|
|
|
class ExtendedJsonLdItemFormatter extends ValueFormatterBase { |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ValueFormatter |
28
|
|
|
*/ |
29
|
|
|
private $itemFormatter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var MediawikiArticleProvider |
33
|
|
|
*/ |
34
|
|
|
private $articleProvider; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ValueFormatter $itemFormatter |
38
|
|
|
* @param MediawikiArticleProvider $articleProvider |
39
|
|
|
* @param FormatterOptions $options |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct( |
42
|
|
|
ValueFormatter $itemFormatter, |
43
|
|
|
MediawikiArticleProvider $articleProvider, |
44
|
|
|
FormatterOptions $options |
45
|
|
|
) { |
46
|
2 |
|
$this->itemFormatter = $itemFormatter; |
47
|
2 |
|
$this->articleProvider = $articleProvider; |
48
|
|
|
|
49
|
2 |
|
parent::__construct($options); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @see ValueFormatter::format |
54
|
|
|
*/ |
55
|
2 |
|
public function format($value) { |
56
|
2 |
|
if(!($value instanceof Item)) { |
57
|
|
|
throw new InvalidArgumentException('$value is not an Item'); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
return $this->toJsonLd($value); |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
private function toJsonLd(Item $item) { |
64
|
2 |
|
$resource = $this->itemFormatter->format($item); |
65
|
|
|
|
66
|
2 |
|
$resource->potentialAction = array( |
67
|
2 |
|
$this->newViewAction( |
68
|
2 |
|
array(new Term('en', 'View on Wikidata'), new Term('fr', 'Voir sur Wikidata')), |
69
|
2 |
|
'//upload.wikimedia.org/wikipedia/commons/f/ff/Wikidata-logo.svg', |
70
|
2 |
|
'//www.wikidata.org/entity/' . $item->getId()->getSerialization() |
71
|
2 |
|
) |
72
|
2 |
|
); |
73
|
|
|
|
74
|
2 |
|
$this->addSiteLinksContentToResource($item->getSiteLinkList(), $resource); |
75
|
|
|
|
76
|
2 |
|
return $resource; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
private function addSiteLinksContentToResource(SiteLinkList $siteLinkList, stdClass $resource) { |
80
|
2 |
|
$wikiId = $this->getOption(ValueFormatter::OPT_LANG) . 'wiki'; |
81
|
|
|
|
82
|
2 |
|
if(!$this->articleProvider->isWikiIdSupported($wikiId)) { |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
try { |
87
|
1 |
|
$article = $this->articleProvider->getArticleForSiteLink($siteLinkList->getBySiteId($wikiId)); |
88
|
1 |
|
$this->addArticleToResource($article, $resource); |
89
|
1 |
|
} catch(OutOfBoundsException $e) { |
|
|
|
|
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
|
93
|
1 |
|
private function addArticleToResource(MediawikiArticle $article, stdClass $resource) { |
94
|
1 |
|
$articleResource = new stdClass(); |
95
|
1 |
|
$articleResource->{'@type'} = 'Article'; |
96
|
1 |
|
$articleResource->{'@id'} = $article->getUrl(); |
97
|
1 |
|
$articleResource->inLanguage = $article->getLanguageCode(); |
98
|
1 |
|
$articleResource->headline = $article->getHeaderText(); |
99
|
1 |
|
$articleResource->license = 'http://creativecommons.org/licenses/by-sa/3.0/'; |
100
|
1 |
|
$articleResource->author = new stdClass(); |
101
|
1 |
|
$articleResource->author->{'@type'} = 'Organization'; |
102
|
1 |
|
$articleResource->author->{'@id'} = 'http://www.wikidata.org/entity/Q52'; |
103
|
1 |
|
$articleResource->author->name = 'Wikipedia'; |
104
|
|
|
|
105
|
1 |
|
if(!property_exists($resource, '@reverse')) { |
106
|
1 |
|
$resource->{'@reverse'} = new stdClass(); |
107
|
1 |
|
} |
108
|
1 |
|
$resource->{'@reverse'}->about = $articleResource; |
109
|
|
|
|
110
|
1 |
|
$resource->potentialAction[] = $this->newViewAction( |
111
|
1 |
|
array(new Term('en', 'View on Wikipedia'), new Term('fr', 'Voir sur Wikipédia')), |
112
|
1 |
|
'//upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/64px-Wikipedia-logo-v2.svg.png', |
113
|
1 |
|
$article->getUrl() |
114
|
1 |
|
); |
115
|
|
|
|
116
|
1 |
|
$image = $article->getImage(); |
117
|
1 |
|
if($image !== null) { |
118
|
1 |
|
$this->addImageToResource($image, $resource); |
119
|
1 |
|
} |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
private function addImageToResource(MediawikiArticleImage $image, stdClass $resource) { |
123
|
1 |
|
$resource->image = new stdClass(); |
124
|
1 |
|
$resource->image->{'@type'} = 'ImageObject'; |
125
|
1 |
|
$resource->image->{'@id'} = 'http://commons.wikimedia.org/wiki/Image:' . str_replace(' ', '_', $image->getTitle()); //TODO configure |
126
|
1 |
|
$resource->image->contentUrl = $image->getUrl(); |
127
|
1 |
|
$resource->image->width = $image->getWidth(); |
128
|
1 |
|
$resource->image->height = $image->getHeight(); |
129
|
1 |
|
$resource->image->name = $image->getTitle(); |
130
|
1 |
|
} |
131
|
|
|
|
132
|
2 |
|
private function newViewAction(array $nameTerms, $image, $target) { |
133
|
2 |
|
$actionResource = new stdClass(); |
134
|
2 |
|
$actionResource->{'@type'} = 'ViewAction'; |
135
|
2 |
|
$actionResource->name = array(); |
136
|
2 |
|
foreach($nameTerms as $term) { |
137
|
2 |
|
$actionResource->name[] = $this->newResourceFromTerm($term); |
138
|
2 |
|
} |
139
|
2 |
|
$actionResource->image = $image; |
140
|
2 |
|
$actionResource->target = $target; |
141
|
2 |
|
return $actionResource; |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
private function newResourceFromTerm(Term $term) { |
145
|
2 |
|
$resource = new stdClass(); |
146
|
2 |
|
$resource->{'@language'} = $term->getLanguageCode(); |
147
|
2 |
|
$resource->{'@value'} = $term->getText(); |
148
|
2 |
|
return $resource; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|