|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace App\Connector\Dam\Handler; |
| 4 | + |
| 5 | +use Ibexa\Contracts\Connector\Dam\Asset; |
| 6 | +use Ibexa\Contracts\Connector\Dam\AssetCollection; |
| 7 | +use Ibexa\Contracts\Connector\Dam\AssetIdentifier; |
| 8 | +use Ibexa\Contracts\Connector\Dam\AssetMetadata; |
| 9 | +use Ibexa\Contracts\Connector\Dam\AssetSource; |
| 10 | +use Ibexa\Contracts\Connector\Dam\AssetUri; |
| 11 | +use Ibexa\Contracts\Connector\Dam\Handler\Handler as HandlerInterface; |
| 12 | +use Ibexa\Contracts\Connector\Dam\Search\AssetSearchResult; |
| 13 | +use Ibexa\Contracts\Connector\Dam\Search\Query; |
| 14 | + |
| 15 | +class WikimediaCommonsHandler implements HandlerInterface |
| 16 | +{ |
| 17 | + public function search(Query $query, int $offset = 0, int $limit = 20): AssetSearchResult |
| 18 | + { |
| 19 | + $searchUrl = 'https://commons.wikimedia.org/w/api.php?action=query&list=search&format=json&srnamespace=6' |
| 20 | + . '&srsearch=' . urlencode($query->getPhrase()) |
| 21 | + . '&sroffset=' . $offset |
| 22 | + . '&srlimit=' . $limit |
| 23 | + ; |
| 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 | + } |
| 34 | + |
| 35 | + $assets = []; |
| 36 | + foreach ($response['query']['search'] as $result) { |
| 37 | + $identifier = str_replace('File:', '', $result['title']); |
| 38 | + $assets[] = $this->fetchAsset($identifier); |
| 39 | + } |
| 40 | + |
| 41 | + return new AssetSearchResult( |
| 42 | + (int) ($response['query']['searchinfo']['totalhits'] ?? 0), |
| 43 | + new AssetCollection($assets) |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + public function fetchAsset(string $id): Asset |
| 48 | + { |
| 49 | + $metadataUrl = 'https://commons.wikimedia.org/w/api.php?action=query&prop=imageinfo&iiprop=extmetadata&format=json' |
| 50 | + . '&titles=File%3a' . urlencode($id) |
| 51 | + ; |
| 52 | + |
| 53 | + $jsonResponse = file_get_contents($metadataUrl); |
| 54 | + if ($jsonResponse === false) { |
| 55 | + throw new \RuntimeException('Couldn\'t retrieve asset metadata'); |
| 56 | + } |
| 57 | + |
| 58 | + $response = json_decode($jsonResponse, true); |
| 59 | + if (!isset($response['query']['pages'])) { |
| 60 | + throw new \RuntimeException('Couldn\'t parse asset metadata'); |
| 61 | + } |
| 62 | + |
| 63 | + $pageData = array_values($response['query']['pages'])[0] ?? null; |
| 64 | + if (!isset($pageData['imageinfo'][0]['extmetadata'])) { |
| 65 | + throw new \RuntimeException('Couldn\'t parse image asset metadata'); |
| 66 | + } |
| 67 | + |
| 68 | + $imageInfo = $pageData['imageinfo'][0]['extmetadata']; |
| 69 | + |
| 70 | + return new Asset( |
| 71 | + new AssetIdentifier($id), |
| 72 | + new AssetSource('commons'), |
| 73 | + new AssetUri('https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/' . urlencode($id)), |
| 74 | + new AssetMetadata([ |
| 75 | + 'page_url' => "https://commons.wikimedia.org/wiki/File:$id", |
| 76 | + 'author' => $imageInfo['Artist']['value'] ?? null, |
| 77 | + 'license' => $imageInfo['LicenseShortName']['value'] ?? null, |
| 78 | + 'license_url' => $imageInfo['LicenseUrl']['value'] ?? null, |
| 79 | + ]) |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
0 commit comments