ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
Handler.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use DOMDocument;
24use Exception;
30use ILIAS\Export\ImportHandler\I\Path\FactoryInterface as FilePathFactoryInterface;
31use ILIAS\Export\ImportHandler\I\Path\HandlerInterface as FilePathHandlerInterface;
32use ILIAS\Export\ImportHandler\I\Validation\HandlerInterface as FileValidationHandlerInterface;
33use ILIAS\Export\ImportHandler\I\Validation\Set\CollectionInterface as FileValidationSetCollectionInterface;
34use ILIAS\Export\ImportStatus\Exception\ilException as ImportStatusException;
35use ILIAS\Export\ImportStatus\I\ilCollectionInterface as ImportStatusHandlerCollectionInterface;
36use ILIAS\Export\ImportStatus\I\ilFactoryInterface as ImportStatusFactoryInterface;
37use ILIAS\Export\ImportStatus\I\ilHandlerInterface as ImportStatusHandlerInterface;
39use ilLogger;
40use LibXMLError;
41
42class Handler implements FileValidationHandlerInterface
43{
44 public const TMP_DIR_NAME = 'temp';
45 public const XML_DIR_NAME = 'xml';
46
47 protected ilLogger $logger;
48 protected ImportStatusFactoryInterface $import_status;
49 protected ParserFactoryInterface $parser;
50 protected FilePathFactoryInterface $path;
51 protected ImportStatusHandlerInterface $success_status;
52
53 public function __construct(
55 ParserFactoryInterface $parser,
56 ImportStatusFactoryInterface $import_status,
57 FilePathFactoryInterface $path,
58 ) {
59 $this->logger = $logger;
60 $this->import_status = $import_status;
61 $this->parser = $parser;
62 $this->path = $path;
63 $this->success_status = $import_status->handler()
64 ->withType(StatusType::SUCCESS)
65 ->withContent($import_status->content()->builder()->string()->withString('Validation SUCCESS'));
66 }
67
71 protected function checkIfFilesExist(array $file_handlers): ImportStatusHandlerCollectionInterface
72 {
73 $status_collection = $this->import_status->collection()->withNumberingEnabled(true);
74 foreach ($file_handlers as $file_handler) {
75 if ($file_handler->fileExists()) {
76 continue;
77 }
78 $status_collection->withAddedStatus($this->import_status->handler()
79 ->withType(StatusType::FAILED)
80 ->withContent($this->import_status->content()->builder()->string()
81 ->withString('File does not exist: ' . $file_handler->getFilePath())));
82 }
83 return $status_collection;
84 }
85
89 protected function collectErrors(
90 ?XMLFileHandlerInterface $xml_file_handler = null,
91 ?XSDFileHandlerInterface $xsd_file_handler = null,
92 array $errors = []
93 ): ImportStatusHandlerCollectionInterface {
94 $status_collection = $this->import_status->collection();
95 foreach ($errors as $error) {
96 $status_collection = $status_collection->getMergedCollectionWith(
97 $this->createErrorMessage($error->message, $xml_file_handler, $xsd_file_handler)
98 );
99 }
100 return $status_collection;
101 }
102
103 protected function createErrorMessage(
104 string $msg,
105 ?XMLFileHandlerInterface $xml_file_handler = null,
106 ?XSDFileHandlerInterface $xsd_file_handler = null
107 ): ImportStatusHandlerCollectionInterface {
108 $status_collection = $this->import_status->collection();
109 $xml_str = is_null($xml_file_handler)
110 ? ''
111 : "<br>XML-File: " . $xml_file_handler->getSubPathToDirBeginningAtPathEnd(self::TMP_DIR_NAME)->getFilePath();
112 $xsd_str = is_null($xsd_file_handler)
113 ? ''
114 : "<br>XSD-File: " . $xsd_file_handler->getSubPathToDirBeginningAtPathEnd(self::XML_DIR_NAME)->getFilePath();
115 $content = $this->import_status->content()->builder()->string()->withString(
116 "Validation FAILED"
117 . $xml_str
118 . $xsd_str
119 . "<br>ERROR Message: " . $msg
120 );
121 $status_collection = $status_collection->withAddedStatus(
122 $this->import_status->handler()->withType(StatusType::FAILED)->withContent($content)
123 );
124 return $status_collection;
125 }
126
127
128 protected function validateXMLAtNodes(
129 XMLFileHandlerInterface $xml_file_handler,
130 XSDFileHandlerInterface $xsd_file_handler,
131 XMLFileNodeInfoCollection $nodes
132 ): ImportStatusHandlerCollectionInterface {
133 // Check if files exist
134 $status_collection = $this->checkIfFilesExist([$xsd_file_handler]);
135 if ($status_collection->hasStatusType(StatusType::FAILED)) {
136 return $status_collection;
137 }
138 if (count($nodes) === 0) {
139 return $this->validateEmptyXML($xml_file_handler, $xsd_file_handler);
140 }
141 $old_value = libxml_use_internal_errors(true);
142 $status_collection = $this->import_status->collection()->withNumberingEnabled(true);
143 foreach ($nodes as $node) {
144 $doc = new DOMDocument();
145 $doc->loadXML($node->getXML(), LIBXML_NOBLANKS);
146 $doc->normalizeDocument();
147 foreach ($xml_file_handler->getNamespaces() as $namespace) {
148 $doc->createAttributeNS($namespace->getNamespace(), $namespace->getPrefix());
149 }
150 try {
151 if ($doc->schemaValidate($xsd_file_handler->getFilePath())) {
152 continue;
153 }
154 } catch (Exception $e) {
155 // Catches xsd file related exceptions to allow for manual error handling
156 }
157 $errors = libxml_get_errors();
158 libxml_clear_errors();
159 $status_collection = $status_collection->getMergedCollectionWith($this->collectErrors(
160 $xml_file_handler,
161 $xsd_file_handler,
162 $errors
163 ));
164 }
165 libxml_use_internal_errors($old_value);
166 return $status_collection->hasStatusType(StatusType::FAILED)
167 ? $status_collection
168 : $this->import_status->collection()->withAddedStatus($this->success_status);
169 }
170
171 protected function validateEmptyXML(
172 XMLFileHandlerInterface $xml_file_handler,
173 XSDFileHandlerInterface $xsd_file_handler
174 ): ImportStatusHandlerCollectionInterface {
175 /*$old_value = libxml_use_internal_errors(true);
176 $status_collection = $this->import_status->collection()->withNumberingEnabled(true);
177 $doc = new DOMDocument();
178 $doc->schemaValidate($xsd_file_handler->getFilePath());
179 $errors = libxml_get_errors();
180 libxml_clear_errors();
181 libxml_use_internal_errors($old_value);
182 return $status_collection->getMergedCollectionWith($this->collectErrors(
183 $xml_file_handler,
184 $xsd_file_handler,
185 $errors
186 ));*/
187 return $this->import_status->collection()->withNumberingEnabled(true);
188 }
189
190 public function validateXMLFile(
191 XMLFileHandlerInterface $xml_file_handler,
192 XSDFileHandlerInterface $xsd_file_handler
193 ): ImportStatusHandlerCollectionInterface {
194 return $this->validateXMLAtPath(
195 $xml_file_handler,
196 $xsd_file_handler,
197 $this->path->handler()->withNode($this->path->node()->anyElement())->withStartAtRoot(true)
198 );
199 }
200
204 public function validateXMLAtPath(
205 XMLFileHandlerInterface $xml_file_handler,
206 XSDFileHandlerInterface $xsd_file_handler,
207 FilePathHandlerInterface $path_handler
208 ): ImportStatusHandlerCollectionInterface {
209 return $this->validateXMLAtNodes(
210 $xml_file_handler,
211 $xsd_file_handler,
212 $this->parser->DOM()->handler()
213 ->withFileHandler($xml_file_handler)
214 ->getNodeInfoAt($path_handler)
215 );
216 }
217
221 public function validateSets(
222 FileValidationSetCollectionInterface $sets
223 ): ImportStatusHandlerCollectionInterface {
224 $status_collection = $this->import_status->collection();
225 foreach ($sets as $set) {
226 $status_collection = $status_collection->getMergedCollectionWith($this->validateXMLAtPath(
227 $set->getXMLFileHandler(),
228 $set->getXSDFileHandler(),
229 $set->getFilePathHandler()
230 ));
231 }
232 return $status_collection;
233 }
234}
__construct(ilLogger $logger, ParserFactoryInterface $parser, ImportStatusFactoryInterface $import_status, FilePathFactoryInterface $path,)
Definition: Handler.php:53
ImportStatusFactoryInterface $import_status
Definition: Handler.php:48
validateSets(FileValidationSetCollectionInterface $sets)
Definition: Handler.php:221
ImportStatusHandlerInterface $success_status
Definition: Handler.php:51
createErrorMessage(string $msg, ?XMLFileHandlerInterface $xml_file_handler=null, ?XSDFileHandlerInterface $xsd_file_handler=null)
Definition: Handler.php:103
validateEmptyXML(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler)
Definition: Handler.php:171
collectErrors(?XMLFileHandlerInterface $xml_file_handler=null, ?XSDFileHandlerInterface $xsd_file_handler=null, array $errors=[])
Definition: Handler.php:89
validateXMLAtNodes(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler, XMLFileNodeInfoCollection $nodes)
Definition: Handler.php:128
validateXMLFile(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler)
Definition: Handler.php:190
validateXMLAtPath(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler, FilePathHandlerInterface $path_handler)
Definition: Handler.php:204
ilErrorHandling $error
Definition: class.ilias.php:69
Component logger with individual log levels by component id.
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'vtt', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...