/**
  * 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;
 }
 /**
  * @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);
 }
Esempio n. 4
0
 /**
  * @since  2.0
  * @param  Query $query
  *
  * @return QueryResult|string
  */
 public function getQueryResult(Query $query)
 {
     if ((!$this->engineOptions->get('smwgIgnoreQueryErrors') || $query->getDescription() instanceof ThingDescription) && $query->querymode != Query::MODE_DEBUG && count($query->getErrors()) > 0) {
         return $this->queryResultFactory->newEmptyQueryResult($query, false);
     }
     // don't query, but return something to the printer
     if ($query->querymode == Query::MODE_NONE) {
         return $this->queryResultFactory->newEmptyQueryResult($query, true);
     }
     if ($query->querymode == Query::MODE_DEBUG) {
         return $this->getDebugQueryResult($query);
     } elseif ($query->querymode == Query::MODE_COUNT) {
         return $this->getCountQueryResult($query);
     }
     return $this->getInstanceQueryResult($query);
 }
 /**
  * @since  2.0
  * @param  Query $query
  *
  * @return QueryResult|string
  */
 public function getQueryResult(Query $query)
 {
     if ((!$this->engineOptions->get('smwgIgnoreQueryErrors') || $query->getDescription() instanceof ThingDescription) && $query->querymode != Query::MODE_DEBUG && count($query->getErrors()) > 0) {
         return $this->queryResultFactory->newEmptyQueryResult($query, false);
     }
     // don't query, but return something to the printer
     if ($query->querymode == Query::MODE_NONE || $query->getLimit() < 1) {
         return $this->queryResultFactory->newEmptyQueryResult($query, true);
     }
     $this->compoundConditionBuilder->setSortKeys($query->sortkeys);
     $compoundCondition = $this->compoundConditionBuilder->buildCondition($query->getDescription());
     $query->addErrors($this->compoundConditionBuilder->getErrors());
     if ($query->querymode == Query::MODE_DEBUG) {
         return $this->getDebugQueryResult($query, $compoundCondition);
     } elseif ($query->querymode == Query::MODE_COUNT) {
         return $this->getCountQueryResult($query, $compoundCondition);
     }
     return $this->getInstanceQueryResult($query, $compoundCondition);
 }
Esempio n. 6
0
 /**
  * Return error array, possibly empty.
  *
  * @return array
  */
 public function getErrors()
 {
     // Just use query errors, as no errors generated in this class at the moment.
     return $this->mQuery->getErrors();
 }
 /**
  * The new SQL store's implementation of query answering. This function
  * works in two stages: First, the nested conditions of the given query
  * object are preprocessed to compute an abstract representation of the
  * SQL query that is to be executed. Since query conditions correspond to
  * joins with property tables in most cases, this abstract representation
  * is essentially graph-like description of how property tables are joined.
  * Moreover, this graph is tree-shaped, since all query conditions are
  * tree-shaped. Each part of this abstract query structure is represented
  * by an SMWSQLStore3Query object in the array m_queries.
  *
  * As a second stage of processing, the thus prepared SQL query is actually
  * executed. Typically, this means that the joins are collapsed into one
  * SQL query to retrieve results. In some cases, such as in dbug mode, the
  * execution might be restricted and not actually perform the whole query.
  *
  * The two-stage process helps to separate tasks, and it also allows for
  * better optimisations: it is left to the execution engine how exactly the
  * query result is to be obtained. For example, one could pre-compute
  * partial suib-results in temporary tables (or even cache them somewhere),
  * instead of passing one large join query to the DB (of course, it might
  * be large only if the configuration of SMW allows it). For some DBMS, a
  * step-wise execution of the query might lead to better performance, since
  * it exploits the tree-structure of the joins, which is important for fast
  * processing -- not all DBMS might be able in seeing this by themselves.
  *
  * @param SMWQuery $query
  *
  * @return mixed: depends on $query->querymode
  */
 public function getQueryResult(SMWQuery $query)
 {
     global $smwgIgnoreQueryErrors, $smwgQSortingSupport;
     if ((!$smwgIgnoreQueryErrors || $query->getDescription() instanceof SMWThingDescription) && $query->querymode != SMWQuery::MODE_DEBUG && count($query->getErrors()) > 0) {
         return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->m_store, false);
         // NOTE: we check this here to prevent unnecessary work, but we check
         // it after query processing below again in case more errors occurred.
     } elseif ($query->querymode == SMWQuery::MODE_NONE) {
         // don't query, but return something to printer
         return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->m_store, true);
     }
     $this->m_qmode = $query->querymode;
     $this->m_queries = array();
     $this->m_hierarchies = array();
     $this->m_querylog = array();
     $this->m_errors = array();
     SMWSQLStore3Query::$qnum = 0;
     $this->m_sortkeys = $query->sortkeys;
     // *** First compute abstract representation of the query (compilation) ***//
     wfProfileIn('SMWSQLStore3Queries::compileMainQuery (SMW)');
     $qid = $this->compileQueries($query->getDescription());
     // compile query, build query "plan"
     wfProfileOut('SMWSQLStore3Queries::compileMainQuery (SMW)');
     if ($qid < 0) {
         // no valid/supported condition; ensure that at least only proper pages are delivered
         $qid = SMWSQLStore3Query::$qnum;
         $q = new SMWSQLStore3Query();
         $q->jointable = SMWSql3SmwIds::tableName;
         $q->joinfield = "{$q->alias}.smw_id";
         $q->where = "{$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL3_SMWIW_OUTDATED) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL3_SMWREDIIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL3_SMWBORDERIW) . " AND {$q->alias}.smw_iw!=" . $this->m_dbs->addQuotes(SMW_SQL3_SMWINTDEFIW);
         $this->m_queries[$qid] = $q;
     }
     if ($this->m_queries[$qid]->jointable != SMWSql3SmwIds::tableName) {
         // manually make final root query (to retrieve namespace,title):
         $rootid = SMWSQLStore3Query::$qnum;
         $qobj = new SMWSQLStore3Query();
         $qobj->jointable = SMWSql3SmwIds::tableName;
         $qobj->joinfield = "{$qobj->alias}.smw_id";
         $qobj->components = array($qid => "{$qobj->alias}.smw_id");
         $qobj->sortfields = $this->m_queries[$qid]->sortfields;
         $this->m_queries[$rootid] = $qobj;
     } else {
         // not such a common case, but worth avoiding the additional inner join:
         $rootid = $qid;
     }
     // Include order conditions (may extend query if needed for sorting):
     if ($smwgQSortingSupport) {
         $this->applyOrderConditions($rootid);
     }
     // Possibly stop if new errors happened:
     if (!$smwgIgnoreQueryErrors && $query->querymode != SMWQuery::MODE_DEBUG && count($this->m_errors) > 0) {
         $query->addErrors($this->m_errors);
         return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->m_store, false);
     }
     // *** Now execute the computed query ***//
     wfProfileIn('SMWSQLStore3Queries::executeMainQuery (SMW)');
     $this->executeQueries($this->m_queries[$rootid]);
     // execute query tree, resolve all dependencies
     wfProfileOut('SMWSQLStore3Queries::executeMainQuery (SMW)');
     switch ($query->querymode) {
         case SMWQuery::MODE_DEBUG:
             $result = $this->getDebugQueryResult($query, $rootid);
             break;
         case SMWQuery::MODE_COUNT:
             $result = $this->getCountQueryResult($query, $rootid);
             break;
         default:
             $result = $this->getInstanceQueryResult($query, $rootid);
             break;
     }
     $this->cleanUp();
     $query->addErrors($this->m_errors);
     return $result;
 }
Esempio n. 8
0
 /**
  * The new SQL store's implementation of query answering. This function
  * works in two stages: First, the nested conditions of the given query
  * object are preprocessed to compute an abstract representation of the
  * SQL query that is to be executed. Since query conditions correspond to
  * joins with property tables in most cases, this abstract representation
  * is essentially graph-like description of how property tables are joined.
  * Moreover, this graph is tree-shaped, since all query conditions are
  * tree-shaped. Each part of this abstract query structure is represented
  * by an QuerySegment object in the array m_queries.
  *
  * As a second stage of processing, the thus prepared SQL query is actually
  * executed. Typically, this means that the joins are collapsed into one
  * SQL query to retrieve results. In some cases, such as in dbug mode, the
  * execution might be restricted and not actually perform the whole query.
  *
  * The two-stage process helps to separate tasks, and it also allows for
  * better optimisations: it is left to the execution engine how exactly the
  * query result is to be obtained. For example, one could pre-compute
  * partial suib-results in temporary tables (or even cache them somewhere),
  * instead of passing one large join query to the DB (of course, it might
  * be large only if the configuration of SMW allows it). For some DBMS, a
  * step-wise execution of the query might lead to better performance, since
  * it exploits the tree-structure of the joins, which is important for fast
  * processing -- not all DBMS might be able in seeing this by themselves.
  *
  * @param Query $query
  *
  * @return mixed depends on $query->querymode
  */
 public function getQueryResult(Query $query)
 {
     if ((!$this->engineOptions->get('smwgIgnoreQueryErrors') || $query->getDescription() instanceof ThingDescription) && $query->querymode != Query::MODE_DEBUG && count($query->getErrors()) > 0) {
         return new QueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->store, false);
         // NOTE: we check this here to prevent unnecessary work, but we check
         // it after query processing below again in case more errors occurred.
     } elseif ($query->querymode == Query::MODE_NONE) {
         // don't query, but return something to printer
         return new QueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->store, true);
     }
     $db = $this->store->getConnection('mw.db');
     $this->queryMode = $query->querymode;
     $this->querySegments = array();
     $this->errors = array();
     QuerySegment::$qnum = 0;
     $this->sortKeys = $query->sortkeys;
     // *** First compute abstract representation of the query (compilation) ***//
     $this->queryBuilder->setSortKeys($this->sortKeys);
     $this->queryBuilder->buildQuerySegmentFor($query->getDescription());
     // compile query, build query "plan"
     $qid = $this->queryBuilder->getLastQuerySegmentId();
     $this->querySegments = $this->queryBuilder->getQuerySegments();
     $this->errors = $this->queryBuilder->getErrors();
     if ($qid < 0) {
         // no valid/supported condition; ensure that at least only proper pages are delivered
         $qid = QuerySegment::$qnum;
         $q = new QuerySegment();
         $q->joinTable = SMWSql3SmwIds::tableName;
         $q->joinfield = "{$q->alias}.smw_id";
         $q->where = "{$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWIW_OUTDATED) . " AND {$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWREDIIW) . " AND {$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWBORDERIW) . " AND {$q->alias}.smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWINTDEFIW);
         $this->querySegments[$qid] = $q;
     }
     if ($this->querySegments[$qid]->joinTable != SMWSql3SmwIds::tableName) {
         // manually make final root query (to retrieve namespace,title):
         $rootid = QuerySegment::$qnum;
         $qobj = new QuerySegment();
         $qobj->joinTable = SMWSql3SmwIds::tableName;
         $qobj->joinfield = "{$qobj->alias}.smw_id";
         $qobj->components = array($qid => "{$qobj->alias}.smw_id");
         $qobj->sortfields = $this->querySegments[$qid]->sortfields;
         $this->querySegments[$rootid] = $qobj;
     } else {
         // not such a common case, but worth avoiding the additional inner join:
         $rootid = $qid;
     }
     // Include order conditions (may extend query if needed for sorting):
     if ($this->engineOptions->get('smwgQSortingSupport')) {
         $this->applyOrderConditions($rootid);
     }
     // Possibly stop if new errors happened:
     if (!$this->engineOptions->get('smwgIgnoreQueryErrors') && $query->querymode != Query::MODE_DEBUG && count($this->errors) > 0) {
         $query->addErrors($this->errors);
         return new QueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this->store, false);
     }
     // *** Now execute the computed query ***//
     $this->querySegmentListResolver->setQueryMode($this->queryMode);
     $this->querySegmentListResolver->setQuerySegmentList($this->querySegments);
     // execute query tree, resolve all dependencies
     $this->querySegmentListResolver->resolveForSegmentId($rootid);
     switch ($query->querymode) {
         case Query::MODE_DEBUG:
             $result = $this->getDebugQueryResult($query, $rootid);
             break;
         case Query::MODE_COUNT:
             $result = $this->getCountQueryResult($query, $rootid);
             break;
         default:
             $result = $this->getInstanceQueryResult($query, $rootid);
             break;
     }
     $this->querySegmentListResolver->cleanUp();
     $query->addErrors($this->errors);
     return $result;
 }
Esempio n. 9
0
 /**
  * @see SMWStore::getQueryResult()
  * @since 1.6
  */
 public function getQueryResult(SMWQuery $query)
 {
     global $smwgIgnoreQueryErrors;
     if ((!$smwgIgnoreQueryErrors || $query->getDescription() instanceof SMWThingDescription) && $query->querymode != SMWQuery::MODE_DEBUG && count($query->getErrors()) > 0) {
         return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this, false);
     }
     if ($query->querymode == SMWQuery::MODE_NONE) {
         // don't query, but return something to printer
         return new SMWQueryResult($query->getDescription()->getPrintrequests(), $query, array(), $this, true);
     } elseif ($query->querymode == SMWQuery::MODE_DEBUG) {
         $queryEngine = new SMWSparqlStoreQueryEngine($this);
         return $queryEngine->getDebugQueryResult($query);
     } elseif ($query->querymode == SMWQuery::MODE_COUNT) {
         $queryEngine = new SMWSparqlStoreQueryEngine($this);
         return $queryEngine->getCountQueryResult($query);
     } else {
         $queryEngine = new SMWSparqlStoreQueryEngine($this);
         return $queryEngine->getInstanceQueryResult($query);
     }
 }
	public function getQueryResult( SMWQuery $query ) {
		global $smwgIgnoreQueryErrors;

		if ( !$smwgIgnoreQueryErrors &&
		     ( $query->querymode != SMWQuery::MODE_DEBUG ) &&
		     ( count( $query->getErrors() ) > 0 ) ) {
			return new SMWQueryResult( $query->getDescription()->getPrintrequests(), $query, array(), $this, false );
			// NOTE: we check this here to prevent unnecessary work, but we may need to check it after query processing below again in case more errors occurred
		}

		if ( $query->querymode == SMWQuery::MODE_NONE ) { // don't query, but return something to printer
			return new SMWQueryResult( $query->getDescription()->getPrintrequests(), $query, array(), $this, true );
		} elseif ( $query->querymode == SMWQuery::MODE_DEBUG ) {
			$queryEngine = new SMWSparqlStoreQueryEngine( $this );
			return $queryEngine->getDebugQueryResult( $query ); 
		} elseif ( $query->querymode == SMWQuery::MODE_COUNT ) {
			$queryEngine = new SMWSparqlStoreQueryEngine( $this );
			return $queryEngine->getCountQueryResult( $query ); 
		} else {
			$queryEngine = new SMWSparqlStoreQueryEngine( $this );
			return $queryEngine->getInstanceQueryResult( $query ); 
		}
	}