public function testGetHash() { $description = $this->getMockForAbstractClass('\\SMW\\Query\\Language\\Description'); $instance = new Query($description, Query::INLINE_QUERY); $instance->setLimit(50); $hash = $instance->getHash(); $this->assertInternalType('string', $hash); $instance->setLimit(100); $this->assertNotEquals($hash, $instance->getHash()); }
public function testQueryForInterwikiAnnotation() { $this->stringBuilder->addString('[[Has type::Page]]'); $this->pageCreator->createPage(Title::newFromText('Use for interwiki annotation', SMW_NS_PROPERTY))->doEdit($this->stringBuilder->getString()); $this->pageCreator->createPage(Title::newFromText(__METHOD__ . '-1'))->doEdit('[[Use for interwiki annotation::Interwiki link]]'); $this->pageCreator->createPage(Title::newFromText(__METHOD__ . '-2'))->doEdit('[[Use for interwiki annotation::iw-test:Interwiki link]]'); $this->stringBuilder->addString('[[Use for interwiki annotation::iw-test:Interwiki link]]'); $description = $this->queryParser->getQueryDescription($this->stringBuilder->getString()); $query = new Query($description, false, false); $query->querymode = Query::MODE_INSTANCES; $query->setLimit(10); // Expects only one result with an interwiki being used as differentiator $this->subjects[] = DIWikiPage::newFromTitle(Title::newFromText(__METHOD__ . '-2')); $this->queryResultValidator->assertThatQueryResultHasSubjects($this->subjects, $this->getStore()->getQueryResult($query)); $this->subjects[] = DIWikiPage::newFromTitle(Title::newFromText(__METHOD__ . '-1')); }
public function testSortableRecordQuery() { $this->getStore()->updateData($this->fixturesProvider->getFactsheet('Berlin')->asEntity()); $this->getStore()->updateData($this->fixturesProvider->getFactsheet('Paris')->asEntity()); /** * PopulationDensity is specified as `_rec` * * @query {{#ask: [[PopulationDensity::SomeDistinctValue]] }} */ $populationDensityValue = $this->fixturesProvider->getFactsheet('Berlin')->getPopulationDensityValue(); $description = new SomeProperty($populationDensityValue->getProperty(), $populationDensityValue->getQueryDescription($populationDensityValue->getWikiValue())); $propertyValue = new PropertyValue('__pro'); $propertyValue->setDataItem($populationDensityValue->getProperty()); $query = new Query($description, false, false); $query->querymode = Query::MODE_INSTANCES; $query->sortkeys = array($populationDensityValue->getProperty()->getKey() => 'ASC'); $query->setLimit(100); $query->setExtraPrintouts(array(new PrintRequest(PrintRequest::PRINT_THIS, ''), new PrintRequest(PrintRequest::PRINT_PROP, null, $propertyValue))); $expected = array($this->fixturesProvider->getFactsheet('Berlin')->asSubject(), $this->fixturesProvider->getFactsheet('Berlin')->getDemographics()->getSubject()); $this->queryResultValidator->assertThatQueryResultHasSubjects($expected, $this->getStore()->getQueryResult($query)); }
/** * Parse a query string given in SMW's query language to create * an SMWQuery. Parameters are given as key-value-pairs in the * given array. The parameter $context defines in what context the * query is used, which affects ceretain general settings. * An object of type SMWQuery is returned. * * The format string is used to specify the output format if already * known. Otherwise it will be determined from the parameters when * needed. This parameter is just for optimisation in a common case. * * @param string $queryString * @param array $params These need to be the result of a list fed to getProcessedParams * @param $context * @param string $format * @param array $extraPrintouts * * @return SMWQuery */ public static function createQuery($queryString, array $params, $context = self::INLINE_QUERY, $format = '', array $extraPrintouts = array()) { global $smwgQDefaultNamespaces, $smwgQFeatures, $smwgQConceptFeatures; // parse query: $queryfeatures = $context == self::CONCEPT_DESC ? $smwgQConceptFeatures : $smwgQFeatures; $qp = new SMWQueryParser($queryfeatures); $qp->setDefaultNamespaces($smwgQDefaultNamespaces); $desc = $qp->getQueryDescription($queryString); if ($format === '' || is_null($format)) { $format = $params['format']->getValue(); } if ($format == 'count') { $querymode = SMWQuery::MODE_COUNT; } elseif ($format == 'debug') { $querymode = SMWQuery::MODE_DEBUG; } else { $printer = self::getResultPrinter($format, $context); $querymode = $printer->getQueryMode($context); } $query = new SMWQuery($desc, $context != self::SPECIAL_PAGE, $context == self::CONCEPT_DESC); $query->setQueryString($queryString); $query->setExtraPrintouts($extraPrintouts); $query->setMainLabel($params['mainlabel']->getValue()); $query->addErrors($qp->getErrors()); // keep parsing errors for later output // set mode, limit, and offset: $query->querymode = $querymode; if (array_key_exists('offset', $params) && is_int($params['offset']->getValue() + 0)) { $query->setOffset(max(0, trim($params['offset']->getValue()) + 0)); } if ($query->querymode == SMWQuery::MODE_COUNT) { // largest possible limit for "count", even inline global $smwgQMaxLimit; $query->setOffset(0); $query->setLimit($smwgQMaxLimit, false); } else { if (array_key_exists('limit', $params) && is_int(trim($params['limit']->getValue()) + 0)) { $query->setLimit(max(0, trim($params['limit']->getValue()) + 0)); if (trim($params['limit']->getValue()) + 0 < 0) { // limit < 0: always show further results link only $query->querymode = SMWQuery::MODE_NONE; } } else { global $smwgQDefaultLimit; $query->setLimit($smwgQDefaultLimit); } } $defaultSort = $format === 'rss' ? 'DESC' : 'ASC'; $sort = self::getSortKeys($params['sort']->getValue(), $params['order']->getValue(), $defaultSort); $query->sortkeys = $sort['keys']; $query->addErrors($sort['errors']); $query->sort = count($query->sortkeys) > 0; // TODO: Why would we do this here? return $query; }
/** * Serialize data associated to a specific page. This method works on the * level of pages, i.e. it serialises parts of SMW content and implements * features like recursive export or backlinks that are available for this * type of data. * * The recursion depth means the following. Depth of 1 or above means * the object is serialised with all property values, and referenced * objects are serialised with depth reduced by 1. Depth 0 means that only * minimal declarations are serialised, so no dependencies are added. A * depth of -1 encodes "infinite" depth, i.e. a complete recursive * serialisation without limit. * * @param SMWDIWikiPage $diWikiPage specifying the page to be exported * @param integer $recursiondepth specifying the depth of recursion */ protected function serializePage(SMWDIWikiPage $diWikiPage, $recursiondepth = 1) { if ($this->isPageDone($diWikiPage, $recursiondepth)) { return; // do not export twice } $this->markPageAsDone($diWikiPage, $recursiondepth); $semData = $this->getSemanticData($diWikiPage, $recursiondepth == 0); // Don't try to serialize an empty page that cause an incomplete exp-data set // (e.g. _REDI as no property page hence DBKey is empty) if ($semData === null || $diWikiPage->getDBKey() === '') { return null; } $expData = SMWExporter::getInstance()->makeExportData($semData); $this->serializer->serializeExpData($expData, $recursiondepth); foreach ($semData->getSubSemanticData() as $subobjectSemData) { $this->serializer->serializeExpData(SMWExporter::getInstance()->makeExportData($subobjectSemData)); } // let other extensions add additional RDF data for this page $additionalDataArray = array(); \Hooks::run('smwAddToRDFExport', array($diWikiPage, &$additionalDataArray, $recursiondepth != 0, $this->add_backlinks)); foreach ($additionalDataArray as $additionalData) { $this->serializer->serializeExpData($additionalData); // serialise } if ($recursiondepth != 0) { $subrecdepth = $recursiondepth > 0 ? $recursiondepth - 1 : ($recursiondepth == 0 ? 0 : -1); foreach ($expData->getProperties() as $property) { if ($property->getDataItem() instanceof SMWWikiPageValue) { $this->queuePage($property->getDataItem(), 0); // no real recursion along properties } $wikipagevalues = false; foreach ($expData->getValues($property) as $valueExpElement) { $valueResource = $valueExpElement instanceof SMWExpData ? $valueExpElement->getSubject() : $valueExpElement; if (!$wikipagevalues && $valueResource->getDataItem() instanceof SMWWikiPageValue) { $wikipagevalues = true; } elseif (!$wikipagevalues) { break; } $this->queuePage($valueResource->getDataItem(), $subrecdepth); } } // Add backlinks: // Note: Backlinks are different from recursive serialisations, since // stub declarations (recdepth==0) still need to have the property that // links back to the object. So objects that would be exported with // recdepth 0 cannot be put into the main queue but must be done right // away. They also might be required many times, if they link back to // many different objects in many ways (we cannot consider them "Done" // if they were serialised at recdepth 0 only). if ($this->add_backlinks) { $inprops = \SMW\StoreFactory::getStore()->getInProperties($diWikiPage); foreach ($inprops as $inprop) { $propWikiPage = $inprop->getCanonicalDiWikiPage(); if (!is_null($propWikiPage)) { $this->queuePage($propWikiPage, 0); // no real recursion along properties } $inSubs = \SMW\StoreFactory::getStore()->getPropertySubjects($inprop, $diWikiPage); foreach ($inSubs as $inSub) { if (!$this->isPageDone($inSub, $subrecdepth)) { $semdata = $this->getSemanticData($inSub, true); if (!$semdata instanceof SMWSemanticData) { continue; } $semdata->addPropertyObjectValue($inprop, $diWikiPage); $expData = SMWExporter::getInstance()->makeExportData($semdata); $this->serializer->serializeExpData($expData, $subrecdepth); } } } if (NS_CATEGORY === $diWikiPage->getNamespace()) { // also print elements of categories $options = new SMWRequestOptions(); $options->limit = 100; // Categories can be large, always use limit $instances = \SMW\StoreFactory::getStore()->getPropertySubjects(new SMW\DIProperty('_INST'), $diWikiPage, $options); $pinst = new SMW\DIProperty('_INST'); foreach ($instances as $instance) { if (!array_key_exists($instance->getHash(), $this->element_done)) { $semdata = $this->getSemanticData($instance, true); if (!$semdata instanceof SMWSemanticData) { continue; } $semdata->addPropertyObjectValue($pinst, $diWikiPage); $expData = SMWExporter::getInstance()->makeExportData($semdata); $this->serializer->serializeExpData($expData, $subrecdepth); } } } elseif (SMW_NS_CONCEPT === $diWikiPage->getNamespace()) { // print concept members (slightly different code) $desc = new SMWConceptDescription($diWikiPage); $desc->addPrintRequest(new PrintRequest(PrintRequest::PRINT_THIS, '')); $query = new SMWQuery($desc); $query->setLimit(100); $res = \SMW\StoreFactory::getStore()->getQueryResult($query); $resarray = $res->getNext(); $pinst = new SMW\DIProperty('_INST'); while ($resarray !== false) { $instance = end($resarray)->getNextDataItem(); if (!$instance instanceof \SMWDataItem) { $resarray = $res->getNext(); continue; } if (!array_key_exists($instance->getHash(), $this->element_done)) { $semdata = $this->getSemanticData($instance, true); if (!$semdata instanceof \SMW\SemanticData) { $resarray = $res->getNext(); continue; } $semdata->addPropertyObjectValue($pinst, $diWikiPage); $expData = SMWExporter::getInstance()->makeExportData($semdata); $this->serializer->serializeExpData($expData); } $resarray = $res->getNext(); } } } } }
/** * Returns all results that have a value near to the searched for value * on the property, ordered, and sorted by ending with the smallest * one. * * @param QueryOptions $pageRequestOptions * @param integer $count How many entities have the exact same value on the property? * @param integer $greater Should the values be bigger? Set false for smaller values. * * @return array of array of SMWWikiPageValue, SMWDataValue with the * first being the entity, and the second the value */ public function doQueryForNearbyResults(PageRequestOptions $pageRequestOptions, $count, $greater = true) { $comparator = $greater ? SMW_CMP_GRTR : SMW_CMP_LESS; $sortOrder = $greater ? 'ASC' : 'DESC'; if ($pageRequestOptions->value !== null && $pageRequestOptions->value->getTypeID() === '_txt' && strlen($pageRequestOptions->valueString) > 72) { $comparator = SMW_CMP_LIKE; } if ($pageRequestOptions->valueString === '' || $pageRequestOptions->valueString === null) { $description = new ThingDescription(); } else { $description = new ValueDescription($pageRequestOptions->value->getDataItem(), $pageRequestOptions->property->getDataItem(), $comparator); } $someProperty = new SomeProperty($pageRequestOptions->property->getDataItem(), $description); $query = new Query($someProperty); $query->setLimit($pageRequestOptions->limit); $query->setOffset($pageRequestOptions->offset); $query->sort = true; $query->sortkeys = array($pageRequestOptions->property->getDataItem()->getKey() => $sortOrder); // Note: printrequests change the caption of properties they // get (they expect properties to be given to them). // Since we want to continue using the property for our // purposes, we give a clone to the print request. $printouts = array(new PrintRequest(PrintRequest::PRINT_THIS, ''), new PrintRequest(PrintRequest::PRINT_PROP, '', clone $pageRequestOptions->property)); $query->setExtraPrintouts($printouts); $queryResults = $this->store->getQueryResult($query); $result = array(); while ($resultArrays = $queryResults->getNext()) { $r = array(); foreach ($resultArrays as $resultArray) { $r[] = $resultArray->getNextDataValue(); } // Note: if results have multiple values for the property // then this code just pick the first, which may not be // the reason why the result is shown here, i.e., it could // be out of order. $result[] = $r; } if (!$greater) { $result = array_reverse($result); } return $result; }
/** * Make SMWQuery suitable for obtaining a list of results based on the * given description, limit, and from or until string. One more result * than the limit will be created, and the results may have to be * reversed in order if $until is nonempty. * * @param $description SMWDescription main query description * @param $limit integer * @param $from string can be empty if no from condition is desired * @param $until string can be empty if no until condition is desired * @return SMWQuery */ public static function getQuery(SMWDescription $description, $limit, $from, $until) { if ($from !== '') { $diWikiPage = new SMWDIWikiPage($from, NS_MAIN, ''); // make a dummy wiki page as boundary $fromDescription = new SMWValueDescription($diWikiPage, null, SMW_CMP_GEQ); $queryDescription = new SMWConjunction(array($description, $fromDescription)); $order = 'ASC'; } elseif ($until !== '') { $diWikiPage = new SMWDIWikiPage($until, NS_MAIN, ''); // make a dummy wiki page as boundary $untilDescription = new SMWValueDescription($diWikiPage, null, SMW_CMP_LESS); // do not include boundary in this case $queryDescription = new SMWConjunction(array($description, $untilDescription)); $order = 'DESC'; } else { $queryDescription = $description; $order = 'ASC'; } $queryDescription->addPrintRequest(new SMWPrintRequest(SMWPrintRequest::PRINT_THIS, '')); $query = new SMWQuery($queryDescription); $query->sortkeys[''] = $order; $query->setLimit($limit + 1); return $query; }
/** * @since 2.2 * * @param QueryTestCaseInterpreter $queryTestCaseInterpreter */ public function processConceptCase(QueryTestCaseInterpreter $queryTestCaseInterpreter) { if (!$queryTestCaseInterpreter->hasCondition()) { $this->markTestSkipped('Found no condition for ' . $queryTestCaseInterpreter->isAbout()); } $description = $this->queryParser->getQueryDescription($queryTestCaseInterpreter->getCondition()); $this->printDescriptionToOutput($queryTestCaseInterpreter->isAbout(), $description); $query = new Query($description, false, true); $query->querymode = $queryTestCaseInterpreter->getQueryMode(); $query->setLimit($queryTestCaseInterpreter->getLimit()); $query->setOffset($queryTestCaseInterpreter->getOffset()); $queryResult = $this->getStore()->getQueryResult($query); $this->printQueryResultToOutput($queryResult); $this->assertEquals($queryTestCaseInterpreter->getExpectedCount(), $queryResult->getCount(), 'Failed asserting query result count on ' . $queryTestCaseInterpreter->isAbout()); $this->assertCount($queryTestCaseInterpreter->getExpectedErrorCount(), $queryResult->getErrors(), 'Failed asserting error count ' . $queryTestCaseInterpreter->isAbout()); foreach ($queryTestCaseInterpreter->getExpectedConceptCache() as $expectedConceptCache) { $concept = Title::newFromText($expectedConceptCache['concept'], SMW_NS_CONCEPT); $this->getStore()->refreshConceptCache($concept); $this->assertEquals($expectedConceptCache['count'], $this->getStore()->getConceptCacheStatus($concept)->getCacheCount(), 'Failed asserting conceptcache count on ' . $queryTestCaseInterpreter->isAbout()); } }
public static function getAllPagesForConcept($conceptName, $substring = null) { global $sfgMaxAutocompleteValues, $sfgAutocompleteOnAllChars; $store = smwfGetStore(); $conceptTitle = Title::makeTitleSafe(SMW_NS_CONCEPT, $conceptName); if (!is_null($substring)) { $substring = strtolower($substring); } // Escape if there's no such concept. if ($conceptTitle == null || !$conceptTitle->exists()) { return "Could not find concept: {$conceptName}"; } $conceptDI = SMWDIWikiPage::newFromTitle($conceptTitle); $desc = new SMWConceptDescription($conceptDI); $printout = new SMWPrintRequest(SMWPrintRequest::PRINT_THIS, ""); $desc->addPrintRequest($printout); $query = new SMWQuery($desc); $query->setLimit($sfgMaxAutocompleteValues); $query_result = $store->getQueryResult($query); $pages = array(); while ($res = $query_result->getNext()) { $pageName = $res[0]->getNextText(SMW_OUTPUT_WIKI); if (is_null($substring)) { $pages[] = $pageName; } else { // Filter on the substring manually. It would // be better to do this filtering in the // original SMW query, but that doesn't seem // possible yet. $lowercasePageName = strtolower($pageName); if ($sfgAutocompleteOnAllChars) { if (strpos($lowercasePageName, $substring) >= 0) { $pages[] = $pageName; } } else { if (strpos($lowercasePageName, $substring) === 0 || strpos($lowercasePageName, ' ' . $substring) > 0) { $pages[] = $pageName; } } } } sort($pages); return $pages; }
/** * Returns all results that have a value near to the searched for value * on the property, ordered, and sorted by ending with the smallest * one. * * @param integer $count How many entities have the exact same value on the property? * @param integer $greater Should the values be bigger? Set false for smaller values. * * @return array of array of SMWWikiPageValue, SMWDataValue with the * first being the entity, and the second the value */ protected function getNearbyResults($count, $greater = true) { $valueDescription = new SMWValueDescription($this->value->getDataItem(), $this->property->getDataItem(), $greater ? SMW_CMP_GRTR : SMW_CMP_LESS); $someProperty = new SMWSomeProperty($this->property->getDataItem(), $valueDescription); $query = new SMWQuery($someProperty); $query->setLimit($this->limit); $query->sort = true; $query->sortkeys = array($this->property->getDataItem()->getKey() => $greater ? 'ASC' : 'DESC'); // Note: printrequests change the caption of properties they // get (they expect properties to be given to them). // Since we want to continue using the property for our // purposes, we give a clone to the print request. $printouts = array(new SMWPrintRequest(SMWPrintRequest::PRINT_THIS, ''), new SMWPrintRequest(SMWPrintRequest::PRINT_PROP, '', clone $this->property)); $query->setExtraPrintouts($printouts); $queryResults = smwfGetStore()->getQueryResult($query); $result = array(); while ($resultArrays = $queryResults->getNext()) { $r = array(); foreach ($resultArrays as $resultArray) { $r[] = $resultArray->getNextDataValue(); } // Note: if results have multiple values for the property // then this code just pick the first, which may not be // the reason why the result is shown here, i.e., it could // be out of order. $result[] = $r; } if (!$greater) { $result = array_reverse($result); } return $result; }
/** * #576 */ public function testSortableDateQuery() { $this->getStore()->updateData($this->fixturesProvider->getFactsheet('Berlin')->asEntity()); // #576 introduced resource caching, therefore make sure that the // instance is cleared after data have been created before further // tests are carried out Exporter::clear(); /** * @query {{#ask: [[Founded::SomeDistinctValue]] }} */ $foundedValue = $this->fixturesProvider->getFactsheet('Berlin')->getFoundedValue(); $description = new SomeProperty($foundedValue->getProperty(), new ValueDescription($foundedValue->getDataItem(), null, SMW_CMP_EQ)); $propertyValue = new PropertyValue('__pro'); $propertyValue->setDataItem($foundedValue->getProperty()); $query = new Query($description, false, false); $query->querymode = Query::MODE_INSTANCES; $query->sortkeys = array($foundedValue->getProperty()->getLabel() => 'ASC'); // Be aware of // Virtuoso 22023 Error SR353: Sorted TOP clause specifies more then // 10001 rows to sort. Only 10000 are allowed. Either decrease the // offset and/or row count or use a scrollable cursor $query->setLimit(100); $query->setExtraPrintouts(array(new PrintRequest(PrintRequest::PRINT_THIS, ''), new PrintRequest(PrintRequest::PRINT_PROP, null, $propertyValue))); $queryResult = $this->getStore()->getQueryResult($query); $this->queryResultValidator->assertThatQueryResultHasSubjects($this->fixturesProvider->getFactsheet('Berlin')->asSubject(), $queryResult); }
/** * @since 2.4 * * @param string $displayTitle * * @return DIProperty|false */ public function getPropertyFromDisplayTitle($displayTitle) { $descriptionFactory = new DescriptionFactory(); $description = $descriptionFactory->newSomeProperty(new DIProperty('_DTITLE'), $descriptionFactory->newValueDescription(new DIBlob($displayTitle))); $query = new Query($description); $query->setLimit(1); $dataItems = $this->cachedPropertyValuesPrefetcher->queryPropertyValuesFor($query); if (is_array($dataItems) && $dataItems !== array()) { $dataItem = end($dataItems); // Cache results as a linked list attached to // the property so that it can be purged all together return new DIProperty($dataItem->getDBKey()); } return false; }