/** * @since 2.5 * * {@inheritDoc} */ public function addResourceValue(ExpData $expData, DIProperty $property, DataItem $dataItem) { $diSubject = $expData->getSubject()->getDataItem(); if ($diSubject === null) { return; } $expNsResource = $this->exporter->getSpecialPropertyResource($property->getKey(), $diSubject->getNamespace()); $expElement = $this->exporter->getDataItemExpElement($dataItem); if ($expElement === null || $expNsResource === null) { return; } $expData->addPropertyObjectValue($expNsResource, $expElement); $this->addResourceHelperValue($expData, $property, $dataItem); }
/** * @since 2.5 * * {@inheritDoc} */ public function addResourceValue(ExpData $expData, DIProperty $property, DataItem $dataItem) { $expElement = $this->exporter->getDataItemExpElement($dataItem); if ($expData->getSubject()->getUri() === '' || $expElement === null) { return; } foreach ($expElement->getProperties() as $subp) { if ($subp->getUri() != $this->exporter->getSpecialNsResource('rdf', 'type')->getUri()) { foreach ($expElement->getValues($subp) as $subval) { $expData->addPropertyObjectValue($subp, $subval); } } } $this->addResourceHelperValue($expData, $property, $dataItem); }
/** * @since 2.5 * * {@inheritDoc} */ public function addResourceValue(ExpData $expData, DIProperty $property, DataItem $dataItem) { $diSubject = $expData->getSubject()->getDataItem(); if ($diSubject === null) { return; } $expNsResource = $this->exporter->getSpecialPropertyResource($property->getKey(), $diSubject->getNamespace()); if ($expNsResource === null) { return; } $dataValue = DataValueFactory::getInstance()->newDataValueByItem($dataItem, $property); if (!$dataValue instanceof ImportValue) { return; } $expData->addPropertyObjectValue($expNsResource, $this->exporter->getDataItemExpElement(new DIBlob($dataValue->getImportReference()))); $this->addResourceHelperValue($expData, $property, $dataItem); }
/** * Extend a given SMWExpData element by adding export data for the * specified property data itme. This method is called when * constructing export data structures from SMWSemanticData objects. * * @param $property SMWDIProperty * @param $dataItems array of SMWDataItem objects for the given property * @param $data SMWExpData to add the data to */ public static function addPropertyValues(SMWDIProperty $property, array $dataItems, SMWExpData &$expData) { if ($property->isUserDefined()) { $pe = self::getResourceElementForProperty($property); $peHelper = self::getResourceElementForProperty($property, true); foreach ($dataItems as $dataItem) { $ed = self::getDataItemExpElement($dataItem); if (!is_null($ed)) { $expData->addPropertyObjectValue($pe, $ed); } $edHelper = self::getDataItemHelperExpElement($dataItem); if (!is_null($edHelper)) { $expData->addPropertyObjectValue($peHelper, $edHelper); } } } else { // pre-defined property, only exported if known $diSubject = $expData->getSubject()->getDataItem(); // subject wikipage required for disambiguating special properties: if (is_null($diSubject) || $diSubject->getDIType() != SMWDataItem::TYPE_WIKIPAGE) { return; } $pe = self::getSpecialPropertyResource($property->getKey(), $diSubject->getNamespace()); if (is_null($pe)) { return; } // unknown special property, not exported // have helper property ready before entering the for loop, even if not needed: $peHelper = self::getResourceElementForProperty($property, true); $filterNamespace = $property->getKey() == '_REDI' || $property->getKey() == '_URI'; foreach ($dataItems as $dataItem) { // Basic namespace filtering to ensure that types match for redirects etc. /// TODO: currently no full check for avoiding OWL DL illegal redirects is done (OWL property type ignored) if ($filterNamespace && !$dataItem instanceof SMWDIUri && (!$dataItem instanceof SMWDIWikiPage || $dataItem->getNamespace() != $diSubject->getNamespace())) { continue; } $ed = self::getDataItemExpElement($dataItem); if (!is_null($ed)) { if ($property->getKey() == '_CONC' && $ed->getSubject()->getUri() === '') { // equivalent to anonymous class -> simplify description foreach ($ed->getProperties() as $subp) { if ($subp->getUri() != self::getSpecialNsResource('rdf', 'type')->getUri()) { foreach ($ed->getValues($subp) as $subval) { $expData->addPropertyObjectValue($subp, $subval); } } } } elseif ($property->getKey() == '_REDI') { $expData->addPropertyObjectValue($pe, $ed); $peUri = self::getSpecialPropertyResource('_URI'); $expData->addPropertyObjectValue($peUri, $ed); } else { $expData->addPropertyObjectValue($pe, $ed); } } $edHelper = self::getDataItemHelperExpElement($dataItem); if (!is_null($edHelper)) { $expData->addPropertyObjectValue($peHelper, $edHelper); } } } }
/** * Serialize the given SMWExpData object, possibly recursively with * increased indentation. * * @param $data SMWExpData containing the data to be serialised. * @param $indent string specifying a prefix for indentation (usually a sequence of tabs) */ protected function serializeNestedExpData(SMWExpData $data, $indent) { if (count($data->getProperties()) == 0) { return; // nothing to export } // Avoid posting turtle property declarations already known for the // subject more than once if ($data->getSubject()->getDataItem() !== null && $data->getSubject()->getDataItem()->getNamespace() === SMW_NS_PROPERTY) { $hash = $data->getHash(); $poolCache = InMemoryPoolCache::getInstance()->getPoolCacheFor('turtle.serializer'); if ($poolCache->contains($hash) && $poolCache->fetch($hash)) { return; } $poolCache->save($hash, true); } $this->recordDeclarationTypes($data); $bnode = false; $this->post_ns_buffer .= $indent; if (!$data->getSubject()->isBlankNode()) { $this->serializeExpResource($data->getSubject()); } else { // blank node $bnode = true; $this->post_ns_buffer .= "["; } if ($indent !== '' && !$bnode) { // called to generate a nested descripion; but Turtle cannot nest non-bnode descriptions, do this later $this->subexpdata[] = $data; return; } elseif (!$bnode) { $this->post_ns_buffer .= "\n "; } $firstproperty = true; foreach ($data->getProperties() as $property) { $this->post_ns_buffer .= $firstproperty ? "\t" : " ;\n {$indent}\t"; $firstproperty = false; $prop_decl_queued = false; $class_type_prop = $this->isOWLClassTypeProperty($property); $this->serializeExpResource($property); $firstvalue = true; foreach ($data->getValues($property) as $value) { $this->post_ns_buffer .= $firstvalue ? ' ' : ' , '; $firstvalue = false; if ($value instanceof SMWExpLiteral) { $prop_decl_type = SMW_SERIALIZER_DECL_APROP; $this->serializeExpLiteral($value); } elseif ($value instanceof SMWExpResource) { $prop_decl_type = SMW_SERIALIZER_DECL_OPROP; $this->serializeExpResource($value); } elseif ($value instanceof SMWExpData) { // resource (maybe blank node), could have subdescriptions $prop_decl_type = SMW_SERIALIZER_DECL_OPROP; $collection = $value->getCollection(); if ($collection !== false) { // RDF-style collection (list) $this->post_ns_buffer .= "( "; foreach ($collection as $subvalue) { $this->serializeNestedExpData($subvalue, $indent . "\t\t"); if ($class_type_prop) { $this->requireDeclaration($subvalue->getSubject(), SMW_SERIALIZER_DECL_CLASS); } } $this->post_ns_buffer .= " )"; } else { if ($class_type_prop) { $this->requireDeclaration($value->getSubject(), SMW_SERIALIZER_DECL_CLASS); } if (count($value->getProperties()) > 0) { // resource with data: serialise $this->post_ns_buffer .= "\n"; $this->serializeNestedExpData($value, $indent . "\t\t"); } else { // resource without data: may need to be queued $this->serializeExpResource($value->getSubject()); } } } if (!$prop_decl_queued) { $this->requireDeclaration($property, $prop_decl_type); $prop_decl_queued = true; } } } $this->post_ns_buffer .= ($bnode ? " ]" : " .") . ($indent === '' ? "\n\n" : ''); }
/** * Find a normalized representation of the given SMWExpData that can * be used in an update of the stored data. Normalization uses * redirects. * Moreover, the method records any auxiliary data that should be * written to the store when including this SMWExpElement into updates. * This auxiliary data is collected in a call-by-ref array. * * @since 1.6 * @param $expData SMWExpData object containing the update data * @param $auxiliaryExpData array of SMWExpData * @param $expandSubject boolean controls if redirects/auxiliary data should also be sought for subject * @return SMWExpData */ protected function expandUpdateExpData(SMWExpData $expData, array &$auxiliaryExpData, $expandSubject) { $subjectExpResource = $expData->getSubject(); if ($expandSubject) { $expandedExpElement = $this->expandUpdateExpElement($subjectExpResource, $auxiliaryExpData); if ($expandedExpElement instanceof SMWExpData) { $newExpData = $expandedExpElement; } else { // instanceof SMWExpResource $newExpData = new SMWExpData($subjectExpResource); } } else { $newExpData = new SMWExpData($subjectExpResource); } foreach ($expData->getProperties() as $propertyResource) { $propertyTarget = $this->expandUpdateExpElement($propertyResource, $auxiliaryExpData); foreach ($expData->getValues($propertyResource) as $expElement) { $elementTarget = $this->expandUpdateExpElement($expElement, $auxiliaryExpData); $newExpData->addPropertyObjectValue($propertyTarget, $elementTarget); } } return $newExpData; }
/** * Update the declaration "todo" and "done" lists for the case that the * given data has been serialized with the type information it provides. * * @param $expData specifying the type data upon which declarations are based */ protected function recordDeclarationTypes(SMWExpData $expData) { foreach ($expData->getSpecialValues('rdf', 'type') as $typeresource) { if ($typeresource instanceof SMWExpNsResource) { switch ($typeresource->getQName()) { case 'owl:Class': $typeflag = SMW_SERIALIZER_DECL_CLASS; break; case 'owl:ObjectProperty': $typeflag = SMW_SERIALIZER_DECL_OPROP; break; case 'owl:DatatypeProperty': $typeflag = SMW_SERIALIZER_DECL_APROP; break; default: $typeflag = 0; } if ($typeflag != 0) { $this->declarationDone($expData->getSubject(), $typeflag); } } } }
/** * Extend a given SMWExpData element by adding export data for the * specified property data itme. This method is called when * constructing export data structures from SMWSemanticData objects. * * @param $property SMWDIProperty * @param $dataItems array of SMWDataItem objects for the given property * @param $data SMWExpData to add the data to */ static public function addPropertyValues( SMWDIProperty $property, array $dataItems, SMWExpData &$expData ) { if ( $property->isUserDefined() ) { $pe = self::getResourceElementForProperty( $property ); $peHelper = self::getResourceElementForProperty( $property, true ); foreach ( $dataItems as $dataItem ) { $ed = self::getDataItemExpElement( $dataItem ); if ( !is_null( $ed ) ) { $expData->addPropertyObjectValue( $pe, $ed ); } $edHelper = self::getDataItemHelperExpElement( $dataItem ); if ( !is_null( $edHelper ) ) { $expData->addPropertyObjectValue( $peHelper, $edHelper ); } } } else { // pre-defined property, only exported if known $diSubject = $expData->getSubject()->getDataItem(); if ( ( $diSubject == null ) || ( $diSubject->getDIType() != SMWDataItem::TYPE_WIKIPAGE ) ) { return; // subject datavalue (wikipage) required for treating special properties properly } $pe = self::getSpecialPropertyResource( $property->getKey(), $diSubject->getNamespace() ); if ( is_null( $pe ) ) return; // unknown special property, not exported if ( $property->getKey() == '_REDI' || $property->getKey() == '_URI' ) { $filterNamespace = true; if ( $property->getKey() == '_REDI' ) { $pe = array( $pe, self::getSpecialPropertyResource( '_URI' ) ); } } else { $filterNamespace = false; } foreach ( $dataItems as $dataItem ) { // Basic namespace filtering to ensure that types match for redirects etc. /// TODO: currently no full check for avoiding OWL DL illegal redirects is done (OWL property type ignored) if ( $filterNamespace && !( $dataItem instanceof SMWDIUri ) && ( !( $dataItem instanceof SMWDIWikiPage ) || ( $dataItem->getNamespace() != $diSubject->getNamespace() ) ) ) { continue; } $ed = self::getDataItemExpElement( $dataItem ); if ( !is_null( $ed ) ) { if ( ( $property->getKey() == '_CONC' ) && ( $ed->getSubject()->getUri() === '' ) ) { // equivalent to anonymous class -> simplify description foreach ( $ed->getProperties() as $subp ) { if ( $subp->getUri() != self::getSpecialNsResource( 'rdf', 'type' )->getUri() ) { foreach ( $ed->getValues( $subp ) as $subval ) { $expData->addPropertyObjectValue( $subp, $subval ); } } } } elseif ( is_array( $pe ) ) { foreach ( $pe as $extraPropertyElement ) { $expData->addPropertyObjectValue( $extraPropertyElement, $ed ); } } else { $expData->addPropertyObjectValue( $pe, $ed ); } } } } }