00001 <?php
00019 class SMWDIProperty extends SMWDataItem {
00020
00035 static protected $m_prop_types;
00036
00041 static protected $m_prop_labels;
00042
00047 static protected $m_prop_aliases;
00048
00054 protected $m_key;
00055
00060 protected $m_inverse;
00061
00066 protected $m_proptypeid;
00067
00078 public function __construct( $key, $inverse = false ) {
00079 if ( ( $key === '' ) || ( $key{0} == '-' ) ) {
00080 throw new SMWDataItemException( "Illegal property key \"$key\"." );
00081 }
00082
00083 if ( $key{0} == '_' ) {
00084 SMWDIProperty::initPropertyRegistration();
00085 if ( !array_key_exists( $key, SMWDIProperty::$m_prop_types ) ) {
00086 throw new SMWDataItemException( "There is no predefined property with \"$key\"." );
00087 }
00088 }
00089
00090 $this->m_key = $key;
00091 $this->m_inverse = $inverse;
00092 }
00093
00094 public function getDIType() {
00095 return SMWDataItem::TYPE_PROPERTY;
00096 }
00097
00098 public function getKey() {
00099 return $this->m_key;
00100 }
00101
00102 public function isInverse() {
00103 return $this->m_inverse;
00104 }
00105
00106 public function getSortKey() {
00107 return $this->m_key;
00108 }
00109
00127 public function isShown() {
00128 return ( ( $this->isUserDefined() ) ||
00129 ( array_key_exists( $this->m_key, SMWDIProperty::$m_prop_types ) &&
00130 SMWDIProperty::$m_prop_types[$this->m_key][1] ) );
00131 }
00132
00139 public function isUserDefined() {
00140 return $this->m_key{0} != '_';
00141 }
00142
00149 public function getLabel() {
00150 if ( $this->isUserDefined() ) {
00151 return str_replace( '_', ' ', $this->m_key );
00152 } else {
00153 SMWDIProperty::initPropertyRegistration();
00154 if ( array_key_exists( $this->m_key, SMWDIProperty::$m_prop_labels ) ) {
00155 return SMWDIProperty::$m_prop_labels[$this->m_key];
00156 } else {
00157 return '';
00158 }
00159 }
00160 }
00161
00170 public function getDiWikiPage() {
00171 if ( $this->m_inverse ) {
00172 return null;
00173 }
00174
00175 if ( $this->isUserDefined() ) {
00176 $dbkey = $this->m_key;
00177 } else {
00178 $dbkey = str_replace( ' ', '_', $this->getLabel() );
00179 }
00180
00181 try {
00182 return new SMWDIWikiPage( $dbkey, SMW_NS_PROPERTY, '' );
00183 } catch ( SMWDataItemException $e ) {
00184 return null;
00185 }
00186 }
00187
00196 public function findPropertyTypeID() {
00197 global $smwgPDefaultType;
00198
00199 if ( !isset( $this->m_proptypeid ) ) {
00200 if ( $this->isUserDefined() ) {
00201 $diWikiPage = new SMWDIWikiPage( $this->getKey(), SMW_NS_PROPERTY, '' );
00202 $typearray = smwfGetStore()->getPropertyValues( $diWikiPage, new SMWDIProperty( '_TYPE' ) );
00203
00204 if ( count( $typearray ) >= 1 ) {
00205 $typeDataItem = reset( $typearray );
00206
00207 if ( $typeDataItem instanceof SMWDIUri ) {
00208 $this->m_proptypeid = $typeDataItem->getFragment();
00209 } else {
00210 $this->m_proptypeid = '__err';
00211 }
00212 } elseif ( count( $typearray ) == 0 ) {
00213 $this->m_proptypeid = $smwgPDefaultType;
00214 }
00215 } else {
00216 $this->m_proptypeid = self::getPredefinedPropertyTypeId( $this->m_key );
00217 }
00218 }
00219
00220 return $this->m_proptypeid;
00221 }
00222
00223
00224 public function getSerialization() {
00225 return ( $this->m_inverse ? '-' : '' ) . $this->m_key;
00226 }
00227
00236 public static function doUnserialize( $serialization ) {
00237 $inverse = false;
00238
00239 if ( $serialization{0} == '-' ) {
00240 $serialization = substr( $serialization, 1 );
00241 $inverse = true;
00242 }
00243
00244 return new SMWDIProperty( $serialization, $inverse );
00245 }
00246
00262 public static function newFromUserLabel( $label, $inverse = false ) {
00263 $id = SMWDIProperty::findPropertyID( $label );
00264
00265 if ( $id === false ) {
00266 return new SMWDIProperty( str_replace( ' ', '_', $label ), $inverse );
00267 } else {
00268 return new SMWDIProperty( $id, $inverse );
00269 }
00270 }
00271
00285 protected static function findPropertyID( $label, $useAlias = true ) {
00286 SMWDIProperty::initPropertyRegistration();
00287 $id = array_search( $label, SMWDIProperty::$m_prop_labels );
00288
00289 if ( $id !== false ) {
00290 return $id;
00291 } elseif ( ( $useAlias ) && ( array_key_exists( $label, SMWDIProperty::$m_prop_aliases ) ) ) {
00292 return SMWDIProperty::$m_prop_aliases[$label];
00293 } else {
00294 return false;
00295 }
00296 }
00297
00308 public static function getPredefinedPropertyTypeId( $key ) {
00309 if ( array_key_exists( $key, SMWDIProperty::$m_prop_types ) ) {
00310 return SMWDIProperty::$m_prop_types[$key][0];
00311 } else {
00312 return '';
00313 }
00314 }
00315
00325 static protected function findPropertyLabel( $id ) {
00326 SMWDIProperty::initPropertyRegistration();
00327 if ( array_key_exists( $id, SMWDIProperty::$m_prop_labels ) ) {
00328 return SMWDIProperty::$m_prop_labels[$id];
00329 } else {
00330 return false;
00331 }
00332 }
00333
00338 static protected function initPropertyRegistration() {
00339 if ( is_array( SMWDIProperty::$m_prop_types ) ) {
00340 return;
00341 }
00342
00343 global $smwgContLang, $smwgUseCategoryHierarchy;
00344 $datatypeLabels = $smwgContLang->getDatatypeLabels();
00345 SMWDIProperty::$m_prop_labels = $smwgContLang->getPropertyLabels() + $datatypeLabels;
00346 SMWDIProperty::$m_prop_aliases = $smwgContLang->getPropertyAliases() + $smwgContLang->getDatatypeAliases();
00347
00348
00349
00350
00351
00352 SMWDIProperty::$m_prop_types = array(
00353 '_TYPE' => array( '__typ', true ),
00354 '_URI' => array( '__spu', true ),
00355 '_INST' => array( '__sin', false ),
00356 '_UNIT' => array( '__sps', true ),
00357 '_IMPO' => array( '__imp', true ),
00358 '_CONV' => array( '__sps', true ),
00359 '_SERV' => array( '__sps', true ),
00360 '_PVAL' => array( '__sps', true ),
00361 '_REDI' => array( '__red', true ),
00362 '_SUBP' => array( '__sup', true ),
00363 '_SUBC' => array( '__suc', !$smwgUseCategoryHierarchy ),
00364 '_CONC' => array( '__con', false ),
00365 '_MDAT' => array( '_dat', false ),
00366 '_CDAT' => array( '_dat', false ),
00367 '_NEWP' => array( '_boo', false ),
00368 '_LEDT' => array( '_wpg', false ),
00369 '_ERRP' => array( '_wpp', false ),
00370 '_LIST' => array( '__pls', true ),
00371 '_SKEY' => array( '__key', false ),
00372 '_SF_DF' => array( '__spf', true ),
00373 '_SF_AF' => array( '__spf', true ),
00374 '_SOBJ' => array( '_wpg', true ),
00375 );
00376
00377 foreach ( $datatypeLabels as $typeid => $label ) {
00378 SMWDIProperty::$m_prop_types[$typeid] = array( $typeid, true );
00379 }
00380
00381 wfRunHooks( 'smwInitProperties' );
00382 }
00383
00397 static public function registerProperty( $id, $typeid, $label = false, $show = false ) {
00398 SMWDIProperty::$m_prop_types[$id] = array( $typeid, $show );
00399
00400 if ( $label != false ) {
00401 SMWDIProperty::$m_prop_labels[$id] = $label;
00402 }
00403 }
00404
00418 static public function registerPropertyAlias( $id, $label ) {
00419 SMWDIProperty::$m_prop_aliases[$label] = $id;
00420 }
00421
00422 }