r22957 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22956‎ | r22957 | r22958 >
Date:15:15, 13 June 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Prepare rewrite of datavalue code
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php (added) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
@@ -0,0 +1,524 @@
 2+<?php
 3+
 4+require_once('SMW_Datatype.php');
 5+require_once('SMW_DataValue.php');
 6+
 7+/**
 8+ * Objects of this type represent all that is known about
 9+ * a certain user-provided data value, especially its various
 10+ * representations as strings, tooltips, numbers, etc.
 11+ *
 12+ * Data values form an additional layer of abstraction between
 13+ * raw data values from the user or database, and the various
 14+ * type handlers that can convert and evaluate these values.
 15+ * Their purpose is to simplify the handling of data values,
 16+ * and to clean up the code that is involved in processing data
 17+ * values.
 18+ */
 19+class SMWOldDataValue extends SMWDataValue {
 20+ /**#@+
 21+ * @access private
 22+ */
 23+ // representations of the actual value:
 24+
 25+ /**
 26+ * The original string as specified by a user, if provided to
 27+ * initialise this object. Otherwise a generated user-friendly string
 28+ * (no xsd). Wikitext.
 29+ */
 30+ var $vuser;
 31+
 32+ /**
 33+ * XML Schema representation of single data value as stored in the DB.
 34+ * This value is important for processing, but might be completely different
 35+ * from the representations used for printout.
 36+ * Plain xml-compatible text. FALSE if value could not be determined.
 37+ */
 38+ var $vxsd;
 39+ /**
 40+ * Float for representing scalar value of $vxsd. Required for all data-types
 41+ * whose values are naturally sortable in a linear way; NULL
 42+ * otherwise.
 43+ */
 44+ var $vnum;
 45+ /**
 46+ * Unit string or empty string, plain text. This is the unit SMW
 47+ * stores in the attribute table. Where possible, datatypes should
 48+ * convert input values to the primary unit and set this to its
 49+ * canonical string representation. Note that units internally are
 50+ * only used to prevent confusion between assignments to one attribute
 51+ * which are not readily comparable. So types need not give a unit
 52+ * string if there is only one unit, and they can give unit strings if
 53+ * there are multiple representations even though they are not "units"
 54+ * in a strict sense.
 55+ */
 56+ var $unit;
 57+ /**
 58+ * Error message, if value could not be intitialised. FALSE otherwise.
 59+ */
 60+ var $error;
 61+
 62+ /**
 63+ * String identifier that describes which of the returned
 64+ * representations corresponds to the input; may be one of the array
 65+ * keys of $others, equal to $unit to denote the main value, or
 66+ * some other string or NULL if the input value was not returned with
 67+ * the parsed results at all.
 68+ * Note: the tooltip contains only the representations that are
 69+ * different from the one given by the users. To prevent a tooltip,
 70+ * just set all keys of $others to the value of $input ('' by
 71+ * default).
 72+ */
 73+ var $input;
 74+
 75+ /**
 76+ * Array of representations for this value. The strings
 77+ * are wiki text, exclusively for human eyes; non-empty keys should be
 78+ * used to identify the representations, so that repetitions in the
 79+ * tooltip can be avoided (cf. $input). The first entry in this
 80+ * array is assumed to be the most suitable representation to
 81+ * present to the user in cases where not all values can be shown.
 82+ */
 83+ var $others;
 84+
 85+ /**
 86+ * Array of desired units, used by attributes of Type:Linear
 87+ * and also for formatting of attributes of Type:DateTime.
 88+ * The first item in the array is the main value,
 89+ * the rest appear in parentheses in factbox.
 90+ * Optional, overrides the Datatype's getUnits().
 91+ * array() if unset.
 92+ *
 93+ * FALSE at initialization.
 94+ * see getDesiredUnits()
 95+
 96+ */
 97+ var $desiredUnits;
 98+ /**
 99+ * Array of links (or rather of message IDs that contain link templates).
 100+ * Some datatypes will look for added links and instantiate them with their
 101+ * processed values to point to helpful online resources. The strings in this
 102+ * array point to messages which contain the actual link strings, so those
 103+ * need to be resolved first.
 104+ *
 105+ * FALSE at initialization.
 106+ * @see getServiceLinks()
 107+ */
 108+ var $serviceLinks;
 109+ // the following can be generated automatically, and are cached afterwards
 110+ var $description; //the user string printed e.g. in the factbox
 111+ var $tooltip; //tooltip for the value in the article, possibly empty.
 112+
 113+ // additional information about the value and the context in which it was given
 114+ var $type_handler; //type handler for this object
 115+ var $infolinks; // an array of additional links provided in long descriptions of the value
 116+ var $attribute; // wiki name (without namespace) of the attribute that this value was
 117+ // assigned to, or FALSE if no attribute was given.
 118+ /**#@-*/
 119+
 120+ /**
 121+ * Just initialise variables. To create value objects, use one of the
 122+ * static methods provided below.
 123+ * @access private
 124+ */
 125+ function SMWOldDataValue($type = NULL, $desiredUnits = false) {
 126+ $this->clear();
 127+
 128+ $this->type_handler = $type;
 129+ $this->attribute = false;
 130+ $this->desiredUnits = $desiredUnits;
 131+ $this->serviceLinks = false;
 132+ }
 133+
 134+ /*********************************************************************/
 135+ /* Set methods */
 136+ /*********************************************************************/
 137+
 138+ /**
 139+ * Set the user value (and compute other representations if possible)
 140+ */
 141+ function setUserValue($value) {
 142+ $this->clear();
 143+ $this->vuser = $value;
 144+ //this is needed since typehandlers are not strictly required to
 145+ //set the user value, especially if errors are reported.
 146+
 147+ if ($this->type_handler === NULL) {
 148+ return false;
 149+ }
 150+ $this->type_handler->processValue($value, $this);
 151+ return true;
 152+ }
 153+
 154+ /**
 155+ * Set the xsd value (and compute other representations if possible)
 156+ */
 157+ function setXSDValue($value, $unit) {
 158+ $this->vxsd = $value;
 159+ if ($this->type_handler === NULL) {
 160+ return false;
 161+ }
 162+ $this->clear();
 163+ // TODO: needs to support desiredUnits as well (mak)
 164+ $this->type_handler->processXSDValue($value, $unit, $this);
 165+ return true;
 166+ }
 167+
 168+ /**
 169+ * Set the main values obtained by processing an input in
 170+ * one call. Typically used by typehandlers, not by external users.
 171+ * @param user - see $vuser
 172+ * @param xsd - see $vxsd
 173+ * @param num - see $vnum
 174+ * @param unit - see $unit
 175+ */
 176+ function setProcessedValues($user, $xsd, $num=NULL, $unit='') {
 177+ $this->vuser = $user;
 178+ $this->vxsd = $xsd;
 179+ $this->vnum = $num;
 180+ $this->unit = $unit;
 181+ }
 182+
 183+ /**
 184+ * Add a new infolink object to the links provided with this value.
 185+ */
 186+ function addInfolink($link) {
 187+ $this->infolinks[] = $link;
 188+ }
 189+
 190+ /**
 191+ * Add an infolink to the inverse search for the given attribute and value.
 192+ * Note this is a query based exactly on what the user entered,
 193+ * not canonical units or number format.
 194+ * TODO: That's dumb, we've already parsed the user entry, so
 195+ * why repeat the effort in SearchTriple? Instead tell the quick search exactly what number
 196+ * and unit to search on. There's a bug that refers to this problem.
 197+ * : OK; but the SearchTriple Special needs reimplementaiton anyway.
 198+ * E.g. it is not very user-friendly. -- mak
 199+ */
 200+ function addQuicksearchLink() {
 201+ $this->infolinks[] = SMWInfolink::newAttributeSearchLink('+', $this->attribute, $this->vuser);
 202+ }
 203+
 204+ /**
 205+ * Add further servicelinks found in the messages encoded in the
 206+ * serviceLinks array. This function is usually called with one
 207+ * or more paramters that specify the strings that are to be
 208+ * inserted into the link templates that are retrieved from the
 209+ * message texts. The number and content of the parameters is
 210+ * depending on the datatype, and the service link message is
 211+ * usually crafted with a particular datatype in mind.
 212+ */
 213+ function addServiceLinks() {
 214+ $args = func_get_args();
 215+ array_unshift($args, ''); // add a 0 element as placeholder
 216+ $serviceLinks = $this->getServiceLinks();
 217+
 218+ foreach ($serviceLinks as $sid) {
 219+ $args[0] = "smw_service_$sid";
 220+ $text = call_user_func_array('wfMsgForContent', $args);
 221+ $links = preg_split("([\n][\s]?)", $text);
 222+ foreach ($links as $link) {
 223+ $linkdat = explode('|',$link,2);
 224+ if (count($linkdat) == 2)
 225+ $this->addInfolink(SMWInfolink::newExternalLink($linkdat[0],$linkdat[1]));
 226+ }
 227+ }
 228+ }
 229+
 230+ /**
 231+ * Set some other representation for this value. See documentation for
 232+ * SMWDataValue->others.
 233+ */
 234+ function setPrintoutString($string, $key = '') {
 235+ $this->others["K$key"] = $string; // use "K" to work around PHP casting string "1" to a numerical index, even when passed with strval($key)
 236+ }
 237+
 238+ /**
 239+ * Select the input value among the given representations. See documentation
 240+ * for SMWDataValue->others.
 241+ */
 242+ function setInput($key) {
 243+ $this->input = "K$key"; // add "K" as in setPrintoutString
 244+ }
 245+
 246+ /**
 247+ * Set the attribute to which this value refers. Used to generate search links.
 248+ * The atriubte is given as a simple wiki text title, without namespace prefix.
 249+ */
 250+ function setAttribute($attribute) {
 251+ $this->attribute = $attribute;
 252+ }
 253+
 254+ /**
 255+ * Set an error message for the current datavalue. The message should be plain
 256+ * text, possibly with light wiki/html markup. Global styling, especially spans
 257+ * enclosing the whole message, are not needed.
 258+ * Note: lighter warnings for the user can also be propagated by adding them
 259+ * to one of the string representations that the user gets to see. Errors will
 260+ * make a value invalid, preventing it, e.g., from being stored in the database.
 261+ */
 262+ function setError($message) {
 263+ $this->error = $message;
 264+ $this->description = false;
 265+ $this->tooltip = false;
 266+ }
 267+
 268+ /**
 269+ * Specify an array of desired units. See SMWDatavalue::desiredUnits for details.
 270+ */
 271+ function setDesiredUnits($desiredUnits) {
 272+ $this->desiredUnits = $desiredUnits;
 273+ }
 274+
 275+
 276+ /**
 277+ * Specify an array of service links. See SMWDataValue::serviceLinks for details.
 278+ */
 279+ function setServiceLinks($serviceLinks) {
 280+ $this->serviceLinks = $serviceLinks;
 281+ }
 282+
 283+ /**
 284+ * Reset the object to contain no value at all, but keep the existing type/attribute.
 285+ */
 286+ function clear() {
 287+ $this->vuser = '';
 288+ $this->vxsd = false;
 289+ $this->vnum = NULL;
 290+ $this->unit = '';
 291+ $this->input = 'K';
 292+ $this->others = array();
 293+ $this->error = false;
 294+
 295+ $this->tooltip = false;
 296+ $this->description = false;
 297+ $this->infolinks = array();
 298+ }
 299+
 300+
 301+ /*********************************************************************/
 302+ /* Get methods */
 303+ /*********************************************************************/
 304+
 305+ public function getShortWikiText($linked = NULL) {
 306+ $this->getUserValue();
 307+ }
 308+
 309+ public function getShortHTMLText($linker = NULL) {
 310+ $this->getUserValue();
 311+ }
 312+
 313+ public function getLongWikiText($linked = NULL) {
 314+ $this->getValueDescription();
 315+ }
 316+
 317+ public function getLongHTMLText($linker = NULL) {
 318+ $this->getValueDescription();
 319+ }
 320+
 321+ /**
 322+ * Return a single user value string. If the data value
 323+ * object was initialised with a user value string, then
 324+ * this original string is returned. The returned value
 325+ * is wiki-source string (though often just plain text).
 326+ * Also, this string typically already contains a unit,
 327+ * and might have a unit that is different from the
 328+ * standard unit that the parsed value was converted to.
 329+ *
 330+ * This method might return FALSE if the data value was
 331+ * initialised not from a user value string and parsing the
 332+ * given value failed.
 333+ */
 334+ function getUserValue() {
 335+ return $this->vuser;
 336+ }
 337+
 338+ /**
 339+ * Return a single value string, obtained by parsing the
 340+ * supplied user or XSD value. Canonical representation
 341+ * that includes a unit. Wikitext.
 342+ */
 343+ function getStringValue() {
 344+ if ( count($this->others) > 0 ) {
 345+ reset($this->others);
 346+ return current($this->others); // return first element
 347+ } else {
 348+ return $this->vuser;
 349+ }
 350+ }
 351+
 352+ /**
 353+ * Return the XSD compliant version of the value, or
 354+ * FALSE if parsing the value failed and no XSD version
 355+ * is available. If the datatype has units, then this
 356+ * value is given in the unit provided by getUnit().
 357+ */
 358+ function getXSDValue() {
 359+ return $this->vxsd;
 360+ }
 361+
 362+ /**
 363+ * Return the numeric representation of the value, or NULL
 364+ * is none is available. This representation is used to
 365+ * compare values of scalar types more efficiently, especially
 366+ * for sorting queries. If the datatype has units, then this
 367+ * value is to be interpreted wrt. the unit provided by getUnit().
 368+ */
 369+ function getNumericValue() {
 370+ return $this->vnum;
 371+ }
 372+
 373+
 374+ /**
 375+ * Return the unit in which the returned value is to be interpreted.
 376+ * This string is a plain UTF-8 string without wiki or html markup.
 377+ * Returns FALSE if no unit is given for the value.
 378+ */
 379+ function getUnit() {
 380+ return $this->unit;
 381+ }
 382+
 383+ /**
 384+ * Return error string or false if no error occured.
 385+ */
 386+ function getError() {
 387+ return $this->error;
 388+ }
 389+
 390+ /**
 391+ * Return the type id for this value, or FALSE if no type was given.
 392+ */
 393+ function getTypeID() {
 394+ if ($this->type_handler !== NULL) {
 395+ return $this->type_handler->getID();
 396+ } else return false;
 397+ }
 398+
 399+ /**
 400+ * Return an array of SMWLink objects that provide additional resources
 401+ * for the given value.
 402+ * Captions can contain some HTML markup which is admissible for wiki
 403+ * text, but no more. Result might have no entries but is always an array.
 404+ */
 405+ function getInfolinks() {
 406+ return $this->infolinks;
 407+ }
 408+
 409+ /**
 410+ * Return the long description of the value, as printed for
 411+ * example in the factbox. If errors occurred, return the error message
 412+ * The result always is a wiki-source string.
 413+ */
 414+ function getValueDescription() {
 415+ if ($this->description === false) {
 416+ if ($this->error === false) {
 417+ if (count($this->others)>0) {
 418+ $sep = '';
 419+ foreach ($this->others as $other) {
 420+ $this->description .= $sep . $other;
 421+ if ('' == $sep) $sep = ' ('; else $sep = ', ';
 422+ }
 423+ if (' (' != $sep) $this->description .= ')';
 424+ }
 425+ } else { $this->description = '<span class="smwwarning">' . $this->error . '</span>'; }
 426+ }
 427+ return $this->description;
 428+ }
 429+
 430+ /**
 431+ * Return the text that is to be used as a tooltip for the value, or
 432+ * the empty string if no tooltip is provided. Tooltip strings also
 433+ * involve some markup for specifying linebreaks etc. which is then
 434+ * interpreted by the function that insertst the JScript into the
 435+ * article.
 436+ */
 437+ function getTooltip() {
 438+ if ($this->tooltip === false) {
 439+ if ($this->error === false) {
 440+ $this->tooltip = '';
 441+ $sep = '';
 442+ foreach ($this->others as $id => $other) {
 443+ if ( $id !== $this->input ) {
 444+ $this->tooltip .= $sep . $other;
 445+ $sep = ' = ';
 446+ }
 447+ }
 448+ } else { $this->tooltip = ''; } // TODO: returning $this->error; does not fully work with the JScript pre-parsing right now
 449+ }
 450+ return $this->tooltip;
 451+ }
 452+
 453+ /**
 454+ * Return a string that identifies the value of the object, and that can
 455+ * be used to compare different value objects.
 456+ */
 457+ function getHash() {
 458+ return $this->getValueDescription() . $this->vxsd . $this->unit;
 459+ // (user_out is needed here to distinguish error messages, which
 460+ // usually have no XSD and no unit)
 461+ }
 462+
 463+ /**
 464+ * Return the array of desired units (possibly empty if not given).
 465+ */
 466+ function getDesiredUnits() {
 467+ // If we don't have a value for this, get it from the attribute.
 468+ if ($this->desiredUnits === false && $this->attribute != false) {
 469+ $this->desiredUnits = SMWTypeHandlerFactory::getUnitsList($this->attribute);
 470+ }
 471+ if ($this->desiredUnits === false) {
 472+ return Array();
 473+ } else {
 474+ return $this->desiredUnits;
 475+ }
 476+ }
 477+
 478+ /**
 479+ * Return the array of service links (possibly empty if not given).
 480+ */
 481+ function getServiceLinks() {
 482+ // If we don't have a value for this, get it from the attribute.
 483+ if ($this->serviceLinks === false && $this->attribute != false) {
 484+ $this->serviceLinks = SMWTypeHandlerFactory::getServiceLinks($this->attribute);
 485+ }
 486+ if ($this->serviceLinks === false) {
 487+ return Array();
 488+ } else {
 489+ return $this->serviceLinks;
 490+ }
 491+ }
 492+
 493+ /**
 494+ * Return the array of possible values (possibly empty if not given).
 495+ * Do not cache the result, as it is cached in the TypeHandlerFactory already.
 496+ */
 497+ function getPossibleValues() {
 498+ // If we don't have a value for this, get it from the attribute.
 499+ if ( $this->attribute != false) {
 500+ return SMWTypeHandlerFactory::getPossibleValues($this->attribute);
 501+ } else {
 502+ return Array();
 503+ }
 504+ }
 505+
 506+ /**
 507+ * Return TRUE if a value was defined and understood by the given type,
 508+ * and false if parsing errors occured or no value was given.
 509+ */
 510+ function isValid() {
 511+ return ( ($this->error === false) && ($this->vuser !== false) );
 512+ }
 513+
 514+ /**
 515+ * Return TRUE if values of the given type generally have a numeric version.
 516+ */
 517+ function isNumeric() {
 518+ if ($this->type_handler !== NULL) {
 519+ return $this->type_handler->isNumeric();
 520+ } else { return false; }
 521+ }
 522+
 523+}
 524+
 525+?>
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_OldDataValue.php
___________________________________________________________________
Added: svn:eol-style
1526 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php
@@ -1,134 +1,14 @@
22 <?php
33
4 -require_once('SMW_Datatype.php');
 4+require_once('SMW_DataValueFactory.php');
55
66 /**
77 * Objects of this type represent all that is known about
88 * a certain user-provided data value, especially its various
99 * representations as strings, tooltips, numbers, etc.
10 - *
11 - * Data values form an additional layer of abstraction between
12 - * raw data values from the user or database, and the various
13 - * type handlers that can convert and evaluate these values.
14 - * Their purpose is to simplify the handling of data values,
15 - * and to clean up the code that is involved in processing data
16 - * values.
1710 */
18 -class SMWDataValue {
19 - /**#@+
20 - * @access private
21 - */
22 - // representations of the actual value:
 11+abstract class SMWDataValue {
2312
24 - /**
25 - * The original string as specified by a user, if provided to
26 - * initialise this object. Otherwise a generated user-friendly string
27 - * (no xsd). Wikitext.
28 - */
29 - var $vuser;
30 -
31 - /**
32 - * XML Schema representation of single data value as stored in the DB.
33 - * This value is important for processing, but might be completely different
34 - * from the representations used for printout.
35 - * Plain xml-compatible text. FALSE if value could not be determined.
36 - */
37 - var $vxsd;
38 - /**
39 - * Float for representing scalar value of $vxsd. Required for all data-types
40 - * whose values are naturally sortable in a linear way; NULL
41 - * otherwise.
42 - */
43 - var $vnum;
44 - /**
45 - * Unit string or empty string, plain text. This is the unit SMW
46 - * stores in the attribute table. Where possible, datatypes should
47 - * convert input values to the primary unit and set this to its
48 - * canonical string representation. Note that units internally are
49 - * only used to prevent confusion between assignments to one attribute
50 - * which are not readily comparable. So types need not give a unit
51 - * string if there is only one unit, and they can give unit strings if
52 - * there are multiple representations even though they are not "units"
53 - * in a strict sense.
54 - */
55 - var $unit;
56 - /**
57 - * Error message, if value could not be intitialised. FALSE otherwise.
58 - */
59 - var $error;
60 -
61 - /**
62 - * String identifier that describes which of the returned
63 - * representations corresponds to the input; may be one of the array
64 - * keys of $others, equal to $unit to denote the main value, or
65 - * some other string or NULL if the input value was not returned with
66 - * the parsed results at all.
67 - * Note: the tooltip contains only the representations that are
68 - * different from the one given by the users. To prevent a tooltip,
69 - * just set all keys of $others to the value of $input ('' by
70 - * default).
71 - */
72 - var $input;
73 -
74 - /**
75 - * Array of representations for this value. The strings
76 - * are wiki text, exclusively for human eyes; non-empty keys should be
77 - * used to identify the representations, so that repetitions in the
78 - * tooltip can be avoided (cf. $input). The first entry in this
79 - * array is assumed to be the most suitable representation to
80 - * present to the user in cases where not all values can be shown.
81 - */
82 - var $others;
83 -
84 - /**
85 - * Array of desired units, used by attributes of Type:Linear
86 - * and also for formatting of attributes of Type:DateTime.
87 - * The first item in the array is the main value,
88 - * the rest appear in parentheses in factbox.
89 - * Optional, overrides the Datatype's getUnits().
90 - * array() if unset.
91 - *
92 - * FALSE at initialization.
93 - * see getDesiredUnits()
94 -
95 - */
96 - var $desiredUnits;
97 - /**
98 - * Array of links (or rather of message IDs that contain link templates).
99 - * Some datatypes will look for added links and instantiate them with their
100 - * processed values to point to helpful online resources. The strings in this
101 - * array point to messages which contain the actual link strings, so those
102 - * need to be resolved first.
103 - *
104 - * FALSE at initialization.
105 - * @see getServiceLinks()
106 - */
107 - var $serviceLinks;
108 - // the following can be generated automatically, and are cached afterwards
109 - var $description; //the user string printed e.g. in the factbox
110 - var $tooltip; //tooltip for the value in the article, possibly empty.
111 -
112 - // additional information about the value and the context in which it was given
113 - var $type_handler; //type handler for this object
114 - var $infolinks; // an array of additional links provided in long descriptions of the value
115 - var $attribute; // wiki name (without namespace) of the attribute that this value was
116 - // assigned to, or FALSE if no attribute was given.
117 - /**#@-*/
118 -
119 - /**
120 - * Just initialise variables. To create value objects, use one of the
121 - * static methods provided below.
122 - * @access private
123 - */
124 - function SMWDataValue($type = NULL, $desiredUnits = false) {
125 - $this->clear();
126 -
127 - $this->type_handler = $type;
128 - $this->attribute = false;
129 - $this->desiredUnits = $desiredUnits;
130 - $this->serviceLinks = false;
131 - }
132 -
13313 /*********************************************************************/
13414 /* Static methods for initialisation */
13515 /*********************************************************************/
@@ -137,15 +17,11 @@
13818 * Create a value from a string supplied by a user for a given attribute.
13919 * If no value is given, an empty container is created, the value of which
14020 * can be set later on.
 21+ *
 22+ * @DEPRECATED
14123 */
14224 static function newAttributeValue($attribute, $value=false) {
143 - $type = SMWTypeHandlerFactory::getTypeHandler($attribute);
144 - $result = new SMWDataValue($type);
145 - $result->attribute = $attribute;
146 - // TODO: Maybe only get this for attributes types that can support it, or only get if requested?
147 - if ($value !== false)
148 - $result->setUserValue($value);
149 - return $result;
 25+ return SMWDataValueFactory::newAttributeValue($attribute, $value);
15026 }
15127
15228 /**
@@ -153,25 +29,22 @@
15430 * property, encoded as a numeric constant.
15531 * If no value is given, an empty container is created, the value of which
15632 * can be set later on.
 33+ *
 34+ * @DEPRECATED
15735 */
15836 static function newSpecialValue($specialprop, $value=false) {
159 - $type = SMWTypeHandlerFactory::getSpecialTypeHandler($specialprop);
160 - $result = new SMWDataValue($type);
161 - if ($value !== false)
162 - $result->setUserValue($value);
163 - return $result;
 37+ return SMWDataValueFactory::newSpecialValue($specialprop, $value);
16438 }
16539
16640 /**
16741 * Create a value from a user-supplied string for which a type handler is known
16842 * If no value is given, an empty container is created, the value of which
16943 * can be set later on.
 44+ *
 45+ * @DEPRECATED
17046 */
171 - static function newTypedValue($type, $value=false, $desiredUnits=array()) {
172 - $result = new SMWDataValue($type);
173 - $result->desiredUnits = $desiredUnits;
174 - if ($value !== false) $result->setUserValue($value);
175 - return $result;
 47+ static function newTypedValue(SMWTypeHandler $type, $value=false) {
 48+ return SMWDataValueFactory::newTypeHandlerValue($type, $value);
17649 }
17750
17851 /*********************************************************************/
@@ -179,212 +52,91 @@
18053 /*********************************************************************/
18154
18255 /**
183 - * Set the user value (and compute other representations if possible)
 56+ * Set the user value (and compute other representations if possible).
 57+ * The given value is a string as supplied by some user.
18458 */
185 - function setUserValue($value) {
186 - $this->clear();
187 - $this->vuser = $value;
188 - //this is needed since typehandlers are not strictly required to
189 - //set the user value, especially if errors are reported.
 59+ abstract public function setUserValue($value);
19060
191 - if ($this->type_handler === NULL) {
192 - return false;
193 - }
194 - $this->type_handler->processValue($value, $this);
195 - return true;
196 - }
197 -
19861 /**
199 - * Set the xsd value (and compute other representations if possible)
 62+ * Set the xsd value (and compute other representations if possible).
 63+ * The given value is a string that was provided by getXSDValue() (all
 64+ * implementations should support round-tripping).
20065 */
201 - function setXSDValue($value, $unit) {
202 - $this->vxsd = $value;
203 - if ($this->type_handler === NULL) {
204 - return false;
205 - }
206 - $this->clear();
207 - // TODO: needs to support desiredUnits as well (mak)
208 - $this->type_handler->processXSDValue($value, $unit, $this);
209 - return true;
210 - }
 66+ abstract public function setXSDValue($value, $unit);
21167
21268 /**
213 - * Set the main values obtained by processing an input in
214 - * one call. Typically used by typehandlers, not by external users.
215 - * @param user - see $vuser
216 - * @param xsd - see $vxsd
217 - * @param num - see $vnum
218 - * @param unit - see $unit
219 - */
220 - function setProcessedValues($user, $xsd, $num=NULL, $unit='') {
221 - $this->vuser = $user;
222 - $this->vxsd = $xsd;
223 - $this->vnum = $num;
224 - $this->unit = $unit;
225 - }
226 -
227 - /**
228 - * Add a new infolink object to the links provided with this value.
229 - */
230 - function addInfolink($link) {
231 - $this->infolinks[] = $link;
232 - }
233 -
234 - /**
235 - * Add an infolink to the inverse search for the given attribute and value.
236 - * Note this is a query based exactly on what the user entered,
237 - * not canonical units or number format.
238 - * TODO: That's dumb, we've already parsed the user entry, so
239 - * why repeat the effort in SearchTriple? Instead tell the quick search exactly what number
240 - * and unit to search on. There's a bug that refers to this problem.
241 - * : OK; but the SearchTriple Special needs reimplementaiton anyway.
242 - * E.g. it is not very user-friendly. -- mak
243 - */
244 - function addQuicksearchLink() {
245 - $this->infolinks[] = SMWInfolink::newAttributeSearchLink('+', $this->attribute, $this->vuser);
246 - }
247 -
248 - /**
249 - * Add further servicelinks found in the messages encoded in the
250 - * serviceLinks array. This function is usually called with one
251 - * or more paramters that specify the strings that are to be
252 - * inserted into the link templates that are retrieved from the
253 - * message texts. The number and content of the parameters is
254 - * depending on the datatype, and the service link message is
255 - * usually crafted with a particular datatype in mind.
256 - */
257 - function addServiceLinks() {
258 - $args = func_get_args();
259 - array_unshift($args, ''); // add a 0 element as placeholder
260 - $serviceLinks = $this->getServiceLinks();
261 -
262 - foreach ($serviceLinks as $sid) {
263 - $args[0] = "smw_service_$sid";
264 - $text = call_user_func_array('wfMsgForContent', $args);
265 - $links = preg_split("([\n][\s]?)", $text);
266 - foreach ($links as $link) {
267 - $linkdat = explode('|',$link,2);
268 - if (count($linkdat) == 2)
269 - $this->addInfolink(SMWInfolink::newExternalLink($linkdat[0],$linkdat[1]));
270 - }
271 - }
272 - }
273 -
274 - /**
27569 * Set some other representation for this value. See documentation for
27670 * SMWDataValue->others.
27771 */
278 - function setPrintoutString($string, $key = '') {
279 - $this->others["K$key"] = $string; // use "K" to work around PHP casting string "1" to a numerical index, even when passed with strval($key)
280 - }
 72+ abstract public function setPrintoutString($string, $key = '');
28173
28274 /**
28375 * Select the input value among the given representations. See documentation
28476 * for SMWDataValue->others.
28577 */
286 - function setInput($key) {
287 - $this->input = "K$key"; // add "K" as in setPrintoutString
288 - }
 78+ abstract public function setInput($key);
28979
29080 /**
29181 * Set the attribute to which this value refers. Used to generate search links.
29282 * The atriubte is given as a simple wiki text title, without namespace prefix.
29383 */
294 - function setAttribute($attribute) {
295 - $this->attribute = $attribute;
296 - }
 84+ abstract public function setAttribute($attribute);
29785
298 - /**
299 - * Set an error message for the current datavalue. The message should be plain
300 - * text, possibly with light wiki/html markup. Global styling, especially spans
301 - * enclosing the whole message, are not needed.
302 - * Note: lighter warnings for the user can also be propagated by adding them
303 - * to one of the string representations that the user gets to see. Errors will
304 - * make a value invalid, preventing it, e.g., from being stored in the database.
305 - */
306 - function setError($message) {
307 - $this->error = $message;
308 - $this->description = false;
309 - $this->tooltip = false;
310 - }
 86+ /*********************************************************************/
 87+ /* Get methods */
 88+ /*********************************************************************/
31189
31290 /**
313 - * Specify an array of desired units. See SMWDatavalue::desiredUnits for details.
 91+ * Returns a short textual representation for this data value. If the value
 92+ * was initialised from a user supplied string, then this original string
 93+ * should be reflected in this short version (i.e. no normalisation should
 94+ * normally happen). There might, however, be additional parts such as code
 95+ * for generating tooltips. The output is in wiki text.
 96+ *
 97+ * The parameter $linked controls linking of values such as titles and should
 98+ * be non-NULL and non-false if this is desired.
31499 */
315 - function setDesiredUnits($desiredUnits) {
316 - $this->desiredUnits = $desiredUnits;
317 - }
 100+ abstract public function getShortWikiText($linked = NULL);
318101
319 -
320102 /**
321 - * Specify an array of service links. See SMWDataValue::serviceLinks for details.
 103+ * Returns a short textual representation for this data value. If the value
 104+ * was initialised from a user supplied string, then this original string
 105+ * should be reflected in this short version (i.e. no normalisation should
 106+ * normally happen). There might, however, be additional parts such as code
 107+ * for generating tooltips. The output is in HTML text.
 108+ *
 109+ * The parameter $linker controls linking of values such as titles and should
 110+ * be some Linker object (or NULL for no linking).
322111 */
323 - function setServiceLinks($serviceLinks) {
324 - $this->serviceLinks = $serviceLinks;
325 - }
 112+ abstract public function getShortHTMLText($linker = NULL);
326113
327114 /**
328 - * Reset the object to contain no value at all, but keep the existing type/attribute.
 115+ * Return the long textual description of the value, as printed for
 116+ * example in the factbox. If errors occurred, return the error message
 117+ * The result always is a wiki-source string.
 118+ *
 119+ * The parameter $linked controls linking of values such as titles and should
 120+ * be non-NULL and non-false if this is desired.
329121 */
330 - function clear() {
331 - $this->vuser = '';
332 - $this->vxsd = false;
333 - $this->vnum = NULL;
334 - $this->unit = '';
335 - $this->input = 'K';
336 - $this->others = array();
337 - $this->error = false;
 122+ abstract public function getLongWikiText($linked = NULL);
338123
339 - $this->tooltip = false;
340 - $this->description = false;
341 - $this->infolinks = array();
342 - }
343 -
344 -
345 - /*********************************************************************/
346 - /* Get methods */
347 - /*********************************************************************/
348 -
349124 /**
350 - * Return a single user value string. If the data value
351 - * object was initialised with a user value string, then
352 - * this original string is returned. The returned value
353 - * is wiki-source string (though often just plain text).
354 - * Also, this string typically already contains a unit,
355 - * and might have a unit that is different from the
356 - * standard unit that the parsed value was converted to.
 125+ * Return the long textual description of the value, as printed for
 126+ * example in the factbox. If errors occurred, return the error message
 127+ * The result always is an HTML string.
357128 *
358 - * This method might return FALSE if the data value was
359 - * initialised not from a user value string and parsing the
360 - * given value failed.
 129+ * The parameter $linker controls linking of values such as titles and should
 130+ * be some Linker object (or NULL for no linking).
361131 */
362 - function getUserValue() {
363 - return $this->vuser;
364 - }
 132+ abstract public function getLongHTMLText($linker = NULL);
365133
366134 /**
367 - * Return a single value string, obtained by parsing the
368 - * supplied user or XSD value. Canonical representation
369 - * that includes a unit. Wikitext.
370 - */
371 - function getStringValue() {
372 - if ( count($this->others) > 0 ) {
373 - reset($this->others);
374 - return current($this->others); // return first element
375 - } else {
376 - return $this->vuser;
377 - }
378 - }
379 -
380 - /**
381135 * Return the XSD compliant version of the value, or
382136 * FALSE if parsing the value failed and no XSD version
383137 * is available. If the datatype has units, then this
384138 * value is given in the unit provided by getUnit().
385139 */
386 - function getXSDValue() {
387 - return $this->vxsd;
388 - }
 140+ abstract public function getXSDValue();
389141
390142 /**
391143 * Return the numeric representation of the value, or NULL
@@ -393,159 +145,44 @@
394146 * for sorting queries. If the datatype has units, then this
395147 * value is to be interpreted wrt. the unit provided by getUnit().
396148 */
397 - function getNumericValue() {
398 - return $this->vnum;
399 - }
 149+ abstract public function getNumericValue();
400150
401 -
402151 /**
403152 * Return the unit in which the returned value is to be interpreted.
404153 * This string is a plain UTF-8 string without wiki or html markup.
405154 * Returns FALSE if no unit is given for the value.
406155 */
407 - function getUnit() {
408 - return $this->unit;
409 - }
 156+ abstract public function getUnit();
410157
411158 /**
412159 * Return error string or false if no error occured.
413160 */
414 - function getError() {
415 - return $this->error;
416 - }
 161+ abstract public function getError();
417162
418163 /**
419 - * Return the type id for this value, or FALSE if no type was given.
420 - */
421 - function getTypeID() {
422 - if ($this->type_handler !== NULL) {
423 - return $this->type_handler->getID();
424 - } else return false;
425 - }
426 -
427 - /**
428164 * Return an array of SMWLink objects that provide additional resources
429165 * for the given value.
430166 * Captions can contain some HTML markup which is admissible for wiki
431167 * text, but no more. Result might have no entries but is always an array.
432168 */
433 - function getInfolinks() {
434 - return $this->infolinks;
435 - }
 169+ abstract public function getInfolinks();
436170
437171 /**
438 - * Return the long description of the value, as printed for
439 - * example in the factbox. If errors occurred, return the error message
440 - * The result always is a wiki-source string.
441 - */
442 - function getValueDescription() {
443 - if ($this->description === false) {
444 - if ($this->error === false) {
445 - if (count($this->others)>0) {
446 - $sep = '';
447 - foreach ($this->others as $other) {
448 - $this->description .= $sep . $other;
449 - if ('' == $sep) $sep = ' ('; else $sep = ', ';
450 - }
451 - if (' (' != $sep) $this->description .= ')';
452 - }
453 - } else { $this->description = '<span class="smwwarning">' . $this->error . '</span>'; }
454 - }
455 - return $this->description;
456 - }
457 -
458 - /**
459 - * Return the text that is to be used as a tooltip for the value, or
460 - * the empty string if no tooltip is provided. Tooltip strings also
461 - * involve some markup for specifying linebreaks etc. which is then
462 - * interpreted by the function that insertst the JScript into the
463 - * article.
464 - */
465 - function getTooltip() {
466 - if ($this->tooltip === false) {
467 - if ($this->error === false) {
468 - $this->tooltip = '';
469 - $sep = '';
470 - foreach ($this->others as $id => $other) {
471 - if ( $id !== $this->input ) {
472 - $this->tooltip .= $sep . $other;
473 - $sep = ' = ';
474 - }
475 - }
476 - } else { $this->tooltip = ''; } // TODO: returning $this->error; does not fully work with the JScript pre-parsing right now
477 - }
478 - return $this->tooltip;
479 - }
480 -
481 - /**
482172 * Return a string that identifies the value of the object, and that can
483173 * be used to compare different value objects.
484174 */
485 - function getHash() {
486 - return $this->getValueDescription() . $this->vxsd . $this->unit;
487 - // (user_out is needed here to distinguish error messages, which
488 - // usually have no XSD and no unit)
489 - }
 175+ abstract public function getHash();
490176
491177 /**
492 - * Return the array of desired units (possibly empty if not given).
493 - */
494 - function getDesiredUnits() {
495 - // If we don't have a value for this, get it from the attribute.
496 - if ($this->desiredUnits === false && $this->attribute != false) {
497 - $this->desiredUnits = SMWTypeHandlerFactory::getUnitsList($this->attribute);
498 - }
499 - if ($this->desiredUnits === false) {
500 - return Array();
501 - } else {
502 - return $this->desiredUnits;
503 - }
504 - }
505 -
506 - /**
507 - * Return the array of service links (possibly empty if not given).
508 - */
509 - function getServiceLinks() {
510 - // If we don't have a value for this, get it from the attribute.
511 - if ($this->serviceLinks === false && $this->attribute != false) {
512 - $this->serviceLinks = SMWTypeHandlerFactory::getServiceLinks($this->attribute);
513 - }
514 - if ($this->serviceLinks === false) {
515 - return Array();
516 - } else {
517 - return $this->serviceLinks;
518 - }
519 - }
520 -
521 - /**
522 - * Return the array of possible values (possibly empty if not given).
523 - * Do not cache the result, as it is cached in the TypeHandlerFactory already.
524 - */
525 - function getPossibleValues() {
526 - // If we don't have a value for this, get it from the attribute.
527 - if ( $this->attribute != false) {
528 - return SMWTypeHandlerFactory::getPossibleValues($this->attribute);
529 - } else {
530 - return Array();
531 - }
532 - }
533 -
534 - /**
535178 * Return TRUE if a value was defined and understood by the given type,
536179 * and false if parsing errors occured or no value was given.
537180 */
538 - function isValid() {
539 - return ( ($this->error === false) && ($this->vuser !== false) );
540 - }
 181+ abstract public function isValid();
541182
542183 /**
543184 * Return TRUE if values of the given type generally have a numeric version.
544185 */
545 - function isNumeric() {
546 - if ($this->type_handler !== NULL) {
547 - return $this->type_handler->isNumeric();
548 - } else { return false; }
549 - }
 186+ abstract public function isNumeric();
550187
551188 }
552189
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php
@@ -0,0 +1,125 @@
 2+<?php
 3+
 4+require_once('SMW_DataValue.php');
 5+require_once('SMW_OldDataValue.php');
 6+
 7+/**
 8+ * Factory class for creating SMWDataValue objects for supplied types or attributes
 9+ * and data values.
 10+ */
 11+class SMWDataValueFactory {
 12+
 13+ /**
 14+ * Array of class names and initialisation data for creating
 15+ * new SMWDataValues. Indexed by type label (without namespace).
 16+ * Each entry has the form
 17+ * array(included?, filepart, classname, parameters = NULL);
 18+ */
 19+ static private $m_valueclasses = array();
 20+
 21+ /**
 22+ * Cache for type labels, indexed by attribute name (both without namespace prefix).
 23+ */
 24+ static private $m_attributelabels = array();
 25+
 26+ /**
 27+ * Create a value from a string supplied by a user for a given attribute.
 28+ * If no value is given, an empty container is created, the value of which
 29+ * can be set later on.
 30+ */
 31+ static public function newAttributeValue($attstring, $value=false) {
 32+ if(!array_key_exists($attstring,SMWDataValueFactory::$m_attributelabels)) {
 33+ $atitle = Title::newFromText($attstring, SMW_NS_ATTRIBUTE);
 34+ if ($atitle !== NULL) {
 35+ $typearray = smwfGetStore()->getSpecialValues($atitle,SMW_SP_HAS_TYPE);
 36+ } else { $typearray = Array(); }
 37+
 38+ if (count($typearray)==1) {
 39+ SMWDataValueFactory::$m_attributelabels[$attstring] = $typearray[0];
 40+ } elseif (count($typearray)==0) {
 41+ ///TODO
 42+ return new SMWOldDataValue(new SMWErrorTypeHandler(wfMsgForContent('smw_notype')));
 43+ } else {
 44+ ///TODO
 45+ return new SMWOldDataValue(new SMWErrorTypeHandler(wfMsgForContent('smw_manytypes')));
 46+ }
 47+ }
 48+
 49+ return SMWDataValueFactory::newTypedValue(SMWDataValueFactory::$m_attributelabels[$attstring], $value);
 50+ }
 51+
 52+ /**
 53+ * Create a value from a string supplied by a user for a given special
 54+ * property, encoded as a numeric constant.
 55+ * If no value is given, an empty container is created, the value of which
 56+ * can be set later on.
 57+ */
 58+ static public function newSpecialValue($specialprop, $value=false) {
 59+ ///TODO
 60+ $type = SMWTypeHandlerFactory::getSpecialTypeHandler($specialprop);
 61+ $result = new SMWOldDataValue($type);
 62+ if ($value !== false) {
 63+ $result->setUserValue($value);
 64+ }
 65+ return $result;
 66+ }
 67+
 68+ /**
 69+ * Create a value from a user-supplied string for which only a type is known
 70+ * (given as a string name without namespace prefix).
 71+ * If no value is given, an empty container is created, the value of which
 72+ * can be set later on.
 73+ */
 74+ static public function newTypedValue($typestring, $value=false) {
 75+ if (array_key_exists($typestring, SMWDataValueFactory::$m_valueclasses)) {
 76+ $vc = SMWDataValueFactory::$m_valueclasses[$typestring];
 77+ // check if class file was already included for this class
 78+ if ($vc[0] == false) {
 79+ global $smwgIP;
 80+ if (file_exists($smwgIP . '/includes/SMW_DV_'. $vc[1] . '.php')) {
 81+ include_once($smwgIP . '/includes/SMW_DV_'. $vc[1] . '.php');
 82+ } else {
 83+ ///TODO: return SMWErrorValue if available
 84+ //return new SMWErrorTypeHandler(wfMsgForContent('smw_unknowntype',$typelabel));
 85+ return NULL;
 86+ }
 87+ $vc[0] = true;
 88+ }
 89+ return new $dv[2]($dv[3]);
 90+ } else {
 91+ ///TODO
 92+ $type = SMWTypeHandlerFactory::getTypeHandlerByLabel($typestring);
 93+ $result = new SMWOldDataValue($type);
 94+ if ($value !== false) {
 95+ $result->setUserValue($value);
 96+ }
 97+ return $result;
 98+ }
 99+ }
 100+
 101+ /**
 102+ * Create a value from a user-supplied string for which a type handler is known
 103+ * If no value is given, an empty container is created, the value of which
 104+ * can be set later on.
 105+ *
 106+ * @DEPRECATED
 107+ */
 108+ static public function newTypeHandlerValue(SMWTypeHandler $type, $value=false) {
 109+ $result = new SMWOldDataValue($type);
 110+ if ($value !== false) {
 111+ $result->setUserValue($value);
 112+ }
 113+ return $result;
 114+ }
 115+
 116+ /**
 117+ * Register a new SMWDataValue class for dealing with some type. Will be included and
 118+ * instantiated dynamically if needed.
 119+ */
 120+ static public function registerDataValueClass($typestring, $filepart, $classname, $param = NULL) {
 121+ SMWDataValueFactory::$m_valueclasses[$typestring] = array(false,$filepart,$classname,$param);
 122+ }
 123+
 124+}
 125+
 126+?>
\ No newline at end of file
Property changes on: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php
___________________________________________________________________
Added: svn:eol-style
1127 + native

Status & tagging log