Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_data_GenerisAdapterRdf | |
0.00% |
0 / 45 |
|
0.00% |
0 / 5 |
462 | |
0.00% |
0 / 1 |
| import | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
| export | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| addClass | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| addResource | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
| isSerializedFile | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * 2013-2017 (update and modification) Open Assessment Technologies SA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | use EasyRdf\Format; |
| 27 | use EasyRdf\Graph; |
| 28 | use oat\generis\model\OntologyRdf; |
| 29 | use oat\oatbox\service\ServiceManager; |
| 30 | use oat\tao\model\upload\UploadService; |
| 31 | use oat\oatbox\filesystem\File; |
| 32 | |
| 33 | /** |
| 34 | * Adapter for RDF/RDFS format |
| 35 | * |
| 36 | * @access public |
| 37 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 38 | * @package tao |
| 39 | */ |
| 40 | class tao_helpers_data_GenerisAdapterRdf extends tao_helpers_data_GenerisAdapter |
| 41 | { |
| 42 | /** |
| 43 | * Import a XML file as is into the ontology |
| 44 | * |
| 45 | * @access public |
| 46 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 47 | * @param string $source |
| 48 | * @param core_kernel_classes_Class $destination |
| 49 | * @param string $namespace |
| 50 | * @return boolean |
| 51 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 52 | * @throws \common_Exception |
| 53 | */ |
| 54 | public function import($source, core_kernel_classes_Class $destination = null, $namespace = null) |
| 55 | { |
| 56 | /** @var UploadService $uploadService */ |
| 57 | $uploadService = ServiceManager::getServiceManager()->get(UploadService::SERVICE_ID); |
| 58 | if (!$source instanceof File) { |
| 59 | $file = $uploadService->getUploadedFlyFile($source); |
| 60 | } else { |
| 61 | $file = $source; |
| 62 | } |
| 63 | |
| 64 | $returnValue = false; |
| 65 | |
| 66 | if ($file->exists()) { |
| 67 | $api = core_kernel_impl_ApiModelOO::singleton(); |
| 68 | if ($destination !== null) { |
| 69 | $targetNamespace = substr($destination->getUri(), 0, strpos($destination->getUri(), '#')); |
| 70 | } elseif ($namespace !== null) { |
| 71 | $targetNamespace = $namespace; |
| 72 | } else { |
| 73 | $targetNamespace = rtrim(common_ext_NamespaceManager::singleton()->getLocalNamespace()->getUri(), '#'); |
| 74 | } |
| 75 | $returnValue = $api->importXmlRdf($targetNamespace, $file); |
| 76 | } |
| 77 | |
| 78 | $uploadService->remove($file); |
| 79 | |
| 80 | return $returnValue; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Export to xml-rdf the ontology of the Class in parameter. |
| 85 | * All the ontologies are exported if the class is not set |
| 86 | * |
| 87 | * @access public |
| 88 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 89 | * @param core_kernel_classes_Class|null $source |
| 90 | * @return string |
| 91 | * @throws \EasyRdf\Exception |
| 92 | */ |
| 93 | public function export(core_kernel_classes_Class $source = null) |
| 94 | { |
| 95 | if ($source === null) { |
| 96 | return core_kernel_api_ModelExporter::exportAll(); |
| 97 | } |
| 98 | |
| 99 | $graph = new Graph(); |
| 100 | if ($source->isClass()) { |
| 101 | $this->addClass($graph, $source); |
| 102 | } else { |
| 103 | $this->addResource($graph, $source); |
| 104 | } |
| 105 | $format = Format::getFormat('rdfxml'); |
| 106 | return $graph->serialise($format); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add a class to the graph |
| 111 | * |
| 112 | * @param Graph $graph |
| 113 | * @param core_kernel_classes_Class $resource |
| 114 | * @ignore |
| 115 | */ |
| 116 | private function addClass(Graph $graph, core_kernel_classes_Class $resource) |
| 117 | { |
| 118 | $this->addResource($graph, $resource); |
| 119 | foreach ($resource->getInstances() as $instance) { |
| 120 | $this->addResource($graph, $instance); |
| 121 | } |
| 122 | foreach ($resource->getSubClasses() as $subclass) { |
| 123 | $this->addClass($graph, $subclass); |
| 124 | } |
| 125 | foreach ($resource->getProperties() as $property) { |
| 126 | $this->addResource($graph, $property); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Add a resource to the graph |
| 132 | * |
| 133 | * @param Graph $graph |
| 134 | * @param core_kernel_classes_Resource $resource |
| 135 | * @ignore |
| 136 | */ |
| 137 | private function addResource(Graph $graph, core_kernel_classes_Resource $resource) |
| 138 | { |
| 139 | foreach ($resource->getRdfTriples() as $triple) { |
| 140 | $language = !empty($triple->lg) ? $triple->lg : null; |
| 141 | if (common_Utils::isUri($triple->object)) { |
| 142 | if ( |
| 143 | $triple->predicate !== OntologyRdf::RDF_TYPE |
| 144 | && strpos($triple->object, LOCAL_NAMESPACE) !== false |
| 145 | ) { |
| 146 | continue; |
| 147 | } |
| 148 | $graph->addResource($triple->subject, $triple->predicate, $triple->object); |
| 149 | } else { |
| 150 | if ($this->isSerializedFile($triple->object)) { |
| 151 | continue; |
| 152 | } |
| 153 | $graph->addLiteral($triple->subject, $triple->predicate, $triple->object, $language); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Check if the given object is a serialized file reference |
| 160 | * |
| 161 | * @param string $object |
| 162 | * @return bool |
| 163 | * @see \oat\generis\model\fileReference\UrlFileSerializer::unserialize |
| 164 | */ |
| 165 | private function isSerializedFile($object) |
| 166 | { |
| 167 | $isFile = false; |
| 168 | $type = substr($object, 0, strpos($object, ':')); |
| 169 | if (in_array($type, ['file', 'dir'])) { |
| 170 | $isFile = true; |
| 171 | } |
| 172 | |
| 173 | return $isFile; |
| 174 | } |
| 175 | } |