Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValue.php |
— | — | @@ -177,7 +177,7 @@ |
178 | 178 | abstract public function getWikiValue(); |
179 | 179 | |
180 | 180 | /** |
181 | | - * Return the numeric representation of the value, or NULL |
| 181 | + * Return the numeric representation of the value, or FALSE |
182 | 182 | * is none is available. This representation is used to |
183 | 183 | * compare values of scalar types more efficiently, especially |
184 | 184 | * for sorting queries. If the datatype has units, then this |
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore.php |
— | — | @@ -44,7 +44,6 @@ |
45 | 45 | |
46 | 46 | $result = array(); |
47 | 47 | if ($specialprop === SMW_SP_HAS_CATEGORY) { // category membership |
48 | | - |
49 | 48 | $sql = 'cl_from=' . $db->addQuotes($subject->getArticleID()); |
50 | 49 | $res = $db->select( $db->tableName('categorylinks'), |
51 | 50 | 'DISTINCT cl_to', |
— | — | @@ -56,9 +55,7 @@ |
57 | 56 | } |
58 | 57 | } |
59 | 58 | $db->freeResult($res); |
60 | | - |
61 | 59 | } elseif ($specialprop === SMW_SP_REDIRECTS_TO) { // redirections |
62 | | - |
63 | 60 | $sql = 'rd_from=' . $db->addQuotes($subject->getArticleID()); |
64 | 61 | $res = $db->select( $db->tableName('redirect'), |
65 | 62 | 'rd_namespace,rd_title', |
— | — | @@ -71,18 +68,22 @@ |
72 | 69 | } |
73 | 70 | } |
74 | 71 | $db->freeResult($res); |
75 | | - |
76 | 72 | } else { // "normal" special property |
77 | | - |
78 | 73 | $sql = 'subject_id=' . $db->addQuotes($subject->getArticleID()) . |
79 | 74 | 'AND property_id=' . $db->addQuotes($specialprop); |
80 | 75 | $res = $db->select( $db->tableName('smw_specialprops'), |
81 | 76 | 'value_string', |
82 | 77 | $sql, 'SMW::getSpecialValues', $this->getSQLOptions($requestoptions) ); |
83 | 78 | // 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 |
86 | 81 | 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)) { |
87 | 88 | $result[] = $row->value_string; |
88 | 89 | } |
89 | 90 | } |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_Types.php |
— | — | @@ -3,31 +3,34 @@ |
4 | 4 | /** |
5 | 5 | * This datavalue implements special processing suitable for defining |
6 | 6 | * 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. |
7 | 12 | */ |
8 | 13 | class SMWTypesValue extends SMWDataValue { |
9 | 14 | |
10 | | - private $m_typevalues = array(); |
| 15 | + private $m_typelabels = false; |
11 | 16 | private $m_error = ''; |
| 17 | + private $m_xsdvalue = false; |
12 | 18 | |
13 | | - /*********************************************************************/ |
14 | | - /* Set methods */ |
15 | | - /*********************************************************************/ |
16 | | - |
17 | 19 | 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(); |
19 | 22 | $types = explode(';', $value); |
20 | 23 | foreach ($types as $type) { |
21 | 24 | $type = ltrim($type, ' ['); |
22 | 25 | $type = rtrim($type, ' ]'); |
23 | 26 | $ttype = Title::newFromText($type,SMW_NS_TYPE); |
24 | 27 | if ($ttype->getNamespace() == SMW_NS_TYPE) { |
25 | | - $this->m_typevalues[] = $ttype->getText(); |
| 28 | + $this->m_typelabels[] = $ttype->getText(); |
26 | 29 | } // else: wrong namespace given -- what now? TODO |
27 | 30 | } |
28 | 31 | } |
29 | 32 | |
30 | 33 | public function setXSDValue($value, $unit) { |
31 | | - $this->setUserValue($value); // no units, compatible syntax |
| 34 | + $this->m_xsdvalue = $value; // lazy parsing |
32 | 35 | } |
33 | 36 | |
34 | 37 | public function setAttribute($attribute) { // ignore |
— | — | @@ -37,19 +40,15 @@ |
38 | 41 | // no output formats supported, ignore |
39 | 42 | } |
40 | 43 | |
41 | | - /*********************************************************************/ |
42 | | - /* Get methods */ |
43 | | - /*********************************************************************/ |
44 | | - |
45 | 44 | public function getShortWikiText($linked = NULL) { |
46 | 45 | if ( ($linked === NULL) || ($linked === false) ) { |
47 | | - return implode(', ', $this->m_typevalues); |
| 46 | + return str_replace('_',' ',implode(', ', $this->getTypeLabels())); |
48 | 47 | } else { |
49 | 48 | global $wgContLang; |
50 | 49 | $result = ''; |
51 | 50 | $typenamespace = $wgContLang->getNsText(SMW_NS_TYPE); |
52 | 51 | $first = true; |
53 | | - foreach ($this->m_typevalues as $type) { |
| 52 | + foreach ($this->getTypeLabels() as $type) { |
54 | 53 | if ($first) { |
55 | 54 | $first = false; |
56 | 55 | } else { |
— | — | @@ -63,7 +62,7 @@ |
64 | 63 | |
65 | 64 | public function getShortHTMLText($linker = NULL) { |
66 | 65 | ///TODO Support linking |
67 | | - return implode(', ', $this->m_typevalues); |
| 66 | + return implode(', ', $this->m_typelabels); |
68 | 67 | } |
69 | 68 | |
70 | 69 | public function getLongWikiText($linked = NULL) { |
— | — | @@ -76,18 +75,21 @@ |
77 | 76 | |
78 | 77 | public function getXSDValue() { |
79 | 78 | 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; |
81 | 83 | } else { |
82 | 84 | return false; |
83 | 85 | } |
84 | 86 | } |
85 | 87 | |
86 | 88 | public function getWikiValue() { |
87 | | - return implode('; ', $this->m_typevalues); |
| 89 | + return str_replace( '_', ' ', $this->getXSDValue() ); |
88 | 90 | } |
89 | 91 | |
90 | 92 | public function getNumericValue() { |
91 | | - return NULL; |
| 93 | + return false; |
92 | 94 | } |
93 | 95 | |
94 | 96 | public function getUnit() { |
— | — | @@ -107,18 +109,39 @@ |
108 | 110 | } |
109 | 111 | |
110 | 112 | public function getHash() { |
111 | | - return implode('[]', $this->m_typevalues); |
| 113 | + return implode('[]', $this->getTypeLabels()); |
112 | 114 | } |
113 | 115 | |
114 | 116 | 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) ) ); |
116 | 119 | } |
117 | 120 | |
118 | 121 | public function isNumeric() { |
119 | 122 | return false; |
120 | 123 | } |
121 | 124 | |
| 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 | + } |
122 | 134 | |
| 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 | + |
123 | 146 | } |
124 | 147 | |
125 | 148 | ?> |
\ No newline at end of file |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DataValueFactory.php |
— | — | @@ -18,9 +18,9 @@ |
19 | 19 | static private $m_valueclasses = array(); |
20 | 20 | |
21 | 21 | /** |
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). |
23 | 23 | */ |
24 | | - static private $m_typelabels = array('Testnary' => 'String;Integer;Wikipage;Date'); ///DEBUG |
| 24 | + static private $m_typelabels = array(); |
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Cache for type ids, indexed by attribute name (without namespace prefix). |
— | — | @@ -56,6 +56,7 @@ |
57 | 57 | */ |
58 | 58 | static public function newAttributeObjectValue(Title $att, $value=false) { |
59 | 59 | $attstring = $att->getText(); |
| 60 | + SMWDataValueFactory::$m_typelabels['Testnary'] = SMWDataValueFactory::newSpecialValue(SMW_SP_HAS_TYPE, 'String;Integer;Wikipage;Date'); /// DEBUG |
60 | 61 | if(!array_key_exists($attstring,SMWDataValueFactory::$m_typelabels)) { |
61 | 62 | $typearray = smwfGetStore()->getSpecialValues($att,SMW_SP_HAS_TYPE); |
62 | 63 | if (count($typearray)==1) { |
— | — | @@ -112,14 +113,13 @@ |
113 | 114 | } |
114 | 115 | |
115 | 116 | /** |
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 |
119 | 119 | * can be set later on. |
120 | 120 | */ |
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()]; |
124 | 124 | // check if class file was already included for this class |
125 | 125 | if ($vc[0] == false) { |
126 | 126 | global $smwgIP; |
— | — | @@ -135,20 +135,20 @@ |
136 | 136 | $result = new $vc[2]($vc[3]); |
137 | 137 | } else { |
138 | 138 | // check for n-ary types |
139 | | - $types = explode(';', $typestring); |
140 | | - if (count($types)>1) { |
| 139 | + if (count($typevalue->getTypeLabels())>1) { |
141 | 140 | if (SMWDataValueFactory::$m_naryincluded == false) { |
142 | 141 | global $smwgIP; |
143 | 142 | include_once($smwgIP . '/includes/SMW_DV_NAry.php'); |
144 | 143 | SMWDataValueFactory::$m_naryincluded = true; |
145 | 144 | } |
146 | | - return new SMWNAryValue($types, $value); |
| 145 | + return new SMWNAryValue($typevalue, $value); |
147 | 146 | } else { |
148 | | - ///TODO |
149 | | - $type = SMWTypeHandlerFactory::getTypeHandlerByLabel($typestring); |
| 147 | + ///TODO migrate to new system |
| 148 | + $type = SMWTypeHandlerFactory::getTypeHandlerByLabel($typevalue->getWikiValue()); |
150 | 149 | $result = new SMWOldDataValue($type); |
151 | 150 | } |
152 | 151 | } |
| 152 | + |
153 | 153 | if ($value !== false) { |
154 | 154 | $result->setUserValue($value); |
155 | 155 | } |
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php |
— | — | @@ -1,5 +1,198 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -///TODO |
| 4 | +/** |
| 5 | + * This DV implements the handling of n-ary relations. |
| 6 | + */ |
5 | 7 | |
| 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 | + |
6 | 199 | ?> |
\ No newline at end of file |