Example #1
0
 /**
  * @param PropertyId $propertyId
  * @param string $dataType
  *
  * @return bool
  */
 private function isMatchingDataType(PropertyId $propertyId, $dataType)
 {
     try {
         return $this->propertyDataTypeLookup->getDataTypeIdForProperty($propertyId) === $dataType;
     } catch (PropertyDataTypeLookupException $ex) {
         return false;
     }
 }
 /**
  * @see StatementFilter::statementMatches
  *
  * @param Statement $statement
  *
  * @return bool
  */
 public function statementMatches(Statement $statement)
 {
     $id = $statement->getPropertyId();
     try {
         $dataType = $this->dataTypeLookup->getDataTypeIdForProperty($id);
     } catch (PropertyDataTypeLookupException $ex) {
         return false;
     }
     return in_array($dataType, $this->dataTypes);
 }
 protected function setUp()
 {
     parent::setUp();
     $numericValidator = new TestValidator('/^\\d+$/');
     $alphabeticValidator = new TestValidator('/^[A-Z]+$/i');
     $lengthValidator = new TestValidator('/^.{1,10}$/');
     $this->dataTypeFactory = new DataTypeFactory(array('numeric' => 'string', 'alphabetic' => 'string'));
     $p1 = new PropertyId('p1');
     $p2 = new PropertyId('p2');
     $this->propertyDataTypeLookup = new InMemoryDataTypeLookup();
     $this->propertyDataTypeLookup->setDataTypeForProperty($p1, 'numeric');
     $this->propertyDataTypeLookup->setDataTypeForProperty($p2, 'alphabetic');
     $this->validatorFactory = $this->getMock('Wikibase\\Repo\\DataTypeValidatorFactory');
     $this->validatorFactory->expects($this->any())->method('getValidators')->will($this->returnCallback(function ($dataTypeId) use($numericValidator, $alphabeticValidator, $lengthValidator) {
         return array($dataTypeId === 'numeric' ? $numericValidator : $alphabeticValidator, $lengthValidator);
     }));
 }
 /**
  * @since 0.4
  *
  * @param PropertyId $propertyId
  *
  * @return string
  * @throws PropertyDataTypeLookupException
  */
 public function getDataTypeIdForProperty(PropertyId $propertyId)
 {
     $dataTypeId = null;
     $info = $this->infoStore->getPropertyInfo($propertyId);
     if ($info !== null && isset($info[PropertyInfoStore::KEY_DATA_TYPE])) {
         $dataTypeId = $info[PropertyInfoStore::KEY_DATA_TYPE];
     }
     if ($dataTypeId === null && $this->fallbackLookup !== null) {
         $dataTypeId = $this->fallbackLookup->getDataTypeIdForProperty($propertyId);
         if ($dataTypeId !== null) {
             wfDebugLog(__CLASS__, __FUNCTION__ . ': No property info found for ' . $propertyId . ', but property ID could be retrieved from fallback store!');
             //TODO: Automatically update the info store?
             //TODO: Suggest to run rebuildPropertyInfo.php
         }
     }
     if ($dataTypeId === null) {
         throw new PropertyDataTypeLookupException($propertyId);
     }
     return $dataTypeId;
 }
 public function getCallbackToAddDataTypeToSnak(PropertyDataTypeLookup $dataTypeLookup)
 {
     return function ($array) use($dataTypeLookup) {
         if (is_array($array)) {
             try {
                 $dataType = $dataTypeLookup->getDataTypeIdForProperty(new PropertyId($array['property']));
                 $array['datatype'] = $dataType;
             } catch (PropertyDataTypeLookupException $e) {
                 //XXX: shall we set $serialization['datatype'] = 'bad' ??
             }
         }
         return $array;
     };
 }
 /**
  * Adds the value of the given property to the RDF graph.
  *
  * @param RdfWriter $writer
  * @param PropertyValueSnak $snak
  * @param string $propertyNamespace The property namespace for this snak
  */
 private function addSnakValue(RdfWriter $writer, PropertyValueSnak $snak, $propertyNamespace)
 {
     $propertyId = $snak->getPropertyId();
     $propertyValueLName = $this->vocabulary->getEntityLName($propertyId);
     $propertyKey = $propertyId->getSerialization();
     // cache data type for all properties we encounter
     if (!isset($this->propertyTypes[$propertyKey])) {
         try {
             $this->propertyTypes[$propertyKey] = $this->propertyLookup->getDataTypeIdForProperty($propertyId);
         } catch (PropertyDataTypeLookupException $e) {
             $this->propertyTypes[$propertyKey] = "unknown";
         }
     }
     $dataType = $this->propertyTypes[$propertyKey];
     $this->valueBuilder->addValue($writer, $propertyNamespace, $propertyValueLName, $dataType, $snak);
 }
 /**
  * @see SnakFormatter::formatSnak
  *
  * Formats the given Snak by looking up its property type and calling the
  * ValueFormatter supplied to the constructor.
  *
  * @param Snak $snak
  *
  * @throws PropertyDataTypeLookupException
  * @throws InvalidArgumentException
  * @throws MismatchingDataValueTypeException
  * @throws FormattingException
  * @return string Either plain text, wikitext or HTML, depending on the ValueFormatter
  *  provided.
  */
 public function formatSnak(Snak $snak)
 {
     if (!$snak instanceof PropertyValueSnak) {
         throw new InvalidArgumentException("Not a PropertyValueSnak: " . get_class($snak));
     }
     $propertyType = null;
     $value = $snak->getDataValue();
     try {
         $propertyType = $this->typeLookup->getDataTypeIdForProperty($snak->getPropertyId());
         $expectedDataValueType = $this->getDataValueTypeForPropertyDataType($propertyType);
     } catch (PropertyDataTypeLookupException $ex) {
         throw $ex;
     } catch (OutOfBoundsException $ex) {
         throw new FormattingException($ex->getMessage(), 0, $ex);
     }
     $this->checkValueType($value, $expectedDataValueType);
     return $this->formatValue($value, $propertyType);
 }
Example #8
0
 /**
  * Validates a Snak.
  * For a PropertyValueSnak, this is done using the validators from the DataType
  * that is associated with the Snak's property.
  * Other Snak types are currently not validated.
  *
  * @see ValueValidator::validate()
  *
  * @param Snak $snak The value to validate
  *
  * @throws InvalidArgumentException
  * @return Result
  */
 public function validate($snak)
 {
     if (!$snak instanceof Snak) {
         throw new InvalidArgumentException('Snak expected');
     }
     // XXX: instead of an instanceof check, we could have multiple validators
     //      with a canValidate() method, to determine which validator to use
     //      for a given snak.
     $propertyId = $snak->getPropertyId();
     try {
         $typeId = $this->propertyDataTypeLookup->getDataTypeIdForProperty($propertyId);
         if ($snak instanceof PropertyValueSnak) {
             $dataValue = $snak->getDataValue();
             $result = $this->validateDataValue($dataValue, $typeId);
         } else {
             $result = Result::newSuccess();
         }
     } catch (PropertyDataTypeLookupException $ex) {
         $result = Result::newError(array(Error::newError("Property {$propertyId} not found!", null, 'no-such-property', array($propertyId))));
     }
     return $result;
 }
 /**
  * @param PropertyId $propertyId
  *
  * @return string
  * @throws PropertyDataTypeLookupException
  */
 private function findDataTypeIdForProperty(PropertyId $propertyId)
 {
     return $this->propertyDataTypeLookup->getDataTypeIdForProperty($propertyId);
 }
 /**
  * @param Snak $snak
  *
  * @throws PropertyDataTypeLookupException
  * @return string The Snak's data type
  */
 private function getSnakDataType(Snak $snak)
 {
     return $this->dataTypeLookup->getDataTypeIdForProperty($snak->getPropertyId());
     // @todo: wrap the PropertyDataTypeLookupException, but make sure ErrorHandlingSnakFormatter still handles it.
 }