Skip to content

Commit 7082cdb

Browse files
authored
Updated Commons DAM example (#3028)
* Added user-agent header in requests to Commons DAM * Fixed author line in view template for Commons DAM * PHP & JS CS Fixes --------- Co-authored-by: vidarl <vidarl@users.noreply.github.com>
1 parent 069d1ff commit 7082cdb

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

code_samples/back_office/images/src/Connector/Dam/Handler/WikimediaCommonsHandler.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class WikimediaCommonsHandler implements HandlerInterface
1616
{
17+
private const USER_AGENT = 'Ibexa DXP Commons Dam Connector';
18+
1719
public function search(Query $query, int $offset = 0, int $limit = 20): AssetSearchResult
1820
{
1921
$searchUrl = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srnamespace=6'
@@ -22,7 +24,16 @@ public function search(Query $query, int $offset = 0, int $limit = 20): AssetSea
2224
. '&srlimit=' . $limit
2325
;
2426

25-
$jsonResponse = file_get_contents($searchUrl);
27+
$opts = [
28+
'http' => [
29+
'method' => 'GET',
30+
'header' => [
31+
'User-Agent: ' . self::USER_AGENT,
32+
],
33+
],
34+
];
35+
36+
$jsonResponse = file_get_contents($searchUrl, false, stream_context_create($opts));
2637
if ($jsonResponse === false) {
2738
return new AssetSearchResult(0, new AssetCollection([]));
2839
}
@@ -50,7 +61,16 @@ public function fetchAsset(string $id): Asset
5061
. '&titles=File%3a' . urlencode($id)
5162
;
5263

53-
$jsonResponse = file_get_contents($metadataUrl);
64+
$opts = [
65+
'http' => [
66+
'method' => 'GET',
67+
'header' => [
68+
'User-Agent: ' . self::USER_AGENT,
69+
],
70+
],
71+
];
72+
73+
$jsonResponse = file_get_contents($metadataUrl, false, stream_context_create($opts));
5474
if ($jsonResponse === false) {
5575
throw new \RuntimeException('Couldn\'t retrieve asset metadata');
5676
}

code_samples/back_office/images/templates/themes/standard/commons_asset_view.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{{ parent() }}
55
<div>
66
<a href="{{ asset.assetMetadata.page_url }}">Image</a>
7-
{% if asset.assetMetadata.author %} by {{ asset.assetMetadata.author }}{% endif %}
7+
{% if asset.assetMetadata.author %} by {{ asset.assetMetadata.author|striptags }}{% endif %}
88
{% if asset.assetMetadata.license and asset.assetMetadata.license_url %}
99
under <a href="{{ asset.assetMetadata.license_url }}">{{ asset.assetMetadata.license }}</a>
1010
{% endif %}.

0 commit comments

Comments
 (0)