Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| RdfPack | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| getIterator | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getTriplesFromFile | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
| 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 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 23 | */ |
| 24 | |
| 25 | namespace oat\tao\helpers\translation\rdf; |
| 26 | |
| 27 | use oat\generis\model\OntologyRdfs; |
| 28 | use tao_helpers_translation_POFileReader; |
| 29 | use common_Utils; |
| 30 | use core_kernel_classes_Triple; |
| 31 | use oat\generis\model\kernel\persistence\file\FileModel; |
| 32 | |
| 33 | /** |
| 34 | * Translation pack of the rdf resources |
| 35 | * |
| 36 | * @access public |
| 37 | * @author Joel |
| 38 | * @package tao |
| 39 | */ |
| 40 | class RdfPack implements \IteratorAggregate |
| 41 | { |
| 42 | /** |
| 43 | * @var string |
| 44 | */ |
| 45 | private $langCode; |
| 46 | |
| 47 | /** |
| 48 | * @var common_ext_Extension |
| 49 | */ |
| 50 | private $extension; |
| 51 | |
| 52 | /** |
| 53 | * Create a new bundle |
| 54 | * @param string $langCode |
| 55 | * @param common_ext_Extension |
| 56 | * @throws commone_exception_InvalidArgumentType |
| 57 | * @throws commone_exception_Errors |
| 58 | */ |
| 59 | public function __construct($langCode, \common_ext_Extension $extension) |
| 60 | { |
| 61 | if (!is_string($langCode)) { |
| 62 | throw new \common_exception_InvalidArgumentType(__CLASS__, __METHOD__, 0, 'string', $this); |
| 63 | } |
| 64 | if (empty($langCode) || empty($extension)) { |
| 65 | throw new \common_exception_Error('$langCode and $extensions needs to be assigned.'); |
| 66 | } |
| 67 | |
| 68 | $this->langCode = $langCode; |
| 69 | $this->extension = $extension; |
| 70 | } |
| 71 | |
| 72 | public function getIterator() |
| 73 | { |
| 74 | $iterator = new \AppendIterator(); |
| 75 | // english pack is always empty since in default rdfs |
| 76 | if ($this->langCode != 'en-US') { |
| 77 | foreach ($this->extension->getManifest()->getInstallModelFiles() as $rdfpath) { |
| 78 | $modelId = FileModel::getModelIdFromXml($rdfpath); |
| 79 | $candidate = $this->extension->getDir() . 'locales' . DIRECTORY_SEPARATOR . $this->langCode |
| 80 | . DIRECTORY_SEPARATOR . basename($rdfpath) . '.po'; |
| 81 | if (file_exists($candidate)) { |
| 82 | $iterator->append($this->getTriplesFromFile($candidate, $modelId)); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | return $iterator; |
| 87 | } |
| 88 | |
| 89 | protected function getTriplesFromFile($file, $modelId) |
| 90 | { |
| 91 | |
| 92 | $translationFileReader = new tao_helpers_translation_POFileReader($file); |
| 93 | $translationFileReader->read(); |
| 94 | $translationFile = $translationFileReader->getTranslationFile(); |
| 95 | /** @var tao_helpers_translation_POTranslationUnit $tu */ |
| 96 | $triples = []; |
| 97 | foreach ($translationFile->getTranslationUnits() as $tu) { |
| 98 | $annotations = $tu->getAnnotations(); |
| 99 | $about = isset($annotations['po-translator-comments']) ? $annotations['po-translator-comments'] : null; |
| 100 | if ( |
| 101 | $about |
| 102 | && common_Utils::isUri($about) |
| 103 | && in_array($tu->getContext(), [OntologyRdfs::RDFS_LABEL, OntologyRdfs::RDFS_COMMENT]) |
| 104 | ) { |
| 105 | $triple = new \core_kernel_classes_Triple(); |
| 106 | $triple->subject = $about; |
| 107 | $triple->predicate = $tu->getContext(); |
| 108 | $triple->object = $tu->getTarget() ? $tu->getTarget() : $tu->getSource(); |
| 109 | $triple->lg = $tu->getTargetLanguage(); |
| 110 | $triple->modelid = $modelId; |
| 111 | $triples[] = $triple; |
| 112 | } |
| 113 | } |
| 114 | return new \ArrayIterator($triples); |
| 115 | } |
| 116 | } |