Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_translation_RDFExtractor | |
0.00% |
0 / 61 |
|
0.00% |
0 / 6 |
462 | |
0.00% |
0 / 1 |
| extract | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
156 | |||
| addTranslatableProperty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| removeTranslatableProperty | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| setTranslatableProperties | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getXmlBase | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| processUnit | |
0.00% |
0 / 15 |
|
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 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * Short description of class tao_helpers_translation_RDFExtractor |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 30 | * @package tao |
| 31 | |
| 32 | */ |
| 33 | class tao_helpers_translation_RDFExtractor extends tao_helpers_translation_TranslationExtractor |
| 34 | { |
| 35 | // --- ASSOCIATIONS --- |
| 36 | |
| 37 | |
| 38 | // --- ATTRIBUTES --- |
| 39 | |
| 40 | /** |
| 41 | * Short description of attribute translatableProperties |
| 42 | * |
| 43 | * @access private |
| 44 | * @var array |
| 45 | */ |
| 46 | private $translatableProperties = []; |
| 47 | |
| 48 | /** |
| 49 | * Short description of attribute xmlBase |
| 50 | * |
| 51 | * @access private |
| 52 | * @var array |
| 53 | */ |
| 54 | private $xmlBase = []; |
| 55 | |
| 56 | // --- OPERATIONS --- |
| 57 | |
| 58 | /** |
| 59 | * Short description of method extract |
| 60 | * |
| 61 | * @access public |
| 62 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 63 | * @return mixed |
| 64 | */ |
| 65 | public function extract() |
| 66 | { |
| 67 | |
| 68 | foreach ($this->getPaths() as $path) { |
| 69 | // In the RDFExtractor, we expect the paths to points directly to the file. |
| 70 | if (!file_exists($path)) { |
| 71 | throw new tao_helpers_translation_TranslationException("No RDF file to parse at '${path}'."); |
| 72 | } elseif (!is_readable($path)) { |
| 73 | throw new tao_helpers_translation_TranslationException( |
| 74 | "'${path}' is not readable. Please check file system rights." |
| 75 | ); |
| 76 | } else { |
| 77 | try { |
| 78 | $tus = []; |
| 79 | $rdfNS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; |
| 80 | $rdfsNS = 'http://www.w3.org/2000/01/rdf-schema#'; |
| 81 | $xmlNS = 'http://www.w3.org/XML/1998/namespace'; // http://www.w3.org/TR/REC-xml-names/#NT-NCName |
| 82 | |
| 83 | $translatableProperties = $this->translatableProperties; |
| 84 | |
| 85 | // Try to parse the file as a DOMDocument. |
| 86 | $doc = new DOMDocument('1.0', 'UTF-8'); |
| 87 | $doc->load(realpath($path)); |
| 88 | if ($doc->documentElement->hasAttributeNS($xmlNS, 'base')) { |
| 89 | $this->xmlBase[$path] = $doc->documentElement->getAttributeNodeNS($xmlNS, 'base')->value; |
| 90 | } |
| 91 | |
| 92 | $descriptions = $doc->getElementsByTagNameNS($rdfNS, 'Description'); |
| 93 | foreach ($descriptions as $description) { |
| 94 | if ($description->hasAttributeNS($rdfNS, 'about')) { |
| 95 | $about = $description->getAttributeNodeNS($rdfNS, 'about')->value; |
| 96 | |
| 97 | // At the moment only get rdfs:label and rdfs:comment |
| 98 | // c.f. array $translatableProperties |
| 99 | // In the future, this should be configured in the constructor |
| 100 | // or by methods. |
| 101 | $children = []; |
| 102 | foreach ($translatableProperties as $prop) { |
| 103 | $uri = explode('#', $prop); |
| 104 | if (count($uri) == 2) { |
| 105 | $uri[0] .= '#'; |
| 106 | $nodeList = $description->getElementsByTagNameNS($uri[0], $uri[1]); |
| 107 | |
| 108 | for ($i = 0; $i < $nodeList->length; $i++) { |
| 109 | $children[] = $nodeList->item($i); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | foreach ($children as $child) { |
| 115 | // Only process if it has a language attribute. |
| 116 | $tus = $this->processUnit($child, $xmlNS, $about, $tus); |
| 117 | } |
| 118 | } else { |
| 119 | // Description about nothing. |
| 120 | continue; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $this->setTranslationUnits($tus); |
| 125 | } catch (DOMException $e) { |
| 126 | throw new tao_helpers_translation_TranslationException( |
| 127 | "Unable to parse RDF file at '${path}'. DOM returns '" . $e->getMessage() . "'." |
| 128 | ); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Short description of method addTranslatableProperty |
| 136 | * |
| 137 | * @access public |
| 138 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 139 | * @param string propertyUri |
| 140 | * @return mixed |
| 141 | */ |
| 142 | public function addTranslatableProperty($propertyUri) |
| 143 | { |
| 144 | |
| 145 | $this->translatableProperties[] = $propertyUri; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Short description of method removeTranslatableProperty |
| 150 | * |
| 151 | * @access public |
| 152 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 153 | * @param string propertyUri |
| 154 | * @return mixed |
| 155 | */ |
| 156 | public function removeTranslatableProperty($propertyUri) |
| 157 | { |
| 158 | |
| 159 | foreach ($this->translatableProperties as $prop) { |
| 160 | if ($prop == $propertyUri) { |
| 161 | unset($prop); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Short description of method setTranslatableProperties |
| 168 | * |
| 169 | * @access public |
| 170 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 171 | * @param array propertyUris |
| 172 | * @return mixed |
| 173 | */ |
| 174 | public function setTranslatableProperties($propertyUris) |
| 175 | { |
| 176 | |
| 177 | $this->translatableProperties = $propertyUris; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Short description of method getXmlBase |
| 182 | * |
| 183 | * @access public |
| 184 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 185 | * @param string path |
| 186 | * @return string |
| 187 | */ |
| 188 | public function getXmlBase($path) |
| 189 | { |
| 190 | $returnValue = (string) ''; |
| 191 | |
| 192 | |
| 193 | if (!isset($this->xmlBase[$path])) { |
| 194 | throw new tao_helpers_translation_TranslationException('Missing xmlBase for file ' . $path); |
| 195 | } |
| 196 | $returnValue = $this->xmlBase[$path]; |
| 197 | |
| 198 | |
| 199 | return (string) $returnValue; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param $child |
| 204 | * @param $xmlNS |
| 205 | * @param $about |
| 206 | * @param $tus |
| 207 | * @return array |
| 208 | */ |
| 209 | protected function processUnit($child, $xmlNS, $about, $tus) |
| 210 | { |
| 211 | if ($child->hasAttributeNS($xmlNS, 'lang')) { |
| 212 | $sourceLanguage = 'en-US'; |
| 213 | $targetLanguage = $child->getAttributeNodeNS($xmlNS, 'lang')->value; |
| 214 | $source = $child->nodeValue; |
| 215 | $target = $child->nodeValue; |
| 216 | |
| 217 | $tu = new tao_helpers_translation_RDFTranslationUnit(); |
| 218 | $tu->setSource($source); |
| 219 | $tu->setTarget($target); |
| 220 | $tu->setSourceLanguage($sourceLanguage); |
| 221 | $tu->setTargetLanguage($targetLanguage); |
| 222 | $tu->setSubject($about); |
| 223 | $tu->setPredicate($child->namespaceURI . $child->localName); |
| 224 | |
| 225 | $tus[] = $tu; |
| 226 | return $tus; |
| 227 | } |
| 228 | return $tus; |
| 229 | } |
| 230 | } |