Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_api_ModelExporter | |
0.00% |
0 / 24 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| exportAll | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| exportModels | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| exportModelByUri | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| statement2rdf | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
30 | |||
| getOntology | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | use EasyRdf\Format; |
| 4 | use EasyRdf\Graph; |
| 5 | use oat\generis\model\data\ModelManager; |
| 6 | |
| 7 | /** |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; under version 2 |
| 11 | * of the License (non-upgradable). |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | * |
| 22 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 23 | * |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | class core_kernel_api_ModelExporter |
| 28 | { |
| 29 | private const DEFAULT_FORMAT = 'rdfxml'; |
| 30 | |
| 31 | /** |
| 32 | * Export the entire ontology |
| 33 | * |
| 34 | * @param string $format (optional) which format resulted ontology will be. |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | public static function exportAll($format = self::DEFAULT_FORMAT) |
| 39 | { |
| 40 | $dbWrapper = core_kernel_classes_DbWrapper::singleton(); |
| 41 | $result = $dbWrapper->query('SELECT DISTINCT "subject", "predicate", "object", "l_language" FROM "statements"'); |
| 42 | return self::statement2rdf($result, $format); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Export models by id |
| 47 | * |
| 48 | * @param array $modelIds |
| 49 | * @param string $format (optional) which format resulted models ontology will be. |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public static function exportModels($modelIds, $format = self::DEFAULT_FORMAT) |
| 54 | { |
| 55 | $dbWrapper = core_kernel_classes_DbWrapper::singleton(); |
| 56 | $result = $dbWrapper->query('SELECT DISTINCT "subject", "predicate", "object", "l_language" FROM "statements" |
| 57 | WHERE "modelid" IN (\'' . implode('\',\'', $modelIds) . '\')'); |
| 58 | |
| 59 | common_Logger::i('Found ' . $result->rowCount() . ' entries for models ' . implode(',', $modelIds)); |
| 60 | return self::statement2rdf($result, $format); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return string |
| 65 | */ |
| 66 | public static function exportModelByUri() |
| 67 | { |
| 68 | return self::exportModels( |
| 69 | self::getOntology()->getReadableModels() |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @throws \EasyRdf\Exception |
| 75 | * @ignore |
| 76 | */ |
| 77 | private static function statement2rdf($statement, $format = self::DEFAULT_FORMAT) |
| 78 | { |
| 79 | $graph = new Graph(); |
| 80 | while ($r = $statement->fetch()) { |
| 81 | if (isset($r['l_language']) && !empty($r['l_language'])) { |
| 82 | $graph->addLiteral($r['subject'], $r['predicate'], $r['object'], $r['l_language']); |
| 83 | } elseif (common_Utils::isUri($r['object'])) { |
| 84 | $graph->addResource($r['subject'], $r['predicate'], $r['object']); |
| 85 | } else { |
| 86 | $graph->addLiteral($r['subject'], $r['predicate'], $r['object']); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | $format = Format::getFormat($format); |
| 91 | |
| 92 | return $graph->serialise($format); |
| 93 | } |
| 94 | |
| 95 | private static function getOntology(): core_kernel_persistence_smoothsql_SmoothModel |
| 96 | { |
| 97 | return ModelManager::getModel(); |
| 98 | } |
| 99 | } |