00001 <?php
00037 class SMWNumberValue extends SMWDataValue {
00038
00044 protected $m_unitvalues;
00045
00055 protected $m_unitin;
00056
00067 static protected function parseNumberValue( $value, &$number, &$unit ) {
00068
00069 $decseparator = wfMsgForContent( 'smw_decseparator' );
00070 $kiloseparator = wfMsgForContent( 'smw_kiloseparator' );
00071
00072 $parts = preg_split( '/([-+]?\s*\d+(?:\\' . $kiloseparator . '\d\d\d)*' .
00073 '(?:\\' . $decseparator . '\d+)?\s*(?:[eE][-+]?\d+)?)/u',
00074 trim( str_replace( array( ' ', ' ', ' ', ' ' ), '', $value ) ),
00075 2, PREG_SPLIT_DELIM_CAPTURE );
00076
00077 if ( count( $parts ) >= 2 ) {
00078 $numstring = str_replace( $kiloseparator, '', preg_replace( '/\s*/u', '', $parts[1] ) );
00079 if ( $decseparator != '.' ) {
00080 $numstring = str_replace( $decseparator, '.', $numstring );
00081 }
00082 list( $number ) = sscanf( $numstring, "%f" );
00083 if ( count( $parts ) >= 3 ) {
00084 $unit = self::normalizeUnit( $parts[2] );
00085 }
00086 }
00087
00088 if ( ( count( $parts ) == 1 ) || ( $numstring === '' ) ) {
00089 return 1;
00090 } elseif ( is_infinite( $number ) ) {
00091 return 2;
00092 } else {
00093 return 0;
00094 }
00095 }
00096
00097 protected function parseUserValue( $value ) {
00098
00099 if ( $this->m_caption === false ) {
00100 $this->m_caption = $value;
00101 }
00102 $this->m_unitin = false;
00103 $this->m_unitvalues = false;
00104 $number = $unit = '';
00105 $error = self::parseNumberValue( $value, $number, $unit );
00106 if ( $error == 1 ) {
00107 $this->addError( wfMsgForContent( 'smw_nofloat', $value ) );
00108 } elseif ( $error == 2 ) {
00109 $this->addError( wfMsgForContent( 'smw_infinite', $value ) );
00110 } elseif ( $this->convertToMainUnit( $number, $unit ) === false ) {
00111 $this->addError( wfMsgForContent( 'smw_unitnotallowed', $unit ) );
00112 }
00113 }
00114
00120 protected function loadDataItem( SMWDataItem $dataItem ) {
00121 if ( $dataItem->getDIType() == SMWDataItem::TYPE_NUMBER ) {
00122 $this->m_dataitem = $dataItem;
00123 $this->m_caption = false;
00124 $this->m_unitin = false;
00125 $this->makeUserValue();
00126 $this->m_unitvalues = false;
00127 return true;
00128 } else {
00129 return false;
00130 }
00131 }
00132
00133 public function setOutputFormat( $formatstring ) {
00134 if ( $formatstring != $this->m_outformat ) {
00135 $this->m_outformat = $formatstring;
00136 if ( $this->isValid() ) {
00137 $this->m_caption = false;
00138 $this->m_unitin = false;
00139 $this->makeUserValue();
00140 }
00141 }
00142 }
00143
00144 public function getShortWikiText( $linked = null ) {
00145 if ( is_null( $linked ) || ( $linked === false ) || ( $this->m_outformat == '-' )
00146 || ( $this->m_outformat == '-u' ) || ( $this->m_outformat == '-n' ) || ( !$this->isValid() ) ) {
00147 return $this->m_caption;
00148 } else {
00149 $this->makeConversionValues();
00150 $tooltip = '';
00151 $i = 0;
00152 $sep = '';
00153 foreach ( $this->m_unitvalues as $unit => $value ) {
00154 if ( $unit != $this->m_unitin ) {
00155 $tooltip .= $sep . smwfNumberFormat( $value );
00156 if ( $unit !== '' ) {
00157 $tooltip .= ' ' . $unit;
00158 }
00159 $sep = ' <br />';
00160 $i++;
00161 if ( $i >= 5 ) {
00162 break;
00163 }
00164 }
00165 }
00166 if ( $tooltip !== '' ) {
00167 SMWOutputs::requireResource( 'ext.smw.tooltips' );
00168 return '<span class="smwttinline">' . $this->m_caption . '<span class="smwttcontent">' . $tooltip . '</span></span>';
00169 } else {
00170 return $this->m_caption;
00171 }
00172 }
00173 }
00174
00175 public function getShortHTMLText( $linker = null ) {
00176 return $this->getShortWikiText( $linker );
00177 }
00178
00179 public function getLongWikiText( $linked = null ) {
00180 if ( !$this->isValid() ) {
00181 return $this->getErrorText();
00182 } else {
00183 $this->makeConversionValues();
00184 $result = '';
00185 $i = 0;
00186 foreach ( $this->m_unitvalues as $unit => $value ) {
00187 if ( $i == 1 ) {
00188 $result .= ' (';
00189 } elseif ( $i > 1 ) {
00190 $result .= ', ';
00191 }
00192 $result .= ( $this->m_outformat != '-' ? smwfNumberFormat( $value ) : $value );
00193 if ( $unit !== '' ) {
00194 $result .= ' ' . $unit;
00195 }
00196 $i++;
00197 if ( $this->m_outformat == '-' ) {
00198 break;
00199 }
00200 }
00201 if ( $i > 1 ) {
00202 $result .= ')';
00203 }
00204 return $result;
00205 }
00206 }
00207
00208 public function getLongHTMLText( $linker = null ) {
00209 return $this->getLongWikiText( $linker );
00210 }
00211
00212 public function getNumber() {
00213 return $this->isValid() ? $this->m_dataitem->getNumber() : 32202;
00214 }
00215
00216 public function getWikiValue() {
00217 if ( $this->isValid() ) {
00218 $unit = $this->getUnit();
00219 return smwfNumberFormat( $this->m_dataitem->getSerialization() ) . ( $unit !== '' ? ' ' . $unit : '' );
00220 } else {
00221 return 'error';
00222 }
00223 }
00224
00232 public function getUnit() {
00233 return '';
00234 }
00235
00244 protected function getServiceLinkParams() {
00245 if ( $this->isValid() ) {
00246 return array( strval( $this->m_dataitem->getNumber() ), strval( round( $this->m_dataitem->getNumber() ) ) );
00247 } else {
00248 return array();
00249 }
00250 }
00251
00257 static protected function normalizeUnit( $unit ) {
00258 $unit = str_replace( array( '[[', ']]' ), '', trim( $unit ) );
00259 $unit = str_replace( array( '²', '<sup>2</sup>' ), '²', $unit );
00260 $unit = str_replace( array( '³', '<sup>3</sup>' ), '³', $unit );
00261 return smwfXMLContentEncode( $unit );
00262 }
00263
00276 protected function convertToMainUnit( $number, $unit ) {
00277 $this->m_dataitem = new SMWDINumber( $number );
00278 $this->m_unitin = '';
00279 return ( $unit === '' );
00280 }
00281
00295 protected function makeConversionValues() {
00296 $this->m_unitvalues = array( '' => $this->m_dataitem->getNumber() );
00297 }
00298
00308 protected function makeUserValue() {
00309 $this->m_caption = '';
00310 if ( $this->m_outformat != '-u' ) {
00311 $this->m_caption .= ( ( $this->m_outformat != '-' ) && ( $this->m_outformat != '-n' ) ? smwfNumberFormat( $this->m_dataitem->getNumber() ) : $this->m_dataitem->getNumber() );
00312 }
00313
00314 $this->m_unitin = '';
00315 }
00316
00323 public function getUnitList() {
00324 return array( '' );
00325 }
00326
00327 }