public function categoryProvider() { $stringBuilder = UtilityFactory::getInstance()->newStringBuilder(); # 0 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\FalseCondition'; $description = new ClassDescription(array()); $orderByProperty = null; $expected = $stringBuilder->addString('<http://www.example.org> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#nothing> .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $conditionType, $expected); # 1 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $category = new DIWikiPage('Foo', NS_CATEGORY); $categoryName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($category)); $description = new ClassDescription($category); $orderByProperty = null; $expected = $stringBuilder->addString("{ ?result rdf:type {$categoryName} . }")->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $conditionType, $expected); # 2 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $categoryFoo = new DIWikiPage('Foo', NS_CATEGORY); $categoryFooName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($categoryFoo)); $categoryBar = new DIWikiPage('Bar', NS_CATEGORY); $categoryBarName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($categoryBar)); $description = new ClassDescription(array($categoryFoo, $categoryBar)); $orderByProperty = null; $expected = $stringBuilder->addString("{ ?result rdf:type {$categoryFooName} . }")->addNewLine()->addString('UNION')->addNewLine()->addString("{ ?result rdf:type {$categoryBarName} . }")->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $conditionType, $expected); # 3 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $description = new ClassDescription(array($categoryFoo, $categoryBar)); $orderByProperty = new DIProperty('Foo'); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString("{ ?result rdf:type {$categoryFooName} . }")->addNewLine()->addString('UNION')->addNewLine()->addString("{ ?result rdf:type {$categoryBarName} . }")->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $conditionType, $expected); return $provider; }
/** * Create an Condition from a NamespaceDescription * * @param NamespaceDescription $description * @param string $joinVariable * @param DIProperty|null $orderByProperty * * @return Condition */ public function buildCondition(Description $description, $joinVariable, $orderByProperty = null) { $nspropExpElement = $this->exporter->getSpecialNsResource('swivt', 'wikiNamespace'); $nsExpElement = new ExpLiteral(strval($description->getNamespace()), 'http://www.w3.org/2001/XMLSchema#integer'); $nsName = TurtleSerializer::getTurtleNameForExpElement($nsExpElement); $condition = "{ ?{$joinVariable} " . $nspropExpElement->getQName() . " {$nsName} . }\n"; $result = new WhereCondition($condition, true, array()); $this->compoundConditionBuilder->addOrderByDataForProperty($result, $joinVariable, $orderByProperty, DataItem::TYPE_WIKIPAGE); return $result; }
/** * Complex SPARQL Update delete operations are not supported in 4Store * as of v1.1.3, hence this implementation uses a less efficient method * for accomplishing this. * * @param $propertyName string Turtle name of marking property * @param $objectName string Turtle name of marking object/value * @param $extraNamespaces array (associative) of namespaceId => namespaceUri * @return boolean stating whether the operations succeeded */ public function deleteContentByValue( $propertyName, $objectName, $extraNamespaces = array() ) { $affectedObjects = $this->select( '*', "?s $propertyName $objectName", array(), $extraNamespaces ); $success = ( $affectedObjects->getErrorCode() == SMWSparqlResultWrapper::ERROR_NOERROR ); foreach ( $affectedObjects as $expElements ) { if ( count( $expElements ) > 0 ) { $turtleName = SMWTurtleSerializer::getTurtleNameForExpElement( reset( $expElements ) ); $success = $this->delete( "$turtleName ?p ?o", "$turtleName ?p ?o", $extraNamespaces ) && $success; } } return $success; }
/** * Complex SPARQL Update delete operations are not supported in 4Store * as of v1.1.3, hence this implementation uses a less efficient method * for accomplishing this. * * @param $propertyName string Turtle name of marking property * @param $objectName string Turtle name of marking object/value * @param $extraNamespaces array (associative) of namespaceId => namespaceUri * @return boolean stating whether the operations succeeded */ public function deleteContentByValue($propertyName, $objectName, $extraNamespaces = array()) { $affectedObjects = $this->select('*', "?s {$propertyName} {$objectName}", array(), $extraNamespaces); $success = $affectedObjects->getErrorCode() == RepositoryResult::ERROR_NOERROR; foreach ($affectedObjects as $expElements) { if (count($expElements) > 0) { $turtleName = TurtleSerializer::getTurtleNameForExpElement(reset($expElements)); $success = $this->delete("{$turtleName} ?p ?o", "{$turtleName} ?p ?o", $extraNamespaces) && $success; } } return $success; }
public function testDeleteSubjectOnMockBaseStore() { $title = Title::newFromText('DeleteSubjectOnMockBaseStore'); $expResource = Exporter::getInstance()->getDataItemExpElement(DIWikiPage::newFromTitle($title)); $resourceUri = TurtleSerializer::getTurtleNameForExpElement($expResource); $extraNamespaces = array($expResource->getNamespaceId() => $expResource->getNamespace()); $baseStore = $this->getMockBuilder('\\SMWStore')->disableOriginalConstructor()->getMockForAbstractClass(); $baseStore->expects($this->once())->method('deleteSubject')->with($this->equalTo($title))->will($this->returnValue(true)); $sparqlDatabase = $this->getMockBuilder('\\SMWSparqlDatabase')->disableOriginalConstructor()->getMock(); $sparqlDatabase->expects($this->once())->method('deleteContentByValue')->will($this->returnValue(true)); $sparqlDatabase->expects($this->once())->method('delete')->with($this->equalTo("{$resourceUri} ?p ?o"), $this->equalTo("{$resourceUri} ?p ?o"), $this->equalTo($extraNamespaces))->will($this->returnValue(true)); $connectionManager = $this->getMockBuilder('\\SMW\\ConnectionManager')->disableOriginalConstructor()->getMock(); $connectionManager->expects($this->any())->method('getConnection')->will($this->returnValue($sparqlDatabase)); $instance = new SPARQLStore($baseStore); $instance->setConnectionManager($connectionManager); $instance->deleteSubject($title); }
private function mapCategoriesToConditionElements(array $categories, $joinVariable) { $condition = ''; $namespaces = array(); $instExpElement = $this->exporter->getSpecialPropertyResource('_INST'); foreach ($categories as $category) { $categoryExpElement = $this->exporter->getResourceElementForWikiPage($category); $categoryName = TurtleSerializer::getTurtleNameForExpElement($categoryExpElement); $namespaces[$categoryExpElement->getNamespaceId()] = $categoryExpElement->getNamespace(); $newcondition = "{ ?{$joinVariable} " . $instExpElement->getQName() . " {$categoryName} . }\n"; if ($condition === '') { $condition = $newcondition; } else { $condition .= "UNION\n{$newcondition}"; } } return array($condition, $namespaces); }
private function findMostSuitablePropertyRepresentation(DIProperty $property, DIProperty $nonInverseProperty, &$namespaces) { $redirectByVariable = $this->compoundConditionBuilder->tryToFindRedirectVariableForDataItem($nonInverseProperty->getDiWikiPage()); // If the property is represented by a redirect then use the variable instead if ($redirectByVariable !== null) { return $redirectByVariable; } // Use helper properties in encoding values, refer to this helper property: if ($this->exporter->hasHelperExpElement($property)) { $propertyExpElement = $this->exporter->getResourceElementForProperty($nonInverseProperty, true); } elseif (!$property->isUserDefined()) { $propertyExpElement = $this->exporter->getSpecialPropertyResource($nonInverseProperty->getKey(), SMW_NS_PROPERTY); } else { $propertyExpElement = $this->exporter->getResourceElementForProperty($nonInverseProperty); } if ($propertyExpElement instanceof ExpNsResource) { $namespaces[$propertyExpElement->getNamespaceId()] = $propertyExpElement->getNamespace(); } return TurtleSerializer::getTurtleNameForExpElement($propertyExpElement); }
private function getPropertyNameByUsingTurtleSerializer(DIProperty $property, DIProperty $nonInverseProperty, &$namespaces) { // Use helper properties in encoding values, refer to this helper property: if ($this->exporter->hasHelperExpElement($property)) { $propertyExpElement = $this->exporter->getResourceElementForProperty($nonInverseProperty, true); } else { $propertyExpElement = $this->exporter->getResourceElementForProperty($nonInverseProperty); } if ($propertyExpElement instanceof ExpNsResource) { $namespaces[$propertyExpElement->getNamespaceId()] = $propertyExpElement->getNamespace(); } return TurtleSerializer::getTurtleNameForExpElement($propertyExpElement); }
/** * Create an SMWSparqlCondition from an SMWValueDescription. * * @param $description SMWValueDescription * @param $joinVariable string name, see buildSparqlCondition() * @param $orderByProperty mixed SMWDIProperty or null, see buildSparqlCondition() * @return SMWSparqlCondition */ protected function buildValueCondition(SMWValueDescription $description, $joinVariable, $orderByProperty) { $dataItem = $description->getDataItem(); switch ($description->getComparator()) { case SMW_CMP_EQ: $comparator = '='; break; case SMW_CMP_LESS: $comparator = '<'; break; case SMW_CMP_GRTR: $comparator = '>'; break; case SMW_CMP_LEQ: $comparator = '<='; break; case SMW_CMP_GEQ: $comparator = '>='; break; case SMW_CMP_NEQ: $comparator = '!='; break; case SMW_CMP_LIKE: $comparator = 'regex'; break; case SMW_CMP_NLKE: $comparator = '!regex'; break; default: $comparator = ''; // unkown, unsupported } if ($comparator === '') { $result = $this->buildTrueCondition($joinVariable, $orderByProperty); } elseif ($comparator == '=') { $expElement = SMWExporter::getDataItemHelperExpElement($dataItem); if (is_null($expElement)) { $expElement = SMWExporter::getDataItemExpElement($dataItem); } $result = new SMWSparqlSingletonCondition($expElement); $this->addOrderByDataForProperty($result, $joinVariable, $orderByProperty, $dataItem->getDIType()); } elseif ($comparator == 'regex' || $comparator == '!regex') { if ($dataItem instanceof SMWDIBlob) { $pattern = '^' . str_replace(array('^', '.', '\\', '+', '{', '}', '(', ')', '|', '^', '$', '[', ']', '*', '?'), array('\\^', '\\.', '\\\\', '\\+', '\\{', '\\}', '\\(', '\\)', '\\|', '\\^', '\\$', '\\[', '\\]', '.*', '.'), $dataItem->getString()) . '$'; $result = new SMWSparqlFilterCondition("{$comparator}( ?{$joinVariable}, \"{$pattern}\", \"s\")", array()); $this->addOrderByDataForProperty($result, $joinVariable, $orderByProperty, $dataItem->getDIType()); } else { $result = $this->buildTrueCondition($joinVariable, $orderByProperty); } } else { $result = new SMWSparqlFilterCondition('', array()); $this->addOrderByData($result, $joinVariable, $dataItem->getDIType()); $orderByVariable = $result->orderByVariable; if ($dataItem instanceof SMWDIWikiPage) { $expElement = SMWExporter::getDataItemExpElement($dataItem->getSortKeyDataItem()); } else { $expElement = SMWExporter::getDataItemHelperExpElement($dataItem); if (is_null($expElement)) { $expElement = SMWExporter::getDataItemExpElement($dataItem); } } $valueName = SMWTurtleSerializer::getTurtleNameForExpElement($expElement); if ($expElement instanceof SMWExpNsResource) { $result->namespaces[$expElement->getNamespaceId()] = $expElement->getNamespace(); } $result->filter = "?{$orderByVariable} {$comparator} {$valueName}"; } return $result; }
public function descriptionProvider() { $stringBuilder = UtilityFactory::getInstance()->newStringBuilder(); # 0 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\TrueCondition'; $description = new Conjunction(); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:page ?url .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 1 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\FalseCondition'; $description = new Conjunction(array(new ValueDescription(new DIWikiPage('Bar', NS_MAIN)))); $description = new Conjunction(array(new ValueDescription(new DIWikiPage('Foo', NS_MAIN)), $description)); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('<http://www.example.org> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#nothing> .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 2 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\TrueCondition'; $description = new Conjunction(array(new ThingDescription())); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:page ?url .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 3 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $description = new SomeProperty(new DIProperty('Foo'), new ThingDescription()); $description = new Conjunction(array($description, new NamespaceDescription(NS_HELP))); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result property:Foo ?v1 .')->addNewLine()->addString('{ ?result swivt:wikiNamespace "12"^^xsd:integer . }')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 4 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\SingletonCondition'; $description = new Conjunction(array(new NamespaceDescription(NS_MAIN), new ValueDescription(new DIWikiPage('SomePageValue', NS_MAIN)))); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('{ wiki:SomePageValue swivt:wikiNamespace "0"^^xsd:integer . }')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 5 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $description = new ValueDescription(new DIBlob('SomePropertyBlobValue'), new DIProperty('Foo'), SMW_CMP_LESS); $description = new SomeProperty(new DIProperty('Foo'), $description); $description = new Conjunction(array($description, new ValueDescription(new DIBlob('SomeOtherPropertyBlobValue'), null, SMW_CMP_LESS), new ValueDescription(new DIBlob('YetAnotherPropertyBlobValue'), null, SMW_CMP_GRTR), new NamespaceDescription(NS_MAIN))); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result property:Foo ?v1 .')->addNewLine()->addString('FILTER( ?v1 < "SomePropertyBlobValue" )')->addNewLine()->addString('{ ?result swivt:wikiNamespace "0"^^xsd:integer . }')->addNewLine()->addString('FILTER( ?result < "SomeOtherPropertyBlobValue" && ?result > "YetAnotherPropertyBlobValue" )')->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 6 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\SingletonCondition'; $description = new ValueDescription(new DIBlob('SomePropertyBlobValue'), new DIProperty('Foo'), SMW_CMP_LESS); $description = new SomeProperty(new DIProperty('Foo'), $description); $description = new Conjunction(array($description, new ValueDescription(new DIBlob('SomeOtherPropertyBlobValue'), null, SMW_CMP_LIKE), new ValueDescription(new DIWikiPage('SomePropertyPageValue', NS_MAIN)), new NamespaceDescription(NS_MAIN))); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('wiki:SomePropertyPageValue property:Foo ?v1 .')->addNewLine()->addString('FILTER( ?v1 < "SomePropertyBlobValue" )')->addNewLine()->addString('{ wiki:SomePropertyPageValue swivt:wikiNamespace "0"^^xsd:integer . }')->addNewLine()->addString('FILTER( regex( ?result, "^SomeOtherPropertyBlobValue$", "s") )')->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 7 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\FilterCondition'; $description = new Conjunction(array(new ValueDescription(new DIBlob('SomeOtherPropertyBlobValue'), null, SMW_CMP_LIKE), new ValueDescription(new DIBlob('YetAnotherPropertyBlobValue'), new DIProperty('Foo'), SMW_CMP_NLKE), new ThingDescription())); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:page ?url .')->addNewLine()->addString('FILTER( regex( ?result, "^SomeOtherPropertyBlobValue$", "s") && ')->addString('!regex( ?result, "^YetAnotherPropertyBlobValue$", "s") )')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 8 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $propertyValue = new DIWikiPage('SomePropertyPageValue', NS_HELP); $propertyValueName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($propertyValue)); $description = new SomeProperty(new DIProperty('Foo'), new ValueDescription($propertyValue)); $category = new DIWikiPage('Bar', NS_CATEGORY); $categoryName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($category)); $description = new Conjunction(array($description, new ClassDescription($category))); $orderByProperty = new DIProperty('Foo'); $sortkeys = array('Foo' => 'ASC'); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString("?result property:Foo {$propertyValueName} .")->addNewLine()->addString('{ ?v1 swivt:wikiPageSortKey ?v1sk .')->addNewLine()->addString('}')->addNewLine()->addString("{ ?result rdf:type {$categoryName} . }")->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); return $provider; }
/** * '[[Born in::<q>[[Category:City]] [[Located in::Outback]]</q>]]' */ public function testNestedPropertyConjunction() { $property = DIProperty::newFromUserLabel('Born in'); $property->setPropertyTypeId('_wpg'); $category = new DIWikiPage('City', NS_CATEGORY); $categoryName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($category)); $conjunction = new Conjunction(array(new ClassDescription($category), new SomeProperty(DIProperty::newFromUserLabel('Located in'), new ValueDescription(new DIWikiPage('Outback', NS_MAIN), DIProperty::newFromUserLabel('Located in'))))); $description = new SomeProperty($property, $conjunction); $instance = new CompoundConditionBuilder(); $condition = $instance->buildCondition($description); $this->assertInstanceOf('\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition', $condition); $expectedConditionString = $this->stringBuilder->addString('?result property:Born_in ?v1 .')->addNewLine()->addString('{ ')->addString("{ ?v1 rdf:type {$categoryName} . }")->addNewLine()->addString('?v1 property:Located_in wiki:Outback .')->addNewLine()->addString('}')->addNewLine()->getString(); $this->assertEquals($expectedConditionString, $instance->convertConditionToString($condition)); }
private function lookupResourceUriTargetFromDatabase(ExpNsResource $expNsResource) { $resourceUri = TurtleSerializer::getTurtleNameForExpElement($expNsResource); $rediUri = TurtleSerializer::getTurtleNameForExpElement(Exporter::getInstance()->getSpecialPropertyResource('_REDI')); $skeyUri = TurtleSerializer::getTurtleNameForExpElement(Exporter::getInstance()->getSpecialPropertyResource('_SKEY')); $respositoryResult = $this->repositoryConnection->select('*', "{$resourceUri} {$skeyUri} ?s OPTIONAL { {$resourceUri} {$rediUri} ?r }", array('LIMIT' => 1), array($expNsResource->getNamespaceId() => $expNsResource->getNamespace())); return $respositoryResult->current(); }
public function descriptionProvider() { $stringBuilder = UtilityFactory::getInstance()->newStringBuilder(); # 0 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\FalseCondition'; $description = new SomeProperty(new DIProperty('Foo'), new Disjunction()); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('<http://www.example.org> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#nothing> .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 1 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $description = new SomeProperty(new DIProperty('Foo'), new ThingDescription()); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result property:Foo ?v1 .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 2 Inverse $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $description = new SomeProperty(new DIProperty('Foo', true), new ThingDescription()); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?v1 property:Foo ?result .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 3 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $description = new SomeProperty(new DIProperty('Foo'), new ThingDescription()); $orderByProperty = new DIProperty('Foo'); $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Foo ?v1 .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 4 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo'); $property->setPropertyTypeId('_txt'); $description = new SomeProperty($property, new ValueDescription(new DIBlob('SomePropertyBlobValue'))); $orderByProperty = null; $sortkeys = array(); $expected = $stringBuilder->addString('?result property:Foo "SomePropertyBlobValue" .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 5 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo'); $property->setPropertyTypeId('_txt'); $description = new SomeProperty($property, new ValueDescription(new DIBlob('SomePropertyBlobValue'))); $orderByProperty = $property; $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Foo "SomePropertyBlobValue" .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 6 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo'); $property->setPropertyTypeId('_wpg'); $propertyValue = new DIWikiPage('SomePropertyPageValue', NS_HELP); $propertyValueName = \SMWTurtleSerializer::getTurtleNameForExpElement(\SMWExporter::getInstance()->getResourceElementForWikiPage($propertyValue)); $description = new SomeProperty($property, new ValueDescription($propertyValue)); $orderByProperty = $property; $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString("?result property:Foo {$propertyValueName} .")->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 7 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo'); $property->setPropertyTypeId('_wpg'); $description = new SomeProperty($property, new ValueDescription(new DIWikiPage('SomePropertyPageValue', NS_HELP), $property, SMW_CMP_LEQ)); $orderByProperty = new DIProperty('SomePropertyPageValue'); $sortkeys = array(); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Foo ?v1 .')->addNewLine()->addString('FILTER( ?v1sk <= "SomePropertyPageValue" )')->addNewLine()->addString('?v1 swivt:wikiPageSortKey ?v1sk .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 8 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo'); $property->setPropertyTypeId('_wpg'); $description = new SomeProperty($property, new ValueDescription(new DIWikiPage('SomePropertyPageValue', NS_HELP), $property, SMW_CMP_LEQ)); $description = new SomeProperty(new DIProperty('Bar'), $description); $orderByProperty = new DIProperty('Bar'); $sortkeys = array('Foo' => 'ASC'); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Bar ?v1 .')->addNewLine()->addString('{ ?v1 property:Foo ?v2 .')->addNewLine()->addString('FILTER( ?v2sk <= "SomePropertyPageValue" )')->addNewLine()->addString('?v2 swivt:wikiPageSortKey ?v2sk .')->addNewLine()->addString('}')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 9 Inverse -> ?v1 property:Foo ?v2 vs. ?v2 property:Foo ?v1 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo', true); $property->setPropertyTypeId('_wpg'); $description = new SomeProperty($property, new ValueDescription(new DIWikiPage('SomePropertyPageValue', NS_HELP), $property, SMW_CMP_LEQ)); $description = new SomeProperty(new DIProperty('Bar'), $description); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Bar ?v1 .')->addNewLine()->addString('{ ?v2 property:Foo ?v1 .')->addNewLine()->addString('FILTER( ?v2sk <= "SomePropertyPageValue" )')->addNewLine()->addString('?v2 swivt:wikiPageSortKey ?v2sk .')->addNewLine()->addString('}')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 10 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('_MDAT'); $description = new SomeProperty($property, new ThingDescription()); $sortkeys = array('_MDAT' => 'ASC'); $propertyLabel = str_replace(' ', '_', $property->getLabel()); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString("?result property:{$propertyLabel}-23aux ?v1 .")->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 11, issue 556 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('Foo'); $property->setPropertyTypeId('_txt'); $description = new SomeProperty($property, new Disjunction(array(new ValueDescription(new DIBlob('Bar')), new ValueDescription(new DIBlob('Baz'))))); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Foo ?v1 .')->addNewLine()->addString('FILTER( ?v1 = "Bar" || ?v1 = "Baz" )')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 12 use the rdf/owl equivalent for a predefined property $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('_SUBC'); $description = new SomeProperty($property, new ValueDescription(new DIBlob('Bar'))); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result rdfs:subClassOf "Bar" .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 13 $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('_SUBP'); $description = new SomeProperty($property, new ValueDescription(new DIWikiPage('Bar', SMW_NS_PROPERTY))); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result rdfs:subPropertyOf property:Bar .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); # 14 aux-property $conditionType = '\\SMW\\SPARQLStore\\QueryEngine\\Condition\\WhereCondition'; $property = new DIProperty('_MDAT'); $description = new SomeProperty($property, new ValueDescription(new DITime(1, 1970, 01, 01, 1, 1))); $expected = $stringBuilder->addString('?result swivt:wikiPageSortKey ?resultsk .')->addNewLine()->addString('?result property:Modification_date-23aux "2440587.5423611"^^xsd:double .')->addNewLine()->getString(); $provider[] = array($description, $orderByProperty, $sortkeys, $conditionType, $expected); return $provider; }
private function createFilterConditionForAnyOtherComparator($dataItem, $joinVariable, $orderByProperty, $comparator) { $result = new FilterCondition('', array()); $this->compoundConditionBuilder->addOrderByData($result, $joinVariable, $dataItem->getDIType()); $orderByVariable = $result->orderByVariable; if ($dataItem instanceof DIWikiPage) { $expElement = $this->exporter->getDataItemExpElement($dataItem->getSortKeyDataItem()); } else { $expElement = $this->exporter->getDataItemHelperExpElement($dataItem); if (is_null($expElement)) { $expElement = $this->exporter->getDataItemExpElement($dataItem); } } $valueName = TurtleSerializer::getTurtleNameForExpElement($expElement); if ($expElement instanceof ExpNsResource) { $result->namespaces[$expElement->getNamespaceId()] = $expElement->getNamespace(); } $result->filter = "?{$orderByVariable} {$comparator} {$valueName}"; return $result; }
/** * Delete a dataitem from the Sparql back-end together with all data that is * associated resources * * @since 2.0 * * @param DataItem $dataItem * * @return boolean */ public function doSparqlDataDelete(DataItem $dataItem) { $extraNamespaces = array(); $expResource = Exporter::getInstance()->getDataItemExpElement($dataItem); $resourceUri = TurtleSerializer::getTurtleNameForExpElement($expResource); if ($expResource instanceof ExpNsResource) { $extraNamespaces = array($expResource->getNamespaceId() => $expResource->getNamespace()); } $masterPageProperty = Exporter::getInstance()->getSpecialNsResource('swivt', 'masterPage'); $masterPagePropertyUri = TurtleSerializer::getTurtleNameForExpElement($masterPageProperty); $success = $this->getConnection()->deleteContentByValue($masterPagePropertyUri, $resourceUri, $extraNamespaces); if ($success) { return $this->getConnection()->delete("{$resourceUri} ?p ?o", "{$resourceUri} ?p ?o", $extraNamespaces); } return false; }
/** * Build the condition (WHERE) string for a given Condition. * The function also expresses the single value of * SingletonCondition objects in the condition, which may * lead to additional namespaces for serializing its URI. * * @param Condition $condition * * @return string */ public function convertConditionToString(Condition &$condition) { $conditionAsString = $condition->getWeakConditionString(); if ($conditionAsString === '' && !$condition->isSafe()) { $swivtPageResource = Exporter::getInstance()->getSpecialNsResource('swivt', 'page'); $conditionAsString = '?' . $this->resultVariable . ' ' . $swivtPageResource->getQName() . " ?url .\n"; } $conditionAsString .= $condition->getCondition(); if ($condition instanceof SingletonCondition) { // prepare for ASK, maybe rather use BIND? $matchElement = $condition->matchElement; if ($matchElement instanceof ExpElement) { $matchElementName = TurtleSerializer::getTurtleNameForExpElement($matchElement); } else { $matchElementName = $matchElement; } if ($matchElement instanceof ExpNsResource) { $condition->namespaces[$matchElement->getNamespaceId()] = $matchElement->getNamespace(); } $conditionAsString = str_replace('?' . $this->resultVariable . ' ', "{$matchElementName} ", $conditionAsString); } return $conditionAsString; }
private function doResolveSubDescriptionsRecursively($subDescriptions, $joinVariable) { // Using a stdClass as data container for simpler handling in follow-up tasks // and as the class is not exposed publicly we don't need to create // an extra "real" class to manage its elements $subConditionElements = new \stdClass(); $subConditionElements->condition = ''; $subConditionElements->filter = ''; $subConditionElements->singletonMatchElement = null; $namespaces = $weakConditions = $orderVariables = array(); $singletonMatchElementName = ''; $hasSafeSubconditions = false; foreach ($subDescriptions as $subDescription) { $this->compoundConditionBuilder->setJoinVariable($joinVariable); $this->compoundConditionBuilder->setOrderByProperty(null); $subCondition = $this->compoundConditionBuilder->mapDescriptionToCondition($subDescription); if ($subCondition instanceof FalseCondition) { return new FalseCondition(); } elseif ($subCondition instanceof TrueCondition) { // ignore true conditions in a conjunction } elseif ($subCondition instanceof WhereCondition) { $subConditionElements->condition .= $subCondition->condition; } elseif ($subCondition instanceof FilterCondition) { $subConditionElements->filter .= ($subConditionElements->filter ? ' && ' : '') . $subCondition->filter; } elseif ($subCondition instanceof SingletonCondition) { $matchElement = $subCondition->matchElement; if ($matchElement instanceof ExpElement) { $matchElementName = TurtleSerializer::getTurtleNameForExpElement($matchElement); } else { $matchElementName = $matchElement; } if ($matchElement instanceof ExpNsResource) { $namespaces[$matchElement->getNamespaceId()] = $matchElement->getNamespace(); } if ($subConditionElements->singletonMatchElement !== null && $singletonMatchElementName !== $matchElementName) { return new FalseCondition(); } $subConditionElements->condition .= $subCondition->condition; $subConditionElements->singletonMatchElement = $subCondition->matchElement; $singletonMatchElementName = $matchElementName; } $hasSafeSubconditions = $hasSafeSubconditions || $subCondition->isSafe(); $namespaces = array_merge($namespaces, $subCondition->namespaces); $weakConditions = array_merge($weakConditions, $subCondition->weakConditions); $orderVariables = array_merge($orderVariables, $subCondition->orderVariables); } $subConditionElements->hasSafeSubconditions = $hasSafeSubconditions; $subConditionElements->namespaces = $namespaces; $subConditionElements->weakConditions = $weakConditions; $subConditionElements->orderVariables = $orderVariables; return $subConditionElements; }
private function doResolveSubDescriptionsRecursively($subDescriptions, $joinVariable, $orderByProperty) { // Using a stdClass as data container for simpler handling in follow-up tasks // and as the class is not exposed publicly we don't need to create // an extra "real" class to manage its elements $subConditionElements = new \stdClass(); $subConditionElements->unionCondition = ''; $subConditionElements->filter = ''; $namespaces = $weakConditions = array(); $hasSafeSubconditions = false; foreach ($subDescriptions as $subDescription) { $subCondition = $this->compoundConditionBuilder->mapDescriptionToCondition($subDescription, $joinVariable, null); if ($subCondition instanceof FalseCondition) { // empty parts in a disjunction can be ignored } elseif ($subCondition instanceof TrueCondition) { return $this->compoundConditionBuilder->buildTrueCondition($joinVariable, $orderByProperty); } elseif ($subCondition instanceof WhereCondition) { $hasSafeSubconditions = $hasSafeSubconditions || $subCondition->isSafe(); $subConditionElements->unionCondition .= ($subConditionElements->unionCondition ? ' UNION ' : '') . "{\n" . $subCondition->condition . "}"; } elseif ($subCondition instanceof FilterCondition) { $subConditionElements->filter .= ($subConditionElements->filter ? ' || ' : '') . $subCondition->filter; } elseif ($subCondition instanceof SingletonCondition) { $hasSafeSubconditions = $hasSafeSubconditions || $subCondition->isSafe(); $matchElement = $subCondition->matchElement; if ($matchElement instanceof ExpElement) { $matchElementName = TurtleSerializer::getTurtleNameForExpElement($matchElement); } else { $matchElementName = $matchElement; } if ($matchElement instanceof ExpNsResource) { $namespaces[$matchElement->getNamespaceId()] = $matchElement->getNamespace(); } if ($subCondition->condition === '') { $subConditionElements->filter .= ($subConditionElements->filter ? ' || ' : '') . "?{$joinVariable} = {$matchElementName}"; } else { $subConditionElements->unionCondition .= ($subConditionElements->unionCondition ? ' UNION ' : '') . "{\n" . $subCondition->condition . " FILTER( ?{$joinVariable} = {$matchElementName} ) }"; } } $namespaces = array_merge($namespaces, $subCondition->namespaces); $weakConditions = array_merge($weakConditions, $subCondition->weakConditions); } $subConditionElements->namespaces = $namespaces; $subConditionElements->weakConditions = $weakConditions; $subConditionElements->hasSafeSubconditions = $hasSafeSubconditions; return $subConditionElements; }
/** * Delete from the SPARQL database all data that is associated with the * given resource. * * @since 1.6 * @param $expResource SMWExpResource * @return boolean success */ protected function deleteSparqlData(SMWExpResource $expResource) { $resourceUri = SMWTurtleSerializer::getTurtleNameForExpElement($expResource); if ($expResource instanceof SMWExpNsResource) { $extraNamespaces = array($expResource->getNamespaceId() => $expResource->getNamespace()); } else { $extraNamespaces = array(); } $masterPageProperty = SMWExporter::getSpecialNsResource('swivt', 'masterPage'); $masterPagePropertyUri = SMWTurtleSerializer::getTurtleNameForExpElement($masterPageProperty); $success = smwfGetSparqlDatabase()->deleteContentByValue($masterPagePropertyUri, $resourceUri, $extraNamespaces); if ($success) { return smwfGetSparqlDatabase()->delete("{$resourceUri} ?p ?o", "{$resourceUri} ?p ?o", $extraNamespaces); } else { return false; } }
/** * @since 2.3 * * @param DataItem|null $dataItem * * @return string|null */ public function tryToFindRedirectVariableForDataItem(DataItem $dataItem = null) { if (!$dataItem instanceof DIWikiPage || !$this->canUseQFeature(SMW_SPARQL_QF_REDI)) { return null; } // Maybe there is a better way to verify the "isRedirect" state other // than by using the Title object if ($dataItem->getTitle() === null || !$dataItem->getTitle()->isRedirect()) { return null; } $redirectExpElement = Exporter::getInstance()->getResourceElementForWikiPage($dataItem); $valueName = TurtleSerializer::getTurtleNameForExpElement($redirectExpElement); // Add unknow redirect target/variable for value if (!isset($this->redirectByVariableReplacementMap[$valueName])) { $namespaces[$redirectExpElement->getNamespaceId()] = $redirectExpElement->getNamespace(); $redirectByVariable = '?' . $this->getNextVariable('r'); $this->redirectByVariableReplacementMap[$valueName] = array($redirectByVariable, $namespaces); } // Reuse an existing variable for the value to allow to be used more than // once when referring to the same property/value redirect list($redirectByVariable, $namespaces) = $this->redirectByVariableReplacementMap[$valueName]; return $redirectByVariable; }
private function doResolveSubDescriptionsRecursively($subDescriptions, $joinVariable, $orderByProperty) { // Using a stdClass as data container for simpler handling in follow-up tasks // and as the class is not exposed publicly we don't need to create // an extra "real" class to manage its elements $subConditionElements = new \stdClass(); $subConditionElements->unionCondition = ''; $subConditionElements->filter = ''; $namespaces = $weakConditions = array(); $hasSafeSubconditions = false; foreach ($subDescriptions as $subDescription) { $this->compoundConditionBuilder->setJoinVariable($joinVariable); $this->compoundConditionBuilder->setOrderByProperty(null); $subCondition = $this->compoundConditionBuilder->mapDescriptionToCondition($subDescription); if ($subCondition instanceof FalseCondition) { // empty parts in a disjunction can be ignored } elseif ($subCondition instanceof TrueCondition) { return $this->compoundConditionBuilder->newTrueCondition($joinVariable, $orderByProperty); } elseif ($subCondition instanceof WhereCondition) { $hasSafeSubconditions = $hasSafeSubconditions || $subCondition->isSafe(); $subConditionElements->unionCondition .= ($subConditionElements->unionCondition ? ' UNION ' : '') . "{\n" . $subCondition->condition . "}"; } elseif ($subCondition instanceof FilterCondition) { $subConditionElements->filter .= ($subConditionElements->filter ? ' || ' : '') . $subCondition->filter; } elseif ($subCondition instanceof SingletonCondition) { $hasSafeSubconditions = $hasSafeSubconditions || $subCondition->isSafe(); $matchElement = $subCondition->matchElement; if ($matchElement instanceof ExpElement) { $matchElementName = TurtleSerializer::getTurtleNameForExpElement($matchElement); } else { $matchElementName = $matchElement; } if ($matchElement instanceof ExpNsResource) { $namespaces[$matchElement->getNamespaceId()] = $matchElement->getNamespace(); } if ($subCondition->condition === '') { $subConditionElements->filter .= ($subConditionElements->filter ? ' || ' : '') . "?{$joinVariable} = {$matchElementName}"; } else { $subConditionElements->unionCondition .= ($subConditionElements->unionCondition ? ' UNION ' : '') . "{\n" . $subCondition->condition . " FILTER( ?{$joinVariable} = {$matchElementName} ) }"; } // Relates to wikipage [[Foo::~*a*||~*A*]] in value regex disjunction // where a singleton is required to search against the sortkey but // replacing the filter with the condition temporary stored in // weakconditions if ($subConditionElements->unionCondition && $subCondition->weakConditions !== array()) { $weakCondition = array_shift($subCondition->weakConditions); $subConditionElements->unionCondition = str_replace("FILTER( ?{$joinVariable} = {$matchElementName} )", $weakCondition, $subConditionElements->unionCondition); } } $namespaces = array_merge($namespaces, $subCondition->namespaces); $weakConditions = array_merge($weakConditions, $subCondition->weakConditions); } $subConditionElements->namespaces = $namespaces; $subConditionElements->weakConditions = $weakConditions; $subConditionElements->hasSafeSubconditions = $hasSafeSubconditions; return $subConditionElements; }
/** * Delete from the SPARQL database all data that is associated with the * given resource. * * @param $expResource SMWExpResource * @return boolean success */ protected function deleteSparqlData( SMWExpResource $expResource ) { $resourceUri = SMWTurtleSerializer::getTurtleNameForExpElement( $expResource ); $masterPageProperty = SMWExporter::getSpecialNsResource( 'swivt', 'masterPage' ); $masterPagePropertyUri = SMWTurtleSerializer::getTurtleNameForExpElement( $masterPageProperty ); $success = smwfGetSparqlDatabase()->deleteContentByValue( $masterPagePropertyUri, $resourceUri ); if ( $success ) { return smwfGetSparqlDatabase()->delete( "$resourceUri ?p ?o", "$resourceUri ?p ?o" ); } else { return false; } }