/**
  * @since  2.4
  *
  * @return string
  */
 public function getQueryId()
 {
     if ($this->queryId === null) {
         $this->queryId = $this->query->getQueryId();
     }
     return $this->queryId;
 }
 /**
  * Generate textual debug output that shows an arbitrary list of informative
  * fields. Used for formatting query debug output.
  *
  * @note All strings given must be usable and safe in wiki and HTML
  * contexts.
  *
  * @param $storeName string name of the storage backend for which this is generated
  * @param $entries array of name => value of informative entries to display
  * @param $query SMWQuery or null, if given add basic data about this query as well
  *
  * @return string
  */
 public static function getStringFrom($storeName, array $entries, Query $query = null)
 {
     if ($query instanceof Query) {
         $preEntries = array();
         $preEntries['ASK Query'] = '<div class="smwpre">' . str_replace('[', '&#x005B;', $query->getDescription()->getQueryString()) . '</div>';
         $entries = array_merge($preEntries, $entries);
         $entries['Query Metrics'] = 'Query-Size:' . $query->getDescription()->getSize() . '<br />' . 'Query-Depth:' . $query->getDescription()->getDepth();
         $errors = '';
         $queryErrors = ProcessingErrorMsgHandler::normalizeMessages($query->getErrors());
         foreach ($queryErrors as $error) {
             $errors .= $error . '<br />';
         }
         if ($errors === '') {
             $errors = 'None';
         }
         $entries['Errors and Warnings'] = $errors;
     }
     $result = '<div style="border: 5px dotted #ffcc00; background: #FFF0BD; padding: 20px; ">' . "<div class='smw-column-header'><big>{$storeName} debug output</big></div>";
     foreach ($entries as $header => $information) {
         $result .= "<div class='smw-column-header'>{$header}</div>";
         if ($information !== '') {
             $result .= "{$information}";
         }
     }
     $result .= '</div>';
     return $result;
 }
 /**
  * Generate textual debug output that shows an arbitrary list of informative
  * fields. Used for formatting query debug output.
  *
  * @note All strings given must be usable and safe in wiki and HTML
  * contexts.
  *
  * @param $storeName string name of the storage backend for which this is generated
  * @param $entries array of name => value of informative entries to display
  * @param $query SMWQuery or null, if given add basic data about this query as well
  *
  * @return string
  */
 public static function formatOutputFor($storeName, array $entries, Query $query = null)
 {
     if ($query instanceof Query) {
         $preEntries = array();
         $preEntries['Ask query'] = '<div class="smwpre">' . str_replace('[', '&#x005B;', $query->getDescription()->getQueryString()) . '</div>';
         $entries = array_merge($preEntries, $entries);
         $entries['Query Metrics'] = 'Query-Size:' . $query->getDescription()->getSize() . '<br />' . 'Query-Depth:' . $query->getDescription()->getDepth();
         $errors = '';
         foreach ($query->getErrors() as $error) {
             $errors .= $error . '<br />';
         }
         if ($errors === '') {
             $errors = 'None';
         }
         $entries['Errors and Warnings'] = $errors;
     }
     $result = '<div style="border: 5px dotted #A1FB00; background: #FFF0BD; padding: 20px; ">' . "<h3>Debug Output by {$storeName}</h3>";
     foreach ($entries as $header => $information) {
         $result .= "<h4>{$header}</h4>";
         if ($information !== '') {
             $result .= "{$information}";
         }
     }
     $result .= '</div>';
     return $result;
 }
Beispiel #4
0
 /**
  * Add data about the query and its parameters to the semantic data of
  * the given parser. The $queryKey is a string key that uniquely
  * identifies the query; this is difficult to create in a stable way
  * from the processed query object and parameters, but easy to get from
  * the raw user input.
  *
  * @param string $queryKey
  * @param SMWQuery $query
  * @param array $params
  * @param Parser $parser
  *
  * @since 1.8
  */
 public static function addQueryData($queryKey, SMWQuery $query, array $params, Parser $parser)
 {
     $mainSemanticData = SMWParseData::getSMWData($parser);
     $subject = $mainSemanticData->getSubject();
     $diSubWikiPage = new SMWDIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), "_QUERY" . $queryKey);
     $semanticData = new SMWContainerSemanticData($diSubWikiPage);
     $description = $query->getDescription();
     // Add query string
     $propertyDi = new SMWDIProperty('_ASKST');
     $valueDi = new SMWDIBlob($description->getQueryString());
     $semanticData->addPropertyObjectValue($propertyDi, $valueDi);
     // Add query size
     $propertyDi = new SMWDIProperty('_ASKSI');
     $valueDi = new SMWDINumber($description->getSize());
     $semanticData->addPropertyObjectValue($propertyDi, $valueDi);
     // Add query depth
     $propertyDi = new SMWDIProperty('_ASKDE');
     $valueDi = new SMWDINumber($description->getDepth());
     $semanticData->addPropertyObjectValue($propertyDi, $valueDi);
     // Add query format
     $propertyDi = new SMWDIProperty('_ASKFO');
     $valueDi = new SMWDIString($params['format']->getValue());
     $semanticData->addPropertyObjectValue($propertyDi, $valueDi);
     $propertyDi = new SMWDIProperty('_ASK');
     $subObjectDi = new SMWDIContainer($semanticData);
     SMWParseData::getSMWData($parser)->addPropertyObjectValue($propertyDi, $subObjectDi);
 }
Beispiel #5
0
 private function makeQueryResultForInstance(FederateResultSet $federateResultSet, Query $query)
 {
     $resultDataItems = array();
     foreach ($federateResultSet as $resultRow) {
         if (count($resultRow) > 0) {
             $dataItem = Exporter::findDataItemForExpElement($resultRow[0]);
             if (!is_null($dataItem)) {
                 $resultDataItems[] = $dataItem;
             }
         }
     }
     if ($federateResultSet->numRows() > $query->getLimit()) {
         array_pop($resultDataItems);
         $hasFurtherResults = true;
     } else {
         $hasFurtherResults = false;
     }
     $result = new QueryResult($query->getDescription()->getPrintrequests(), $query, $resultDataItems, $this->store, $hasFurtherResults);
     switch ($federateResultSet->getErrorCode()) {
         case FederateResultSet::ERROR_NOERROR:
             break;
         case FederateResultSet::ERROR_INCOMPLETE:
             $result->addErrors(array(wfMessage('smw_db_sparqlqueryincomplete')->inContentLanguage()->text()));
             break;
         default:
             $result->addErrors(array(wfMessage('smw_db_sparqlqueryproblem')->inContentLanguage()->text()));
             break;
     }
     return $result;
 }
 /**
  * @since 2.1
  *
  * @param Title $title
  * @param Query $query
  * @param string $format
  * @param integer|null $duration
  *
  * @return ProfileAnnotator
  */
 public function newJointProfileAnnotator(Title $title, Query $query, $format, $duration = null)
 {
     $profiler = new NullProfile(new Subobject($title), $query->getHash());
     $profiler = new DescriptionProfile($profiler, $query->getDescription());
     $profiler = new FormatProfile($profiler, $format);
     $profiler = new DurationProfile($profiler, $duration);
     return $profiler;
 }
 /**
  * @since 2.1
  *
  * @param Query $query
  * @param string $format
  * @param integer|null $duration
  *
  * @return ProfileAnnotator
  */
 public function newJointProfileAnnotator(Query $query, $format, $duration = null)
 {
     $nullProfileAnnotator = new NullProfileAnnotator(new Subobject($query->getSubject()->getTitle()), $query->getQueryId());
     $descriptionProfileAnnotator = new DescriptionProfileAnnotator($nullProfileAnnotator, $query->getDescription());
     $formatProfileAnnotator = new FormatProfileAnnotator($descriptionProfileAnnotator, $format);
     $durationProfileAnnotator = new DurationProfileAnnotator($formatProfileAnnotator, $duration);
     return $durationProfileAnnotator;
 }
 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());
 }
 /**
  * #1416 create container manually to avoid any issues that may arise from
  * a failed Title::makeTitleSafe.
  */
 private function newDIContainer(Query $query)
 {
     $subject = $query->getContextPage();
     if ($subject === null) {
         $containerSemanticData = ContainerSemanticData::makeAnonymousContainer();
     } else {
         $subject = new DIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), $query->getQueryId());
         $containerSemanticData = new ContainerSemanticData($subject);
     }
     return new DIContainer($containerSemanticData);
 }
 public function testGetImmediateEmptyQueryResultForLimitLessThanOne()
 {
     $connection = $this->getMockBuilder('\\SMW\\MediaWiki\\Database')->disableOriginalConstructor()->getMock();
     $this->store->expects($this->never())->method('getConnection')->will($this->returnValue($connection));
     $this->querySegmentListBuilder->expects($this->any())->method('getErrors')->will($this->returnValue(array()));
     $description = $this->getMockForAbstractClass('\\SMW\\Query\\Language\\Description');
     $instance = new QueryEngine($this->store, $this->querySegmentListBuilder, $this->querySegmentListProcessor, $this->engineOptions);
     $query = new Query($description);
     $query->setUnboundLimit(-1);
     $this->assertInstanceOf('\\SMWQueryResult', $instance->getQueryResult($query));
 }
	/**
	 * Enable a NotifyMe with specified id and querystring
	 *
	 * used for inline query only
	 */
	static public function enableNotify( $notify_id, $querystring, &$msg = NULL ) {
		wfProfileIn( 'SMWNotifyProcessor::enableNotify (SMW)' );

		$sStore = NMStorage::getDatabase();
		global $smwgQDefaultNamespaces;

		SMWQueryProcessor::processFunctionParams( SMWNotifyProcessor::getQueryRawParams( $querystring ), $querystring, $params, $printouts );
		$relatedArticles = array();
		foreach ( $printouts as $po ) {
			$printoutArticles[] = array(
				'namespace' => SMW_NS_PROPERTY,
				'title' => Title::makeTitle( SMW_NS_PROPERTY, $po->getText( SMW_OUTPUT_WIKI ) )->getDBkey() );
		}

		$qp = new SMWNotifyParser( $notify_id, $printoutArticles );
		$qp->setDefaultNamespaces( $smwgQDefaultNamespaces );
		$desc = $qp->getQueryDescription( $querystring );

		if ( !$qp->m_result ) {
			$qp->m_errors[] = wfMsg( 'smw_nm_proc_pagenotexist' );
		}

		if ( isset( $msg ) && $qp->hasSubquery() ) {
			$msg .= "\n" . wfMsg( 'smw_nm_proc_subqueryhint' );
		}

		$query = new SMWQuery( $desc, true, false );
		$query->setQueryString( $querystring );
		$query->addErrors( $qp->getErrors() ); // keep parsing errors for later output

		$res = $sStore->getNMQueryResult( $query );

		if ( count( $query->getErrors() ) > 0 ) {
			if ( isset( $msg ) ) {
				$msg .= "\n\n" . implode( '\n', $query->getErrors() ) . "\n\n" . wfMsg( 'smw_nm_proc_enablehint' );
			}
			$sStore->disableNotifyState( $notify_id );
			wfProfileOut( 'SMWNotifyProcessor::enableNotify (SMW)' );
			return false;
		}

		$sStore->updateNMSql( $notify_id, $res['sql'], $res['tmp_hierarchy'] );
		if ( count( $res['page_ids'] ) > 0 ) {
			$add_monitor = array();
			foreach ( $res['page_ids'] as $page_id ) {
				$add_monitor[] = array( 'notify_id' => $notify_id, 'page_id' => $page_id );
			}
			$sStore->addNotifyMonitor( $add_monitor );
		}
		$sStore->updateNotifyState( $notify_id, 1 );
		wfProfileOut( 'SMWNotifyProcessor::enableNotify (SMW)' );

		return true;
	}
Beispiel #12
0
 public function testEmptyGetQueryResultWhereQueryContainsErrors()
 {
     $connection = $this->getMockBuilder('\\SMW\\SPARQLStore\\RepositoryConnection')->disableOriginalConstructor()->getMockForAbstractClass();
     $store = $this->getMockBuilder('\\SMW\\Store')->disableOriginalConstructor()->getMockForAbstractClass();
     $compoundConditionBuilder = $this->getMockBuilder('\\SMW\\SPARQLStore\\QueryEngine\\CompoundConditionBuilder')->disableOriginalConstructor()->getMock();
     $description = $this->getMockForAbstractClass('\\SMW\\Query\\Language\\Description');
     $engineOptions = new EngineOptions();
     $engineOptions->set('smwgIgnoreQueryErrors', false);
     $instance = new QueryEngine($connection, $compoundConditionBuilder, new QueryResultFactory($store), $engineOptions);
     $query = new Query($description);
     $query->addErrors(array('Foo'));
     $this->assertInstanceOf('\\SMWQueryResult', $instance->getQueryResult($query));
     $this->assertEmpty($instance->getQueryResult($query)->getResults());
 }
 public function testNullQueryResult()
 {
     $term = '[[Some_string_to_query]]';
     $description = new ValueDescription(new DIWikiPage($term, NS_MAIN), null);
     $query = new Query($description, false, false);
     $query->querymode = Query::MODE_INSTANCES;
     $description = $query->getDescription();
     $namespacesDisjunction = new Disjunction(array_map(function ($ns) {
         return new NamespaceDescription($ns);
     }, array(NS_MAIN)));
     $description = new Conjunction(array($description, $namespacesDisjunction));
     $query->setDescription($description);
     $this->assertInstanceOf('\\SMWQueryResult', ApplicationFactory::getInstance()->getStore()->getQueryResult($query));
 }
 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'));
 }
 /**
  * @since 2.5
  *
  * @param Query $query
  *
  * @return string
  */
 public static function get(Query $query)
 {
     $serialized = array();
     $serialized['conditions'] = $query->getQueryString();
     $serialized['parameters'] = array('limit=' . $query->getLimit(), 'offset=' . $query->getOffset(), 'mainlabel=' . $query->getMainlabel());
     if ($query->getQuerySource() !== null && $query->getQuerySource() !== '') {
         $serialized['parameters'] = array_merge($serialized['parameters'], array('source=' . $query->getQuerySource()));
     }
     list($serialized['sort'], $serialized['order']) = self::doSerializeSortKeys($query);
     $serialized['printouts'] = self::doSerializePrintouts($query);
     $encoded = $serialized['conditions'] . '|' . ($serialized['printouts'] !== array() ? implode('|', $serialized['printouts']) . '|' : '') . implode('|', $serialized['parameters']) . ($serialized['sort'] !== array() ? '|sort=' . implode(',', $serialized['sort']) : '') . ($serialized['order'] !== array() ? '|order=' . implode(',', $serialized['order']) : '');
     return $encoded;
 }
Beispiel #16
0
 /**
  * Returns a serialized SMWQueryResult object with additional meta data
  *
  * This methods extends the serializeToArray() for additional meta
  * that are useful when handling data via the api
  *
  * @note should be used instead of SMWQueryResult::serializeToArray()
  * as this method contains additional informaion
  *
  * @since 1.9
  *
  * @return array
  */
 public function toArray()
 {
     // @note micro optimization: We call getSerializedQueryResult()
     // only once and create the hash here instead of calling getHash()
     // to avoid getSerializedQueryResult() being called again
     // @note count + offset equals total therefore we deploy both values
     $serializeArray = $this->serializeToArray();
     return array_merge($serializeArray, array('meta' => array('hash' => md5(FormatJson::encode($serializeArray)), 'count' => $this->getCount(), 'offset' => $this->mQuery->getOffset())));
 }
 /**
  * Returns a serialized SMWQueryResult object with additional meta data
  *
  * This methods extends the serializeToArray() for additional meta
  * that are useful when handling data via the api
  *
  * @note should be used instead of SMWQueryResult::serializeToArray()
  * as this method contains additional informaion
  *
  * @since 1.9
  *
  * @return array
  */
 public function toArray()
 {
     $time = microtime(true);
     // @note micro optimization: We call getSerializedQueryResult()
     // only once and create the hash here instead of calling getHash()
     // to avoid getSerializedQueryResult() being called again
     // @note count + offset equals total therefore we deploy both values
     $serializeArray = $this->serializeToArray();
     return array_merge($serializeArray, array('meta' => array('hash' => HashBuilder::createHashIdForContent($serializeArray), 'count' => $this->getCount(), 'offset' => $this->mQuery->getOffset(), 'source' => $this->mQuery->getQuerySource(), 'time' => number_format(microtime(true) - $time, 6, '.', ''))));
 }
 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));
 }
 /**
  * @since 2.5
  *
  * @param Query|null $query
  *
  * @return string
  */
 public function getFormattedErrorString(Query $query = null)
 {
     if ($query === null || !is_array($query->getErrors()) || $query->getErrors() === array()) {
         return '';
     }
     $errors = array();
     foreach (ProcessingErrorMsgHandler::normalizeMessages($query->getErrors()) as $value) {
         if ($value === '') {
             continue;
         }
         if (is_array($value)) {
             $value = implode(" ", $value);
         }
         $errors[] = $value;
     }
     if (count($errors) > 1) {
         $error = '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
     } else {
         $error = implode(' ', $errors);
     }
     return Html::rawElement('div', array('class' => 'smw-callout smw-callout-error'), $error);
 }
Beispiel #20
0
 /**
  * Returns an SMWInfolink object with the QueryResults print requests as parameters.
  *
  * @since 1.8
  *
  * @return SMWInfolink
  */
 public function getLink()
 {
     $params = array(trim($this->mQuery->getQueryString()));
     foreach ($this->mQuery->getExtraPrintouts() as $printout) {
         $serialization = $printout->getSerialisation();
         // TODO: this is a hack to get rid of the mainlabel param in case it was automatically added
         // by SMWQueryProcessor::addThisPrintout. Should be done nicer when this link creation gets redone.
         if ($serialization !== '?#') {
             $params[] = $serialization;
         }
     }
     // Note: the initial : prevents SMW from reparsing :: in the query string.
     return SMWInfolink::newInternalLink('', ':Special:Ask', false, $params);
 }
 /**
  * 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;
 }
Beispiel #22
0
 /**
  * Create an SMWInfolink object representing a link to further query results.
  * This link can then be serialised or extended by further params first.
  * The optional $caption can be used to set the caption of the link (though this
  * can also be changed afterwards with SMWInfolink::setCaption()). If empty, the
  * message 'smw_iq_moreresults' is used as a caption.
  * 
  * TODO: have this work for all params without manually overriding and adding everything
  * (this is possible since the param handling changes in 1.7) 
  * 
  * @param string|false $caption
  * 
  * @return SMWInfolink
  */
 public function getQueryLink($caption = false)
 {
     $params = array(trim($this->mQuery->getQueryString()));
     foreach ($this->mQuery->getExtraPrintouts() as $printout) {
         $serialization = $printout->getSerialisation();
         // TODO: this is a hack to get rid of the mainlabel param in case it was automatically added
         // by SMWQueryProcessor::addThisPrintout. Should be done nicer when this link creation gets redone.
         if ($serialization !== '?#') {
             $params[] = $serialization;
         }
     }
     if ($this->mQuery->getMainLabel() !== false) {
         $params['mainlabel'] = $this->mQuery->getMainLabel();
     }
     $params['offset'] = $this->mQuery->getOffset() + count($this->mResults);
     if ($params['offset'] === 0) {
         unset($params['offset']);
     }
     if ($this->mQuery->getLimit() > 0) {
         $params['limit'] = $this->mQuery->getLimit();
     }
     if (count($this->mQuery->sortkeys) > 0) {
         $order = implode(',', $this->mQuery->sortkeys);
         $sort = implode(',', array_keys($this->mQuery->sortkeys));
         if ($sort !== '' || $order != 'ASC') {
             $params['order'] = $order;
             $params['sort'] = $sort;
         }
     }
     if ($caption == false) {
         $caption = ' ' . wfMsgForContent('smw_iq_moreresults');
         // The space is right here, not in the QPs!
     }
     // Note: the initial : prevents SMW from reparsing :: in the query string.
     $result = SMWInfolink::newInternalLink($caption, ':Special:Ask', false, $params);
     return $result;
 }
 /**
  * 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();
                 }
             }
         }
     }
 }
 /**
  * @since 2.5
  *
  * @param Store $store
  * @param Query $query
  * @param DIWikiPage[]|[] $results = array()
  * @param boolean $continue
  *
  * @return QueryResult
  */
 public function newQueryResult(Store $store, Query $query, $results = array(), $continue = false)
 {
     $queryResult = new QueryResult($query->getDescription()->getPrintrequests(), $query, $results, $store, $continue);
     return $queryResult;
 }
 private function createConceptFor(DIWikiPage $concept, Description $description, $documentation = '')
 {
     $semanticData = $this->semanticDataFactory->setSubject($concept)->newEmptySemanticData();
     $query = new Query($description, false, true);
     $semanticData->addPropertyObjectValue(new DIProperty('_CONC'), new DIConcept($query->getDescription()->getQueryString(), $documentation, $query->getDescription()->getQueryFeatures(), $query->getDescription()->getSize(), $query->getDescription()->getDepth()));
     $this->getStore()->updateData($semanticData);
 }
 /**
  * Get a SPARQL option array for the given query.
  *
  * @param SMWQuery $query
  * @param SMWSparqlCondition $sparqlCondition (storing order by variable names)
  * @return array
  */
 protected function getSparqlOptions(SMWQuery $query, SMWSparqlCondition $sparqlCondition)
 {
     global $smwgQSortingSupport, $smwgQRandSortingSupport;
     $result = array('LIMIT' => $query->getLimit() + 1, 'OFFSET' => $query->getOffset());
     // Build ORDER BY options using discovered sorting fields.
     if ($smwgQSortingSupport) {
         $orderByString = '';
         foreach ($this->m_sortkeys as $propkey => $order) {
             if ($order != 'RANDOM' && array_key_exists($propkey, $sparqlCondition->orderVariables)) {
                 $orderByString .= "{$order}(?" . $sparqlCondition->orderVariables[$propkey] . ") ";
             } elseif ($order == 'RANDOM' && $smwgQRandSortingSupport) {
                 // not supported in SPARQL; might be possible via function calls in some stores
             }
         }
         if ($orderByString !== '') {
             $result['ORDER BY'] = $orderByString;
         }
     }
     return $result;
 }
 /**
  * Get a SQL option array for the given query and preprocessed query object at given id.
  *
  * @param SMWQuery $query
  * @param integer $rootid
  */
 protected function getSQLOptions(SMWQuery $query, $rootid)
 {
     global $smwgQSortingSupport, $smwgQRandSortingSupport;
     $result = array('LIMIT' => $query->getLimit() + 1, 'OFFSET' => $query->getOffset());
     // Build ORDER BY options using discovered sorting fields.
     if ($smwgQSortingSupport) {
         $qobj = $this->m_queries[$rootid];
         foreach ($this->m_sortkeys as $propkey => $order) {
             if ($order != 'RANDOM' && array_key_exists($propkey, $qobj->sortfields)) {
                 // Field was successfully added.
                 $result['ORDER BY'] = (array_key_exists('ORDER BY', $result) ? $result['ORDER BY'] . ', ' : '') . $qobj->sortfields[$propkey] . " {$order} ";
             } elseif ($order == 'RANDOM' && $smwgQRandSortingSupport) {
                 $result['ORDER BY'] = (array_key_exists('ORDER BY', $result) ? $result['ORDER BY'] . ', ' : '') . ' RAND() ';
             }
         }
     }
     return $result;
 }
 /**
  * @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());
     }
 }
Beispiel #30
0
 /**
  * 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;
 }