00001 <?php
00049 abstract class SMWDataValue {
00050
00061 protected $m_dataitem;
00062
00070 protected $m_property = null;
00071
00080 protected $m_contextPage = null;
00081
00086 protected $m_caption;
00087
00092 protected $m_typeid;
00093
00098 protected $m_infolinks = array();
00099
00105 protected $m_outformat = false;
00106
00111 private $mHasSearchLink;
00112
00117 private $mHasServiceLinks;
00118
00124 private $mErrors = array();
00125
00131 private $mHasErrors = false;
00132
00138 public function __construct( $typeid ) {
00139 $this->m_typeid = $typeid;
00140 }
00141
00143
00152 public function setUserValue( $value, $caption = false ) {
00153 wfProfileIn( 'SMWDataValue::setUserValue (SMW)' );
00154
00155 $this->m_dataitem = null;
00156 $this->mErrors = array();
00157 $this->mHasErrors = false;
00158 $this->m_infolinks = array();
00159 $this->mHasSearchLink = false;
00160 $this->mHasServiceLinks = false;
00161 $this->m_caption = is_string( $caption ) ? trim( $caption ) : false;
00162
00163
00164 $this->parseUserValue( $value );
00165
00166
00167
00168
00169
00170
00171 if ( ( strpos( $value, "\x7f" ) !== false ) || ( strpos( $value, "\x07" ) !== false ) ) {
00172 $this->addError( wfMsgForContent( 'smw_parseerror' ) );
00173 }
00174
00175 if ( $this->isValid() ) {
00176 $this->checkAllowedValues();
00177 }
00178
00179 wfProfileOut( 'SMWDataValue::setUserValue (SMW)' );
00180 }
00181
00196 public function setDataItem( SMWDataItem $dataItem ) {
00197 $this->m_dataitem = null;
00198 $this->mErrors = $this->m_infolinks = array();
00199 $this->mHasErrors = $this->mHasSearchLink = $this->mHasServiceLinks = $this->m_caption = false;
00200 return $this->loadDataItem( $dataItem );
00201 }
00202
00210 public function setProperty( SMWDIProperty $property ) {
00211 $this->m_property = $property;
00212 }
00213
00223 public function setContextPage( SMWDIWikiPage $contextPage ) {
00224 $this->m_contextPage = $contextPage;
00225 }
00226
00233 public function setCaption( $caption ) {
00234 $this->m_caption = $caption;
00235 }
00236
00242 public function addInfolink( SMWInfolink $link ) {
00243 $this->m_infolinks[] = $link;
00244 }
00245
00254 public function addServiceLinks() {
00255 if ( $this->mHasServiceLinks ) {
00256 return;
00257 }
00258 if ( !is_null( $this->m_property ) ) {
00259 $propertyDiWikiPage = $this->m_property->getDiWikiPage();
00260 }
00261 if ( is_null( $this->m_property ) || is_null( $propertyDiWikiPage ) ) {
00262 return;
00263 }
00264
00265 $args = $this->getServiceLinkParams();
00266
00267 if ( $args === false ) {
00268 return;
00269 }
00270
00271 array_unshift( $args, '' );
00272 $servicelinks = smwfGetStore()->getPropertyValues( $propertyDiWikiPage, new SMWDIProperty( '_SERV' ) );
00273
00274 foreach ( $servicelinks as $dataItem ) {
00275 if ( !( $dataItem instanceof SMWDIString ) ) continue;
00276
00277 $args[0] = 'smw_service_' . str_replace( ' ', '_', $dataItem->getString() );
00278 $text = call_user_func_array( 'wfMsgForContent', $args );
00279 $links = preg_split( "/[\n][\s]?/u", $text );
00280
00281 foreach ( $links as $link ) {
00282 $linkdat = explode( '|', $link, 2 );
00283
00284 if ( count( $linkdat ) == 2 ) {
00285 $this->addInfolink( SMWInfolink::newExternalLink( $linkdat[0], trim( $linkdat[1] ) ) );
00286 }
00287 }
00288 }
00289 $this->mHasServiceLinks = true;
00290 }
00291
00310 public function setOutputFormat( $formatString ) {
00311 $this->m_outformat = $formatString;
00312 }
00313
00322 public function addError( $error ) {
00323 if ( is_array( $error ) ) {
00324 $this->mErrors = array_merge( $this->mErrors, $error );
00325 $this->mHasErrors = $this->mHasErrors || ( count( $error ) > 0 );
00326 } else {
00327 $this->mErrors[] = $error;
00328 $this->mHasErrors = true;
00329 }
00330 }
00331
00339 protected function clearErrors() {
00340 $this->mErrors = array();
00341 $this->mHasErrors = false;
00342 }
00343
00345
00353 abstract protected function parseUserValue( $value );
00354
00372 abstract protected function loadDataItem( SMWDataItem $dataItem );
00373
00374
00376
00401 public function getQueryDescription( $value ) {
00402 $comparator = SMW_CMP_EQ;
00403
00404 self::prepareValue( $value, $comparator );
00405
00406 $this->setUserValue( $value );
00407
00408 if ( !$this->isValid() ) {
00409 return new SMWThingDescription();
00410 } else {
00411 return new SMWValueDescription( $this->getDataItem(), $this->m_property, $comparator );
00412 }
00413 }
00414
00423 static protected function prepareValue( &$value, &$comparator ) {
00424
00425 foreach ( SMWQueryLanguage::getComparatorStrings() as $srting ) {
00426 if ( strpos( $value, $srting ) === 0 ) {
00427 $comparator = SMWQueryLanguage::getComparatorFromString( substr( $value, 0, strlen( $srting ) ) );
00428 $value = substr( $value, strlen( $srting ) );
00429 break;
00430 }
00431 }
00432 }
00433
00435
00448 public function getDataItem() {
00449 if ( $this->isValid() ) {
00450 return $this->m_dataitem;
00451 } else {
00452 return new SMWDIError( $this->mErrors );
00453 }
00454 }
00455
00466 abstract public function getShortWikiText( $linked = null );
00467
00478 abstract public function getShortHTMLText( $linker = null );
00479
00488 abstract public function getLongWikiText( $linked = null );
00489
00498 abstract public function getLongHTMLText( $linker = null );
00499
00510 public function getShortText( $outputformat, $linker = null ) {
00511 switch ( $outputformat ) {
00512 case SMW_OUTPUT_WIKI:
00513 return $this->getShortWikiText( $linker );
00514 case SMW_OUTPUT_HTML:
00515 case SMW_OUTPUT_FILE:
00516 default:
00517 return $this->getShortHTMLText( $linker );
00518 }
00519 }
00520
00529 public function getLongText( $outputformat, $linker = null ) {
00530 switch ( $outputformat ) {
00531 case SMW_OUTPUT_WIKI:
00532 return $this->getLongWikiText( $linker );
00533 case SMW_OUTPUT_HTML:
00534 case SMW_OUTPUT_FILE:
00535 default:
00536 return $this->getLongHTMLText( $linker );
00537 }
00538 }
00539
00549 public function getInfolinkText( $outputformat, $linker = null ) {
00550 $result = '';
00551 $first = true;
00552 $extralinks = array();
00553
00554 switch ( $outputformat ) {
00555 case SMW_OUTPUT_WIKI:
00556 foreach ( $this->getInfolinks() as $link ) {
00557 if ( $first ) {
00558 $result .= '<!-- --> ' . $link->getWikiText();
00559
00560 $first = false;
00561 } else {
00562 $extralinks[] = $link->getWikiText();
00563 }
00564 }
00565 break;
00566
00567 case SMW_OUTPUT_HTML: case SMW_OUTPUT_FILE: default:
00568 foreach ( $this->getInfolinks() as $link ) {
00569 if ( $first ) {
00570 $result .= '  ' . $link->getHTML( $linker );
00571 $first = false;
00572 } else {
00573 $extralinks[] = $link->getHTML( $linker );
00574 }
00575 }
00576 break;
00577 }
00578
00579 if ( count( $extralinks ) > 0 ) {
00580 $result .= smwfEncodeMessages( $extralinks, 'info', ', <!--br-->', false );
00581 }
00582
00583 return $result;
00584 }
00585
00592 abstract public function getWikiValue();
00593
00600 public function getTypeID() {
00601 return $this->m_typeid;
00602 }
00603
00610 public function getInfolinks() {
00611 if ( $this->isValid() && !is_null( $this->m_property ) ) {
00612 if ( !$this->mHasSearchLink ) {
00613 $this->mHasSearchLink = true;
00614 $this->m_infolinks[] = SMWInfolink::newPropertySearchLink( '+',
00615 $this->m_property->getLabel(), $this->getWikiValue() );
00616 }
00617
00618 if ( !$this->mHasServiceLinks ) {
00619 $this->addServiceLinks();
00620 }
00621 }
00622
00623 return $this->m_infolinks;
00624 }
00625
00631 protected function getServiceLinkParams() {
00632 return false;
00633 }
00634
00643 public function getHash() {
00644 return $this->isValid() ? $this->m_dataitem->getHash() : implode( "\t", $this->mErrors );
00645 }
00646
00653 public function isNumeric() {
00654 if ( isset( $this->m_dataitem ) ) {
00655 return is_numeric( $this->m_dataitem->getSortKey() );
00656 } else {
00657 return false;
00658 }
00659 }
00660
00667 public function isValid() {
00668 return !$this->mHasErrors && isset( $this->m_dataitem );
00669 }
00670
00677 public function getErrorText() {
00678 return smwfEncodeMessages( $this->mErrors );
00679 }
00680
00687 public function getErrors() {
00688 return $this->mErrors;
00689 }
00690
00695 protected function checkAllowedValues() {
00696 if ( !is_null( $this->m_property ) ) {
00697 $propertyDiWikiPage = $this->m_property->getDiWikiPage();
00698 }
00699
00700 if ( is_null( $this->m_property ) || is_null( $propertyDiWikiPage ) ||
00701 !isset( $this->m_dataitem ) ) {
00702 return;
00703 }
00704
00705 $allowedvalues = smwfGetStore()->getPropertyValues(
00706 $propertyDiWikiPage,
00707 new SMWDIProperty( '_PVAL' )
00708 );
00709
00710 if ( count( $allowedvalues ) == 0 ) {
00711 return;
00712 }
00713
00714 $hash = $this->m_dataitem->getHash();
00715 $testdv = SMWDataValueFactory::newTypeIDValue( $this->getTypeID() );
00716 $accept = false;
00717 $valuestring = '';
00718 foreach ( $allowedvalues as $di ) {
00719 if ( $di->getDIType() === SMWDataItem::TYPE_STRING ) {
00720 $testdv->setUserValue( $di->getString() );
00721
00722 if ( $hash === $testdv->getDataItem()->getHash() ) {
00723 $accept = true;
00724 break;
00725 } else {
00726 if ( $valuestring !== '' ) {
00727 $valuestring .= ', ';
00728 }
00729 $valuestring .= $di->getString();
00730 }
00731 }
00732 }
00733
00734 if ( !$accept ) {
00735 $this->addError(
00736 wfMsgForContent( 'smw_notinenum', $this->getWikiValue(), $valuestring )
00737 );
00738 }
00739 }
00740
00741 }