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

InternalLinkValidator.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\RichText;
8
9 use DOMDocument;
10 use eZ\Publish\SPI\Persistence\Content\Handler as ContentHandler;
11 use eZ\Publish\SPI\Persistence\Content\Location\Handler as LocationHandler;
12 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
13 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
14
20 class InternalLinkValidator
21 {
23 private $contentHandler;
24
26 private $locationHandler;
27
34 public function __construct(ContentHandler $contentHandler, LocationHandler $locationHandler)
35 {
36 $this->contentHandler = $contentHandler;
37 $this->locationHandler = $locationHandler;
38 }
39
47 public function validateDocument(DOMDocument $xml)
48 {
49 $errors = [];
50
51 $xpath = new \DOMXPath($xml);
52 $xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
53
54 foreach (['link', 'ezlink'] as $tagName) {
55 $xpathExpression = $this->getXPathForLinkTag($tagName);
57 foreach ($xpath->query($xpathExpression) as $element) {
58 $url = $element->getAttribute('xlink:href');
59 preg_match('~^(.+)://([^#]*)?(#.*|\\s*)?$~', $url, $matches);
60 list(, $scheme, $id) = $matches;
61
62 if (empty($id)) {
63 continue;
64 }
65
66 if (!$this->validate($scheme, $id)) {
67 $errors[] = $this->getInvalidLinkError($scheme, $url);
68 }
69 }
70 }
71
72 return $errors;
73 }
74
85 public function validate($scheme, $id)
86 {
87 try {
88 switch ($scheme) {
89 case 'ezcontent':
90 $this->contentHandler->loadContentInfo($id);
91 break;
92 case 'ezremote':
93 $this->contentHandler->loadContentInfoByRemoteId($id);
94 break;
95 case 'ezlocation':
96 $this->locationHandler->load($id);
97 break;
98 default:
99 throw new InvalidArgumentException($scheme, "Given scheme '{$scheme}' is not supported.");
100 }
101 } catch (NotFoundException $e) {
102 return false;
103 }
104
105 return true;
106 }
107
118 private function getInvalidLinkError($scheme, $url)
119 {
120 switch ($scheme) {
121 case 'ezcontent':
122 case 'ezremote':
123 return sprintf('Invalid link "%s": target content cannot be found', $url);
124 case 'ezlocation':
125 return sprintf('Invalid link "%s": target location cannot be found', $url);
126 default:
127 throw new InvalidArgumentException($scheme, "Given scheme '{$scheme}' is not supported.");
128 }
129 }
130
138 private function getXPathForLinkTag($tagName)
139 {
140 return "//docbook:{$tagName}[starts-with(@xlink:href, 'ezcontent://') or starts-with(@xlink:href, 'ezlocation://') or starts-with(@xlink:href, 'ezremote://')]";
141 }
142 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator
Validator for RichText internal format links.
Definition: InternalLinkValidator.php:21
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator\$locationHandler
$locationHandler
Definition: InternalLinkValidator.php:26
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator\validate
validate($scheme, $id)
Validates following link formats: 'ezcontent://<contentId>', 'ezremote://<contentRemoteId>',...
Definition: InternalLinkValidator.php:85
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator\getXPathForLinkTag
getXPathForLinkTag($tagName)
Generates XPath expression for given link tag.
Definition: InternalLinkValidator.php:138
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator\__construct
__construct(ContentHandler $contentHandler, LocationHandler $locationHandler)
InternalLinkValidator constructor.
Definition: InternalLinkValidator.php:34
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator\getInvalidLinkError
getInvalidLinkError($scheme, $url)
Builds error message for invalid url.
Definition: InternalLinkValidator.php:118
eZ\Publish\Core\FieldType\RichText\InternalLinkValidator\$contentHandler
$contentHandler
Definition: InternalLinkValidator.php:23
eZ\Publish\SPI\Persistence\Content\Handler
The Content Handler interface defines content operations on the storage engine.
Definition: SPI/Persistence/Content/Handler.php:20
eZ\Publish\SPI\Persistence\Content\Location\Handler
The Location Handler interface defines operations on Location elements in the storage engine.
Definition: SPI/Persistence/Content/Location/Handler.php:15
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.