tao-test/app/tao/models/classes/import/class.RdfImporter.php

189 lines
6.9 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
*
*/
use oat\generis\model\OntologyRdf;
use oat\generis\model\OntologyRdfs;
use oat\oatbox\event\EventManagerAwareTrait;
use oat\tao\model\event\RdfImportEvent;
use oat\tao\model\import\ImportHandlerHelperTrait;
use oat\tao\model\import\TaskParameterProviderInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
/**
* RDF Import Handler
*
* @access public
* @author Joel Bout, <joel@taotesting.com>
* @package tao
*/
class tao_models_classes_import_RdfImporter implements tao_models_classes_import_ImportHandler, ServiceLocatorAwareInterface, TaskParameterProviderInterface
{
use EventManagerAwareTrait;
use ImportHandlerHelperTrait;
/**
* (non-PHPdoc)
* @see tao_models_classes_import_ImportHandler::getLabel()
*/
public function getLabel()
{
return __('RDF');
}
/**
* (non-PHPdoc)
* @see tao_models_classes_import_ImportHandler::getForm()
*/
public function getForm()
{
$form = new tao_models_classes_import_RdfImportForm();
return $form->getForm();
}
/**
* @param core_kernel_classes_Class $class
* @param tao_helpers_form_Form|array $form
* @param string|null $userId owner of the resource
* @return common_report_Report
* @throws common_Exception
* @throws common_exception_Error
*/
public function import($class, $form, $userId = null)
{
$parser = new tao_models_classes_Parser($this->fetchUploadedFile($form), ['extension' => 'rdf']);
$parser->validate();
if (!$parser->isValid()) {
$report = common_report_Report::createFailure(__('Nothing imported'));
$report->add($parser->getReport());
} else {
$report = $this->flatImport($parser->getContent(), $class);
if (!$report->containsError()) {
$this->getUploadService()->remove($parser->getSource());
}
if ($report->getType() == common_report_Report::TYPE_SUCCESS) {
$this->getEventManager()->trigger(new RdfImportEvent($report));
}
}
return $report;
}
/**
* Imports the rdf file into the selected class
*
* @param string $content
* @param core_kernel_classes_Class $class
* @return common_report_Report
*/
protected function flatImport($content, core_kernel_classes_Class $class)
{
$report = common_report_Report::createSuccess(__('Data imported successfully'));
$graph = new EasyRdf_Graph();
$graph->parse($content);
// keep type property
$map = [
OntologyRdf::RDF_PROPERTY => OntologyRdf::RDF_PROPERTY
];
foreach ($graph->resources() as $resource) {
$map[$resource->getUri()] = common_Utils::getNewUri();
}
$format = EasyRdf_Format::getFormat('php');
$data = $graph->serialise($format);
foreach ($data as $subjectUri => $propertiesValues) {
$resource = new core_kernel_classes_Resource($map[$subjectUri]);
$subreport = $this->importProperties($resource, $propertiesValues, $map, $class);
$report->add($subreport);
}
return $report;
}
/**
* Import the properties of the resource
*
* @param core_kernel_classes_Resource $resource
* @param array $propertiesValues
* @param array $map
* @param core_kernel_classes_Class $class
* @return common_report_Report
*/
protected function importProperties(core_kernel_classes_Resource $resource, $propertiesValues, $map, $class)
{
if (isset($propertiesValues[OntologyRdf::RDF_TYPE])) {
// assuming single Type
if (count($propertiesValues[OntologyRdf::RDF_TYPE]) > 1) {
return new common_report_Report(common_report_Report::TYPE_ERROR, __('Resource not imported due to multiple types'));
} else {
foreach ($propertiesValues[OntologyRdf::RDF_TYPE] as $k => $v) {
$classType = isset($map[$v['value']])
? new core_kernel_classes_Class($map[$v['value']])
: $class;
//$resource->setType($classType);
$classType->createInstance(null, null, $resource->getUri());
}
}
unset($propertiesValues[OntologyRdf::RDF_TYPE]);
}
if (isset($propertiesValues[OntologyRdfs::RDFS_SUBCLASSOF])) {
$resource = new core_kernel_classes_Class($resource);
// assuming single subclass
if (isset($propertiesValues[OntologyRdf::RDF_TYPE]) && count($propertiesValues[OntologyRdf::RDF_TYPE]) > 1) {
return new common_report_Report(common_report_Report::TYPE_ERROR, __('Resource not imported due to multiple super classes'));
}
foreach ($propertiesValues[OntologyRdfs::RDFS_SUBCLASSOF] as $k => $v) {
$classSup = isset($map[$v['value']])
? new core_kernel_classes_Class($map[$v['value']])
: $class;
$resource->setSubClassOf($classSup);
}
unset($propertiesValues[OntologyRdfs::RDFS_SUBCLASSOF]);
}
foreach ($propertiesValues as $prop => $values) {
$property = new core_kernel_classes_Property(isset($map[$prop]) ? $map[$prop] : $prop);
foreach ($values as $k => $v) {
$value = isset($map[$v['value']]) ? $map[$v['value']] : $v['value'];
if (isset($v['lang'])) {
$resource->setPropertyValueByLg($property, $value, $v['lang']);
} else {
$resource->setPropertyValue($property, $value);
}
}
}
$msg = $resource instanceof core_kernel_classes_Class
? __('Successfully imported class "%s"', $resource->getLabel())
: __('Successfully imported "%s"', $resource->getLabel());
return new common_report_Report(common_report_Report::TYPE_SUCCESS, $msg, $resource);
}
}