|
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
|
|
|
parent::__construct($options); |
|
50
|
|
|
} |
|
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
|
|
|
return $this->toJsonLd($value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
private function toJsonLd(Item $item) { |
|
64
|
|
|
$resource = $this->itemFormatter->format($item); |
|
65
|
|
|
|
|
66
|
|
|
$resource->potentialAction = array( |
|
67
|
2 |
|
$this->newViewAction( |
|
68
|
|
|
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
|
|
|
'//www.wikidata.org/entity/' . $item->getId()->getSerialization() |
|
71
|
|
|
) |
|
72
|
2 |
|
); |
|
73
|
|
|
|
|
74
|
|
|
$this->addSiteLinksContentToResource($item->getSiteLinkList(), $resource); |
|
75
|
|
|
|
|
76
|
2 |
|
return $resource; |
|
77
|
2 |
|
} |
|
78
|
|
|
|
|
79
|
1 |
|
private function addSiteLinksContentToResource(SiteLinkList $siteLinkList, stdClass $resource) { |
|
80
|
|
|
$wikiId = $this->getOption(ValueFormatter::OPT_LANG) . 'wiki'; |
|
81
|
|
|
|
|
82
|
|
|
if(!$this->articleProvider->isWikiIdSupported($wikiId)) { |
|
83
|
1 |
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$article = $this->articleProvider->getArticleForSiteLink($siteLinkList->getBySiteId($wikiId)); |
|
88
|
|
|
$this->addArticleToResource($article, $resource); |
|
89
|
|
|
} catch(OutOfBoundsException $e) { |
|
|
|
|
|
|
90
|
1 |
|
} |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
private function addArticleToResource(MediawikiArticle $article, stdClass $resource) { |
|
94
|
|
|
$articleResource = new stdClass(); |
|
95
|
1 |
|
$articleResource->{'@type'} = 'Article'; |
|
96
|
|
|
$articleResource->{'@id'} = $article->getUrl(); |
|
97
|
|
|
$articleResource->inLanguage = $article->getLanguageCode(); |
|
98
|
|
|
$articleResource->headline = $article->getHeaderText(); |
|
99
|
1 |
|
$articleResource->license = 'http://creativecommons.org/licenses/by-sa/3.0/'; |
|
100
|
|
|
$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
|
|
|
if(!property_exists($resource, '@reverse')) { |
|
106
|
|
|
$resource->{'@reverse'} = new stdClass(); |
|
107
|
|
|
} |
|
108
|
1 |
|
$resource->{'@reverse'}->about = $articleResource; |
|
109
|
|
|
|
|
110
|
1 |
|
$resource->potentialAction[] = $this->newViewAction( |
|
111
|
|
|
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
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$image = $article->getImage(); |
|
117
|
1 |
|
if($image !== null) { |
|
118
|
|
|
$this->addImageToResource($image, $resource); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
private function addImageToResource(MediawikiArticleImage $image, stdClass $resource) { |
|
123
|
|
|
$resource->image = new stdClass(); |
|
124
|
1 |
|
$resource->image->{'@type'} = 'ImageObject'; |
|
125
|
|
|
$resource->image->{'@id'} = 'http://commons.wikimedia.org/wiki/Image:' . str_replace(' ', '_', $image->getTitle()); //TODO configure |
|
126
|
|
|
$resource->image->contentUrl = $image->getUrl(); |
|
127
|
|
|
$resource->image->width = $image->getWidth(); |
|
128
|
|
|
$resource->image->height = $image->getHeight(); |
|
129
|
|
|
$resource->image->name = $image->getTitle(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
private function newViewAction(array $nameTerms, $image, $target) { |
|
133
|
|
|
$actionResource = new stdClass(); |
|
134
|
1 |
|
$actionResource->{'@type'} = 'ViewAction'; |
|
135
|
1 |
|
$actionResource->name = array(); |
|
136
|
|
|
foreach($nameTerms as $term) { |
|
137
|
|
|
$actionResource->name[] = $this->newResourceFromTerm($term); |
|
138
|
|
|
} |
|
139
|
1 |
|
$actionResource->image = $image; |
|
140
|
1 |
|
$actionResource->target = $target; |
|
141
|
1 |
|
return $actionResource; |
|
142
|
1 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function newResourceFromTerm(Term $term) { |
|
145
|
|
|
$resource = new stdClass(); |
|
146
|
|
|
$resource->{'@language'} = $term->getLanguageCode(); |
|
147
|
|
|
$resource->{'@value'} = $term->getText(); |
|
148
|
|
|
return $resource; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|