/** * 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); }
/** * @see SMWStore::getPropertySubjects * * @param SMWDIProperty $property * @param mixed $value SMWDataItem or null * @param SMWRequestOptions $requestoptions * * @return array of SMWDIWikiPage */ public function getPropertySubjects(SMWDIProperty $property, $value, $requestoptions = null) { /// TODO: should we share code with #ask query computation here? Just use queries? wfProfileIn("SMWSQLStore2::getPropertySubjects (SMW)"); if ($property->isInverse()) { // inverses are working differently $noninverse = new SMWDIProperty($property->getKey(), false); $result = $this->getPropertyValues($value, $noninverse, $requestoptions); wfProfileOut("SMWSQLStore2::getPropertySubjects (SMW)"); return $result; } // First build $select, $from, and $where for the DB query $where = $from = ''; $pid = $this->getSMWPropertyID($property); $tableid = self::findPropertyTableID($property); if ($pid == 0 || $tableid === '') { wfProfileOut("SMWSQLStore::getPropertySubjects (SMW)"); return array(); } $proptables = self::getPropertyTables(); $proptable = $proptables[$tableid]; $db = wfGetDB(DB_SLAVE); if ($proptable->idsubject) { // join in smw_ids to get title data $from = $db->tableName('smw_ids') . " INNER JOIN " . $db->tableName($proptable->name) . " AS t1 ON t1.s_id=smw_id"; $select = 'smw_title, smw_namespace, smw_sortkey, smw_iw, smw_subobject'; } else { // no join needed, title+namespace as given in proptable $from = $db->tableName($proptable->name) . " AS t1"; $select = 's_title AS smw_title, s_namespace AS smw_namespace, s_title AS smw_sortkey, \'\' AS smw_iw, \'\' AS smw_subobject'; } if ($proptable->fixedproperty == false) { $where .= ($where ? ' AND ' : '') . "t1.p_id=" . $db->addQuotes($pid); } $this->prepareValueQuery($from, $where, $proptable, $value, 1); // *** Now execute the query and read the results ***// $result = array(); $res = $db->select($from, 'DISTINCT ' . $select, $where . $this->getSQLConditions($requestoptions, 'smw_sortkey', 'smw_sortkey', $where !== ''), 'SMW::getPropertySubjects', $this->getSQLOptions($requestoptions, 'smw_sortkey')); foreach ($res as $row) { try { if ($row->smw_iw === '' || $row->smw_iw[0] != ':') { // filter special objects $result[] = new SMWDIWikiPage($row->smw_title, $row->smw_namespace, $row->smw_iw, $row->smw_subobject); } } catch (SMWDataItemException $e) { // silently drop data, should be extremely rare and will usually fix itself at next edit } } $db->freeResult($res); wfProfileOut("SMWSQLStore2::getPropertySubjects (SMW)"); return $result; }
/** * @see SMWStore::getPropertySubjects * * @todo This method cannot retrieve subjects for sortkeys, i.e., for * property _SKEY. Only empty arrays will be returned there. * * @param SMWDIProperty $property * @param SMWDataItem|null $value * @param SMWRequestOptions|null $requestOptions * * @return array of DIWikiPage */ public function getPropertySubjects(SMWDIProperty $property, SMWDataItem $value = null, SMWRequestOptions $requestOptions = null) { /// TODO: should we share code with #ask query computation here? Just use queries? if ($property->isInverse()) { // inverses are working differently $noninverse = new SMW\DIProperty($property->getKey(), false); $result = $this->getPropertyValues($value, $noninverse, $requestOptions); return $result; } // #1222, Filter those where types don't match (e.g property = _txt // and value = _wpg) if ($value !== null && DataTypeRegistry::getInstance()->getDataItemId($property->findPropertyTypeID()) !== $value->getDIType()) { return array(); } // First build $select, $from, and $where for the DB query $where = $from = ''; $pid = $this->store->smwIds->getSMWPropertyID($property); $tableid = $this->store->findPropertyTableID($property); if ($pid == 0 || $tableid === '') { return array(); } $proptables = $this->store->getPropertyTables(); $proptable = $proptables[$tableid]; $db = $this->store->getConnection(); if ($proptable->usesIdSubject()) { // join with ID table to get title data $from = $db->tableName(SMWSql3SmwIds::TABLE_NAME) . " INNER JOIN " . $db->tableName($proptable->getName()) . " AS t1 ON t1.s_id=smw_id"; $select = 'smw_title, smw_namespace, smw_iw, smw_sortkey, smw_subobject'; } else { // no join needed, title+namespace as given in proptable $from = $db->tableName($proptable->getName()) . " AS t1"; $select = 's_title AS smw_title, s_namespace AS smw_namespace, \'\' AS smw_iw, s_title AS smw_sortkey, \'\' AS smw_subobject'; } if (!$proptable->isFixedPropertyTable()) { $where .= ($where ? ' AND ' : '') . "t1.p_id=" . $db->addQuotes($pid); } $this->prepareValueQuery($from, $where, $proptable, $value, 1); // *** Now execute the query and read the results ***// $result = array(); if (!$proptable->isFixedPropertyTable()) { if ($where !== '' && strpos(SMW_SQL3_SMWIW_OUTDATED, $where) === false) { $where .= " AND smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWIW_OUTDATED) . " AND smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWDELETEIW); } else { $where .= " smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWIW_OUTDATED) . " AND smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWDELETEIW); } } $res = $db->select($from, 'DISTINCT ' . $select, $where . $this->store->getSQLConditions($requestOptions, 'smw_sortkey', 'smw_sortkey', $where !== ''), __METHOD__, $this->store->getSQLOptions($requestOptions, 'smw_sortkey')); $diHandler = $this->store->getDataItemHandlerForDIType(SMWDataItem::TYPE_WIKIPAGE); foreach ($res as $row) { try { if ($row->smw_iw === '' || $row->smw_iw[0] != ':') { // filter special objects $result[] = $diHandler->dataItemFromDBKeys(array_values((array) $row)); } } catch (DataItemHandlerException $e) { // silently drop data, should be extremely rare and will usually fix itself at next edit } } $db->freeResult($res); return $result; }
/** * Create a value for the given property, provided as an SMWDIProperty * object. If no value is given, an empty container is created, the * value of which can be set later on. * * @param $property SMWDIProperty property object for which this value is made * @param $valueString mixed user value string, or false if unknown * @param $caption mixed user-defined caption, or false if none given * @param $contextPage SMWDIWikiPage that provides a context for parsing the value string, or null * * @return SMWDataValue */ public static function newPropertyObjectValue(SMWDIProperty $property, $valueString = false, $caption = false, $contextPage = null) { $typeId = $property->isInverse() ? '_wpg' : $property->findPropertyTypeID(); return self::newTypeIdValue($typeId, $valueString, $caption, $property, $contextPage); }
/** * Remove a value for a property identified by its SMWDataItem object. * This method removes a property-value specified by the property and * dataitem. If there are no more property-values for this property it * also removes the property from the mProperties. * * @note There is no check whether the type of the given data item * agrees with the type of the property. Since property types can * change, all parts of SMW are prepared to handle mismatched data item * types anyway. * * @param $property SMWDIProperty * @param $dataItem SMWDataItem * * @since 1.8 */ public function removePropertyObjectValue(SMWDIProperty $property, SMWDataItem $dataItem) { //delete associated subSemanticData if ($dataItem instanceof SMWDIContainer) { $this->removeSubSemanticData($dataItem->getSemanticData()); $dataItem = $dataItem->getSemanticData()->getSubject(); } if ($property->isInverse()) { // inverse properties cannot be used for annotation return; } if (!array_key_exists($property->getKey(), $this->mPropVals) || !array_key_exists($property->getKey(), $this->mProperties)) { return; } if ($this->mNoDuplicates) { //this didn't get checked for my tests, but should work unset($this->mPropVals[$property->getKey()][$dataItem->getHash()]); } else { foreach ($this->mPropVals[$property->getKey()] as $index => $di) { if ($di->equals($dataItem)) { unset($this->mPropVals[$property->getKey()][$index]); } } $this->mPropVals[$property->getKey()] = array_values($this->mPropVals[$property->getKey()]); } if ($this->mPropVals[$property->getKey()] === array()) { unset($this->mProperties[$property->getKey()]); unset($this->mPropVals[$property->getKey()]); } }
public function getPropertySubjects(SMWDIProperty $property, $value, $requestoptions = null) { wfProfileIn("SMWSQLStoreLight::getPropertySubjects (SMW)"); if ($property->isInverse()) { // inverses are working differently $noninverse = clone $property; $noninverse->setInverse(false); $result = $this->getPropertyValues($value, $noninverse, $requestoptions); wfProfileOut("SMWSQLStoreLight::getPropertySubjects (SMW)"); return $result; } // *** First build $select, $from, and $where for the DB query ***// $tablename = SMWSQLStoreLight::findPropertyTableName($property); $db = wfGetDB(DB_SLAVE); $from = $db->tableName('page') . " AS p INNER JOIN " . $db->tableName($tablename) . " AS t ON t.pageid=p.page_id"; $where = 't.propname=' . $db->addQuotes($property->getDBkey()); if ($value !== null) { $valuestring = $tablename == 'smwsimple_special' ? reset($value->getDBkeys()) : serialize($value->getDBkeys()); $where .= ' AND t.value=' . $db->addQuotes($valuestring); } $select = array('p.page_title AS title', 'p.page_namespace AS namespace'); // *** Now execute the query and read the results ***// $result = array(); $res = $db->select($from, $select, $where . $this->getSQLConditions($requestoptions, 'p.page_title', 'p.page_title'), 'SMW::getPropertySubjects', $this->getSQLOptions($requestoptions, 'p.page_title') + array('DISTINCT')); foreach ($res as $row) { $result[] = SMWWikiPageValue::makePage($row->title, $row->namespace, $row->title); } $db->freeResult($res); wfProfileOut("SMWSQLStoreLight::getPropertySubjects (SMW)"); return $result; }
/** * Store a value for a property identified by its SMWDataItem object. * * @note There is no check whether the type of the given data item * agrees with the type of the property. Since property types can * change, all parts of SMW are prepared to handle mismatched data item * types anyway. * * @param $property SMWDIProperty * @param $dataItem SMWDataItem */ public function addPropertyObjectValue(SMWDIProperty $property, SMWDataItem $dataItem) { if ($property->isInverse()) { // inverse properties cannot be used for annotation return; } if (!array_key_exists($property->getKey(), $this->mPropVals)) { $this->mPropVals[$property->getKey()] = array(); $this->mProperties[$property->getKey()] = $property; } if ($this->mNoDuplicates) { $this->mPropVals[$property->getKey()][$dataItem->getHash()] = $dataItem; } else { $this->mPropVals[$property->getKey()][] = $dataItem; } if (!$property->isUserDefined()) { if ($property->isShown()) { $this->mHasVisibleSpecs = true; $this->mHasVisibleProps = true; } } else { $this->mHasVisibleProps = true; } }
/** * Store a value for a property identified by its SMWDataItem object. * * @note There is no check whether the type of the given data item * agrees with the type of the property. Since property types can * change, all parts of SMW are prepared to handle mismatched data item * types anyway. * * @param SMWDIProperty $property * @param SWLPropertyChange $change */ public function addPropertyObjectChange(SMWDIProperty $property, SWLPropertyChange $change) { if ($property->isInverse()) { // inverse properties cannot be used for annotation return; } if (!array_key_exists($property->getKey(), $this->changes)) { $this->changes[$property->getKey()] = array(); $this->properties[$property->getKey()] = $property; } $this->changes[$property->getKey()][] = $change; $this->hasChanges = true; }
/** * Get the array of all stored values for some property. * * @since 1.8 * * @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)) { // Not catching exception here; the $this->unstubProperty($property->getKey(), $property); $propertyTypeId = $property->findPropertyTypeID(); $propertyDiId = SMWDataValueFactory::getDataItemId($propertyTypeId); foreach ($this->mStubPropVals[$property->getKey()] as $dbkeys) { try { $diHandler = $this->store->getDataItemHandlerForDIType($propertyDiId); $di = $diHandler->dataItemFromDBKeys($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); }