* @package tao */ class tao_helpers_translation_RDFExtractor extends tao_helpers_translation_TranslationExtractor { // --- ASSOCIATIONS --- // --- ATTRIBUTES --- /** * Short description of attribute translatableProperties * * @access private * @var array */ private $translatableProperties = []; /** * Short description of attribute xmlBase * * @access private * @var array */ private $xmlBase = []; // --- OPERATIONS --- /** * Short description of method extract * * @access public * @author Joel Bout, * @return mixed */ public function extract() { foreach ($this->getPaths() as $path) { // In the RDFExtractor, we expect the paths to points directly to the file. if (!file_exists($path)) { throw new tao_helpers_translation_TranslationException("No RDF file to parse at '${path}'."); } elseif (!is_readable($path)) { throw new tao_helpers_translation_TranslationException("'${path}' is not readable. Please check file system rights."); } else { try { $tus = []; $rdfNS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'; $rdfsNS = 'http://www.w3.org/2000/01/rdf-schema#'; $xmlNS = 'http://www.w3.org/XML/1998/namespace'; // http://www.w3.org/TR/REC-xml-names/#NT-NCName $translatableProperties = $this->translatableProperties; // Try to parse the file as a DOMDocument. $doc = new DOMDocument('1.0', 'UTF-8'); $doc->load(realpath($path)); if ($doc->documentElement->hasAttributeNS($xmlNS, 'base')) { $this->xmlBase[$path] = $doc->documentElement->getAttributeNodeNS($xmlNS, 'base')->value; } $descriptions = $doc->getElementsByTagNameNS($rdfNS, 'Description'); foreach ($descriptions as $description) { if ($description->hasAttributeNS($rdfNS, 'about')) { $about = $description->getAttributeNodeNS($rdfNS, 'about')->value; // At the moment only get rdfs:label and rdfs:comment // c.f. array $translatableProperties // In the future, this should be configured in the constructor // or by methods. $children = []; foreach ($translatableProperties as $prop) { $uri = explode('#', $prop); if (count($uri) == 2) { $uri[0] .= '#'; $nodeList = $description->getElementsByTagNameNS($uri[0], $uri[1]); for ($i = 0; $i < $nodeList->length; $i++) { $children[] = $nodeList->item($i); } } } foreach ($children as $child) { // Only process if it has a language attribute. $tus = $this->processUnit($child, $xmlNS, $about, $tus); } } else { // Description about nothing. continue; } } $this->setTranslationUnits($tus); } catch (DOMException $e) { throw new tao_helpers_translation_TranslationException("Unable to parse RDF file at '${path}'. DOM returns '" . $e->getMessage() . "'."); } } } } /** * Short description of method addTranslatableProperty * * @access public * @author Joel Bout, * @param string propertyUri * @return mixed */ public function addTranslatableProperty($propertyUri) { $this->translatableProperties[] = $propertyUri; } /** * Short description of method removeTranslatableProperty * * @access public * @author Joel Bout, * @param string propertyUri * @return mixed */ public function removeTranslatableProperty($propertyUri) { foreach ($this->translatableProperties as $prop) { if ($prop == $propertyUri) { unset($prop); } } } /** * Short description of method setTranslatableProperties * * @access public * @author Joel Bout, * @param array propertyUris * @return mixed */ public function setTranslatableProperties($propertyUris) { $this->translatableProperties = $propertyUris; } /** * Short description of method getXmlBase * * @access public * @author Joel Bout, * @param string path * @return string */ public function getXmlBase($path) { $returnValue = (string) ''; if (!isset($this->xmlBase[$path])) { throw new tao_helpers_translation_TranslationException('Missing xmlBase for file ' . $path); } $returnValue = $this->xmlBase[$path]; return (string) $returnValue; } /** * @param $child * @param $xmlNS * @param $about * @param $tus * @return array */ protected function processUnit($child, $xmlNS, $about, $tus) { if ($child->hasAttributeNS($xmlNS, 'lang')) { $sourceLanguage = 'en-US'; $targetLanguage = $child->getAttributeNodeNS($xmlNS, 'lang')->value; $source = $child->nodeValue; $target = $child->nodeValue; $tu = new tao_helpers_translation_RDFTranslationUnit(); $tu->setSource($source); $tu->setTarget($target); $tu->setSourceLanguage($sourceLanguage); $tu->setTargetLanguage($targetLanguage); $tu->setSubject($about); $tu->setPredicate($child->namespaceURI . $child->localName); $tus[] = $tu; return $tus; } return $tus; } }