r20189 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20188‎ | r20189 | r20190 >
Date:09:02, 7 March 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Fixed getTypeHandlerByID and getTypeLabelbyID to support custom types.
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Datatype.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Datatype.php
@@ -30,6 +30,32 @@
3131 /**
3232 * Static class for registering and retrieving typehandlers.
3333 * It also caches information about attributes found during page parsing.
 34+ *
 35+ * Currently, types are identified in three different ways within the
 36+ * storage and wiki. In the wiki, users always use the full name of some
 37+ * page in the namespace "Type:" (or whatever its localised version is
 38+ * called like). The annotation is stored internally without the namespace
 39+ * prefix but still using the (possibly also localised) name of the page.
 40+ * This abbreviated name is the "label" of the type. When storing attribute
 41+ * values, types are identified via an "id". This is an language-independent
 42+ * identifier for that type. For instance, English "Type:Integer" has the
 43+ * label "Integer" and the ID "int".
 44+ *
 45+ * For custom types, IDs must depend on the label of the type, and (to distinguish
 46+ * IDs from those of builtin types) is always prefixed with "Type:" (ignoring any
 47+ * localisation for the label of the type namespace). For example, a German custom
 48+ * type "Datentyp:Länge" has the label "Länge" and the ID "Type:Länge".
 49+ *
 50+ * Originally, the distinction of labels and ids was introduced to make the storage
 51+ * contents less dependent on the wikis text content and language. After the
 52+ * extension of the type system with custom types, this design should be revised.
 53+ * The issue is that built-in types need a internationalisation lookup for
 54+ * identifying them from their labels. Avoiding this was the intention of the IDs,
 55+ * but the implementation does not really achieve this at the moment (type labels
 56+ * are not part of the message system yet, and they are registered by their labels
 57+ * anyway suc that no second lookup is necessary).
 58+ *
 59+ * TODO: This is a mess. Completely revise the type registration and identification system.
3460 */
3561 class SMWTypeHandlerFactory {
3662
@@ -76,7 +102,10 @@
77103 // maybe a custom type?
78104 if ( (mb_substr($typeid,0,5) == 'Type:') ||
79105 ('geoarea' == $typeid) || ('geolength' == $typeid) ) { // compatibility with <0.5 DB entries
 106+ //TODO: when restructuring the type system, provide an update method that "fixes" those old types
80107 // Reuse existing float type handler:
 108+ //FIXME: This is bad, since be create a cache entry with the wrong type. It is a good shortcut
 109+ // for the given purpose, but not for other purposes where the same cache entry will be used!
81110 SMWTypeHandlerFactory::$typeLabelsByID[$typeid] = SMWTypeHandlerFactory::$typeLabelsByID['float'];
82111 $th = SMWTypeHandlerFactory::getTypeHandlerByLabel(SMWTypeHandlerFactory::$typeLabelsByID['float'], false);
83112 return $th->getXSDType();
@@ -91,12 +120,13 @@
92121 * should be reasonably light-weight.
93122 */
94123 static function getTypeHandlerByID($typeid) {
95 - if (array_key_exists($typeid,SMWTypeHandlerFactory::$typeLabelsByID)) {
96 - $label = SMWTypeHandlerFactory::$typeLabelsByID[$typeid];
 124+ $label = getTypeLabelByID($typeid);
 125+ if ( $label !== NULL ) {
97126 $th = SMWTypeHandlerFactory::getTypeHandlerByLabel($label);
98127 return $th;
 128+ } else {
 129+ return NULL;
99130 }
100 - return NULL;
101131 }
102132
103133 /**
@@ -108,6 +138,15 @@
109139 if (array_key_exists($typeid,SMWTypeHandlerFactory::$typeLabelsByID)) {
110140 return SMWTypeHandlerFactory::$typeLabelsByID[$typeid];
111141 }
 142+ // maybe a custom type? The label then is the string without the prefix "Type:"
 143+ if (mb_substr($typeid,0,5) == 'Type:') {
 144+ SMWTypeHandlerFactory::$typeLabelsByID[$typeid] = mb_substr($typeid,5);
 145+ return mb_substr($typeid,5);
 146+ } elseif ( ('geoarea' == $typeid) || ('geolength' == $typeid) ) { // compatibility with <0.5 DB entries
 147+ //TODO: when restructuring the type system, provide an update method that "fixes" those old types
 148+ SMWTypeHandlerFactory::$typeLabelsByID[$typeid] = SMWTypeHandlerFactory::$typeLabelsByID['float']; // the best we can do
 149+ return SMWTypeHandlerFactory::$typeLabelsByID['float'];
 150+ }
112151 return NULL;
113152 }
114153

Status & tagging log