/**
  * @since  2.2
  *
  * @param QueryTestCaseInterpreter $queryTestCaseInterpreter
  */
 public function processQueryCase(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, false);
     $query->querymode = $queryTestCaseInterpreter->getQueryMode();
     $query->setLimit($queryTestCaseInterpreter->getLimit());
     $query->setOffset($queryTestCaseInterpreter->getOffset());
     $query->setExtraPrintouts($queryTestCaseInterpreter->getExtraPrintouts());
     $query->setSortKeys($queryTestCaseInterpreter->getSortKeys());
     $query->setContextPage($queryTestCaseInterpreter->getSubject());
     if ($queryTestCaseInterpreter->isRequiredToClearStoreCache()) {
         $this->getStore()->clear();
     }
     $queryResult = $this->getStore()->getQueryResult($query);
     $this->printQueryResultToOutput($queryResult);
     if (is_string($queryResult)) {
         return;
     }
     $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());
     if ($queryTestCaseInterpreter->getExpectedErrorCount() > 0) {
         return null;
     }
     if ($queryTestCaseInterpreter->isFromCache() !== null) {
         $this->assertEquals($queryTestCaseInterpreter->isFromCache(), $queryResult->isFromCache(), 'Failed asserting isFromCache for ' . $queryTestCaseInterpreter->isAbout());
     }
     $this->queryResultValidator->assertThatQueryResultHasSubjects($queryTestCaseInterpreter->getExpectedSubjects(), $queryResult, $queryTestCaseInterpreter->isAbout());
     $this->queryResultValidator->assertThatDataItemIsSet($queryTestCaseInterpreter->getExpectedDataItems(), $queryResult, $queryTestCaseInterpreter->isAbout());
     $this->queryResultValidator->assertThatDataValueIsSet($queryTestCaseInterpreter->getExpectedDataValues(), $queryResult, $queryTestCaseInterpreter->isAbout());
 }
Exemplo n.º 2
0
 public function testToArray()
 {
     $description = $this->getMockForAbstractClass('\\SMW\\Query\\Language\\Description');
     $printRequest = $this->getMockBuilder('SMW\\Query\\PrintRequest')->disableOriginalConstructor()->getMock();
     $instance = new Query($description, Query::INLINE_QUERY);
     $instance->setExtraPrintouts(array($printRequest));
     $serialized = $instance->toArray();
     $this->assertInternalType('array', $serialized);
     $expected = array('conditions', 'parameters', 'printouts');
     foreach ($expected as $key) {
         $this->assertArrayHasKey($key, $serialized);
     }
     $expectedParameters = array('limit', 'offset', 'mainlabel', 'sortkeys', 'querymode');
     foreach ($expectedParameters as $key) {
         $this->assertArrayHasKey($key, $serialized['parameters']);
     }
 }
 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;
 }
Exemplo n.º 5
0
 /**
  * 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;
 }
 /**
  * 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);
 }