00001 <?php
00017 class SMWRecordValue extends SMWDataValue {
00018
00020 protected $m_diProperties = null;
00021
00022 protected function parseUserValue( $value ) {
00023 $this->parseUserValueOrQuery( $value, false );
00024 }
00025
00026 protected function parseUserValueOrQuery( $value, $queryMode ) {
00027 if ( $value === '' ) {
00028 $this->addError( wfMsg( 'smw_novalues' ) );
00029
00030 if ( $queryMode ) {
00031 return new SMWThingDescription();
00032 } else {
00033 return;
00034 }
00035 }
00036
00037 if ( $queryMode ) {
00038 $subdescriptions = array();
00039 } elseif ( is_null( $this->m_contextPage ) ) {
00040 $semanticData = SMWContainerSemanticData::makeAnonymousContainer();
00041 } else {
00042 $subobjectName = '_' . hash( 'md4', $value, false );
00043 $subject = new SMWDIWikiPage( $this->m_contextPage->getDBkey(),
00044 $this->m_contextPage->getNamespace(), $this->m_contextPage->getInterwiki(),
00045 $subobjectName );
00046 $semanticData = new SMWContainerSemanticData( $subject );
00047 }
00048
00049 $values = preg_split( '/[\s]*;[\s]*/u', trim( $value ) );
00050 $valueIndex = 0;
00051 $propertyIndex = 0;
00052 $empty = true;
00053
00054 foreach ( $this->getPropertyDataItems() as $diProperty ) {
00055 if ( !array_key_exists( $valueIndex, $values ) ) {
00056 break;
00057 }
00058
00059 if ( $queryMode ) {
00060 $comparator = SMW_CMP_EQ;
00061 SMWDataValue::prepareValue( $values[$valueIndex], $comparator );
00062 }
00063
00064
00065 if ( ( $values[$valueIndex] === '' ) || ( $values[$valueIndex] == '?' ) ) {
00066 $valueIndex++;
00067 } else {
00068 $dataValue = SMWDataValueFactory::newPropertyObjectValue( $diProperty, $values[$valueIndex] );
00069
00070 if ( $dataValue->isValid() ) {
00071 if ( $queryMode ) {
00072 $subdescriptions[] = new SMWSomeProperty( $diProperty, new SMWValueDescription( $dataValue->getDataItem(), $comparator ) );
00073 } else {
00074 $semanticData->addPropertyObjectValue( $diProperty, $dataValue->getDataItem() );
00075 }
00076
00077 $valueIndex++;
00078 $empty = false;
00079 } elseif ( ( count( $values ) - $valueIndex ) == ( count( $this->m_diProperties ) - $propertyIndex ) ) {
00080
00081 if ( !$queryMode ) {
00082 $semanticData->addPropertyObjectValue( $diProperty, $dataValue->getDataItem() );
00083 }
00084
00085 $this->addError( $dataValue->getErrors() );
00086 ++$valueIndex;
00087 }
00088 }
00089 ++$propertyIndex;
00090 }
00091
00092 if ( $empty ) {
00093 $this->addError( wfMsg( 'smw_novalues' ) );
00094 }
00095
00096 if ( $queryMode ) {
00097 switch ( count( $subdescriptions ) ) {
00098 case 0: return new SMWThingDescription();
00099 case 1: return reset( $subdescriptions );
00100 default: return new SMWConjunction( $subdescriptions );
00101 }
00102 } else {
00103 $this->m_dataitem = new SMWDIContainer( $semanticData );
00104 }
00105 }
00106
00112 protected function loadDataItem( SMWDataItem $dataItem ) {
00113 if ( $dataItem->getDIType() == SMWDataItem::TYPE_CONTAINER ) {
00114 $this->m_dataitem = $dataItem;
00115 return true;
00116 } else {
00117 return false;
00118 }
00119 }
00120
00125 public function getQueryDescription( $value ) {
00126 return $this->parseUserValueOrQuery( $value, true );
00127 }
00128
00129 public function getShortWikiText( $linked = null ) {
00130 if ( $this->m_caption !== false ) {
00131 return $this->m_caption;
00132 }
00133 return $this->makeOutputText( 0, $linked );
00134 }
00135
00136 public function getShortHTMLText( $linker = null ) {
00137 if ( $this->m_caption !== false ) {
00138 return $this->m_caption;
00139 }
00140 return $this->makeOutputText( 1, $linker );
00141 }
00142
00143 public function getLongWikiText( $linked = null ) {
00144 return $this->makeOutputText( 2, $linked );
00145 }
00146
00147 public function getLongHTMLText( $linker = null ) {
00148 return $this->makeOutputText( 3, $linker );
00149 }
00150
00151 public function getWikiValue() {
00152 return $this->makeOutputText( 4 );
00153 }
00154
00156 protected function checkAllowedValues() { }
00157
00163 public function setProperty( SMWDIProperty $property ) {
00164 parent::setProperty( $property );
00165 $this->m_diProperties = null;
00166 }
00167
00169
00175 public function getDVs() {
00176 return $this->getDataItems();
00177 }
00178
00189 public function getDataItems() {
00190 if ( $this->isValid() ) {
00191 $result = array();
00192 $index = 0;
00193 foreach ( $this->getPropertyDataItems() as $diProperty ) {
00194 $values = $this->getDataItem()->getSemanticData()->getPropertyValues( $diProperty );
00195 if ( count( $values ) > 0 ) {
00196 $result[$index] = reset( $values );
00197 } else {
00198 $result[$index] = null;
00199 }
00200 $index += 1;
00201 }
00202 return $result;
00203 } else {
00204 return array();
00205 }
00206 }
00207
00218 public function getPropertyDataItems() {
00219 if ( is_null( $this->m_diProperties ) ) {
00220 $this->m_diProperties = self::findPropertyDataItems( $this->m_property );
00221
00222 if ( count( $this->m_diProperties ) == 0 ) {
00223 $this->addError( 'The list of properties to be used for the data fields has not been specified properly.' );
00224 }
00225 }
00226
00227 return $this->m_diProperties;
00228 }
00229
00240 public static function findPropertyDataItems( $diProperty ) {
00241 if ( !is_null( $diProperty ) ) {
00242 $propertyDiWikiPage = $diProperty->getDiWikiPage();
00243
00244 if ( !is_null( $propertyDiWikiPage ) ) {
00245 $listDiProperty = new SMWDIProperty( '_LIST' );
00246 $dataItems = smwfGetStore()->getPropertyValues( $propertyDiWikiPage, $listDiProperty );
00247
00248 if ( count( $dataItems ) == 1 ) {
00249 $propertyListValue = new SMWPropertyListValue( '__pls' );
00250 $propertyListValue->setDataItem( reset( $dataItems ) );
00251
00252 if ( $propertyListValue->isValid() ) {
00253 return $propertyListValue->getPropertyDataItems();
00254 }
00255 }
00256 }
00257 }
00258
00259 return array();
00260 }
00261
00263
00264 protected function makeOutputText( $type = 0, $linker = null ) {
00265 if ( !$this->isValid() ) {
00266 return ( ( $type == 0 ) || ( $type == 1 ) ) ? '' : $this->getErrorText();
00267 }
00268
00269 $result = '';
00270 $i = 0;
00271 foreach ( $this->getPropertyDataItems() as $propertyDataItem ) {
00272 if ( $i == 1 ) {
00273 $result .= ( $type == 4 ) ? '; ' : ' (';
00274 } elseif ( $i > 1 ) {
00275 $result .= ( $type == 4 ) ? '; ' : ', ';
00276 }
00277 ++$i;
00278 $propertyValues = $this->m_dataitem->getSemanticData()->getPropertyValues( $propertyDataItem );
00279 $dataItem = reset( $propertyValues );
00280 if ( $dataItem !== false ) {
00281 $dataValue = SMWDataValueFactory::newDataItemValue( $dataItem, $propertyDataItem );
00282 $result .= $this->makeValueOutputText( $type, $dataValue, $linker );
00283 } else {
00284 $result .= '?';
00285 }
00286 }
00287 if ( ( $i > 1 ) && ( $type != 4 ) ) $result .= ')';
00288
00289 return $result;
00290 }
00291
00292 protected function makeValueOutputText( $type, $dataValue, $linker ) {
00293 switch ( $type ) {
00294 case 0: return $dataValue->getShortWikiText( $linker );
00295 case 1: return $dataValue->getShortHTMLText( $linker );
00296 case 2: return $dataValue->getShortWikiText( $linker );
00297 case 3: return $dataValue->getShortHTMLText( $linker );
00298 case 4: return $dataValue->getWikiValue();
00299 }
00300 }
00301
00302 }
00303