Skip to content

Commit 24faff1

Browse files
committed
Implement reviewer comments
1 parent 9382168 commit 24faff1

File tree

4 files changed

+59
-28
lines changed

4 files changed

+59
-28
lines changed

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

+40-10
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,64 @@ public function search(Query $query, int $offset = 0, int $limit = 20): AssetSea
2121
. '&sroffset=' . $offset
2222
. '&srlimit=' . $limit
2323
;
24-
$response = json_decode(file_get_contents($searchUrl), true);
24+
25+
$jsonResponse = file_get_contents($searchUrl);
26+
if ($jsonResponse === false) {
27+
return new AssetSearchResult(0, new AssetCollection([]));
28+
}
29+
30+
$response = json_decode($jsonResponse, true);
31+
if (!isset($response['query']['search'])) {
32+
return new AssetSearchResult(0, new AssetCollection([]));
33+
}
2534

2635
$assets = [];
2736
foreach ($response['query']['search'] as $result) {
2837
$identifier = str_replace('File:', '', $result['title']);
29-
$assets[] = $this->fetchAsset($identifier);
38+
$asset = $this->fetchAsset($identifier);
39+
if ($asset) {
40+
$assets[] = $asset;
41+
}
3042
}
3143

32-
return new AssetSearchResult((int) $response['query']['searchinfo']['totalhits'], new AssetCollection($assets));
44+
return new AssetSearchResult(
45+
(int) ($response['query']['searchinfo']['totalhits'] ?? 0),
46+
new AssetCollection($assets)
47+
);
3348
}
3449

35-
public function fetchAsset(string $id): Asset
50+
public function fetchAsset(string $id): ?Asset
3651
{
37-
$metadataUrl = 'https://commons.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json'
52+
$metadataUrl = 'https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json'
3853
. '&titles=File%3a' . urlencode($id)
3954
;
40-
$response = json_decode(file_get_contents($metadataUrl), true);
41-
$imageInfo = array_values($response['query']['pages'])[0]['imageinfo'][0]['extmetadata'];
55+
56+
$jsonResponse = file_get_contents($metadataUrl);
57+
if ($jsonResponse === false) {
58+
return null;
59+
}
60+
61+
$response = json_decode($jsonResponse, true);
62+
if (!isset($response['query']['pages'])) {
63+
return null;
64+
}
65+
66+
$pageData = array_values($response['query']['pages'])[0] ?? null;
67+
if (!isset($pageData['imageinfo'][0]['extmetadata'])) {
68+
return null;
69+
}
70+
71+
$imageInfo = $pageData['imageinfo'][0]['extmetadata'];
4272

4373
return new Asset(
4474
new AssetIdentifier($id),
4575
new AssetSource('commons'),
4676
new AssetUri('https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/' . urlencode($id)),
4777
new AssetMetadata([
4878
'page_url' => "https://commons.wikimedia.org/wiki/File:$id",
49-
'author' => array_key_exists('Artist', $imageInfo) ? $imageInfo['Artist']['value'] : null,
50-
'license' => array_key_exists('LicenseShortName', $imageInfo) ? $imageInfo['LicenseShortName']['value'] : null,
51-
'license_url' => array_key_exists('LicenseUrl', $imageInfo) ? $imageInfo['LicenseUrl']['value'] : null,
79+
'author' => $imageInfo['Artist']['value'] ?? null,
80+
'license' => $imageInfo['LicenseShortName']['value'] ?? null,
81+
'license_url' => $imageInfo['LicenseUrl']['value'] ?? null,
5282
])
5383
);
5484
}

code_samples/back_office/images/src/Connector/Dam/Transformation/WikimediaCommonsTransformationFactory.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77

88
class WikimediaCommonsTransformationFactory implements TransformationFactoryInterface
99
{
10-
public function build(?string $transformationName = null, array $transformationParameters = []): Transformation
10+
public function build(?string $transformationName = null, array $transformationParameters = []): ?Transformation
1111
{
12-
return $this->buildAll()[$transformationName];
12+
$transformations = $this->buildAll();
13+
return $transformations[$transformationName] ?? null;
1314
}
1415

15-
public function buildAll(): iterable
16+
public function buildAll(): array
1617
{
1718
return [
18-
'reference' => new Transformation('reference'),
19-
'tiny' => new Transformation('tiny', ['width' => 30]),
20-
'small' => new Transformation('small', ['width' => 100]),
21-
'medium' => new Transformation('medium', ['width' => 200]),
22-
'large' => new Transformation('large', ['width' => 300]),
19+
'reference' => new Transformation('reference', []),
20+
'tiny' => new Transformation('tiny', ['width' => '30']),
21+
'small' => new Transformation('small', ['width' => '100']),
22+
'medium' => new Transformation('medium', ['width' => '200']),
23+
'large' => new Transformation('large', ['width' => '300']),
2324
];
2425
}
2526
}

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
{% block asset_preview %}
44
{{ parent() }}
55
<div>
6-
<a href="{{ asset.assetMetadata.page_url }}">Image</a> by {{ asset.assetMetadata.author }} under
7-
<a href="{{ asset.assetMetadata.license_url }}">{{ asset.assetMetadata.license }}</a>. </div>
6+
<a href="{{ asset.assetMetadata.page_url }}">Image</a>
7+
{% if asset.assetMetadata.author %} by {{ asset.assetMetadata.author }}{% endif %}
8+
{% if asset.assetMetadata.license and asset.assetMetadata.license_url %}
9+
under <a href="{{ asset.assetMetadata.license_url }}">{{ asset.assetMetadata.license }}</a>
10+
{% endif %}.
11+
</div>
812
{% endblock %}

docs/content_management/images/add_image_asset_from_dam.md

+4-8
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Then, in `config\services.yaml`, register the handler as a service:
110110

111111
### Create transformation factory
112112

113-
The transformation factory transforms image variations coming from Wikimedia Commons to the ones used by [[= product_name =]].
113+
The transformation factory maps [[= product_name =]]'s image variations to corresponding variations from Wikimedia Commons.
114114

115115
In `src\Connector\Dam\Transformation` folder, create the `WikimediaCommonsTransformationFactory.php` file that resembles the following example:
116116

@@ -128,7 +128,7 @@ Then register the transformation factory as a service:
128128
### Register variations generator
129129

130130
The variation generator applies map parameters coming from the transformation factory to build a fetch request to the DAM.
131-
The solution uses the built-in `URLBasedVariationGenerator` class, which adds all the map elements as query parameters of the request.
131+
The solution uses the built-in `URLBasedVariationGenerator` class, which adds all the map elements as query parameters to the request.
132132

133133
For example, the handler generates the following URL with `new AssetUri()`:
134134

@@ -154,16 +154,12 @@ In `templates/bundles/WikimediaCommonsConnector/`, add the `commons_asset_view.h
154154
[[= include_file('code_samples/back_office/images/templates/themes/standard/commons_asset_view.html.twig') =]]
155155
```
156156

157-
Then, register the template in configuration files:
157+
Then, register the template and a fallback template in configuration files:
158158

159159
```yaml
160160
[[= include_file('code_samples/back_office/images/config/packages/views.yaml') =]]
161161
```
162162

163-
!!! caution
164-
165-
As it overrides the parameter, make sure that you define the default template, too.
166-
167163
### Add Wikimedia Commons connection to DAM configuration
168164

169165
You can now configure a connection with Wikimedia Commons under the `ibexa.system.<scope>.content.dam` key:
@@ -176,4 +172,4 @@ ibexa:
176172
dam: [ commons ]
177173
```
178174

179-
Once you clear the cache, you can search for images to see whether images from the newly configured DAM are displayed correctly, including their variations.
175+
Once you clear the cache, you can search for images to see whether images from the newly configured DAM are displayed correctly, including their variations.

0 commit comments

Comments
 (0)