/**
  * Check if property is range restricted and, if so, whether the current value is allowed.
  * Creates an error if the value is illegal.
  */
 protected function checkAllowedValues()
 {
     if (!is_null($this->m_property)) {
         $propertyDiWikiPage = $this->m_property->getDiWikiPage();
     }
     if (is_null($this->m_property) || is_null($propertyDiWikiPage) || !isset($this->m_dataitem)) {
         return;
         // no property known, or no data to check
     }
     $allowedvalues = \SMW\StoreFactory::getStore()->getPropertyValues($propertyDiWikiPage, new SMWDIProperty('_PVAL'));
     if (count($allowedvalues) == 0) {
         return;
     }
     $hash = $this->m_dataitem->getHash();
     $testdv = DataValueFactory::getInstance()->newTypeIDValue($this->getTypeID());
     $accept = false;
     $valuestring = '';
     foreach ($allowedvalues as $di) {
         if ($di instanceof SMWDIBlob) {
             $testdv->setUserValue($di->getString());
             if ($hash === $testdv->getDataItem()->getHash()) {
                 $accept = true;
                 break;
             } else {
                 if ($valuestring !== '') {
                     $valuestring .= ', ';
                 }
                 $valuestring .= $di->getString();
             }
         }
     }
     if (!$accept) {
         $this->addError(wfMessage('smw_notinenum', $this->getWikiValue(), $valuestring)->inContentLanguage()->text());
     }
 }
示例#2
0
 /**
  * Create an SMWExpElement for some internal resource, given by an
  * SMWDIProperty object.
  * This code is only applied to user-defined properties, since the
  * code for special properties in
  * SMWExporter::getSpecialPropertyResource() may require information
  * about the namespace in which some special property is used.
  *
  * @param $diProperty SMWDIProperty
  * @param $helperProperty boolean determines if an auxiliary property resource to store a helper value (see SMWExporter::getDataItemHelperExpElement()) should be generated
  * @return SMWExpResource
  */
 public static function getResourceElementForProperty(SMWDIProperty $diProperty, $helperProperty = false)
 {
     $diWikiPage = $diProperty->getDiWikiPage();
     if (is_null($diWikiPage)) {
         throw new Exception('SMWExporter::getResourceElementForProperty() can only be used for non-inverse, user-defined properties.');
     } elseif ($helperProperty) {
         return self::getResourceElementForWikiPage($diWikiPage, 'aux');
     } else {
         return self::getResourceElementForWikiPage($diWikiPage);
     }
 }
 /**
  * Return a wiki page value that can be used for displaying this
  * property, or null if no such wiki page exists (for predefined
  * properties without any label).
  * @return SMWWikiPageValue or null
  */
 public function getWikiPageValue()
 {
     if (!isset($this->m_wikipage)) {
         $diWikiPage = $this->m_dataitem->getDiWikiPage();
         // A page representation for an inverse property is not possible
         // therefore construct its non-inverse representation (label/caption
         // remains and will show with an inverse indicator)
         if ($diWikiPage === null && $this->m_dataitem->isInverse()) {
             $property = new SMWDIProperty($this->m_dataitem->getKey());
             $diWikiPage = $property->getDiWikiPage();
         }
         if ($diWikiPage !== null) {
             $this->m_wikipage = \SMW\DataValueFactory::getInstance()->newDataItemValue($diWikiPage, null, $this->m_caption);
             $this->m_wikipage->setOutputFormat($this->m_outformat);
             $this->addError($this->m_wikipage->getErrors());
         } else {
             // should rarely happen ($value is only changed if the input $value really was a label for a predefined prop)
             $this->m_wikipage = null;
         }
     }
     return $this->m_wikipage;
 }
 /**
  * @see SMWDataItemHandler::dataItemFromDBKeys()
  * @since 1.8
  * @param array|string $dbkeys expecting array here
  * @throws SMWDataItemException
  * @return SMWDataItem
  */
 public function dataItemFromDBKeys($dbkeys)
 {
     if (is_array($dbkeys) && count($dbkeys) == 5) {
         $namespace = intval($dbkeys[1]);
         if ($namespace == SMW_NS_PROPERTY && $dbkeys[0] != '' && $dbkeys[0][0] == '_' && $dbkeys[2] == '') {
             // Correctly interpret internal property keys
             $property = new SMWDIProperty($dbkeys[0]);
             $wikipage = $property->getDiWikiPage($dbkeys[4]);
             if (!is_null($wikipage)) {
                 return $wikipage;
             }
         } else {
             return $this->newDiWikiPage($dbkeys);
         }
     }
     throw new SMWDataItemException('Failed to create data item from DB keys.');
 }
 /**
  * Produce a formatted string representation for showing a property in
  * the list of unused properties.
  *
  * @since 1.8
  *
  * @param SMWDIProperty $property
  * @return string
  */
 protected function formatPropertyItem(SMWDIProperty $property)
 {
     $linker = smwfGetLinker();
     $errors = array();
     if ($property->isUserDefined()) {
         $proplink = $linker->link($property->getDiWikiPage()->getTitle(), $property->getLabel());
         $types = smwfGetStore()->getPropertyValues($property->getDiWikiPage(), new SMWDIProperty('_TYPE'));
         if (count($types) >= 1) {
             $typeDataValue = SMWDataValueFactory::newDataItemValue(current($types), new SMWDIProperty('_TYPE'));
         } else {
             $typeDataValue = SMWTypesValue::newFromTypeId('_wpg');
             $errors[] = wfMessage('smw_propertylackstype', $typeDataValue->getLongHTMLText())->text();
         }
         $typeString = $typeDataValue->getLongHTMLText($linker);
     } else {
         $typeid = $property->findPropertyTypeID();
         $typeDataValue = SMWTypesValue::newFromTypeId($typeid);
         $typeString = $typeDataValue->getLongHTMLText($linker);
         $propertyDataValue = SMWDataValueFactory::newDataItemValue($property, null);
         $proplink = $propertyDataValue->getShortHtmlText($linker);
     }
     return wfMessage('smw_unusedproperty_template', $proplink, $typeString)->text() . ' ' . smwfEncodeMessages($errors);
 }
 /**
  * Produce a formatted string representation for showing a property and
  * its usage count in the list of used properties.
  *
  * @since 1.8
  *
  * @param SMWDIProperty $property
  * @param integer $useCount
  * @return string
  */
 protected function formatPropertyItem(SMWDIProperty $property, $useCount)
 {
     global $wgLang;
     $linker = smwfGetLinker();
     $errors = array();
     $diWikiPage = $property->getDiWikiPage();
     $title = !is_null($diWikiPage) ? $diWikiPage->getTitle() : null;
     if ($property->isUserDefined() && is_null($title)) {
         // Show even messed up property names.
         $typestring = '';
         $proplink = $property->getLabel();
         $errors[] = wfMessage('smw_notitle', $property->getLabel())->escaped();
     } elseif ($property->isUserDefined()) {
         if ($useCount <= 5) {
             $errors[] = wfMessage('smw_propertyhardlyused')->escaped();
         }
         // User defined types default to Page
         global $smwgPDefaultType;
         $typeDataValue = SMWTypesValue::newFromTypeId($smwgPDefaultType);
         $typestring = $typeDataValue->getLongHTMLText($linker);
         $label = htmlspecialchars($property->getLabel());
         if ($title->exists()) {
             $typeProperty = new SMWDIProperty('_TYPE');
             $types = smwfGetStore()->getPropertyValues($diWikiPage, $typeProperty);
             if (count($types) >= 1) {
                 $typeDataValue = SMWDataValueFactory::newDataItemValue(current($types), $typeProperty);
                 $typestring = $typeDataValue->getLongHTMLText($linker);
             } else {
                 $errors[] = wfMessage('smw_propertylackstype')->rawParams($typestring)->escaped();
             }
             $proplink = $linker->link($title, $label);
         } else {
             $errors[] = wfMessage('smw_propertylackspage')->escaped();
             $proplink = $linker->link($title, $label, array(), array('action' => 'view'));
         }
     } else {
         // predefined property
         $typeid = $property->findPropertyTypeID();
         $typeDataValue = SMWTypesValue::newFromTypeId($typeid);
         $typestring = $typeDataValue->getLongHTMLText($linker);
         $propertyDataValue = SMWDataValueFactory::newDataItemValue($property, null);
         $proplink = $propertyDataValue->getShortHtmlText($linker);
     }
     $warnings = smwfEncodeMessages($errors, 'warning', '', false);
     $useCount = $wgLang->formatNum($useCount);
     if ($typestring === '') {
         // Builtins have no type
         // @todo Should use numParams for $useCount?
         return wfMessage('smw_property_template_notype')->rawParams($proplink)->params($useCount)->text() . ' ' . $warnings;
     } else {
         // @todo Should use numParams for $useCount?
         return wfMessage('smw_property_template')->rawParams($proplink, $typestring)->params($useCount)->escaped() . ' ' . $warnings;
     }
 }