/**
  * @since 2.2
  *
  * @param Description $description
  *
  * @return QuerySegment
  */
 public function interpretDescription(Description $description)
 {
     $query = new QuerySegment();
     $cqid = QuerySegment::$qnum;
     $cquery = new QuerySegment();
     $cquery->type = QuerySegment::Q_CLASS_HIERARCHY;
     $cquery->joinfield = array();
     foreach ($description->getCategories() as $category) {
         $categoryId = $this->querySegmentListBuilder->getStore()->getObjectIds()->getSMWPageID($category->getDBkey(), NS_CATEGORY, $category->getInterwiki(), '');
         if ($categoryId != 0) {
             $cquery->joinfield[] = $categoryId;
         }
     }
     if (count($cquery->joinfield) == 0) {
         // Empty result.
         $query->type = QuerySegment::Q_VALUE;
         $query->joinTable = '';
         $query->joinfield = '';
     } else {
         // Instance query with disjunction of classes (categories)
         $query->joinTable = $this->querySegmentListBuilder->getStore()->findPropertyTableID(new DIProperty('_INST'));
         $query->joinfield = "{$query->alias}.s_id";
         $query->components[$cqid] = "{$query->alias}.o_id";
         $this->querySegmentListBuilder->addQuerySegment($cquery);
     }
     return $query;
 }
 public function testWhenSomeQuerySegments_getQuerySegmentListReturnsThemAll()
 {
     $instance = new QuerySegmentListBuilder($this->store);
     $firstQuerySegment = new QuerySegment();
     $firstQuerySegment->segmentNumber = 42;
     $instance->addQuerySegment($firstQuerySegment);
     $secondQuerySegment = new QuerySegment();
     $secondQuerySegment->segmentNumber = 23;
     $instance->addQuerySegment($secondQuerySegment);
     $expected = array(42 => $firstQuerySegment, 23 => $secondQuerySegment);
     $this->assertSame($expected, $instance->getQuerySegmentList());
 }
 /**
  * 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 || $query->getLimit() < 1) {
         // 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.queryengine');
     $this->queryMode = $query->querymode;
     $this->querySegmentList = array();
     $this->errors = array();
     QuerySegment::$qnum = 0;
     $this->sortKeys = $query->sortkeys;
     // Anchor IT_TABLE as root element
     $rootSegmentNumber = QuerySegment::$qnum;
     $rootSegment = new QuerySegment();
     $rootSegment->joinTable = SMWSql3SmwIds::TABLE_NAME;
     $rootSegment->joinfield = "{$rootSegment->alias}.smw_id";
     $this->querySegmentListBuilder->addQuerySegment($rootSegment);
     // *** First compute abstract representation of the query (compilation) ***//
     $this->querySegmentListBuilder->setSortKeys($this->sortKeys);
     $this->querySegmentListBuilder->getQuerySegmentFrom($query->getDescription());
     // compile query, build query "plan"
     $qid = $this->querySegmentListBuilder->getLastQuerySegmentId();
     $this->querySegmentList = $this->querySegmentListBuilder->getQuerySegmentList();
     $this->errors = $this->querySegmentListBuilder->getErrors();
     if ($qid < 0) {
         // no valid/supported condition; ensure that at least only proper pages are delivered
         $qid = $rootSegmentNumber;
         $q = $this->querySegmentList[$rootSegmentNumber];
         $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->querySegmentList[$rootSegmentNumber] = $q;
     }
     if (isset($this->querySegmentList[$qid]->joinTable) && $this->querySegmentList[$qid]->joinTable != SMWSql3SmwIds::TABLE_NAME) {
         // manually make final root query (to retrieve namespace,title):
         $rootid = $rootSegmentNumber;
         $qobj = $this->querySegmentList[$rootSegmentNumber];
         $qobj->components = array($qid => "{$qobj->alias}.smw_id");
         $qobj->sortfields = $this->querySegmentList[$qid]->sortfields;
         $this->querySegmentList[$rootSegmentNumber] = $qobj;
     } else {
         // not such a common case, but worth avoiding the additional inner join:
         $rootid = $qid;
     }
     // var_dump( json_encode( $this->querySegmentList, JSON_PRETTY_PRINT ) );
     // 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->querySegmentListProcessor->setQueryMode($this->queryMode);
     $this->querySegmentListProcessor->setQuerySegmentList($this->querySegmentList);
     // execute query tree, resolve all dependencies
     $this->querySegmentListProcessor->doResolveQueryDependenciesById($rootid);
     $this->applyExtraWhereCondition($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->querySegmentListProcessor->cleanUp();
     $query->addErrors($this->errors);
     return $result;
 }
 public function testWhenSomeQuerySegments_getQuerySegmentListReturnsThemAll()
 {
     $instance = new QuerySegmentListBuilder($this->store, $this->descriptionInterpreterFactory);
     $firstQuerySegment = new QuerySegment();
     $instance->addQuerySegment($firstQuerySegment);
     $secondQuerySegment = new QuerySegment();
     $instance->addQuerySegment($secondQuerySegment);
     $expected = array(0 => $firstQuerySegment, 1 => $secondQuerySegment);
     $this->assertSame($expected, $instance->getQuerySegmentList());
 }
 /**
  * Modify the given query object to account for some property condition for
  * the given property. If it is not possible to generate a query for the
  * given data, the query type is changed to QueryContainer::Q_NOQUERY. Callers need
  * to check for this and discard the query in this case.
  *
  * @note This method does not support sortkey (_SKEY) property queries,
  * since they do not have a normal property table. This should not be a
  * problem since comparators on sortkeys are supported indirectly when
  * using comparators on wikipages. There is no reason to create any
  * query with _SKEY ad users cannot do so either (no user label).
  *
  * @since 1.8
  */
 private function interpretPropertyConditionForDescription(QuerySegment $query, SomeProperty $description)
 {
     $db = $this->querySegmentListBuilder->getStore()->getConnection('mw.db');
     $property = $description->getProperty();
     $tableid = $this->querySegmentListBuilder->getStore()->findPropertyTableID($property);
     if ($tableid === '') {
         // Give up
         $query->type = QuerySegment::Q_NOQUERY;
         return;
     }
     $proptables = $this->querySegmentListBuilder->getStore()->getPropertyTables();
     $proptable = $proptables[$tableid];
     if (!$proptable->usesIdSubject()) {
         // no queries with such tables
         // (only redirects are affected in practice)
         $query->type = QuerySegment::Q_NOQUERY;
         return;
     }
     $typeid = $property->findPropertyTypeID();
     $diType = DataTypeRegistry::getInstance()->getDataItemId($typeid);
     if ($property->isInverse() && $diType !== DataItem::TYPE_WIKIPAGE) {
         // can only invert properties that point to pages
         $query->type = QuerySegment::Q_NOQUERY;
         return;
     }
     $diHandler = $this->querySegmentListBuilder->getStore()->getDataItemHandlerForDIType($diType);
     $indexField = $diHandler->getIndexField();
     // TODO: strictly speaking, the DB key is not what we want here,
     // since sortkey is based on a "wiki value"
     $sortkey = $property->getKey();
     // *** Now construct the query ... ***//
     $query->joinTable = $proptable->getName();
     // *** Add conditions for selecting rows for this property ***//
     if (!$proptable->isFixedPropertyTable()) {
         $pid = $this->querySegmentListBuilder->getStore()->getObjectIds()->getSMWPropertyID($property);
         // Construct property hierarchy:
         $pqid = QuerySegment::$qnum;
         $pquery = new QuerySegment();
         $pquery->type = QuerySegment::Q_PROP_HIERARCHY;
         $pquery->joinfield = array($pid);
         $query->components[$pqid] = "{$query->alias}.p_id";
         $pquery->segmentNumber = $pqid;
         $this->querySegmentListBuilder->addQuerySegment($pquery);
         // Alternative code without property hierarchies:
         // $query->where = "{$query->alias}.p_id=" . $this->m_dbs->addQuotes( $pid );
     }
     // else: no property column, no hierarchy queries
     // *** Add conditions on the value of the property ***//
     if ($diType === DataItem::TYPE_WIKIPAGE) {
         $o_id = $indexField;
         if ($property->isInverse()) {
             $s_id = $o_id;
             $o_id = 's_id';
         } else {
             $s_id = 's_id';
         }
         $query->joinfield = "{$query->alias}.{$s_id}";
         // process page description like main query
         $sub = $this->querySegmentListBuilder->buildQuerySegmentFor($description->getDescription());
         if ($sub >= 0) {
             $query->components[$sub] = "{$query->alias}.{$o_id}";
         }
         if (array_key_exists($sortkey, $this->querySegmentListBuilder->getSortKeys())) {
             // TODO: This SMW IDs table is possibly duplicated in the query.
             // Example: [[has capital::!Berlin]] with sort=has capital
             // Can we prevent that? (PERFORMANCE)
             $query->from = ' INNER JOIN ' . $db->tableName(SMWSql3SmwIds::TABLE_NAME) . " AS ids{$query->alias} ON ids{$query->alias}.smw_id={$query->alias}.{$o_id}";
             $query->sortfields[$sortkey] = "ids{$query->alias}.smw_sortkey";
         }
     } else {
         // non-page value description
         $query->joinfield = "{$query->alias}.s_id";
         $this->interpretInnerValueDescription($query, $description->getDescription(), $proptable, $diHandler, 'AND');
         if (array_key_exists($sortkey, $this->querySegmentListBuilder->getSortKeys())) {
             $query->sortfields[$sortkey] = "{$query->alias}.{$indexField}";
         }
     }
 }