http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88319

Revision: 88319
Author:   mkroetzsch
Date:     2011-05-17 16:12:43 +0000 (Tue, 17 May 2011)
Log Message:
-----------
No more "user defined datatypes" to get custom unit conversions. Instead, this 
is now done by using the new type "Quantity" for a property, and by putting all 
unit declaration on the property page.

Modified Paths:
--------------
    trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php
    trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
    trunk/extensions/SemanticMediaWiki/includes/SMW_SetupLight.php
    trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Number.php
    trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Types.php
    trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStoreLight.php
    trunk/extensions/SemanticMediaWiki/languages/SMW_Language.php
    trunk/extensions/SemanticMediaWiki/languages/SMW_LanguageEn.php
    trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php

Added Paths:
-----------
    trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Quantity.php

Removed Paths:
-------------
    trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Linear.php

Modified: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php        
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php        
2011-05-17 16:12:43 UTC (rev 88319)
@@ -111,12 +111,9 @@
        static public function newTypeIdValue( $typeid, $value = false, 
$caption = false, $property = null ) {
                self::initDatatypes();
 
-               if ( array_key_exists( $typeid, self::$mTypeClasses ) ) { // 
direct response for basic types
+               if ( array_key_exists( $typeid, self::$mTypeClasses ) ) {
                        $result = new self::$mTypeClasses[$typeid]( $typeid );
-               } elseif ( ( $typeid != '' ) && ( $typeid{0} != '_' ) ) { // 
custom type with linear conversion
-                       //var_dump($typeid);var_dump(self::$mTypeClasses);exit;
-                       $result = new self::$mTypeClasses['__lin']( $typeid );
-               } else { // type really unknown
+               } else {
                        smwfLoadExtensionMessages( 'SemanticMediaWiki' );
                        return new SMWErrorValue( $typeid, wfMsgForContent( 
'smw_unknowntype', $typeid ), $value, $caption );
                }
@@ -227,6 +224,7 @@
                        '_dat'  => 'SMWTimeValue', // Time type
                        '_boo'  => 'SMWBoolValue', // Boolean type
                        '_rec'  => 'SMWRecordValue', // Value list type 
(replacing former nary properties)
+                       '_qty'  => 'SMWQuantityValue', // Type for numbers with 
units of measurement
                        // Special types are not avaialble directly for users 
(and have no local language name):
                        '__typ' => 'SMWTypesValue', // Special type page type
                        '__tls' => 'SMWTypeListValue', // Special type list for 
decalring _rec properties
@@ -238,7 +236,6 @@
                        '__spf' => 'SMWWikiPageValue', // Special Form page 
type for Semantic Forms
                        '__sin' => 'SMWWikiPageValue', // Special instance of 
type
                        '__red' => 'SMWWikiPageValue', // Special redirect type
-                       '__lin' => 'SMWLinearValue', // Special linear unit 
conversion type
                        '__err' => 'SMWErrorValue', // Special error type
                        '__imp' => 'SMWImportValue', // Special import 
vocabulary type
                        '__pro' => 'SMWPropertyValue', // Property type 
(possibly predefined, no always based on a page)
@@ -263,6 +260,7 @@
                        '_boo'  => SMWDataItem::TYPE_BOOLEAN, // Boolean type
                        '_rec'  => SMWDataItem::TYPE_CONTAINER, // Value list 
type (replacing former nary properties)
                        '_geo'  => SMWDataItem::TYPE_GEO, // Geographical 
coordinates
+                       '_qty'  => SMWDataItem::TYPE_NUMBER, // Type for 
numbers with units of measurement
                        // Special types are not avaialble directly for users 
(and have no local language name):
                        '__typ' => SMWDataItem::TYPE_URI, // Special type page 
type
                        '__tls' => SMWDataItem::TYPE_STRING, // Special type 
list for declaring _rec properties
@@ -274,7 +272,6 @@
                        '__spf' => SMWDataItem::TYPE_WIKIPAGE, // Special Form 
page type for Semantic Forms
                        '__sin' => SMWDataItem::TYPE_WIKIPAGE, // Special 
instance of type
                        '__red' => SMWDataItem::TYPE_WIKIPAGE, // Special 
redirect type
-                       '__lin' => SMWDataItem::TYPE_NUMBER, // Special linear 
unit conversion type
                        '__err' => SMWDataItem::TYPE_STRING, // Special error 
type
                        '__imp' => SMWDataItem::TYPE_STRING, // Special import 
vocabulary type
                        '__pro' => SMWDataItem::TYPE_PROPERTY, // Property type 
(possibly predefined, no always based on a page)

Modified: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php   2011-05-17 
15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php   2011-05-17 
16:12:43 UTC (rev 88319)
@@ -167,7 +167,7 @@
        $wgAutoloadClasses['SMWTypeListValue']          = $dvDir . 
'SMW_DV_TypeList.php';
        $wgAutoloadClasses['SMWNumberValue']            = $dvDir . 
'SMW_DV_Number.php';
        $wgAutoloadClasses['SMWTemperatureValue']       = $dvDir . 
'SMW_DV_Temperature.php';
-       $wgAutoloadClasses['SMWLinearValue']            = $dvDir . 
'SMW_DV_Linear.php';
+       $wgAutoloadClasses['SMWQuantityValue']          = $dvDir . 
'SMW_DV_Quantity.php';
        $wgAutoloadClasses['SMWTimeValue']              = $dvDir . 
'SMW_DV_Time.php';
        $wgAutoloadClasses['SMWBoolValue']              = $dvDir . 
'SMW_DV_Bool.php';
        $wgAutoloadClasses['SMWConceptValue']           = $dvDir . 
'SMW_DV_Concept.php';

Modified: trunk/extensions/SemanticMediaWiki/includes/SMW_SetupLight.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/SMW_SetupLight.php      
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/SMW_SetupLight.php      
2011-05-17 16:12:43 UTC (rev 88319)
@@ -131,7 +131,7 @@
 //     $wgAutoloadClasses['SMWTypeListValue']          = $dvDir . 
'SMW_DV_TypeList.php';
        $wgAutoloadClasses['SMWNumberValue']            = $dvDir . 
'SMW_DV_Number.php';
 //     $wgAutoloadClasses['SMWTemperatureValue']       = $dvDir . 
'SMW_DV_Temperature.php';
-//     $wgAutoloadClasses['SMWLinearValue']            = $dvDir . 
'SMW_DV_Linear.php';
+//     $wgAutoloadClasses['SMWQuantityValue']          = $dvDir . 
'SMW_DV_Quantity.php';
        $wgAutoloadClasses['SMWTimeValue']              = $dvDir . 
'SMW_DV_Time.php';
 //     $wgAutoloadClasses['SMWBoolValue']              = $dvDir . 
'SMW_DV_Bool.php';
 //     $wgAutoloadClasses['SMWConceptValue']           = $dvDir . 
'SMW_DV_Concept.php';
@@ -299,7 +299,6 @@
        SMWDataValueFactory::registerDatatype('_rec', 'SMWErrorValue');
        SMWDataValueFactory::registerDatatype('__tls', 'SMWErrorValue');
        SMWDataValueFactory::registerDatatype('__con', 'SMWErrorValue');
-       SMWDataValueFactory::registerDatatype('__lin', 'SMWErrorValue');
        SMWDataValueFactory::registerDatatype('__imp', 'SMWErrorValue');
        return true;
 }

Deleted: 
trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Linear.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Linear.php    
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Linear.php    
2011-05-17 16:12:43 UTC (rev 88319)
@@ -1,193 +0,0 @@
-<?php
-/**
- * @file
- * @ingroup SMWDataValues
- */
-
-/**
- * This datavalue implements unit support custom units, for which users have
- * provided linear conversion factors within the wiki. Those user settings
- * are retrieved from a type page, the DB key of which is the type id of this
- * object.
- *
- * @author Markus Krötzsch
- * @ingroup SMWDataValues
- */
-class SMWLinearValue extends SMWNumberValue {
-
-       /// Array with format (canonical unit ID string) => (conversion factor)
-       protected $m_unitfactors = false;
-       /// Array with format (normalised unit string) => (canonical unit ID 
string)
-       protected $m_unitids = false;
-       /// Ordered array of (normalized) units that should be displayed in 
tooltips, etc.
-       protected $m_displayunits = false;
-       /// Main unit in canonical form (recognised by the conversion factor 1)
-       protected $m_mainunit = false;
-
-       protected function convertToMainUnit( $number, $unit ) {
-               $this->initConversionData();
-               if ( array_key_exists( $unit, $this->m_unitids ) ) {
-                       $this->m_unitin = $this->m_unitids[$unit];
-                       $this->m_dataitem = new SMWDINumber( $number / 
$this->m_unitfactors[$this->m_unitin], $this->m_typeid );
-                       return true;
-               } else { // unsupported unit
-                       return false;
-               } 
-       }
-
-       protected function makeConversionValues() {
-               if ( $this->m_unitvalues !== false ) return; // do this only 
once
-               $this->m_unitvalues = array();
-               if ( !$this->isValid() ) return;
-               $this->initDisplayData();
-               if ( count( $this->m_displayunits ) == 0 ) { // no display 
units, just show all
-                       foreach ( $this->m_unitfactors as $unit => $factor ) {
-                               if ( $unit != '' ) { // filter out the empty 
fallback unit that is always there
-                                       $this->m_unitvalues[$unit] = 
$this->m_dataitem->getNumber() * $factor;
-                               }
-                       }
-               } else {
-                       foreach ( $this->m_displayunits as $unit ) {
-                               /// NOTE We keep non-ID units unless the input 
unit is used, so display units can be used to pick
-                               /// the preferred form of a unit. Doing this 
requires us to recompute the conversion values whenever
-                               /// the m_unitin changes.
-                               $unitkey = ( $this->m_unitids[$unit] == 
$this->m_unitin ) ? $this->m_unitids[$unit] : $unit;
-                               $this->m_unitvalues[$unitkey] = 
$this->m_dataitem->getNumber() * $this->m_unitfactors[$this->m_unitids[$unit]];
-                       }
-               }
-       }
-
-       protected function makeUserValue() {
-               $printunit = false; // the normalised string of a known unit to 
use for printouts
-               // Check if a known unit is given as outputformat:
-               if ( ( $this->m_outformat ) && ( $this->m_outformat != '-' ) &&
-                    ( $this->m_outformat != '-n' ) && ( $this->m_outformat != 
'-u' )) { // first try given output unit
-                       $wantedunit = SMWNumberValue::normalizeUnit( 
$this->m_outformat );
-                       if ( array_key_exists( $wantedunit, $this->m_unitids ) 
) {
-                               $printunit = $wantedunit;
-                       }
-               }
-               // Alternatively, try to use the main display unit as a default:
-               if ( $printunit === false ) {
-                       $this->initDisplayData();
-                       if ( count( $this->m_displayunits ) > 0 ) {
-                               $printunit = reset( $this->m_displayunits );
-                       }
-               }
-               // Finally, fall back to main unit:
-               if ( $printunit === false ) {
-                       $printunit = $this->getUnit();
-               }
-
-               $this->m_unitin = $this->m_unitids[$printunit];
-               $this->m_unitvalues = false; // this array depends on m_unitin 
if displayunits were used, better invalidate it here
-               $value = $this->m_dataitem->getNumber() * 
$this->m_unitfactors[$this->m_unitin];
-
-               $this->m_caption = '';
-               if ( $this->m_outformat != '-u' ) { // -u is the format for 
displaying the unit only
-                       $this->m_caption .= ( ( $this->m_outformat != '-' ) && 
( $this->m_outformat != '-n' ) ? smwfNumberFormat( $value ) : $value );
-               }
-               if ( ( $printunit != '' ) && ( $this->m_outformat != '-n' ) ) { 
// -n is the format for displaying the number only
-                       if ( $this->m_outformat != '-u' ) {
-                               $this->m_caption .=  ( $this->m_outformat != 
'-' ? '&#160;' : ' ' );
-                       }
-                       $this->m_caption .= $printunit;
-               }
-       }
-
-       public function getUnitList() {
-               $this->initConversionData();
-               return array_keys( $this->m_unitfactors );
-       }
-
-       public function getUnit() {
-               $this->initConversionData();
-               return $this->m_mainunit;
-       }
-
-/// The remaining functions are relatively "private" but are kept protected 
since
-/// subclasses might exploit this to, e.g., "fake" conversion factors instead 
of
-/// getting them from the database. A cheap way of making built-in types.
-
-       /**
-        * 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;
-
-               try {
-                       $typeDiWikiPage = new SMWDIWikiPage( $this->m_typeid, 
SMW_NS_TYPE, '' );
-               } catch ( SMWDataItemException $e ) {
-                       smwfLoadExtensionMessages( 'SemanticMediaWiki' );
-                       $this->addError( wfMsgForContent( 'smw_unknowntype', 
SMWDataValueFactory::findTypeLabel( $this->getTypeID() ) ) );
-                       return;
-               }
-               $factors = smwfGetStore()->getPropertyValues( $typeDiWikiPage, 
new SMWDIProperty( '_CONV' ) );
-               if ( count( $factors ) == 0 ) { // no custom type
-                       smwfLoadExtensionMessages( 'SemanticMediaWiki' );
-                       $this->addError( wfMsgForContent( 'smw_unknowntype', 
SMWDataValueFactory::findTypeLabel( $this->getTypeID() ) ) );
-                       return;
-               }
-               $number = $unit = '';
-               foreach ( $factors as $di ) {
-                       if ( ( $di->getDIType() !== SMWDataItem::TYPE_STRING ) 
|| 
-                            ( SMWNumberValue::parseNumberValue( 
$di->getString(), $number, $unit ) != 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[''] = '';
-       }
-
-       /**
-        * This method initializes $m_displayunits.
-        */
-       protected function initDisplayData() {
-               if ( $this->m_displayunits !== false ) return; // do the below 
only once
-               $this->initConversionData(); // needed to normalise unit strings
-               $this->m_displayunits = array();
-               if ( ( $this->m_property === null ) || ( 
$this->m_property->getDIWikiPage() === null ) ) {
-                       return;
-               }
-               $dataItems = smwfGetStore()->getPropertyValues( 
$this->m_property->getDIWikiPage(), new SMWDIProperty( '_UNIT' ) );
-               $units = array();
-               foreach ( $dataItems as $di ) { // Join all if many annotations 
exist. Discouraged (random order) but possible.
-                       if ( $di->getDIType() === SMWDataItem::TYPE_STRING ) {
-                               $units = $units + preg_split( '/\s*,\s*/u', 
$di->getString() );
-                       }
-               }
-               foreach ( $units as $unit ) {
-                       $unit = SMWNumberValue::normalizeUnit( $unit );
-                       if ( array_key_exists( $unit, $this->m_unitids ) ) {
-                               $this->m_displayunits[] = $unit; // do not 
avoid duplicates, users can handle this
-                       } // note: we ignore unsuppported units -- no way to 
display them
-               }
-       }
-
-}

Modified: 
trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Number.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Number.php    
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Number.php    
2011-05-17 16:12:43 UTC (rev 88319)
@@ -100,7 +100,6 @@
                if ( $this->m_caption === false ) {
                        $this->m_caption = $value;
                }
-               $this->m_dataitem = null;
                $this->m_unitin = false;
                $this->m_unitvalues = false;
                $number = $unit = '';
@@ -112,9 +111,6 @@
                } elseif ( $this->convertToMainUnit( $number, $unit ) === false 
) { // so far so good: now convert unit and check if it is allowed
                        $this->addError( wfMsgForContent( 'smw_unitnotallowed', 
$unit ) );
                } // note that convertToMainUnit() also sets m_dataitem if valid
-               if ( $this->m_dataitem === null ) { // make sure this is always 
set
-                       $this->m_dataitem = new SMWDINumber( 32202, 
$this->m_typeid );
-               }
        }
 
        /**
@@ -215,12 +211,16 @@
        }
 
        public function getNumber() {
-               return $this->m_dataitem->getNumber();
+               return $this->isValid() ? $this->m_dataitem->getNumber() : 
32202;
        }
 
        public function getWikiValue() {
-               $unit = $this->getUnit();
-               return strval( $this->m_dataitem->getSerialization() ) . ( 
$unit != '' ? ' ' . $unit : '' );
+               if ( $this->isValid() ) {
+                       $unit = $this->getUnit();
+                       return strval( $this->m_dataitem->getSerialization() ) 
. ( $unit != '' ? ' ' . $unit : '' );
+               } else {
+                       return 'error';
+               }
        }
 
        /**
@@ -234,12 +234,20 @@
                return '';
        }
 
+       /**
+        * Create links to mapping services based on a wiki-editable message.
+        * The parameters available to the message are:
+        * $1: string of numerical value in English punctuation
+        * $2: string of integer version of value, in English punctuation
+        *
+        * @return array
+        */
        protected function getServiceLinkParams() {
-               // Create links to mapping services based on a wiki-editable 
message. The parameters
-               // available to the message are:
-               // $1: string of numerical value in English punctuation
-               // $2: string of integer version of value, in English 
punctuation
-               return array( strval( $this->m_dataitem->getNumber() ), strval( 
round( $this->m_dataitem->getNumber() ) ) );
+               if ( $this->isValid() ) {
+                       return array( strval( $this->m_dataitem->getNumber() ), 
strval( round( $this->m_dataitem->getNumber() ) ) );
+               } else {
+                       return array();
+               }
        }
 
        public function getExportData() {
@@ -276,7 +284,7 @@
         * @return boolean specifying if the unit string is allowed
         */
        protected function convertToMainUnit( $number, $unit ) {
-               $this->m_dataitem = new SMWDINumber( $number, $this->m_typeid );
+               $this->m_dataitem = new SMWDINumber( $number );
                $this->m_unitin = '';
                return ( $unit == '' );
        }

Copied: 
trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Quantity.php 
(from rev 88307, 
trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Linear.php)
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Quantity.php  
                        (rev 0)
+++ trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Quantity.php  
2011-05-17 16:12:43 UTC (rev 88319)
@@ -0,0 +1,192 @@
+<?php
+/**
+ * @file
+ * @ingroup SMWDataValues
+ */
+
+/**
+ * This datavalue implements unit support custom units, for which users have
+ * provided linear conversion factors within the wiki. Those user settings
+ * are retrieved from a property page associated with this object.
+ *
+ * @author Markus Krötzsch
+ * @ingroup SMWDataValues
+ */
+class SMWQuantityValue extends SMWNumberValue {
+
+       /// Array with format (canonical unit ID string) => (conversion factor)
+       protected $m_unitfactors = false;
+       /// Array with format (normalised unit string) => (canonical unit ID 
string)
+       protected $m_unitids = false;
+       /// Ordered array of (normalized) units that should be displayed in 
tooltips, etc.
+       protected $m_displayunits = false;
+       /// Main unit in canonical form (recognised by the conversion factor 1)
+       protected $m_mainunit = false;
+
+       protected function convertToMainUnit( $number, $unit ) {
+               $this->initConversionData();
+               if ( array_key_exists( $unit, $this->m_unitids ) ) {
+                       $this->m_unitin = $this->m_unitids[$unit];
+                       $this->m_dataitem = new SMWDINumber( $number / 
$this->m_unitfactors[$this->m_unitin], $this->m_typeid );
+                       return true;
+               } else { // unsupported unit
+                       return false;
+               } 
+       }
+
+       protected function makeConversionValues() {
+               if ( $this->m_unitvalues !== false ) return; // do this only 
once
+               $this->m_unitvalues = array();
+               if ( !$this->isValid() ) return;
+               $this->initDisplayData();
+               if ( count( $this->m_displayunits ) == 0 ) { // no display 
units, just show all
+                       foreach ( $this->m_unitfactors as $unit => $factor ) {
+                               if ( $unit != '' ) { // filter out the empty 
fallback unit that is always there
+                                       $this->m_unitvalues[$unit] = 
$this->m_dataitem->getNumber() * $factor;
+                               }
+                       }
+               } else {
+                       foreach ( $this->m_displayunits as $unit ) {
+                               /// NOTE We keep non-ID units unless the input 
unit is used, so display units can be used to pick
+                               /// the preferred form of a unit. Doing this 
requires us to recompute the conversion values whenever
+                               /// the m_unitin changes.
+                               $unitkey = ( $this->m_unitids[$unit] == 
$this->m_unitin ) ? $this->m_unitids[$unit] : $unit;
+                               $this->m_unitvalues[$unitkey] = 
$this->m_dataitem->getNumber() * $this->m_unitfactors[$this->m_unitids[$unit]];
+                       }
+               }
+       }
+
+       protected function makeUserValue() {
+               $printunit = false; // the normalised string of a known unit to 
use for printouts
+               // Check if a known unit is given as outputformat:
+               if ( ( $this->m_outformat ) && ( $this->m_outformat != '-' ) &&
+                    ( $this->m_outformat != '-n' ) && ( $this->m_outformat != 
'-u' )) { // first try given output unit
+                       $wantedunit = SMWNumberValue::normalizeUnit( 
$this->m_outformat );
+                       if ( array_key_exists( $wantedunit, $this->m_unitids ) 
) {
+                               $printunit = $wantedunit;
+                       }
+               }
+               // Alternatively, try to use the main display unit as a default:
+               if ( $printunit === false ) {
+                       $this->initDisplayData();
+                       if ( count( $this->m_displayunits ) > 0 ) {
+                               $printunit = reset( $this->m_displayunits );
+                       }
+               }
+               // Finally, fall back to main unit:
+               if ( $printunit === false ) {
+                       $printunit = $this->getUnit();
+               }
+
+               $this->m_unitin = $this->m_unitids[$printunit];
+               $this->m_unitvalues = false; // this array depends on m_unitin 
if displayunits were used, better invalidate it here
+               $value = $this->m_dataitem->getNumber() * 
$this->m_unitfactors[$this->m_unitin];
+
+               $this->m_caption = '';
+               if ( $this->m_outformat != '-u' ) { // -u is the format for 
displaying the unit only
+                       $this->m_caption .= ( ( $this->m_outformat != '-' ) && 
( $this->m_outformat != '-n' ) ? smwfNumberFormat( $value ) : $value );
+               }
+               if ( ( $printunit != '' ) && ( $this->m_outformat != '-n' ) ) { 
// -n is the format for displaying the number only
+                       if ( $this->m_outformat != '-u' ) {
+                               $this->m_caption .=  ( $this->m_outformat != 
'-' ? '&#160;' : ' ' );
+                       }
+                       $this->m_caption .= $printunit;
+               }
+       }
+
+       public function getUnitList() {
+               $this->initConversionData();
+               return array_keys( $this->m_unitfactors );
+       }
+
+       public function getUnit() {
+               $this->initConversionData();
+               return $this->m_mainunit;
+       }
+
+/// The remaining functions are relatively "private" but are kept protected 
since
+/// subclasses might exploit this to, e.g., "fake" conversion factors instead 
of
+/// getting them from the database. A cheap way of making built-in types.
+
+       /**
+        * 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 ( $this->m_property !== null ) {
+                       $propertyDiWikiPage = 
$this->m_property->getDiWikiPage();
+               }
+               if ( ( $this->m_property === null ) || ( $propertyDiWikiPage 
=== null ) ) {
+                       return; // we cannot find conversion factors without 
the property
+               }
+
+               $factors = smwfGetStore()->getPropertyValues( 
$propertyDiWikiPage, new SMWDIProperty( '_CONV' ) );
+               if ( count( $factors ) == 0 ) { // no custom type
+                       smwfLoadExtensionMessages( 'SemanticMediaWiki' );
+                       $this->addError( wfMsgForContent( 'smw_nounitsdeclared' 
) );
+                       return;
+               }
+               $number = $unit = '';
+               foreach ( $factors as $di ) {
+                       if ( ( $di->getDIType() !== SMWDataItem::TYPE_STRING ) 
|| 
+                            ( SMWNumberValue::parseNumberValue( 
$di->getString(), $number, $unit ) != 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[''] = '';
+       }
+
+       /**
+        * This method initializes $m_displayunits.
+        */
+       protected function initDisplayData() {
+               if ( $this->m_displayunits !== false ) return; // do the below 
only once
+               $this->initConversionData(); // needed to normalise unit strings
+               $this->m_displayunits = array();
+               if ( ( $this->m_property === null ) || ( 
$this->m_property->getDIWikiPage() === null ) ) {
+                       return;
+               }
+               $dataItems = smwfGetStore()->getPropertyValues( 
$this->m_property->getDIWikiPage(), new SMWDIProperty( '_UNIT' ) );
+               $units = array();
+               foreach ( $dataItems as $di ) { // Join all if many annotations 
exist. Discouraged (random order) but possible.
+                       if ( $di->getDIType() === SMWDataItem::TYPE_STRING ) {
+                               $units = $units + preg_split( '/\s*,\s*/u', 
$di->getString() );
+                       }
+               }
+               foreach ( $units as $unit ) {
+                       $unit = SMWNumberValue::normalizeUnit( $unit );
+                       if ( array_key_exists( $unit, $this->m_unitids ) ) {
+                               $this->m_displayunits[] = $unit; // do not 
avoid duplicates, users can handle this
+                       } // note: we ignore unsuppported units -- no way to 
display them
+               }
+       }
+
+}

Modified: 
trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Types.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Types.php     
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/datavalues/SMW_DV_Types.php     
2011-05-17 16:12:43 UTC (rev 88319)
@@ -145,10 +145,11 @@
        /**
         * Is this a built-in datatype shipped with SMW (or an extension of 
SMW)?
         * (Alternatively it would be a user-defined derived datatype.)
+        *
+        * @deprecated As of SMW 1.6, there are no more user-defined datatypes, 
making this method useless. Will vanish in SMW 1.6.
         */
        public function isBuiltIn() {
-               $v = $this->getDBkey();
-               return ( $v { 0 } == '_' );
+               return true;
        }
 
        /**

Modified: 
trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStoreLight.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStoreLight.php   
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStoreLight.php   
2011-05-17 16:12:43 UTC (rev 88319)
@@ -38,7 +38,6 @@
                '__sps' => true, // Special string type
                '__spu' => true, // Special uri type
                '__spf' => true, // Special form type (for Semantic Forms)
-               '__lin' => true, // Special linear unit conversion type
                '__imp' => true, // Special import vocabulary type
        );
 

Modified: trunk/extensions/SemanticMediaWiki/languages/SMW_Language.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/languages/SMW_Language.php       
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/languages/SMW_Language.php       
2011-05-17 16:12:43 UTC (rev 88319)
@@ -58,6 +58,7 @@
                'Number'                => '_num',
                'Geographic coordinate' => '_geo',
                'Temperature'           => '_tem',
+               'Quantity'              => '_qty',
                'Date'                  => '_dat',
                'Email'                 => '_ema',
                'Annotation URI'        => '_anu',

Modified: trunk/extensions/SemanticMediaWiki/languages/SMW_LanguageEn.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/languages/SMW_LanguageEn.php     
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/languages/SMW_LanguageEn.php     
2011-05-17 16:12:43 UTC (rev 88319)
@@ -40,6 +40,7 @@
                '_anu' => 'Annotation URI',  // name of the annotation URI type 
(OWL annotation property)
                '_tel' => 'Telephone number',  // name of the telephone (URI) 
type
                '_rec' => 'Record', // name of record data type
+               '_qty' => 'Quantity', // name of the number type with units of 
measurement
        );
        
        protected $m_DatatypeAliases = array(

Modified: trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php       
2011-05-17 15:29:50 UTC (rev 88318)
+++ trunk/extensions/SemanticMediaWiki/languages/SMW_Messages.php       
2011-05-17 16:12:43 UTC (rev 88319)
@@ -114,7 +114,8 @@
        'smw_nofloat'       => '"$1" is not a number.',
        'smw_infinite'      => 'Numbers as large as "$1" are not supported.',
        'smw_unitnotallowed'=> '"$1" is not declared as a valid unit of 
measurement for this property.',
-       'smw_novalues'          => 'No values specified.',
+       'smw_nounitsdeclared' => 'No units of measurement were declared for 
this property.',
+       'smw_novalues'      => 'No values specified.',
 
        // Currently unused, floats silently store units.  'smw_unexpectedunit' 
=> 'this property supports no unit conversion',
 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to