/** * Add data about the query and its parameters to the semantic data of * the given parser. The $queryKey is a string key that uniquely * identifies the query; this is difficult to create in a stable way * from the processed query object and parameters, but easy to get from * the raw user input. * * @param string $queryKey * @param SMWQuery $query * @param array $params * @param Parser $parser * * @since 1.8 */ public static function addQueryData($queryKey, SMWQuery $query, array $params, Parser $parser) { $mainSemanticData = SMWParseData::getSMWData($parser); $subject = $mainSemanticData->getSubject(); $diSubWikiPage = new SMWDIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), "_QUERY" . $queryKey); $semanticData = new SMWContainerSemanticData($diSubWikiPage); $description = $query->getDescription(); // Add query string $propertyDi = new SMWDIProperty('_ASKST'); $valueDi = new SMWDIBlob($description->getQueryString()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); // Add query size $propertyDi = new SMWDIProperty('_ASKSI'); $valueDi = new SMWDINumber($description->getSize()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); // Add query depth $propertyDi = new SMWDIProperty('_ASKDE'); $valueDi = new SMWDINumber($description->getDepth()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); // Add query format $propertyDi = new SMWDIProperty('_ASKFO'); $valueDi = new SMWDIString($params['format']->getValue()); $semanticData->addPropertyObjectValue($propertyDi, $valueDi); $propertyDi = new SMWDIProperty('_ASK'); $subObjectDi = new SMWDIContainer($semanticData); SMWParseData::getSMWData($parser)->addPropertyObjectValue($propertyDi, $subObjectDi); }
private function newDiContainer($subject, $property, $errorMsg) { $containerSemanticData = new ContainerSemanticData($subject); if ($property !== null) { $containerSemanticData->addPropertyObjectValue(new DIProperty('_ERRP'), $property->getDiWikiPage()); } $containerSemanticData->addPropertyObjectValue(new DIProperty('_ERRT'), new DIBlob($errorMsg)); return new DIContainer($containerSemanticData); }
/** * Construct a data container that refers to an anonymous subject. See * the documentation of the class for details. * * @since 1.7 * * @param boolean $noDuplicates stating if duplicate data should be avoided */ public static function makeAnonymousContainer($noDuplicates = true, $skipAnonymousCheck = false) { $subject = new SMWDIWikiPage('SMWInternalObject', NS_SPECIAL, '', 'int'); $containerSemanticData = new SMWContainerSemanticData($subject, $noDuplicates); if ($skipAnonymousCheck) { $containerSemanticData->skipAnonymousCheck(); } return $containerSemanticData; }
/** * #1416 create container manually to avoid any issues that may arise from * a failed Title::makeTitleSafe. */ private function newDIContainer(Query $query) { $subject = $query->getContextPage(); if ($subject === null) { $containerSemanticData = ContainerSemanticData::makeAnonymousContainer(); } else { $subject = new DIWikiPage($subject->getDBkey(), $subject->getNamespace(), $subject->getInterwiki(), $query->getQueryId()); $containerSemanticData = new ContainerSemanticData($subject); } return new DIContainer($containerSemanticData); }
/** * @see SMWStore::getPropertyValues * * @param $subject mixed SMWDIWikiPage or null * @param $property SMWDIProperty * @param $requestoptions SMWRequestOptions * * @return array of SMWDataItem */ public function getPropertyValues($subject, SMWDIProperty $property, $requestoptions = null) { wfProfileIn("SMWSQLStore2::getPropertyValues (SMW)"); if ($property->isInverse()) { // inverses are working differently $noninverse = new SMWDIProperty($property->getKey(), false); $result = $this->getPropertySubjects($noninverse, $subject, $requestoptions); } elseif (!is_null($subject)) { // subject given, use semantic data cache $sd = $this->getSemanticData($subject, array($property->findPropertyTypeID())); $result = $this->applyRequestOptions($sd->getPropertyValues($property), $requestoptions); } else { // no subject given, get all values for the given property $pid = $this->getSMWPropertyID($property); $tableid = self::findPropertyTableID($property); if ($pid == 0 || $tableid === '') { wfProfileOut("SMWSQLStore2::getPropertyValues (SMW)"); return array(); } $proptables = self::getPropertyTables(); $data = $this->fetchSemanticData($pid, $property, $proptables[$tableid], false, $requestoptions); $result = array(); $propertyTypeId = $property->findPropertyTypeID(); $propertyDiId = SMWDataValueFactory::getDataItemId($propertyTypeId); if ($propertyDiId == SMWDataItem::TYPE_CONTAINER) { foreach ($data as $dbkeys) { try { $diSubWikiPage = SMWCompatibilityHelpers::dataItemFromDBKeys('_wpg', $dbkeys); $semanticData = new SMWContainerSemanticData($diSubWikiPage); $semanticData->copyDataFrom($this->getSemanticData($diSubWikiPage)); $result[] = new SMWDIContainer($semanticData); } catch (SMWDataItemException $e) { // maybe type assignment changed since data was stored; // don't worry, but we can only drop the data here } } } else { foreach ($data as $dbkeys) { try { $result[] = SMWCompatibilityHelpers::dataItemFromDBKeys($propertyTypeId, $dbkeys); } catch (SMWDataItemException $e) { // maybe type assignment changed since data was stored; // don't worry, but we can only drop the data here } } } } wfProfileOut("SMWSQLStore2::getPropertyValues (SMW)"); return $result; }
/** * @see SMWDataValue::loadDataItem() * @param $dataitem SMWDataItem * @return boolean */ protected function loadDataItem(SMWDataItem $dataItem) { if ($dataItem->getDIType() == SMWDataItem::TYPE_CONTAINER) { $this->m_dataitem = $dataItem; return true; } elseif ($dataItem->getDIType() == SMWDataItem::TYPE_WIKIPAGE) { $semanticData = new SMWContainerSemanticData($dataItem); $semanticData->copyDataFrom(\SMW\StoreFactory::getStore()->getSemanticData($dataItem)); $this->m_dataitem = new SMWDIContainer($semanticData); return true; } else { return false; } }
/** * 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); }
private function newContainerSemanticData($value) { if ($this->m_contextPage === null) { $containerSemanticData = ContainerSemanticData::makeAnonymousContainer(); $containerSemanticData->skipAnonymousCheck(); } else { $subobjectName = '_REF' . md5($value); $subject = new DIWikiPage($this->m_contextPage->getDBkey(), $this->m_contextPage->getNamespace(), $this->m_contextPage->getInterwiki(), $subobjectName); $containerSemanticData = new ContainerSemanticData($subject); } return $containerSemanticData; }
/** * Method to create a dataitem from a type ID and array of DB keys. * Throws SMWDataItemException if problems occur, to get our callers * used to it. * * @param $typeid string id for the given type * @param $dbkeys array of mixed * * @return SMWDataItem */ public static function dataItemFromDBKeys($typeid, $dbkeys) { switch (SMWDataValueFactory::getDataItemId($typeid)) { case SMWDataItem::TYPE_ERROR: case SMWDataItem::TYPE_NOTYPE: break; case SMWDataItem::TYPE_NUMBER: return SMWDINumber::doUnserialize($dbkeys[0]); case SMWDataItem::TYPE_STRING: return new SMWDIString($dbkeys[0]); case SMWDataItem::TYPE_BLOB: return new SMWDIBlob($dbkeys[0]); case SMWDataItem::TYPE_BOOLEAN: return new SMWDIBoolean($dbkeys[0] == '1'); case SMWDataItem::TYPE_URI: if ($typeid == '__typ' && $dbkeys[0][0] == '_') { // b/c: old data stored as type ids return SMWTypesValue::getTypeUriFromTypeId($dbkeys[0]); } else { return SMWDIUri::doUnserialize($dbkeys[0]); } case SMWDataItem::TYPE_TIME: $timedate = explode('T', $dbkeys[0], 2); if (count($dbkeys) == 2 && count($timedate) == 2) { $date = reset($timedate); $year = $month = $day = $hours = $minutes = $seconds = $timeoffset = false; if (end($timedate) === '' || SMWTimeValue::parseTimeString(end($timedate), $hours, $minutes, $seconds, $timeoffset) == true) { $d = explode('/', $date, 3); if (count($d) == 3) { list($year, $month, $day) = $d; } elseif (count($d) == 2) { list($year, $month) = $d; } elseif (count($d) == 1) { list($year) = $d; } if ($month === '') { $month = false; } if ($day === '') { $day = false; } $calendarmodel = SMWDITime::CM_GREGORIAN; return new SMWDITime($calendarmodel, $year, $month, $day, $hours, $minutes, $seconds); } } break; case SMWDataItem::TYPE_GEO: return new SMWDIGeoCoord(array('lat' => (double) $dbkeys[0], 'lon' => (double) $dbkeys[1])); case SMWDataItem::TYPE_CONTAINER: // provided for backwards compatibility only; // today containers are read from the store as substructures, // not retrieved as single complex values $semanticData = SMWContainerSemanticData::makeAnonymousContainer(); foreach (reset($dbkeys) as $value) { if (is_array($value) && count($value) == 2) { $diP = new SMWDIProperty(reset($value), false); $diV = self::dataItemFromDBKeys($diP->findPropertyTypeID(), end($value)); $semanticData->addPropertyObjectValue($diP, $diV); } } return new SMWDIContainer($semanticData); case SMWDataItem::TYPE_WIKIPAGE: if ($typeid == '__spf') { $pagedbkey = str_replace(' ', '_', $dbkeys[0]); return new SMWDIWikiPage($pagedbkey, SF_NS_FORM, ''); } elseif (count($dbkeys) >= 5) { // with subobject name (and sortkey) return new SMWDIWikiPage($dbkeys[0], intval($dbkeys[1]), $dbkeys[2], $dbkeys[4]); } elseif (count($dbkeys) >= 3) { // without subobject name (just for b/c) return new SMWDIWikiPage($dbkeys[0], intval($dbkeys[1]), $dbkeys[2]); } break; case SMWDataItem::TYPE_CONCEPT: if (count($dbkeys) >= 5) { return new SMWDIConcept($dbkeys[0], smwfXMLContentEncode($dbkeys[1]), $dbkeys[2], $dbkeys[3], $dbkeys[4]); } break; case SMWDataItem::TYPE_PROPERTY: return new SMWDIProperty($dbkeys[0], false); } throw new SMWDataItemException('Failed to create data item from DB keys.'); }
public function testTryToGetLocalPropertyDescriptionForUserdefinedProperty() { $property = $this->dataItemFactory->newDIProperty('Foo'); $pdesc = $this->dataItemFactory->newDIProperty('_PDESC'); $pdesc->setPropertyTypeId('_mlt_rec'); $container = $this->getMockBuilder('\\Onoi\\BlobStore\\Container')->disableOriginalConstructor()->getMock(); $this->blobStore->expects($this->once())->method('read')->will($this->returnValue($container)); $this->cachedPropertyValuesPrefetcher->expects($this->once())->method('getPropertyValues')->with($this->equalTo($property->getDiWikiPage()), $this->anything(), $this->anything())->will($this->returnValue(array($this->dataItemFactory->newDIContainer(ContainerSemanticData::makeAnonymousContainer())))); $this->cachedPropertyValuesPrefetcher->expects($this->once())->method('getBlobStore')->will($this->returnValue($this->blobStore)); $instance = new PropertySpecificationLookup($this->cachedPropertyValuesPrefetcher, $this->intermediaryMemoryCache); $this->assertInternalType('string', $instance->getPropertyDescriptionBy($property)); }
protected function parseUserValueOrQuery($value, $queryMode) { if ($value === '') { $this->addError(wfMsg('smw_novalues')); if ($queryMode) { return new SMWThingDescription(); } else { return; } } if ($queryMode) { $subdescriptions = array(); } elseif (is_null($this->m_contextPage)) { $semanticData = SMWContainerSemanticData::makeAnonymousContainer(); } else { $subobjectName = '_' . hash('md4', $value, false); // md4 is probably fastest of PHP's hashes $subject = new SMWDIWikiPage($this->m_contextPage->getDBkey(), $this->m_contextPage->getNamespace(), $this->m_contextPage->getInterwiki(), $subobjectName); $semanticData = new SMWContainerSemanticData($subject); } $values = preg_split('/[\\s]*;[\\s]*/u', trim($value)); $valueIndex = 0; // index in value array $propertyIndex = 0; // index in property list $empty = true; foreach ($this->getPropertyDataItems() as $diProperty) { if (!array_key_exists($valueIndex, $values)) { break; // stop if there are no values left } if ($queryMode) { // special handling for supporting query parsing $comparator = SMW_CMP_EQ; SMWDataValue::prepareValue($values[$valueIndex], $comparator); } // generating the DVs: if ($values[$valueIndex] === '' || $values[$valueIndex] == '?') { // explicit omission $valueIndex++; } else { $dataValue = SMWDataValueFactory::newPropertyObjectValue($diProperty, $values[$valueIndex]); if ($dataValue->isValid()) { // valid DV: keep if ($queryMode) { $subdescriptions[] = new SMWSomeProperty($diProperty, new SMWValueDescription($dataValue->getDataItem(), $comparator)); } else { $semanticData->addPropertyObjectValue($diProperty, $dataValue->getDataItem()); } $valueIndex++; $empty = false; } elseif (count($values) - $valueIndex == count($this->m_diProperties) - $propertyIndex) { // too many errors: keep this one to have enough slots left if (!$queryMode) { $semanticData->addPropertyObjectValue($diProperty, $dataValue->getDataItem()); } $this->addError($dataValue->getErrors()); ++$valueIndex; } } ++$propertyIndex; } if ($empty) { $this->addError(wfMsg('smw_novalues')); } if ($queryMode) { switch (count($subdescriptions)) { case 0: return new SMWThingDescription(); case 1: return reset($subdescriptions); default: return new SMWConjunction($subdescriptions); } } else { $this->m_dataitem = new SMWDIContainer($semanticData); } }
private function newContainerSemanticData($value) { if ($this->m_contextPage === null) { $containerSemanticData = ContainerSemanticData::makeAnonymousContainer(); $containerSemanticData->skipAnonymousCheck(); } else { $subobjectName = '_' . hash('md4', $value, false); // md4 is probably fastest of PHP's hashes $subject = new DIWikiPage($this->m_contextPage->getDBkey(), $this->m_contextPage->getNamespace(), $this->m_contextPage->getInterwiki(), $subobjectName); $containerSemanticData = new ContainerSemanticData($subject); } return $containerSemanticData; }