/**
  * For a given SMW type id, obtain the "signature" from which the
  * appropriate property table and information about sorting/filtering
  * data of this type can be obtained. The result is an array of three
  * entries: a signature string, the index of the value field, and
  * the index of the label label field.
  */
 public static function getTypeSignature($typeid)
 {
     $dataItemId = SMWDataValueFactory::getDataItemId($typeid);
     return array(SMWCompatibilityHelpers::getSignatureFromDataItemId($dataItemId, $typeid), SMWCompatibilityHelpers::getIndexFromDataItemId($dataItemId, $typeid, false), SMWCompatibilityHelpers::getIndexFromDataItemId($dataItemId, $typeid, true));
 }
 /**
  * Get the array of all stored values for some property.
  *
  * @param $property SMWDIProperty
  * @return array of SMWDataItem
  */
 public function getPropertyValues(SMWDIProperty $property)
 {
     if ($property->isInverse()) {
         // we never have any data for inverses
         return array();
     }
     if (array_key_exists($property->getKey(), $this->mStubPropVals)) {
         $this->unstubProperty($property->getKey(), $property);
         $propertyTypeId = $property->findPropertyTypeID();
         $propertyDiId = SMWDataValueFactory::getDataItemId($propertyTypeId);
         foreach ($this->mStubPropVals[$property->getKey()] as $dbkeys) {
             try {
                 if ($propertyDiId == SMWDataItem::TYPE_CONTAINER) {
                     $diSubWikiPage = SMWCompatibilityHelpers::dataItemFromDBKeys('_wpg', $dbkeys);
                     $semanticData = new SMWContainerSemanticData($diSubWikiPage);
                     $semanticData->copyDataFrom(smwfGetStore()->getSemanticData($diSubWikiPage));
                     $di = new SMWDIContainer($semanticData);
                 } else {
                     $di = SMWCompatibilityHelpers::dataItemFromDBKeys($propertyTypeId, $dbkeys);
                 }
                 if ($this->mNoDuplicates) {
                     $this->mPropVals[$property->getKey()][$di->getHash()] = $di;
                 } else {
                     $this->mPropVals[$property->getKey()][] = $di;
                 }
             } catch (SMWDataItemException $e) {
                 // ignore data
             }
         }
         unset($this->mStubPropVals[$property->getKey()]);
     }
     return parent::getPropertyValues($property);
 }
 /**
  * Returns the set of SQL values needed to insert the data for this
  * internal object into the database.
  */
 function getStorageSQL($internalObject)
 {
     if (method_exists('SMWDIWikiPage', 'getSubobjectName')) {
         // SMW 1.6
         $ioID = $this->makeSMWPageID($internalObject->getName(), $internalObject->getNamespace(), '', '');
     } else {
         $ioID = $this->makeSMWPageID($internalObject->getName(), $internalObject->getNamespace(), '');
     }
     $upRels2 = array();
     $upAtts2 = array();
     $upText2 = array();
     $upCoords = array();
     // set all the properties pointing from this internal object
     foreach ($internalObject->getPropertyValuePairs() as $propertyValuePair) {
         list($property, $value) = $propertyValuePair;
         $tableid = SMWSQLStore2::findPropertyTableID($property);
         $isRelation = $tableid == 'smw_rels2';
         $isAttribute = $tableid == 'smw_atts2';
         $isText = $tableid == 'smw_text2';
         $isCoords = $tableid == 'smw_coords';
         if ($isRelation) {
             if (method_exists('SMWDIWikiPage', 'getSubobjectName')) {
                 // SMW 1.6
                 $mainPageID = $this->makeSMWPageID($value->getDBkey(), $value->getNamespace(), $value->getInterwiki(), '');
             } else {
                 $mainPageID = $this->makeSMWPageID($value->getDBkey(), $value->getNamespace(), $value->getInterwiki());
             }
             $upRels2[] = array('s_id' => $ioID, 'p_id' => $this->makeSMWPropertyID($property), 'o_id' => $mainPageID);
         } elseif ($isAttribute) {
             if (class_exists('SMWCompatibilityHelpers')) {
                 // SMW 1.6
                 $dataItem = $value->getDataItem();
                 $keys = SMWCompatibilityHelpers::getDBkeysFromDataItem($dataItem);
                 $valueNum = $dataItem->getSortKey();
             } else {
                 $keys = $value->getDBkeys();
                 if (method_exists($value, 'getValueKey')) {
                     $valueNum = $value->getValueKey();
                 } else {
                     $valueNum = $value->getNumericValue();
                 }
             }
             $upAttr = array('s_id' => $ioID, 'p_id' => $this->makeSMWPropertyID($property), 'value_xsd' => $keys[0], 'value_num' => $valueNum);
             // 'value_unit' DB field was removed in SMW 1.6
             if (version_compare(SMW_VERSION, '1.6 alpha', '<')) {
                 $upAttr['value_unit'] = $value->getUnit();
             }
             $upAtts2[] = $upAttr;
         } elseif ($isText) {
             if (method_exists($value, 'getShortWikiText')) {
                 // SMW 1.6
                 $key = $value->getShortWikiText();
             } else {
                 $keys = $value->getDBkeys();
                 $key = $keys[0];
             }
             $upText2[] = array('s_id' => $ioID, 'p_id' => $this->makeSMWPropertyID($property), 'value_blob' => $key);
         } elseif ($isCoords) {
             $keys = $value->getDBkeys();
             $upCoords[] = array('s_id' => $ioID, 'p_id' => $this->makeSMWPropertyID($property), 'lat' => $keys[0], 'lon' => $keys[1]);
         }
     }
     return array($upRels2, $upAtts2, $upText2, $upCoords);
 }
 /**
  * Given an SMWDescription that is just a conjunction or disjunction of
  * SMWValueDescription objects, create and return a plain WHERE condition
  * string for it.
  *
  * @param $query
  * @param SMWDescription $description
  * @param SMWSQLStore2Table $proptable
  * @param integer $valueIndex
  * @param string $operator
  */
 protected function compileAttributeWhere($query, SMWDescription $description, SMWSQLStore2Table $proptable, $valueIndex, $operator = 'AND')
 {
     $where = '';
     if ($description instanceof SMWValueDescription) {
         $dataItem = $description->getDataItem();
         $keys = SMWCompatibilityHelpers::getDBkeysFromDataItem($dataItem);
         // Try comparison based on value field and comparator.
         if ($valueIndex >= 0) {
             // Find field name for comparison.
             $smwidjoinfield = false;
             $fieldName = $this->getDBFieldsForDVIndex($proptable->objectfields, $valueIndex, $smwidjoinfield);
             // Do not support smw_id joined data for now.
             if ($fieldName && !$smwidjoinfield) {
                 $comparator = false;
                 $customSQL = false;
                 // See if the getSQLCondition method exists and call it if this is the case.
                 if (method_exists($description, 'getSQLCondition')) {
                     $customSQL = $description->getSQLCondition($query->alias, array_keys($proptable->objectfields), $this->m_dbs);
                 }
                 if ($customSQL) {
                     $where = $customSQL;
                 } else {
                     $value = $keys[$valueIndex];
                     switch ($description->getComparator()) {
                         case SMW_CMP_EQ:
                             $comparator = '=';
                             break;
                         case SMW_CMP_LESS:
                             $comparator = '<';
                             break;
                         case SMW_CMP_GRTR:
                             $comparator = '>';
                             break;
                         case SMW_CMP_LEQ:
                             $comparator = '<=';
                             break;
                         case SMW_CMP_GEQ:
                             $comparator = '>=';
                             break;
                         case SMW_CMP_NEQ:
                             $comparator = '!=';
                             break;
                         case SMW_CMP_LIKE:
                         case SMW_CMP_NLKE:
                             $comparator = ' LIKE ';
                             if ($description->getComparator() == SMW_CMP_NLKE) {
                                 $comparator = " NOT{$comparator}";
                             }
                             $value = str_replace(array('%', '_', '*', '?'), array('\\%', '\\_', '%', '_'), $value);
                     }
                     if ($comparator) {
                         $where = "{$query->alias}.{$fieldName}{$comparator}" . $this->m_dbs->addQuotes($value);
                     }
                 }
             }
         }
         if ($where === '') {
             // comparators did not apply; match all fields
             $i = 0;
             foreach ($proptable->objectfields as $fname => $ftype) {
                 if ($i >= count($keys)) {
                     break;
                 }
                 if ($ftype == 'p') {
                     // Special case: page id, resolve this in advance
                     $oid = $this->getSMWPageID($dataItem->getDBkey(), $dataItem->getNamespace(), $dataItem->getInterwiki(), $dataItem->getSubobjectName());
                     $where .= ($where ? ' AND ' : '') . "{$query->alias}.{$fname}=" . $this->m_dbs->addQuotes($oid);
                     break;
                 } elseif ($ftype != 'l') {
                     // plain value, but not a text blob
                     $where .= ($where ? ' AND ' : '') . "{$query->alias}.{$fname}=" . $this->m_dbs->addQuotes($keys[$i]);
                 }
                 $i++;
             }
         }
     } elseif ($description instanceof SMWConjunction || $description instanceof SMWDisjunction) {
         $op = $description instanceof SMWConjunction ? 'AND' : 'OR';
         foreach ($description->getDescriptions() as $subdesc) {
             $this->compileAttributeWhere($query, $subdesc, $proptable, $valueIndex, $op);
         }
     }
     if ($where !== '') {
         $query->where .= ($query->where ? " {$operator} " : '') . "({$where})";
     }
 }