00001 <?php
00026 class SMWDataValueFactory {
00027
00033 static private $mTypeLabels;
00034
00040 static private $mTypeAliases;
00041
00048 static private $mTypeClasses;
00049
00055 static private $mTypeDataItemIds;
00056
00062 static private $mDefaultDataItemTypeIds = array(
00063 SMWDataItem::TYPE_BLOB => '_txt',
00064 SMWDataItem::TYPE_STRING => '_str',
00065 SMWDataItem::TYPE_URI => '_uri',
00066 SMWDataItem::TYPE_WIKIPAGE => '_wpg',
00067 SMWDataItem::TYPE_NUMBER => '_num',
00068 SMWDataItem::TYPE_TIME => '_dat',
00069 SMWDataItem::TYPE_BOOLEAN => '_boo',
00070 SMWDataItem::TYPE_CONTAINER => '_rec',
00071 SMWDataItem::TYPE_GEO => '_geo',
00072 SMWDataItem::TYPE_CONCEPT => '__con',
00073 SMWDataItem::TYPE_PROPERTY => '__pro',
00074
00075
00076
00077 );
00078
00091 static public function newTypeIdValue( $typeId, $valueString = false, $caption = false,
00092 $property = null, $contextPage = null ) {
00093
00094 self::initDatatypes();
00095
00096 if ( array_key_exists( $typeId, self::$mTypeClasses ) ) {
00097 $result = new self::$mTypeClasses[$typeId]( $typeId );
00098 } else {
00099 return new SMWErrorValue( $typeId,
00100 wfMsgForContent( 'smw_unknowntype', $typeId ),
00101 $valueString, $caption );
00102 }
00103
00104 if ( !is_null( $property ) ) {
00105 $result->setProperty( $property );
00106 }
00107
00108 if ( !is_null( $contextPage ) ) {
00109 $result->setContextPage( $contextPage );
00110 }
00111
00112 if ( $valueString !== false ) {
00113 $result->setUserValue( $valueString, $caption );
00114 }
00115
00116 return $result;
00117 }
00118
00128 static public function newDataItemValue( SMWDataItem $dataItem, $property, $caption = false ) {
00129 if ( !is_null( $property ) ) {
00130 $typeId = $property->findPropertyTypeID();
00131 } else {
00132 $typeId = self::$mDefaultDataItemTypeIds[$dataItem->getDiType()];
00133 }
00134
00135 $result = self::newTypeIdValue( $typeId, false, $caption, $property );
00136 $result->setDataItem( $dataItem );
00137 if ( $caption !== false ) {
00138 $result->setCaption( $caption );
00139 }
00140 return $result;
00141 }
00142
00143
00152 static public function getDataItemId( $typeId ) {
00153 self::initDatatypes();
00154 if ( array_key_exists( $typeId, self::$mTypeDataItemIds ) ) {
00155 return self::$mTypeDataItemIds[$typeId];
00156 } else {
00157 return SMWDataItem::TYPE_NOTYPE;
00158 }
00159 }
00160
00173 static public function newPropertyObjectValue( SMWDIProperty $property, $valueString = false,
00174 $caption = false, $contextPage = null ) {
00175
00176 $typeId = $property->isInverse() ? '_wpg' : $property->findPropertyTypeID();
00177 return self::newTypeIdValue( $typeId, $valueString, $caption, $property, $contextPage );
00178 }
00179
00185 static protected function initDatatypes() {
00186 global $smwgContLang;
00187
00188 if ( is_array( self::$mTypeLabels ) ) {
00189 return;
00190 }
00191
00192 self::$mTypeLabels = $smwgContLang->getDatatypeLabels();
00193 self::$mTypeAliases = $smwgContLang->getDatatypeAliases();
00194
00195
00196
00197
00198
00199 self::$mTypeClasses = array(
00200 '_txt' => 'SMWStringValue',
00201 '_cod' => 'SMWStringValue',
00202 '_str' => 'SMWStringValue',
00203 '_ema' => 'SMWURIValue',
00204 '_uri' => 'SMWURIValue',
00205 '_anu' => 'SMWURIValue',
00206 '_tel' => 'SMWURIValue',
00207 '_wpg' => 'SMWWikiPageValue',
00208 '_wpp' => 'SMWWikiPageValue',
00209 '_wpc' => 'SMWWikiPageValue',
00210 '_wpf' => 'SMWWikiPageValue',
00211 '_num' => 'SMWNumberValue',
00212 '_tem' => 'SMWTemperatureValue',
00213 '_dat' => 'SMWTimeValue',
00214 '_boo' => 'SMWBoolValue',
00215 '_rec' => 'SMWRecordValue',
00216 '_qty' => 'SMWQuantityValue',
00217
00218 '__typ' => 'SMWTypesValue',
00219 '__pls' => 'SMWPropertyListValue',
00220 '__con' => 'SMWConceptValue',
00221 '__sps' => 'SMWStringValue',
00222 '__spu' => 'SMWURIValue',
00223 '__sup' => 'SMWWikiPageValue',
00224 '__suc' => 'SMWWikiPageValue',
00225 '__spf' => 'SMWWikiPageValue',
00226 '__sin' => 'SMWWikiPageValue',
00227 '__red' => 'SMWWikiPageValue',
00228 '__err' => 'SMWErrorValue',
00229 '__imp' => 'SMWImportValue',
00230 '__pro' => 'SMWPropertyValue',
00231 '__key' => 'SMWStringValue',
00232 );
00233
00234 self::$mTypeDataItemIds = array(
00235 '_txt' => SMWDataItem::TYPE_BLOB,
00236 '_cod' => SMWDataItem::TYPE_BLOB,
00237 '_str' => SMWDataItem::TYPE_STRING,
00238 '_ema' => SMWDataItem::TYPE_URI,
00239 '_uri' => SMWDataItem::TYPE_URI,
00240 '_anu' => SMWDataItem::TYPE_URI,
00241 '_tel' => SMWDataItem::TYPE_URI,
00242 '_wpg' => SMWDataItem::TYPE_WIKIPAGE,
00243 '_wpp' => SMWDataItem::TYPE_WIKIPAGE,
00244 '_wpc' => SMWDataItem::TYPE_WIKIPAGE,
00245 '_wpf' => SMWDataItem::TYPE_WIKIPAGE,
00246 '_num' => SMWDataItem::TYPE_NUMBER,
00247 '_tem' => SMWDataItem::TYPE_NUMBER,
00248 '_dat' => SMWDataItem::TYPE_TIME,
00249 '_boo' => SMWDataItem::TYPE_BOOLEAN,
00250 '_rec' => SMWDataItem::TYPE_CONTAINER,
00251 '_geo' => SMWDataItem::TYPE_GEO,
00252 '_qty' => SMWDataItem::TYPE_NUMBER,
00253
00254 '__typ' => SMWDataItem::TYPE_URI,
00255 '__pls' => SMWDataItem::TYPE_STRING,
00256 '__con' => SMWDataItem::TYPE_CONCEPT,
00257 '__sps' => SMWDataItem::TYPE_STRING,
00258 '__spu' => SMWDataItem::TYPE_URI,
00259 '__sup' => SMWDataItem::TYPE_WIKIPAGE,
00260 '__suc' => SMWDataItem::TYPE_WIKIPAGE,
00261 '__spf' => SMWDataItem::TYPE_WIKIPAGE,
00262 '__sin' => SMWDataItem::TYPE_WIKIPAGE,
00263 '__red' => SMWDataItem::TYPE_WIKIPAGE,
00264 '__err' => SMWDataItem::TYPE_STRING,
00265 '__imp' => SMWDataItem::TYPE_STRING,
00266 '__pro' => SMWDataItem::TYPE_PROPERTY,
00267 '__key' => SMWDataItem::TYPE_STRING,
00268 );
00269
00270 wfRunHooks( 'smwInitDatatypes' );
00271 }
00272
00282 static public function registerDatatype( $id, $className, $dataItemId, $label = false ) {
00283 self::$mTypeClasses[$id] = $className;
00284 self::$mTypeDataItemIds[$id] = $dataItemId;
00285
00286 if ( $label != false ) {
00287 self::$mTypeLabels[$id] = $label;
00288 }
00289 }
00290
00300 static public function registerDatatypeAlias( $id, $label ) {
00301 self::$mTypeAliases[$label] = $id;
00302 }
00303
00316 static public function findTypeID( $label, $useAlias = true ) {
00317 self::initDatatypes();
00318 $id = array_search( $label, self::$mTypeLabels );
00319
00320 if ( $id !== false ) {
00321 return $id;
00322 } elseif ( ( $useAlias ) && ( array_key_exists( $label, self::$mTypeAliases ) ) ) {
00323 return self::$mTypeAliases[$label];
00324 } else {
00325 return '';
00326 }
00327 }
00328
00338 static public function findTypeLabel( $id ) {
00339 self::initDatatypes();
00340
00341 if ( array_key_exists( $id, self::$mTypeLabels ) ) {
00342 return self::$mTypeLabels[$id];
00343 } else {
00344
00345
00346 return '';
00347 }
00348 }
00349
00358 static public function getKnownTypeLabels() {
00359 self::initDatatypes();
00360 return self::$mTypeLabels;
00361 }
00362
00363 }