Skip to content

Commit

Permalink
Export: import validation refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chlulei committed Oct 24, 2024
1 parent 21617cf commit 3aeec4c
Show file tree
Hide file tree
Showing 176 changed files with 3,093 additions and 2,679 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

declare(strict_types=1);

use ILIAS\Export\ImportHandler\ilFactory as ilImportHandlerFactory;
use ILIAS\Export\ImportHandler\Factory as ilImportHandlerFactory;
use ILIAS\Export\ImportStatus\ilFactory as ilImportStatusFactory;
use ILIAS\Export\ImportStatus\StatusType;
use ILIAS\Export\ImportStatus\I\ilCollectionInterface as ilImportStatusCollectionInterface;
use ILIAS\Export\Schema\ilXmlSchemaFactory as ilXMLSchemaFactory;

/**
* Description of ilDidacticTemplateImport
Expand Down Expand Up @@ -94,17 +94,16 @@ public function validateImportFile(): ilImportStatusCollectionInterface
$status = new ilImportStatusFactory();
if ($this->getInputType() !== self::IMPORT_FILE) {
return $status->collection()->withAddedStatus($status->handler()
->withType(ImportStatus\StatusType::FAILED)
->withType(StatusType::FAILED)
->withContent($status->content()->builder()->string()
->withString("Invalid import status, import status 'IMPORT_FILE' expected.")));
}
$schema = new ilXMLSchemaFactory();
$import = new ilImportHandlerFactory();
$xml_spl_info = new SplFileInfo($this->getInputFile());
$xsd_spl_info = $schema->getLatest(self::SCHEMA_TYPE);
$xml_file_handler = $import->file()->xml()->withFileInfo($xml_spl_info);
$xsd_spl_info = $import->schema()->folder()->handler()->getLatest(self::SCHEMA_TYPE);
$xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_spl_info);
$xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_spl_info);
return $import->file()->validation()->handler()->validateXMLFile($xml_file_handler, $xsd_file_handler);
return $import->validation()->handler()->validateXMLFile($xml_file_handler, $xsd_file_handler);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions components/ILIAS/Export/README-technical.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ $xml_file_spl = new SplFileInfo('path to my xml file')
$xsd_file_spl = new SplFileInfo('path to my xsd file')

// Initialize a xml/xsd file handler
$import = new \ILIAS\Export\ImportHandler\ilFactory();
$xml_file_handler = $import->file()->xml()->withFileInfo($xml_file_spl);
$import = new \ILIAS\Export\ImportHandler\Factory();
$xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_file_spl);
$xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_file_spl);

/** @var \ILIAS\Export\ImportStatus\ilCollection $validation_results */
Expand All @@ -274,17 +274,17 @@ $xml_file_spl = new SplFileInfo('path to my xml file')
$xsd_file_spl = new SplFileInfo('path to my xsd file')

// Initialize a xml/xsd file handler
$import = new \ILIAS\Export\ImportHandler\ilFactory();
$xml_file_handler = $import->file()->xml()->withFileInfo($xml_file_spl);
$import = new \ILIAS\Export\ImportHandler\Factory();
$xml_file_handler = $import->file()->xml()->handler()->withFileInfo($xml_file_spl);
$xsd_file_handler = $import->file()->xsd()->withFileInfo($xsd_file_spl);

// Build xPath to xml node
// $path->toString() = '/RootElement/namespace:TargetElement'
/** @var \ILIAS\Export\ImportHandler\File\Path\ilHandler $path */
$path = $import->file()->path()->handler()
/** @var \ILIAS\Export\ImportHandler\Path\Handler $path */
$path = $import->path()->handler()
->withStartAtRoot(true)
->withNode($import->file()->path()->node()->simple()->withName('RootElement'))
->withNode($import->file()->path()->node()->simple()->withName('namespace:TargetElement'));
->withNode($import->path()->node()->simple()->withName('RootElement'))
->withNode($import->path()->node()->simple()->withName('namespace:TargetElement'));

// Because the path contains the namespace 'namespace' we have to add the namespace
// info to the xml file handler
Expand Down
99 changes: 99 additions & 0 deletions components/ILIAS/Export/classes/ImportHandler/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Export\ImportHandler;

use ILIAS\Data\Factory as ilDataFactory;
use ILIAS\Export\ImportHandler\File\Factory as ilImportHandlerFileFactory;
use ILIAS\Export\ImportHandler\I\FactoryInterface as ilImportHandlerFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\FactoryInterface as ilImportHandlerFileFactoryInterface;
use ILIAS\Export\ImportHandler\I\Parser\FactoryInterface as ilImportHandlerParserFactoryInterface;
use ILIAS\Export\ImportHandler\I\Path\FactoryInterface as ilImportHandlerPathFactoryInterface;
use ILIAS\Export\ImportHandler\I\Schema\FactoryInterface as ilImportHandlerSchemaFactoryInterface;
use ILIAS\Export\ImportHandler\I\Validation\FactoryInterface as ilImportHandlerValidationFactoryInterface;
use ILIAS\Export\ImportHandler\Parser\Factory as ilImportHandlerParserFactory;
use ILIAS\Export\ImportHandler\Path\Factory as ilImportHandlerPathFactory;
use ILIAS\Export\ImportHandler\Schema\Factory as ilImportHandlerSchemaFactory;
use ILIAS\Export\ImportHandler\Validation\Factory as ilImportHandlerValidationFactory;
use ILIAS\Export\ImportStatus\ilFactory as ilImportStatusFactory;
use ilLanguage;
use ilLogger;

class Factory implements ilImportHandlerFactoryInterface
{
protected ilLogger $logger;
protected ilLanguage $lng;
protected ilImportStatusFactory $import_status_factory;
protected ilDataFactory $data_factory;

public function __construct()
{
global $DIC;
$this->logger = $DIC->logger()->root();
$this->lng = $DIC->language();
$this->lng->loadLanguageModule("exp");
$this->import_status_factory = new ilImportStatusFactory();
$this->data_factory = new ilDataFactory();
}

public function parser(): ilImportHandlerParserFactoryInterface
{
return new ilImportHandlerParserFactory(
$this,
$this->logger
);
}

public function file(): ilImportHandlerFileFactoryInterface
{
return new ilImportHandlerFileFactory(
$this,
$this->import_status_factory,
$this->logger,
$this->lng,
$this->data_factory
);
}

public function schema(): ilImportHandlerSchemaFactoryInterface
{
return new ilImportHandlerSchemaFactory(
$this,
$this->data_factory,
$this->logger
);
}

public function path(): ilImportHandlerPathFactoryInterface
{
return new ilImportHandlerPathFactory(
$this->logger
);
}

public function validation(): ilImportHandlerValidationFactoryInterface
{
return new ilImportHandlerValidationFactory(
$this->import_status_factory,
$this,
$this->logger
);
}
}
78 changes: 78 additions & 0 deletions components/ILIAS/Export/classes/ImportHandler/File/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
*********************************************************************/

declare(strict_types=1);

namespace ILIAS\Export\ImportHandler\File;

use ILIAS\Data\Factory as ilDataFactory;
use ILIAS\Export\ImportHandler\File\Namespace\Factory as ilFileNamespaceFactory;
use ILIAS\Export\ImportHandler\File\XML\Factory as ilXMLFileFactory;
use ILIAS\Export\ImportHandler\File\XSD\Factory as ilXSDFileFactory;
use ILIAS\Export\ImportHandler\I\FactoryInterface as ilImportHandlerFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\FactoryInterface as ilFileFactory;
use ILIAS\Export\ImportHandler\I\File\Namespace\FactoryInterface as ilFileNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\XML\FactoryInterface as ilXMLFileFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\XSD\FactoryInterface as ilXSDFileFactoryInterface;
use ILIAS\Export\ImportStatus\ilFactory as ilImportStatusFactory;
use ilLanguage;
use ilLogger;

class Factory implements ilFileFactory
{
protected ilImportHandlerFactoryInterface $import_handler;
protected ilLogger $logger;
protected ilLanguage $lng;
protected ilImportStatusFactory $import_status_factory;
protected ilDataFactory $data_factory;

public function __construct(
ilImportHandlerFactoryInterface $import_handler,
ilImportStatusFactory $import_status_factory,
ilLogger $logger,
ilLanguage $lng,
ilDataFactory $data_factory
) {
$this->import_handler = $import_handler;
$this->import_status_factory = $import_status_factory;
$this->logger = $logger;
$this->lng = $lng;
$this->data_factory = $data_factory;
}

public function xml(): ilXMLFileFactoryInterface
{
return new ilXMLFileFactory(
$this->import_handler,
$this->import_status_factory,
$this->logger,
$this->lng,
$this->data_factory
);
}

public function xsd(): ilXSDFileFactoryInterface
{
return new ilXSDFileFactory();
}

public function namespace(): ilFileNamespaceFactoryInterface
{
return new ilFileNamespaceFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@

namespace ILIAS\Export\ImportHandler\File;

use ilImportException;
use ILIAS\Export\ImportHandler\I\File\ilHandlerInterface as ilFileHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilCollectionInterface as ilFileNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilFactoryInterface as ilFileNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilHandlerInterface as ilFileNamespaceHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\HandlerInterface as ilFileHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\CollectionInterface as ilFileNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\FactoryInterface as ilFileNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\HandlerInterface as ilFileNamespaceHandlerInterface;
use SplFileInfo;

class ilHandler implements ilFileHandlerInterface
class Handler implements ilFileHandlerInterface
{
protected SplFileInfo $xml_file_info;
protected SplFileInfo $spl_file_info;
protected ilFileNamespaceFactoryInterface $namespace;
protected ilFileNamespaceCollectionInterface $namespaces;

Expand All @@ -55,20 +54,20 @@ public function getNamespaces(): ilFileNamespaceCollectionInterface
public function withFileInfo(SplFileInfo $file_info): ilFileHandlerInterface
{
$clone = clone $this;
$clone->xml_file_info = $file_info;
$clone->spl_file_info = $file_info;
return $clone;
}

public function getFileName(): string
{
return $this->xml_file_info->getFilename();
return $this->spl_file_info->getFilename();
}

public function getFilePath(): string
{
return $this->fileExists()
? $this->xml_file_info->getRealPath()
: $this->xml_file_info->getPath() . DIRECTORY_SEPARATOR . $this->xml_file_info->getFilename();
? $this->spl_file_info->getRealPath()
: $this->spl_file_info->getPath() . DIRECTORY_SEPARATOR . $this->spl_file_info->getFilename();
}

public function getSubPathToDirBeginningAtPathEnd(string $dir_name): ilFileHandlerInterface
Expand All @@ -84,7 +83,7 @@ public function getSubPathToDirBeginningAtPathEnd(string $dir_name): ilFileHandl
}
}
$clone = clone $this;
$clone->xml_file_info = new SplFileInfo($trimmed_str);
$clone->spl_file_info = new SplFileInfo($trimmed_str);
return $clone;
}

Expand All @@ -101,18 +100,18 @@ public function getSubPathToDirBeginningAtPathStart(string $dir_name): ilFileHan
}
}
$clone = clone $this;
$clone->xml_file_info = new SplFileInfo($trimmed_str);
$clone->spl_file_info = new SplFileInfo($trimmed_str);
return $clone;
}

public function getPathToFileLocation(): string
{
return $this->xml_file_info->getPath();
return $this->spl_file_info->getPath();
}

public function fileExists(): bool
{
return $this->xml_file_info->getRealPath() !== false;
return $this->spl_file_info->getRealPath() !== false;
}

public function getPathPart(string $pattern): string|null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

namespace ILIAS\Export\ImportHandler\File\Namespace;

use ILIAS\Export\ImportHandler\I\File\Namespace\ilCollectionInterface as ilParserNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilHandlerInterface as ilParserNamespaceHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\CollectionInterface as ilParserNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\HandlerInterface as ilParserNamespaceHandlerInterface;

class ilCollection implements ilParserNamespaceCollectionInterface
class Collection implements ilParserNamespaceCollectionInterface
{
protected array $elements;
protected int $index;
Expand Down Expand Up @@ -75,7 +75,7 @@ public function key(): int

public function valid(): bool
{
return 0 <= $this->index && $this->index < $this->count();
return isset($this->elements[$this->index]);
}

public function rewind(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

namespace ILIAS\Export\ImportHandler\File\Namespace;

use ILIAS\Export\ImportHandler\File\Namespace\ilCollection as ilParserNamespaceCollection;
use ILIAS\Export\ImportHandler\File\Namespace\ilHandler as ilParserNamespaceHandler;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilCollectionInterface as ilParserNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilFactoryInterface as ilParserNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\ilHandlerInterface as ilParserNamespaceHandlerInterface;
use ILIAS\Export\ImportHandler\File\Namespace\Collection as ilParserNamespaceCollection;
use ILIAS\Export\ImportHandler\File\Namespace\Handler as ilParserNamespaceHandler;
use ILIAS\Export\ImportHandler\I\File\Namespace\CollectionInterface as ilParserNamespaceCollectionInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\FactoryInterface as ilParserNamespaceFactoryInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\HandlerInterface as ilParserNamespaceHandlerInterface;

class ilFactory implements ilParserNamespaceFactoryInterface
class Factory implements ilParserNamespaceFactoryInterface
{
public function handler(): ilParserNamespaceHandlerInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

namespace ILIAS\Export\ImportHandler\File\Namespace;

use ILIAS\Export\ImportHandler\I\File\Namespace\ilHandlerInterface as ilParserNamespaceHandlerInterface;
use ILIAS\Export\ImportHandler\I\File\Namespace\HandlerInterface as ilParserNamespaceHandlerInterface;

class ilHandler implements ilParserNamespaceHandlerInterface
class Handler implements ilParserNamespaceHandlerInterface
{
protected string $prefix;
protected string $namespace;
Expand Down
Loading

0 comments on commit 3aeec4c

Please sign in to comment.