r23484 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23483‎ | r23484 | r23485 >
Date:17:20, 27 June 2007
Author:mkroetzsch
Status:old
Tags:
Comment:
Extension of Datavalue for types, uniform use of this value for type representation, inclusion of code for n-ary
values (pre-alpha)
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php
@@ -177,7 +177,7 @@
178178 abstract public function getWikiValue();
179179
180180 /**
181 - * Return the numeric representation of the value, or NULL
 181+ * Return the numeric representation of the value, or FALSE
182182 * is none is available. This representation is used to
183183 * compare values of scalar types more efficiently, especially
184184 * for sorting queries. If the datatype has units, then this
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php
@@ -44,7 +44,6 @@
4545
4646 $result = array();
4747 if ($specialprop === SMW_SP_HAS_CATEGORY) { // category membership
48 -
4948 $sql = 'cl_from=' . $db->addQuotes($subject->getArticleID());
5049 $res = $db->select( $db->tableName('categorylinks'),
5150 'DISTINCT cl_to',
@@ -56,9 +55,7 @@
5756 }
5857 }
5958 $db->freeResult($res);
60 -
6159 } elseif ($specialprop === SMW_SP_REDIRECTS_TO) { // redirections
62 -
6360 $sql = 'rd_from=' . $db->addQuotes($subject->getArticleID());
6461 $res = $db->select( $db->tableName('redirect'),
6562 'rd_namespace,rd_title',
@@ -71,18 +68,22 @@
7269 }
7370 }
7471 $db->freeResult($res);
75 -
7672 } else { // "normal" special property
77 -
7873 $sql = 'subject_id=' . $db->addQuotes($subject->getArticleID()) .
7974 'AND property_id=' . $db->addQuotes($specialprop);
8075 $res = $db->select( $db->tableName('smw_specialprops'),
8176 'value_string',
8277 $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) );
8378 // rewrite result as array
84 - ///TODO: this should not be an array of strings unless it was saved as such, do specialprop typechecks
85 - if($db->numRows( $res ) > 0) {
 79+ switch ($specialprop) {
 80+ case SMW_SP_HAS_TYPE: // type values
8681 while($row = $db->fetchObject($res)) {
 82+ $result[] = SMWDataValueFactory::newSpecialValue($specialprop,$row->value_string);
 83+ }
 84+ break;
 85+ default: // plain strings
 86+ ///TODO: this should also be handled by the appropriate special handlers
 87+ while($row = $db->fetchObject($res)) {
8788 $result[] = $row->value_string;
8889 }
8990 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php
@@ -3,31 +3,34 @@
44 /**
55 * This datavalue implements special processing suitable for defining
66 * types of properties (n-ary or binary).
 7+ * Two main use-cases exist for this class:
 8+ * - to parse and format a use-provided string in a rather tolerant way
 9+ * - to efficiently be generated from XSD values and to provide according
 10+ * wiki values, in order to support speedy creation of datavalues in
 11+ * SMWDataValueFactory.
712 */
813 class SMWTypesValue extends SMWDataValue {
914
10 - private $m_typevalues = array();
 15+ private $m_typelabels = false;
1116 private $m_error = '';
 17+ private $m_xsdvalue = false;
1218
13 - /*********************************************************************/
14 - /* Set methods */
15 - /*********************************************************************/
16 -
1719 public function setUserValue($value) {
18 - $this->m_typevalues = array();
 20+ // no use for being lazy here: plain user values are never useful
 21+ $this->m_typelabels = array();
1922 $types = explode(';', $value);
2023 foreach ($types as $type) {
2124 $type = ltrim($type, ' [');
2225 $type = rtrim($type, ' ]');
2326 $ttype = Title::newFromText($type,SMW_NS_TYPE);
2427 if ($ttype->getNamespace() == SMW_NS_TYPE) {
25 - $this->m_typevalues[] = $ttype->getText();
 28+ $this->m_typelabels[] = $ttype->getText();
2629 } // else: wrong namespace given -- what now? TODO
2730 }
2831 }
2932
3033 public function setXSDValue($value, $unit) {
31 - $this->setUserValue($value); // no units, compatible syntax
 34+ $this->m_xsdvalue = $value; // lazy parsing
3235 }
3336
3437 public function setAttribute($attribute) { // ignore
@@ -37,19 +40,15 @@
3841 // no output formats supported, ignore
3942 }
4043
41 - /*********************************************************************/
42 - /* Get methods */
43 - /*********************************************************************/
44 -
4544 public function getShortWikiText($linked = NULL) {
4645 if ( ($linked === NULL) || ($linked === false) ) {
47 - return implode(', ', $this->m_typevalues);
 46+ return str_replace('_',' ',implode(', ', $this->getTypeLabels()));
4847 } else {
4948 global $wgContLang;
5049 $result = '';
5150 $typenamespace = $wgContLang->getNsText(SMW_NS_TYPE);
5251 $first = true;
53 - foreach ($this->m_typevalues as $type) {
 52+ foreach ($this->getTypeLabels() as $type) {
5453 if ($first) {
5554 $first = false;
5655 } else {
@@ -63,7 +62,7 @@
6463
6564 public function getShortHTMLText($linker = NULL) {
6665 ///TODO Support linking
67 - return implode(', ', $this->m_typevalues);
 66+ return implode(', ', $this->m_typelabels);
6867 }
6968
7069 public function getLongWikiText($linked = NULL) {
@@ -76,18 +75,21 @@
7776
7877 public function getXSDValue() {
7978 if ($this->isValid()) {
80 - return str_replace(' ','_',implode(', ', $this->m_typevalues));
 79+ if ($this->m_xsdvalue === false) {
 80+ $this->m_xsdvalue = str_replace(' ','_',implode('; ', $this->m_typelabels));
 81+ }
 82+ return $this->m_xsdvalue;
8183 } else {
8284 return false;
8385 }
8486 }
8587
8688 public function getWikiValue() {
87 - return implode('; ', $this->m_typevalues);
 89+ return str_replace( '_', ' ', $this->getXSDValue() );
8890 }
8991
9092 public function getNumericValue() {
91 - return NULL;
 93+ return false;
9294 }
9395
9496 public function getUnit() {
@@ -107,18 +109,39 @@
108110 }
109111
110112 public function getHash() {
111 - return implode('[]', $this->m_typevalues);
 113+ return implode('[]', $this->getTypeLabels());
112114 }
113115
114116 public function isValid() {
115 - return ( ($this->m_error == '') && (count($this->m_typevalues)>0) );
 117+ return ( ($this->m_error == '') &&
 118+ ( ($this->m_typelabels !== false) || ($this->m_xsdvalue !== false) ) );
116119 }
117120
118121 public function isNumeric() {
119122 return false;
120123 }
121124
 125+ /**
 126+ * Retrieve type labels if needed. Can be done lazily.
 127+ */
 128+ public function getTypeLabels() {
 129+ if ( ($this->m_typelabels === false) && ($this->m_xsdvalue !== false) ) {
 130+ $this->m_typelabels = explode('; ', $this->m_xsdvalue);
 131+ }
 132+ return $this->m_typelabels; // false only if nothing set yet
 133+ }
122134
 135+ /**
 136+ * Retrieve type values.
 137+ */
 138+ public function getTypeValues() {
 139+ $result = array();
 140+ foreach ($this->getTypeLabels() as $tl) {
 141+ $result[] = SMWDataValueFactory::newSpecialValue(SMW_SP_HAS_TYPE, $tl);
 142+ }
 143+ return $result;
 144+ }
 145+
123146 }
124147
125148 ?>
\ No newline at end of file
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php
@@ -18,9 +18,9 @@
1919 static private $m_valueclasses = array();
2020
2121 /**
22 - * Cache for type specifications (right now strings), indexed by attribute name (both without namespace prefix).
 22+ * Cache for type specifications (type datavalues), indexed by attribute name (both without namespace prefix).
2323 */
24 - static private $m_typelabels = array('Testnary' => 'String;Integer;Wikipage;Date'); ///DEBUG
 24+ static private $m_typelabels = array();
2525
2626 /**
2727 * Cache for type ids, indexed by attribute name (without namespace prefix).
@@ -56,6 +56,7 @@
5757 */
5858 static public function newAttributeObjectValue(Title $att, $value=false) {
5959 $attstring = $att->getText();
 60+ SMWDataValueFactory::$m_typelabels['Testnary'] = SMWDataValueFactory::newSpecialValue(SMW_SP_HAS_TYPE, 'String;Integer;Wikipage;Date'); /// DEBUG
6061 if(!array_key_exists($attstring,SMWDataValueFactory::$m_typelabels)) {
6162 $typearray = smwfGetStore()->getSpecialValues($att,SMW_SP_HAS_TYPE);
6263 if (count($typearray)==1) {
@@ -112,14 +113,13 @@
113114 }
114115
115116 /**
116 - * Create a value from a user-supplied string for which only a type is known
117 - * (given as a string name without namespace prefix).
118 - * If no value is given, an empty container is created, the value of which
 117+ * Create a value from a type value (basically containing strings).
 118+ * If no $value is given, an empty container is created, the value of which
119119 * can be set later on.
120120 */
121 - static public function newTypedValue($typestring, $value=false) {
122 - if (array_key_exists($typestring, SMWDataValueFactory::$m_valueclasses)) {
123 - $vc = SMWDataValueFactory::$m_valueclasses[$typestring];
 121+ static public function newTypedValue(SMWDataValue $typevalue, $value=false) {
 122+ if (array_key_exists($typevalue->getWikiValue(), SMWDataValueFactory::$m_valueclasses)) {
 123+ $vc = SMWDataValueFactory::$m_valueclasses[$typevalue->getWikiValue()];
124124 // check if class file was already included for this class
125125 if ($vc[0] == false) {
126126 global $smwgIP;
@@ -135,20 +135,20 @@
136136 $result = new $vc[2]($vc[3]);
137137 } else {
138138 // check for n-ary types
139 - $types = explode(';', $typestring);
140 - if (count($types)>1) {
 139+ if (count($typevalue->getTypeLabels())>1) {
141140 if (SMWDataValueFactory::$m_naryincluded == false) {
142141 global $smwgIP;
143142 include_once($smwgIP . '/includes/SMW_DV_NAry.php');
144143 SMWDataValueFactory::$m_naryincluded = true;
145144 }
146 - return new SMWNAryValue($types, $value);
 145+ return new SMWNAryValue($typevalue, $value);
147146 } else {
148 - ///TODO
149 - $type = SMWTypeHandlerFactory::getTypeHandlerByLabel($typestring);
 147+ ///TODO migrate to new system
 148+ $type = SMWTypeHandlerFactory::getTypeHandlerByLabel($typevalue->getWikiValue());
150149 $result = new SMWOldDataValue($type);
151150 }
152151 }
 152+
153153 if ($value !== false) {
154154 $result->setUserValue($value);
155155 }
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php
@@ -1,5 +1,198 @@
22 <?php
33
4 -///TODO
 4+/**
 5+ * This DV implements the handling of n-ary relations.
 6+ */
57
 8+require_once('SMW_DataValue.php');
 9+
 10+class SMWNAryValue extends SMWDataValue {
 11+
 12+ /**
 13+ * The original string as specified by a user, if provided to
 14+ * initialise this object. Otherwise a generated user-friendly string
 15+ * (no xsd). Wikitext.
 16+ */
 17+ private $uvalue;
 18+
 19+ /**
 20+ * XML Schema representation of single data value as stored in the DB.
 21+ * This value is important for processing, but might be completely different
 22+ * from the representations used for printout.
 23+ * Plain xml-compatible text. FALSE if value could not be determined.
 24+ */
 25+ private $vxsd;
 26+
 27+ /**
 28+ * The array of the data values within this container value
 29+ */
 30+ private $m_values = array();
 31+
 32+ /**
 33+ * variable for representing error messages
 34+ */
 35+ private $m_error;
 36+
 37+ /**
 38+ * constructor to create n-ary data value types and set their initial
 39+ * value appropriately.
 40+ */
 41+ function SMWNAryValue($type, $value) {
 42+ $values = explode(';', $value);
 43+ $types = $type->getTypeValues();
 44+ if (sizeof($types) > sizeof($values)) {
 45+ $this->m_error = "[relation only supports " . (sizeof($types) . " parameters. However, you supplied " . sizeof($values)) . " parameters]"; /// TODO: internationalise
 46+ } else if (sizeof($types) < sizeof($values)) {
 47+ ///TODO - use default values for types that were not explicitly specified?
 48+ } else {
 49+ // everything specified and passed correctly
 50+ for ($i = 0; $i < sizeof($types); $i++) {
 51+ $this->m_values[$i] = SMWDataValueFactory::newTypedValue($types[$i], $values[$i]);
 52+ }
 53+ }
 54+ }
 55+
 56+ public function setUserValue($value) {
 57+ $this->uvalue = $value;
 58+ /// TODO: acutally *set* the value, including $this->m_values!
 59+ }
 60+
 61+ public function setXSDValue($value, $unit) {
 62+ // ignore parameter $unit
 63+ $this->vxsd = $value;
 64+ /// TODO: acutally *set* the value, including $this->m_values!
 65+ }
 66+
 67+ public function setAttribute($attribute) {
 68+ /// TODO
 69+ }
 70+
 71+ public function setOutputFormat($formatstring) {
 72+ /// TODO
 73+ }
 74+
 75+ public function getShortWikiText($linked = NULL) {
 76+ if ($this->uvalue) {
 77+ return $this->uvalue;
 78+ }
 79+ $result = '';
 80+ $first = true;
 81+ foreach ($this->m_values as $value) {
 82+ if ($first) {
 83+ $first = false;
 84+ } else {
 85+ $result .= ", ";
 86+ }
 87+ $result .= $value->getShortWikiText($linked);
 88+ }
 89+ return $result;
 90+ }
 91+
 92+ public function getShortHTMLText($linker = NULL) {
 93+ /// TODO: beautify with (...) like WikiText
 94+ $result = '';
 95+ $first = true;
 96+ foreach ($this->m_values as $value) {
 97+ if ($first) {
 98+ $first = false;
 99+ } else {
 100+ $result .= ", ";
 101+ }
 102+ $result .= $value->getShortHTMLText($linker);
 103+ }
 104+ return $result;
 105+ }
 106+
 107+ public function getLongWikiText($linked = NULL) {
 108+ $result = '';
 109+ $first = true;
 110+ $second = true;
 111+ foreach ($this->m_values as $value) {
 112+ if ($first) {
 113+ $first = false;
 114+ } else if ($second) {
 115+ $result .= ' (';
 116+ $second = false;
 117+ }
 118+ else {
 119+ $result .= ", ";
 120+ }
 121+ $result .= $value->getLongWikiText($linked);
 122+ }
 123+ return $second ? $result : $result .= ')';
 124+ }
 125+
 126+ public function getLongHTMLText($linker = NULL) {
 127+ /// TODO: beautify with (...) like WikiText
 128+ $result = '';
 129+ $first = true;
 130+ foreach ($this->m_values as $value) {
 131+ if ($first) {
 132+ $first = false;
 133+ } else {
 134+ $result .= ", ";
 135+ }
 136+ $result .= $type->getLongHTMLText($linker);
 137+ }
 138+ return $result;
 139+ }
 140+
 141+ public function getXSDValue() {
 142+ $result = '';
 143+ $first = true;
 144+ $second = true;
 145+ foreach ($this->m_values as $value) {
 146+ if ($first) {
 147+ $first = false;
 148+ } else {
 149+ $result .= "; ";
 150+ }
 151+ $result .= $value->getXSDValue();
 152+ }
 153+ return $result;
 154+ }
 155+
 156+ public function getWikiValue() {
 157+ ///TODO
 158+ return 'TODO';
 159+ }
 160+
 161+ public function getNumericValue() {
 162+ return false;
 163+ }
 164+
 165+ public function getUnit() {
 166+ return false;
 167+ }
 168+
 169+ public function getError() {
 170+ return $this->m_error;
 171+ }
 172+
 173+ public function getTypeID() {
 174+ return 'nary';
 175+ }
 176+
 177+ public function getInfolinks() {
 178+ return array();
 179+ }
 180+
 181+ public function getHash() {
 182+ $hash = '';
 183+ foreach ($this->m_values as $value) {
 184+ $hash .= $value->getHash();
 185+ /// FIXME: this is wrong (different value combinations yield same hash)
 186+ }
 187+ return md5($hash);
 188+ }
 189+
 190+ public function isValid() {
 191+ return (count($this->m_values) > 0);
 192+ }
 193+
 194+ public function isNumeric() {
 195+ return false; // the n-ary is clearly non numeric (n-ary values cannot be ordered by numbers)
 196+ }
 197+}
 198+
6199 ?>
\ No newline at end of file

Status & tagging log