/**
  * @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->queryBuilder->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->queryBuilder->getStore()->findPropertyTableID(new DIProperty('_INST'));
         $query->joinfield = "{$query->alias}.s_id";
         $query->components[$cqid] = "{$query->alias}.o_id";
         $this->queryBuilder->addQuerySegmentForId($cqid, $cquery);
     }
     return $query;
 }
 /**
  * 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->queryBuilder->getStore()->getConnection('mw.db');
     $property = $description->getProperty();
     $tableid = $this->queryBuilder->getStore()->findPropertyTableID($property);
     if ($tableid === '') {
         // Give up
         $query->type = QuerySegment::Q_NOQUERY;
         return;
     }
     $proptables = $this->queryBuilder->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->queryBuilder->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->queryBuilder->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";
         $this->queryBuilder->addQuerySegmentForId($pqid, $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->queryBuilder->buildQuerySegmentFor($description->getDescription());
         if ($sub >= 0) {
             $query->components[$sub] = "{$query->alias}.{$o_id}";
         }
         if (array_key_exists($sortkey, $this->queryBuilder->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::tableName) . " 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->queryBuilder->getSortKeys())) {
             $query->sortfields[$sortkey] = "{$query->alias}.{$indexField}";
         }
     }
 }
 public function testWhenSomeQuerySegments_getQuerySegmentsReturnsThemAll()
 {
     $instance = new QueryBuilder($this->store);
     $firstQuerySegment = new QuerySegment();
     $secondQuerySegment = new QuerySegment();
     $instance->addQuerySegmentForId(42, $firstQuerySegment);
     $instance->addQuerySegmentForId(23, $secondQuerySegment);
     $this->assertSame(array(42 => $firstQuerySegment, 23 => $secondQuerySegment), $instance->getQuerySegments());
 }