From 0e2bc9a995502cecb7c86544e3c3e8de37991f2b Mon Sep 17 00:00:00 2001 From: Daniel Lienert Date: Sat, 1 Feb 2020 23:31:59 +0100 Subject: [PATCH] FEATURE: Add a copyright notice template to generate the copyright notice --- Classes/AssetSource/MediaWikiAssetProxy.php | 59 ++++++++++++++++++-- Classes/AssetSource/MediaWikiAssetSource.php | 14 +++++ Configuration/Settings.yaml | 1 + 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/Classes/AssetSource/MediaWikiAssetProxy.php b/Classes/AssetSource/MediaWikiAssetProxy.php index a033690..30d1748 100644 --- a/Classes/AssetSource/MediaWikiAssetProxy.php +++ b/Classes/AssetSource/MediaWikiAssetProxy.php @@ -10,6 +10,8 @@ * source code. */ +use Neos\Eel\EelEvaluatorInterface; +use Neos\Eel\Utility; use Neos\Flow\Annotations as Flow; use Neos\Http\Factories\UriFactory; use Neos\Media\Domain\Model\AssetSource\AssetProxy\AssetProxyInterface; @@ -50,6 +52,18 @@ final class MediaWikiAssetProxy implements AssetProxyInterface, HasRemoteOrigina */ protected $uriFactory; + /** + * @var array + * @Flow\InjectConfiguration(path="defaultContext", package="Neos.Fusion") + */ + protected $defaultContextConfiguration; + + /** + * @var EelEvaluatorInterface + * @Flow\Inject(lazy=false) + */ + protected $eelEvaluator; + /** * MediaWikiAssetProxy constructor. * @param string[] $assetData @@ -80,10 +94,11 @@ public function getIdentifier(): string /** * @return string + * @throws \Neos\Eel\Exception */ public function getLabel(): string { - return (string)$this->getProperty('extmetadata.ObjectName.value'); + return $this->resolveValue(['extmetadata.ObjectName.value', 'extmetadata.ImageDescription.value']); } /** @@ -190,10 +205,11 @@ protected function getProperty(string $propertyPath) * * @param string $propertyName * @return bool + * @throws \Neos\Eel\Exception */ public function hasIptcProperty(string $propertyName): bool { - return isset($this->getIptcProperties()[$propertyName]); + return isset($this->getIptcProperties()[$propertyName]) && !empty($this->getIptcProperties()[$propertyName]); } /** @@ -201,6 +217,7 @@ public function hasIptcProperty(string $propertyName): bool * * @param string $propertyName * @return string + * @throws \Neos\Eel\Exception */ public function getIptcProperty(string $propertyName): string { @@ -211,16 +228,50 @@ public function getIptcProperty(string $propertyName): string * Returns all known IPTC metadata properties as key => value (e.g. "Title" => "My Photo") * * @return string[] + * @throws \Neos\Eel\Exception */ public function getIptcProperties(): array { if ($this->iptcProperties === null) { $this->iptcProperties = [ - 'Title' => strip_tags($this->getProperty('extmetadata.ImageDescription.value')), - 'Creator' => strip_tags($this->getProperty('extmetadata.Artist.value')), + 'Title' => $this->resolveValue(['extmetadata.ImageDescription.value', 'extmetadata.ObjectName.value']), + 'Creator' => $this->resolveValue(['extmetadata.Artist.value']), + 'CopyrightNotice' => $this->compileCopyrightNotice(), ]; } return $this->iptcProperties; } + + /** + * @return string + * @throws \Neos\Eel\Exception + */ + private function compileCopyrightNotice(): string + { + $context = [ + 'LicenseUrl' => $this->resolveValue(['extmetadata.LicenseUrl.value', 'descriptionurl']), + 'Title' => $this->resolveValue(['extmetadata.ImageDescription.value', 'extmetadata.ObjectName.value']), + 'Creator' => $this->resolveValue(['extmetadata.Artist.value']), + ]; + + return Utility::evaluateEelExpression($this->assetSource->getCopyRightNoticeTemplate(), $this->eelEvaluator, $context, $this->defaultContextConfiguration); + } + + /** + * @param array $candidatePaths + * @return string + */ + private function resolveValue(array $candidatePaths): string + { + foreach ($candidatePaths as $candidatePath) { + if ($this->getProperty($candidatePath) !== null && trim($this->getProperty($candidatePath)) !== '') { + return strip_tags((string)$this->getProperty($candidatePath)); + } + } + + return ''; + } + + } diff --git a/Classes/AssetSource/MediaWikiAssetSource.php b/Classes/AssetSource/MediaWikiAssetSource.php index 21b566f..83b04e2 100644 --- a/Classes/AssetSource/MediaWikiAssetSource.php +++ b/Classes/AssetSource/MediaWikiAssetSource.php @@ -38,6 +38,11 @@ final class MediaWikiAssetSource implements AssetSourceInterface */ protected $assetSourceOptions; + /** + * @var string + */ + private $copyRightNoticeTemplate; + /** * PexelsAssetSource constructor. * @param string $assetSourceIdentifier @@ -47,6 +52,7 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt { $this->assetSourceIdentifier = $assetSourceIdentifier; $this->assetSourceOptions = $assetSourceOptions; + $this->copyRightNoticeTemplate = $assetSourceOptions['copyRightNoticeTemplate'] ?? ''; } /** @@ -124,4 +130,12 @@ public function isReadOnly(): bool { return true; } + + /** + * @return string + */ + public function getCopyRightNoticeTemplate(): string + { + return $this->copyRightNoticeTemplate; + } } diff --git a/Configuration/Settings.yaml b/Configuration/Settings.yaml index 7a01b4d..1d4005d 100644 --- a/Configuration/Settings.yaml +++ b/Configuration/Settings.yaml @@ -10,6 +10,7 @@ Neos: searchStrategyOptions: articleLimit: 10 useQueryResultCache: true + copyRightNoticeTemplate: '${Creator + "" + Title + ""}' excludedIdentifierPatterns: - '*.svg' # - '*Wikiquote-logo.svg'