Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_export_RdfExporter | |
0.00% |
0 / 47 |
|
0.00% |
0 / 5 |
306 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExportForm | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| export | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| getRdfString | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
72 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | use oat\oatbox\event\EventManager; |
| 24 | use oat\oatbox\service\ServiceManager; |
| 25 | use oat\tao\model\event\RdfExportEvent; |
| 26 | use oat\oatbox\Configurable; |
| 27 | |
| 28 | /** |
| 29 | * The tao default rdf export |
| 30 | * |
| 31 | * @access public |
| 32 | * @author Joel Bout, <joel@taotesting.com> |
| 33 | * @package tao |
| 34 | */ |
| 35 | class tao_models_classes_export_RdfExporter extends Configurable implements tao_models_classes_export_ExportHandler |
| 36 | { |
| 37 | /** |
| 38 | * @return EventManager |
| 39 | */ |
| 40 | protected function getEventManager() |
| 41 | { |
| 42 | return ServiceManager::getServiceManager()->get(EventManager::SERVICE_ID); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @inheritdoc |
| 47 | */ |
| 48 | public function getLabel() |
| 49 | { |
| 50 | return __('RDF'); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @inheritdoc |
| 55 | */ |
| 56 | public function getExportForm(core_kernel_classes_Resource $resource) |
| 57 | { |
| 58 | if ($resource instanceof core_kernel_classes_Class) { |
| 59 | $formData = ['class' => $resource]; |
| 60 | } else { |
| 61 | $formData = ['instance' => $resource]; |
| 62 | } |
| 63 | |
| 64 | return (new tao_models_classes_export_RdfExportForm($formData)) |
| 65 | ->getForm(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Run the export process. |
| 70 | * |
| 71 | * @param array $formValues |
| 72 | * @param string $destination |
| 73 | * @return string |
| 74 | * @throws \EasyRdf\Exception |
| 75 | * @throws common_exception_Error |
| 76 | * @throws Exception |
| 77 | */ |
| 78 | public function export($formValues, $destination) |
| 79 | { |
| 80 | if (isset($formValues['filename'], $formValues['resource'])) { |
| 81 | $class = new core_kernel_classes_Class($formValues['resource']); |
| 82 | $adapter = new tao_helpers_data_GenerisAdapterRdf(); |
| 83 | $rdf = $adapter->export($class); |
| 84 | |
| 85 | if (!empty($rdf)) { |
| 86 | $name = $formValues['filename'] . '_' . time() . '.rdf'; |
| 87 | $path = tao_helpers_File::concat([$destination, $name]); |
| 88 | |
| 89 | if (!tao_helpers_File::securityCheck($path, true)) { |
| 90 | throw new Exception('Unauthorized file name'); |
| 91 | } |
| 92 | |
| 93 | if (file_put_contents($path, $rdf)) { |
| 94 | $this->getEventManager()->trigger(new RdfExportEvent($class)); |
| 95 | |
| 96 | return $path; |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return ''; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Exports an array of instances into an rdf string. |
| 106 | * |
| 107 | * @param array $instances |
| 108 | * @return string |
| 109 | */ |
| 110 | public function getRdfString($instances) |
| 111 | { |
| 112 | $api = core_kernel_impl_ApiModelOO::singleton(); |
| 113 | $rdf = ''; |
| 114 | $xmls = []; |
| 115 | foreach ($instances as $instance) { |
| 116 | $xmls[] = $api->getResourceDescriptionXML($instance->getUri()); |
| 117 | } |
| 118 | |
| 119 | if (count($xmls) === 1) { |
| 120 | $rdf = $xmls[0]; |
| 121 | } elseif (count($xmls) > 1) { |
| 122 | $baseDom = new DomDocument(); |
| 123 | $baseDom->formatOutput = true; |
| 124 | $baseDom->loadXML($xmls[0]); |
| 125 | |
| 126 | for ($i = 1, $iMax = count($xmls); $i < $iMax; $i++) { |
| 127 | $xmlDoc = new SimpleXMLElement($xmls[$i]); |
| 128 | foreach ($xmlDoc->getNamespaces() as $nsName => $nsUri) { |
| 129 | if (!$baseDom->documentElement->hasAttribute('xmlns:' . $nsName)) { |
| 130 | $baseDom->documentElement->setAttribute('xmlns:' . $nsName, $nsUri); |
| 131 | } |
| 132 | } |
| 133 | $newDom = new DOMDocument(); |
| 134 | $newDom->loadXML($xmls[$i]); |
| 135 | $descriptions = $newDom->getElementsByTagNameNS( |
| 136 | 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', |
| 137 | 'Description' |
| 138 | ); |
| 139 | |
| 140 | foreach ($descriptions as $desc) { |
| 141 | $newNode = $baseDom->importNode($desc, true); |
| 142 | $baseDom->documentElement->appendChild($newNode); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | $rdf = $baseDom->saveXML(); |
| 147 | } |
| 148 | |
| 149 | return $rdf; |
| 150 | } |
| 151 | } |