00001 <?php
00024 class SMWSemanticData {
00025
00031 static protected $mPropertyPrefix = '';
00032
00041 public $stubObject;
00042
00049 protected $mPropVals = array();
00050
00056 protected $mProperties = array();
00057
00063 protected $mHasVisibleProps = false;
00064
00073 protected $mHasVisibleSpecs = false;
00074
00086 protected $mNoDuplicates;
00087
00095 protected $mSubject;
00096
00103 public function __construct( SMWDIWikiPage $subject, $noDuplicates = true ) {
00104 $this->clear();
00105 $this->mSubject = $subject;
00106 $this->mNoDuplicates = $noDuplicates;
00107 }
00108
00122 public function __sleep() {
00123 return array( 'mSubject' );
00124 }
00125
00131 public function getSubject() {
00132 return $this->mSubject;
00133 }
00134
00140 public function getProperties() {
00141 ksort( $this->mProperties, SORT_STRING );
00142 return $this->mProperties;
00143 }
00144
00151 public function getPropertyValues( SMWDIProperty $property ) {
00152 if ( $property->isInverse() ) {
00153 return array();
00154 }
00155
00156 if ( array_key_exists( $property->getKey(), $this->mPropVals ) ) {
00157 return $this->mPropVals[$property->getKey()];
00158 } else {
00159 return array();
00160 }
00161 }
00162
00171 public function getHash() {
00172 $ctx = hash_init( 'md5' );
00173
00174
00175 hash_update( $ctx, '_#_' . $this->mSubject->getSerialization() );
00176
00177 foreach ( $this->getProperties() as $property ) {
00178 hash_update( $ctx, '_#_' . $property->getKey() . '##' );
00179
00180 foreach ( $this->getPropertyValues( $property ) as $di ) {
00181 hash_update( $ctx, '_#_' . $di->getSerialization() );
00182 }
00183 }
00184
00185 return hash_final( $ctx );
00186 }
00187
00197 public function hasVisibleProperties() {
00198 return $this->mHasVisibleProps;
00199 }
00200
00211 public function hasVisibleSpecialProperties() {
00212 return $this->mHasVisibleSpecs;
00213 }
00214
00226 public function addPropertyObjectValue( SMWDIProperty $property, SMWDataItem $dataItem ) {
00227 if ( $property->isInverse() ) {
00228 return;
00229 }
00230
00231 if ( !array_key_exists( $property->getKey(), $this->mPropVals ) ) {
00232 $this->mPropVals[$property->getKey()] = array();
00233 $this->mProperties[$property->getKey()] = $property;
00234 }
00235
00236 if ( $this->mNoDuplicates ) {
00237 $this->mPropVals[$property->getKey()][$dataItem->getHash()] = $dataItem;
00238 } else {
00239 $this->mPropVals[$property->getKey()][] = $dataItem;
00240 }
00241
00242 if ( !$property->isUserDefined() ) {
00243 if ( $property->isShown() ) {
00244 $this->mHasVisibleSpecs = true;
00245 $this->mHasVisibleProps = true;
00246 }
00247 } else {
00248 $this->mHasVisibleProps = true;
00249 }
00250 }
00251
00259 public function addPropertyValue( $propertyName, SMWDataItem $dataItem ) {
00260 $propertyKey = smwfNormalTitleDBKey( $propertyName );
00261
00262 if ( array_key_exists( $propertyKey, $this->mProperties ) ) {
00263 $property = $this->mProperties[$propertyKey];
00264 } else {
00265 if ( self::$mPropertyPrefix === '' ) {
00266 global $wgContLang;
00267 self::$mPropertyPrefix = $wgContLang->getNsText( SMW_NS_PROPERTY ) . ':';
00268 }
00269
00270 $propertyDV = SMWPropertyValue::makeUserProperty( self::$mPropertyPrefix . $propertyName );
00271
00272 if ( !$propertyDV->isValid() ) {
00273 return;
00274 }
00275
00276 $property = $propertyDV->getDataItem();
00277 }
00278
00279 $this->addPropertyObjectValue( $property, $dataItem );
00280 }
00281
00285 public function clear() {
00286 $this->mPropVals = array();
00287 $this->mProperties = array();
00288 $this->mHasVisibleProps = false;
00289 $this->mHasVisibleSpecs = false;
00290 $this->stubObject = false;
00291 }
00292
00300 public function importDataFrom( SMWSemanticData $semanticData ) {
00301
00302 if ( count( $this->mProperties ) == 0 &&
00303 ( $semanticData->mNoDuplicates >= $this->mNoDuplicates ) ) {
00304 $this->mProperties = $semanticData->getProperties();
00305 $this->mPropVals = array();
00306 foreach ( $this->mProperties as $property ) {
00307 $this->mPropVals[$property->getKey()] = $semanticData->getPropertyValues( $property );
00308 }
00309 $this->mHasVisibleProps = $semanticData->hasVisibleProperties();
00310 $this->mHasVisibleSpecs = $semanticData->hasVisibleSpecialProperties();
00311 } else {
00312 foreach ( $semanticData->getProperties() as $property ) {
00313 $values = $semanticData->getPropertyValues( $property );
00314 foreach ( $values as $dataItem ) {
00315 $this->addPropertyObjectValue( $property, $dataItem);
00316 }
00317 }
00318 }
00319 }
00320
00321 }