ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Handler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use DOMDocument;
24 use Exception;
25 use ILIAS\Export\ImportHandler\I\File\HandlerInterface as FileHandlerInterface;
26 use ILIAS\Export\ImportHandler\I\File\XML\HandlerInterface as XMLFileHandlerInterface;
27 use ILIAS\Export\ImportHandler\I\File\XSD\HandlerInterface as XSDFileHandlerInterface;
28 use ILIAS\Export\ImportHandler\I\Parser\FactoryInterface as ParserFactoryInterface;
30 use ILIAS\Export\ImportHandler\I\Path\FactoryInterface as FilePathFactoryInterface;
31 use ILIAS\Export\ImportHandler\I\Path\HandlerInterface as FilePathHandlerInterface;
32 use ILIAS\Export\ImportHandler\I\Validation\HandlerInterface as FileValidationHandlerInterface;
33 use ILIAS\Export\ImportHandler\I\Validation\Set\CollectionInterface as FileValidationSetCollectionInterface;
36 use ILIAS\Export\ImportStatus\I\ilFactoryInterface as ImportStatusFactoryInterface;
39 use ilLogger;
40 use LibXMLError;
41 
42 class 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;
52 
53  public function __construct(
54  ilLogger $logger,
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 = []
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
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
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
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
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
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
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 }
if($err=$client->getError()) $namespace
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
__construct(ilLogger $logger, ParserFactoryInterface $parser, ImportStatusFactoryInterface $import_status, FilePathFactoryInterface $path,)
Definition: Handler.php:53
createErrorMessage(string $msg, ?XMLFileHandlerInterface $xml_file_handler=null, ?XSDFileHandlerInterface $xsd_file_handler=null)
Definition: Handler.php:103
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
validateSets(FileValidationSetCollectionInterface $sets)
Definition: Handler.php:221
validateEmptyXML(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler)
Definition: Handler.php:171
validateXMLAtPath(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler, FilePathHandlerInterface $path_handler)
Definition: Handler.php:204
validateXMLFile(XMLFileHandlerInterface $xml_file_handler, XSDFileHandlerInterface $xsd_file_handler)
Definition: Handler.php:190
ImportStatusFactoryInterface $import_status
Definition: Handler.php:48
ilErrorHandling $error
Definition: class.ilias.php:69
ImportStatusHandlerInterface $success_status
Definition: Handler.php:51