/**
  * 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}";
         }
     }
 }