|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2018 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2018 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Processor; |
|
18
|
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\File\FileExtensionCheckerInterface; |
|
20
|
|
|
use SWP\Bundle\ContentBundle\Manager\MediaManagerInterface; |
|
21
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
|
22
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
|
23
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRendition; |
|
24
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
25
|
|
|
|
|
26
|
|
|
class EmbeddedImageProcessor implements EmbeddedImageProcessorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private const DEFAULT_ARTICLE_BODY_IMAGE_RENDITION = 'original'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var MediaManagerInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
private $mediaManager; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var FileExtensionCheckerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $fileExtensionChecker; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $defaultImageRendition; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(MediaManagerInterface $mediaManager, FileExtensionCheckerInterface $fileExtensionChecker) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->mediaManager = $mediaManager; |
|
48
|
|
|
$this->fileExtensionChecker = $fileExtensionChecker; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void |
|
52
|
|
|
{ |
|
53
|
|
|
if (null === $article->getBody()) { |
|
54
|
|
|
return; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$body = preg_replace('/\s+/', ' ', trim($article->getBody())); |
|
58
|
|
|
$mediaId = str_replace('/', '\\/', $articleMedia->getKey()); |
|
59
|
|
|
|
|
60
|
|
|
preg_match( |
|
61
|
|
|
"/(<!-- ?EMBED START Image {id: \"$mediaId\"} ?-->)(.+?)(<!-- ?EMBED END Image {id: \"$mediaId\"} ?-->)/im", |
|
62
|
|
|
str_replace(PHP_EOL, '', $body), |
|
63
|
|
|
$embeds |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
if (empty($embeds)) { |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$figureString = trim($embeds[2]); |
|
71
|
|
|
$crawler = new Crawler($figureString); |
|
72
|
|
|
$images = $crawler->filter('figure img'); |
|
73
|
|
|
|
|
74
|
|
|
/** @var \DOMElement $imageElement */ |
|
75
|
|
|
foreach ($images as $imageElement) { |
|
76
|
|
|
/** @var ImageRendition $rendition */ |
|
77
|
|
|
foreach ($articleMedia->getRenditions() as $rendition) { |
|
78
|
|
|
if ($this->getDefaultImageRendition() === $rendition->getName()) { |
|
79
|
|
|
$this->processImageElement($imageElement, $rendition, $articleMedia); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$figCaptionNode = $crawler->filter('figure figcaption')->getNode(0); |
|
85
|
|
|
if (null !== $figCaptionNode) { |
|
86
|
|
|
$this->appendImageByline($articleMedia, $figCaptionNode); |
|
|
|
|
|
|
87
|
|
|
$this->appendImageCopyrightNotice($articleMedia, $figCaptionNode); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$article->setBody(str_replace($figureString, $crawler->filter('body')->html(), $body)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function setDefaultImageRendition(string $renditionName): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->defaultImageRendition = $renditionName; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function supports(string $type): bool |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->fileExtensionChecker->isImage($type); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function appendImageByline(ArticleMediaInterface $articleMedia, \DOMElement $figCaptionNode): void |
|
104
|
|
|
{ |
|
105
|
|
|
$element = new \DOMElement('span'); |
|
106
|
|
|
$figCaptionNode->appendChild($element); |
|
107
|
|
|
|
|
108
|
|
|
$authorDiv = $figCaptionNode->childNodes[1]; |
|
109
|
|
|
$authorDiv->textContent = $this->applyByline($articleMedia); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function applyByline(ArticleMediaInterface $articleMedia): string |
|
113
|
|
|
{ |
|
114
|
|
|
return $articleMedia->getByLine(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
private function appendImageCopyrightNotice(ArticleMediaInterface $articleMedia, \DOMElement $figCaptionNode): void |
|
118
|
|
|
{ |
|
119
|
|
|
$copyrightNotice = $this->applyCopyrightNotice($articleMedia); |
|
120
|
|
|
if (null != $copyrightNotice) { |
|
121
|
|
|
$element = new \DOMElement('span'); |
|
122
|
|
|
$figCaptionNode->appendChild($element); |
|
123
|
|
|
$authorDiv = $figCaptionNode->childNodes[2]; |
|
124
|
|
|
$authorDiv->textContent = $copyrightNotice; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function applyCopyrightNotice(ArticleMediaInterface $articleMedia): ?string |
|
129
|
|
|
{ |
|
130
|
|
|
return $articleMedia->getCopyrightNotice(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
protected function processImageElement(\DOMElement $imageElement, ImageRendition $rendition, ArticleMediaInterface $articleMedia): void |
|
134
|
|
|
{ |
|
135
|
|
|
$attributes = $imageElement->attributes; |
|
136
|
|
|
$altAttribute = null; |
|
137
|
|
|
if ($imageElement->hasAttribute('alt')) { |
|
138
|
|
|
$altAttribute = $attributes->getNamedItem('alt'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
while ($attributes->length) { |
|
|
|
|
|
|
142
|
|
|
$imageElement->removeAttribute($attributes->item(0)->name); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$imageElement->setAttribute('src', $this->mediaManager->getMediaUri($rendition->getImage())); |
|
146
|
|
|
$imageElement->setAttribute('data-media-id', $articleMedia->getKey()); |
|
147
|
|
|
$imageElement->setAttribute('data-image-id', $rendition->getImage()->getAssetId()); |
|
148
|
|
|
$imageElement->setAttribute('data-rendition-name', $this->getDefaultImageRendition()); |
|
149
|
|
|
$imageElement->setAttribute('width', (string) $rendition->getWidth()); |
|
150
|
|
|
$imageElement->setAttribute('height', (string) $rendition->getHeight()); |
|
151
|
|
|
$imageElement->setAttribute('loading', 'lazy'); |
|
152
|
|
|
|
|
153
|
|
|
if (null !== $altAttribute && '' !== $altAttribute->nodeValue) { |
|
154
|
|
|
$imageElement->setAttribute('alt', $altAttribute->nodeValue); |
|
155
|
|
|
} else { |
|
156
|
|
|
$imageElement->setAttribute('alt', $articleMedia->getHeadline()); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
protected function getDefaultImageRendition(): string |
|
161
|
|
|
{ |
|
162
|
|
|
if (null === $this->defaultImageRendition) { |
|
163
|
|
|
return self::DEFAULT_ARTICLE_BODY_IMAGE_RENDITION; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this->defaultImageRendition; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.