Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

RichTextStorage.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\RichText;
8
9 use eZ\Publish\SPI\FieldType\GatewayBasedStorage;
10 use eZ\Publish\SPI\FieldType\StorageGateway;
11 use eZ\Publish\SPI\Persistence\Content\VersionInfo;
12 use eZ\Publish\SPI\Persistence\Content\Field;
13 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
14 use Psr\Log\LoggerInterface;
15 use DOMDocument;
16 use DOMXPath;
17
18 class RichTextStorage extends GatewayBasedStorage
19 {
21 protected $logger;
22
24 protected $gateway;
25
30 public function __construct(StorageGateway $gateway, LoggerInterface $logger = null)
31 {
32 parent::__construct($gateway);
33 $this->logger = $logger;
34 }
35
39 public function storeFieldData(VersionInfo $versionInfo, Field $field, array $context)
40 {
41 $document = new DOMDocument();
42 $document->loadXML($field->value->data);
43
44 $xpath = new DOMXPath($document);
45 $xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
46 // This will select only links with non-empty 'xlink:href' attribute value
47 $xpathExpression = "//docbook:link[string( @xlink:href ) and not( starts-with( @xlink:href, 'ezurl://' )" .
48 "or starts-with( @xlink:href, 'ezcontent://' )" .
49 "or starts-with( @xlink:href, 'ezlocation://' )" .
50 "or starts-with( @xlink:href, '#' ) )]";
51
52 $links = $xpath->query($xpathExpression);
53
54 if (empty($links)) {
55 return false;
56 }
57
58 $urlSet = [];
59 $remoteIdSet = [];
60 $linksInfo = [];
61
63 foreach ($links as $index => $link) {
64 preg_match(
65 '~^(ezremote://)?([^#]*)?(#.*|\\s*)?$~',
66 $link->getAttribute('xlink:href'),
67 $matches
68 );
69 $linksInfo[$index] = $matches;
70
71 if (empty($matches[1])) {
72 $urlSet[$matches[2]] = true;
73 } else {
74 $remoteIdSet[$matches[2]] = true;
75 }
76 }
77
78 $urlIdMap = $this->gateway->getUrlIdMap(array_keys($urlSet));
79 $contentIds = $this->gateway->getContentIds(array_keys($remoteIdSet));
80 $urlLinkSet = [];
81
82 foreach ($links as $index => $link) {
83 list(, $scheme, $url, $fragment) = $linksInfo[$index];
84
85 if (empty($scheme)) {
86 // Insert the same URL only once
87 if (!isset($urlIdMap[$url])) {
88 $urlIdMap[$url] = $this->gateway->insertUrl($url);
89 }
90 // Link the same URL only once
91 if (!isset($urlLinkSet[$url])) {
92 $this->gateway->linkUrl(
93 $urlIdMap[$url],
94 $field->id,
95 $versionInfo->versionNo
96 );
97 $urlLinkSet[$url] = true;
98 }
99 $href = "ezurl://{$urlIdMap[$url]}{$fragment}";
100 } else {
101 if (!isset($contentIds[$url])) {
102 throw new NotFoundException('Content', $url);
103 }
104 $href = "ezcontent://{$contentIds[$url]}{$fragment}";
105 }
106
107 $link->setAttribute('xlink:href', $href);
108 }
109
110 $field->value->data = $document->saveXML();
111
112 return true;
113 }
114
122 public function getFieldData(VersionInfo $versionInfo, Field $field, array $context)
123 {
124 $document = new DOMDocument();
125 $document->loadXML($field->value->data);
126
127 $xpath = new DOMXPath($document);
128 $xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
129 $xpathExpression = "//docbook:link[starts-with( @xlink:href, 'ezurl://' )]|//docbook:ezlink[starts-with( @xlink:href, 'ezurl://' )]";
130
131 $links = $xpath->query($xpathExpression);
132
133 if (empty($links)) {
134 return;
135 }
136
137 $urlIdSet = [];
138 $urlInfo = [];
139
141 foreach ($links as $index => $link) {
142 preg_match(
143 '~^ezurl://([^#]*)?(#.*|\\s*)?$~',
144 $link->getAttribute('xlink:href'),
145 $matches
146 );
147 $urlInfo[$index] = $matches;
148
149 if (!empty($matches[1])) {
150 $urlIdSet[$matches[1]] = true;
151 }
152 }
153
154 $idUrlMap = $this->gateway->getIdUrlMap(array_keys($urlIdSet));
155
156 foreach ($links as $index => $link) {
157 list(, $urlId, $fragment) = $urlInfo[$index];
158
159 if (isset($idUrlMap[$urlId])) {
160 $href = $idUrlMap[$urlId] . $fragment;
161 } else {
162 // URL id is empty or not in the DB
163 if (isset($this->logger)) {
164 $this->logger->error("URL with ID {$urlId} not found");
165 }
166 $href = '#';
167 }
168
169 $link->setAttribute('xlink:href', $href);
170 }
171
172 $field->value->data = $document->saveXML();
173 }
174
175 public function deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
176 {
177 foreach ($fieldIds as $fieldId) {
178 $this->gateway->unlinkUrl($fieldId, $versionInfo->versionNo);
179 }
180 }
181
187 public function hasFieldData()
188 {
189 return true;
190 }
191
192 public function getIndexData(VersionInfo $versionInfo, Field $field, array $context)
193 {
194 }
195 }
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Field
The Field Criterion class.
Definition: API/Repository/Values/Content/Query/Criterion/Field.php:19
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\FieldType\RichText\RichTextStorage
Definition: RichTextStorage.php:19
eZ\Publish\Core\FieldType\RichText\RichTextStorage\__construct
__construct(StorageGateway $gateway, LoggerInterface $logger=null)
Definition: RichTextStorage.php:30
eZ\Publish\Core\FieldType\RichText\RichTextStorage\$logger
$logger
Definition: RichTextStorage.php:21
eZ\Publish\Core\FieldType\RichText\RichTextStorage\$gateway
$gateway
Definition: RichTextStorage.php:24
eZ\Publish\Core\FieldType\RichText\RichTextStorage\deleteFieldData
deleteFieldData(VersionInfo $versionInfo, array $fieldIds, array $context)
Deletes field data for all $fieldIds in the version identified by $versionInfo.
Definition: RichTextStorage.php:175
eZ\Publish\Core\FieldType\RichText\RichTextStorage\hasFieldData
hasFieldData()
Checks if field type has external data to deal with.
Definition: RichTextStorage.php:187
eZ\Publish\Core\FieldType\RichText\RichTextStorage\getIndexData
getIndexData(VersionInfo $versionInfo, Field $field, array $context)
Get index data for external data for search backend.
Definition: RichTextStorage.php:192
eZ\Publish\SPI\FieldType\GatewayBasedStorage
Field Type External Storage gateway base class.
Definition: SPI/FieldType/GatewayBasedStorage.php:16
eZ\Publish\SPI\FieldType\StorageGateway
Base class for FieldType external storage gateways.
Definition: SPI/FieldType/StorageGateway.php:13
eZ\Publish\SPI\Persistence\Content\Field
Definition: SPI/Persistence/Content/Field.php:12
eZ\Publish\SPI\Persistence\Content\VersionInfo
This class holds version information data.
Definition: SPI/Persistence/Content/VersionInfo.php:15
eZ\Publish\SPI\FieldType\FieldStorage\getFieldData
getFieldData(VersionInfo $versionInfo, Field $field, array $context)
Populates $field value property based on the external data.
eZ\Publish\SPI\FieldType\FieldStorage\storeFieldData
storeFieldData(VersionInfo $versionInfo, Field $field, array $context)
Allows custom field types to store data in an external source (e.g.
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.