/**
  * @since 2.4
  *
  * @param DIProperty $property
  */
 public function fetchConversionData(DIProperty $property)
 {
     $this->unitIds = array();
     $this->unitFactors = array();
     $this->mainUnit = false;
     $this->prefixalUnitPreference = array();
     $this->errors = array();
     $factors = $this->cachedPropertyValuesPrefetcher->getPropertyValues($property->getDiWikiPage(), new DIProperty('_CONV'));
     $this->numberValue->setContextPage($property->getDiWikiPage());
     if ($factors === null || $factors === array()) {
         // no custom type
         return $this->errors[] = 'smw_nounitsdeclared';
     }
     $number = '';
     $unit = '';
     foreach ($factors as $di) {
         // ignore corrupted data and bogus inputs
         if (!$di instanceof DIBlob || $this->numberValue->parseNumberValue($di->getString(), $number, $unit, $asPrefix) != 0 || $number == 0) {
             continue;
         }
         $this->matchUnitAliases($number, $asPrefix, preg_split('/\\s*,\\s*/u', $unit));
     }
     // No unit with factor 1? Make empty string the main unit.
     if ($this->mainUnit === false) {
         $this->mainUnit = '';
     }
     // Always add an extra empty unit; not as a synonym for the main unit
     // but as a new unit with ID '' so if users do not give any unit, the
     // conversion tooltip will still display the main unit for clarity
     // (the empty unit is never displayed; we filter it when making
     // conversion values)
     $this->unitFactors = array('' => 1) + $this->unitFactors;
     $this->unitIds[''] = '';
 }
Пример #2
0
 /**
  * This method initializes $m_unitfactors, $m_unitids, and $m_mainunit.
  */
 protected function initConversionData()
 {
     if ($this->m_unitids !== false) {
         return;
         // do the below only once
     }
     $this->m_unitids = array();
     $this->m_unitfactors = array();
     $this->m_mainunit = false;
     if (!is_null($this->m_property)) {
         $propertyDiWikiPage = $this->m_property->getDiWikiPage();
     }
     if (is_null($this->m_property) || is_null($propertyDiWikiPage)) {
         return;
         // we cannot find conversion factors without the property
     }
     $factors = \SMW\StoreFactory::getStore()->getPropertyValues($propertyDiWikiPage, new SMWDIProperty('_CONV'));
     if (count($factors) == 0) {
         // no custom type
         $this->addError(wfMessage('smw_nounitsdeclared')->inContentLanguage()->text());
         return;
     }
     $number = $unit = '';
     foreach ($factors as $di) {
         if (!$di instanceof SMWDIBlob || SMWNumberValue::parseNumberValue($di->getString(), $number, $unit) != 0 || $number == 0) {
             continue;
             // ignore corrupted data and bogus inputs
         }
         $unit_aliases = preg_split('/\\s*,\\s*/u', $unit);
         $first = true;
         foreach ($unit_aliases as $unit) {
             $unit = SMWNumberValue::normalizeUnit($unit);
             if ($first) {
                 $unitid = $unit;
                 if ($number == 1) {
                     // add main unit to front of array (displayed first)
                     $this->m_mainunit = $unit;
                     $this->m_unitfactors = array($unit => 1) + $this->m_unitfactors;
                 } else {
                     // non-main units are not ordered (can be modified via display units)
                     $this->m_unitfactors[$unit] = $number;
                 }
                 $first = false;
             }
             // add all known units to m_unitids to simplify checking for them
             $this->m_unitids[$unit] = $unitid;
         }
     }
     if ($this->m_mainunit === false) {
         // No unit with factor 1? Make empty string the main unit.
         $this->m_mainunit = '';
     }
     // always add an extra empty unit; not as a synonym for the main unit but as a new unit with ID ''
     // so if users do not give any unit, the conversion tooltip will still display the main unit for clarity
     // (the empty unit is never displayed; we filter it when making conversion values)
     $this->m_unitfactors = array('' => 1) + $this->m_unitfactors;
     $this->m_unitids[''] = '';
 }