Skip to content

Commit

Permalink
FEATURE: Add a copyright notice template to generate the copyright no…
Browse files Browse the repository at this point in the history
…tice
  • Loading branch information
daniellienert committed Feb 1, 2020
1 parent 3fbe066 commit 0e2bc9a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
59 changes: 55 additions & 4 deletions Classes/AssetSource/MediaWikiAssetProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']);
}

/**
Expand Down Expand Up @@ -190,17 +205,19 @@ 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]);
}

/**
* Returns the given IPTC metadata property if it exists, or an empty string otherwise.
*
* @param string $propertyName
* @return string
* @throws \Neos\Eel\Exception
*/
public function getIptcProperty(string $propertyName): string
{
Expand All @@ -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 '';
}


}
14 changes: 14 additions & 0 deletions Classes/AssetSource/MediaWikiAssetSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class MediaWikiAssetSource implements AssetSourceInterface
*/
protected $assetSourceOptions;

/**
* @var string
*/
private $copyRightNoticeTemplate;

/**
* PexelsAssetSource constructor.
* @param string $assetSourceIdentifier
Expand All @@ -47,6 +52,7 @@ public function __construct(string $assetSourceIdentifier, array $assetSourceOpt
{
$this->assetSourceIdentifier = $assetSourceIdentifier;
$this->assetSourceOptions = $assetSourceOptions;
$this->copyRightNoticeTemplate = $assetSourceOptions['copyRightNoticeTemplate'] ?? '';
}

/**
Expand Down Expand Up @@ -124,4 +130,12 @@ public function isReadOnly(): bool
{
return true;
}

/**
* @return string
*/
public function getCopyRightNoticeTemplate(): string
{
return $this->copyRightNoticeTemplate;
}
}
1 change: 1 addition & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Neos:
searchStrategyOptions:
articleLimit: 10
useQueryResultCache: true
copyRightNoticeTemplate: '${Creator + "<a href=" + LicenseUrl + ">" + Title + "</a>"}'
excludedIdentifierPatterns:
- '*.svg'
# - '*Wikiquote-logo.svg'
Expand Down

0 comments on commit 0e2bc9a

Please sign in to comment.