Index: trunk/extensions/SemanticMediaWiki/includes/SMW_DV_NAry.php |
— | — | @@ -8,246 +8,310 @@ |
9 | 9 | |
10 | 10 | class SMWNAryValue extends SMWDataValue { |
11 | 11 | |
12 | | - /** |
13 | | - * The array of the data values within this container value |
14 | | - */ |
15 | | - private $m_values = Array(); |
| 12 | + /** |
| 13 | + * The array of the data values within this container value |
| 14 | + */ |
| 15 | + private $m_values = Array(); |
16 | 16 | |
17 | | - /** |
18 | | - * Is this n-ary datavalue valid? |
19 | | - */ |
20 | | - private $isValid; |
| 17 | + /** |
| 18 | + * Is this n-ary datavalue valid? |
| 19 | + */ |
| 20 | + private $isValid; |
21 | 21 | |
22 | | - /** |
23 | | - * types as we received them when datafactory called us |
24 | | - */ |
25 | | - private $m_type = Array(); |
| 22 | + /** |
| 23 | + * types as we received them when datafactory called us |
| 24 | + */ |
| 25 | + private $m_type = Array(); |
26 | 26 | |
27 | | - /** |
28 | | - * Set type array. Must be done before setting any values. |
29 | | - */ |
30 | | - function setType($type) { |
31 | | - $this->m_type = $type; |
32 | | - } |
| 27 | + /** |
| 28 | + * Set type array. Must be done before setting any values. |
| 29 | + */ |
| 30 | + function setType($type) { |
| 31 | + $this->m_type = $type; |
| 32 | + } |
33 | 33 | |
34 | | - private function parseValues($commaValues) { |
35 | | - return preg_split('/[\s]*;[\s]*/', $commaValues, sizeof($this->m_type->getTypeLabels())); |
36 | | - } |
| 34 | + private function parseValues($commaValues) { |
| 35 | + return preg_split('/[\s]*;[\s]*/', $commaValues, sizeof($this->m_type->getTypeLabels())); |
| 36 | + } |
37 | 37 | |
38 | | - protected function parseUserValue($value) { |
39 | | - $types = $this->m_type->getTypeValues(); |
40 | | - if ($value!='') { |
41 | | - $values = $this->parseValues($value); |
42 | | - // check if all values were specified |
43 | | - if (sizeof($values) < sizeof($types)) { |
44 | | - $valueindex = 0; |
45 | | - // less values specified -> test for closest matchings |
46 | | - for ($i = 0; $i < sizeof($types); $i++) { |
47 | | - // check if enough slots available -> if not set isValid to false... |
48 | | - if ((sizeof($values)-$valueindex) > ((sizeof($types)-$i))) { |
49 | | - $this->isValid = false; |
50 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], false); |
51 | | - } else { |
52 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$valueindex]); |
53 | | - if ($this->m_values[$i]->isValid()) { |
54 | | - $valueindex++; |
55 | | - } else { |
56 | | - /// TODO: Remove previously set (user)value from container!!! |
57 | | - } |
58 | | - } |
59 | | - } |
60 | | - } else { |
61 | | - // everything specified and passed correctly |
62 | | - for ($i = 0; $i < sizeof($types); $i++) { |
63 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$i]); |
64 | | - } |
65 | | - $this->isValid = true; |
66 | | - } |
67 | | - } else { |
68 | | - // no values specified -> create empty DVs |
69 | | - for ($i = 0; $i < sizeof($types); $i++) { |
70 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], false); |
71 | | - } |
72 | | - } |
73 | | - } |
| 38 | + protected function parseUserValue($value) { |
| 39 | + $types = $this->m_type->getTypeValues(); |
| 40 | + if ($value!='') { |
| 41 | + $values = $this->parseValues($value); |
| 42 | + // check if all values were specified |
| 43 | + if (sizeof($values) < sizeof($types)) { |
| 44 | + $valueindex = 0; |
| 45 | + // less values specified -> test for closest matchings |
| 46 | + for ($i = 0; $i < sizeof($types); $i++) { |
| 47 | + // check if enough slots available... |
| 48 | + if ((sizeof($values)-$valueindex) > ((sizeof($types)-$i))) { |
| 49 | + $this->isValid = false; |
| 50 | + $this->addError("N-ary DV is invalid"); |
| 51 | + $this->m_values[$i] = null; |
| 52 | + } else { |
| 53 | + // are values left to set? |
| 54 | + if (sizeof($values) > $valueindex) { |
| 55 | + $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$valueindex]); |
| 56 | + if ($this->m_values[$i]->isValid()) { |
| 57 | + $valueindex++; |
| 58 | + } else { |
| 59 | + /// TODO: Remove previously set (user)value from container!!! |
| 60 | + $this->m_values[$i] = null; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } else { |
| 66 | + // everything specified and passed correctly |
| 67 | + for ($i = 0; $i < sizeof($types); $i++) { |
| 68 | + $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], $values[$i]); |
| 69 | + } |
| 70 | + $this->isValid = true; |
| 71 | + } |
| 72 | + } else { |
| 73 | + // no values specified -> create empty DVs |
| 74 | + for ($i = 0; $i < sizeof($types); $i++) { |
| 75 | + $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i], false); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
74 | 79 | |
75 | | - protected function parseXSDValue($value, $unit) { |
76 | | - // get DVtypes |
77 | | - $types = $this->m_type->getTypeValues(); |
78 | | - // get values supplied by user |
79 | | - $values = $this->parseValues($value); |
80 | | - $units = $this->parseValues($unit); |
| 80 | + protected function parseXSDValue($value, $unit) { |
| 81 | + // get DVtypes |
| 82 | + $types = $this->m_type->getTypeValues(); |
| 83 | + // get values supplied by user |
| 84 | + $values = $this->parseValues($value); |
| 85 | + $units = $this->parseValues($unit); |
81 | 86 | |
82 | | - // create user specified DVs |
83 | | - if (sizeof($values) < sizeof($types)) { |
84 | | - $valueindex = 0; |
85 | | - // less values specified -> test for closest matchings |
86 | | - for ($i = 0; $i < sizeof($types); $i++) { |
87 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i]); |
88 | | - // check if enough slots available -> if not set isValid to false... |
89 | | - if ((sizeof($values)-$valueindex) > ((sizeof($types)-$i))) { |
90 | | - $this->isValid = false; |
91 | | - } else { |
92 | | - // is value valid? |
93 | | - if ($this->m_values[$i]) { |
94 | | - $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$valueindex] : null)); |
95 | | - if ($this->m_values[$i]->isValid()) { |
96 | | - $valueindex++; |
97 | | - } else { |
98 | | - /// TODO! |
99 | | - } |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | - } else { |
104 | | - // everything specified and passed correctly - set XSDValue of DV containers. |
105 | | - for ($i = 0; $i < sizeof($types); $i++) { |
106 | | - $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i]); |
107 | | - $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$i] : null)); |
108 | | - } |
109 | | - } |
110 | | - } |
| 87 | + // create user specified DVs |
| 88 | + if (sizeof($values) < sizeof($types)) { |
| 89 | + $valueindex = 0; |
| 90 | + // less values specified -> test for closest matchings |
| 91 | + for ($i = 0; $i < sizeof($types); $i++) { |
| 92 | + $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i]); |
| 93 | + // check if enough slots available -> if not set isValid to false... |
| 94 | + if ((sizeof($values)-$valueindex) > ((sizeof($types)-$i))) { |
| 95 | + $this->isValid = false; |
| 96 | + $this->addError("N-ary DV is invalid"); |
| 97 | + } else { |
| 98 | + // is value valid? |
| 99 | + if ($this->m_values[$i]) { |
| 100 | + $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$valueindex] : null)); |
| 101 | + if ($this->m_values[$i]->isValid()) { |
| 102 | + $valueindex++; |
| 103 | + } else { |
| 104 | + $this->m_values[$i] = null; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + // everything specified and passed correctly - set XSDValue of DV containers. |
| 111 | + for ($i = 0; $i < sizeof($types); $i++) { |
| 112 | + $this->m_values[$i] = SMWDataValueFactory::newTypeObjectValue($types[$i]); |
| 113 | + $this->m_values[$i]->setXSDValue($values[$i], (is_array($unit)? $units[$i] : null)); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
111 | 117 | |
112 | | - public function setOutputFormat($formatstring) { |
113 | | - /// TODO |
114 | | - } |
| 118 | + public function setOutputFormat($formatstring) { |
| 119 | + /// TODO |
| 120 | + } |
115 | 121 | |
116 | | - public function getShortWikiText($linked = NULL) { |
117 | | - if ($this->m_caption !== false) { |
118 | | - return $this->m_caption; |
119 | | - } |
120 | | - /// TODO: beautify with (...) like LongText |
121 | | - $result = ''; |
122 | | - $first = true; |
123 | | - foreach ($this->m_values as $value) { |
124 | | - if ($value->getShortWikiText($linked)) { |
125 | | - if ($first) { |
126 | | - $first = false; |
127 | | - } else { |
128 | | - $result .= ", "; |
129 | | - } |
130 | | - $result .= $value->getShortWikiText($linked); |
131 | | - } |
132 | | - } |
133 | | - return $result; |
134 | | - } |
| 122 | + public function getShortWikiText($linked = NULL) { |
| 123 | + if ($this->m_caption !== false) { |
| 124 | + return $this->m_caption; |
| 125 | + } |
| 126 | + /// TODO: beautify with (...) like LongText |
| 127 | + $result = ''; |
| 128 | + $first = true; |
| 129 | + $second = true; |
| 130 | + foreach ($this->m_values as $value) { |
| 131 | + if ($first) { |
| 132 | + $first = false; |
| 133 | + } else if ($second) { |
| 134 | + $result .= ' ('; |
| 135 | + $second = false; |
| 136 | + } else { |
| 137 | + $result .= ", "; |
| 138 | + } |
| 139 | + if ($value) { |
| 140 | + if ($value->getShortWikiText($linked)) { |
| 141 | + $result .= $value->getShortWikiText($linked); |
| 142 | + } |
| 143 | + } else { |
| 144 | + $result .= "?"; |
| 145 | + } |
| 146 | + } |
| 147 | + return $second ? $result : $result .= ')'; |
| 148 | + } |
135 | 149 | |
136 | | - public function getShortHTMLText($linker = NULL) { |
137 | | - if ($this->m_caption !== false) { |
138 | | - return $this->m_caption; |
139 | | - } |
140 | | - /// TODO: beautify with (...) like LongText |
141 | | - $result = ''; |
142 | | - $first = true; |
143 | | - foreach ($this->m_values as $value) { |
144 | | - if ($value->getShortHTMLText($linker)) { |
145 | | - if ($first) { |
146 | | - $first = false; |
147 | | - } else { |
148 | | - $result .= ", "; |
149 | | - } |
150 | | - $result .= $value->getShortHTMLText($linker); |
151 | | - } |
152 | | - } |
153 | | - return $result; |
154 | | - } |
| 150 | + public function getShortHTMLText($linker = NULL) { |
| 151 | + if ($this->m_caption !== false) { |
| 152 | + return $this->m_caption; |
| 153 | + } |
| 154 | + /// TODO: beautify with (...) like LongText |
| 155 | + $result = ''; |
| 156 | + $first = true; |
| 157 | + foreach ($this->m_values as $value) { |
| 158 | + if ($first) { |
| 159 | + $first = false; |
| 160 | + } else { |
| 161 | + $result .= ", "; |
| 162 | + } |
| 163 | + if ($value) { |
| 164 | + if ($value->getShortHTMLText($linker)) { |
| 165 | + $result .= $value->getShortHTMLText($linker); |
| 166 | + } |
| 167 | + } else { |
| 168 | + $result .= "?"; |
| 169 | + } |
| 170 | + } |
| 171 | + return $result; |
| 172 | + } |
155 | 173 | |
156 | | - public function getLongWikiText($linked = NULL) { |
157 | | - $result = ''; |
158 | | - $first = true; |
159 | | - $second = true; |
160 | | - foreach ($this->m_values as $value) { |
161 | | - if ($first) { |
162 | | - $first = false; |
163 | | - } else if ($second) { |
164 | | - $result .= ' ('; |
165 | | - $second = false; |
166 | | - } |
167 | | - else { |
168 | | - $result .= ", "; |
169 | | - } |
170 | | - $result .= $value->getLongWikiText($linked); |
171 | | - } |
172 | | - return $second ? $result : $result .= ')'; |
173 | | - } |
| 174 | + public function getLongWikiText($linked = NULL) { |
| 175 | + $result = ''; |
| 176 | + $first = true; |
| 177 | + $second = true; |
| 178 | + foreach ($this->m_values as $value) { |
| 179 | + if ($first) { |
| 180 | + $first = false; |
| 181 | + } else if ($second) { |
| 182 | + $result .= ' ('; |
| 183 | + $second = false; |
| 184 | + } |
| 185 | + else { |
| 186 | + $result .= ", "; |
| 187 | + } |
| 188 | + if ($value) { |
| 189 | + $result .= $value->getLongWikiText($linked); |
| 190 | + } else { |
| 191 | + $result .= null; |
| 192 | + } |
| 193 | + } |
| 194 | + return $second ? $result : $result .= ')'; |
| 195 | + } |
174 | 196 | |
175 | | - public function getLongHTMLText($linker = NULL) { |
176 | | - /// TODO: beautify with (...) like WikiText |
177 | | - $result = ''; |
178 | | - $first = true; |
179 | | - foreach ($this->m_values as $value) { |
180 | | - if ($first) { |
181 | | - $first = false; |
182 | | - } else { |
183 | | - $result .= ", "; |
184 | | - } |
185 | | - $result .= $value->getLongHTMLText($linker); |
186 | | - } |
187 | | - return $result; |
188 | | - } |
| 197 | + public function getLongHTMLText($linker = NULL) { |
| 198 | + /// TODO: beautify with (...) like WikiText |
| 199 | + $result = ''; |
| 200 | + $first = true; |
| 201 | + foreach ($this->m_values as $value) { |
| 202 | + if ($first) { |
| 203 | + $first = false; |
| 204 | + } else { |
| 205 | + $result .= ", "; |
| 206 | + } |
| 207 | + if ($value) { |
| 208 | + $result .= $value->getLongHTMLText($linker); |
| 209 | + } else { |
| 210 | + $result .= "?"; |
| 211 | + } |
| 212 | + } |
| 213 | + return $result; |
| 214 | + } |
189 | 215 | |
190 | | - public function getXSDValue() { |
191 | | - $xsdvals = Array(); |
192 | | - for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) { |
193 | | - $xsdvals[$i] = $this->m_values[$i]->getXSDValue(); |
194 | | - } |
195 | | - return implode(';', $xsdvals); |
196 | | - } |
| 216 | + public function getXSDValue() { |
| 217 | + $xsdvals = Array(); |
| 218 | + for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) { |
| 219 | + if ($this->m_values[$i]) { |
| 220 | + $xsdvals[$i] = $this->m_values[$i]->getXSDValue(); |
| 221 | + } |
| 222 | + } |
| 223 | + return implode(';', $xsdvals); |
| 224 | + } |
197 | 225 | |
198 | | - public function getWikiValue() { |
199 | | - $result = ''; |
200 | | - $first = true; |
201 | | - foreach ($this->m_values as $value) { |
202 | | - if ($first) { |
203 | | - $first = false; |
204 | | - } else { |
205 | | - $result .= ";"; |
206 | | - } |
207 | | - $result .= $value->getWikiValue(); |
208 | | - } |
209 | | - return $result; |
210 | | - } |
| 226 | + public function getWikiValue() { |
| 227 | + $result = ''; |
| 228 | + $first = true; |
| 229 | + foreach ($this->m_values as $value) { |
| 230 | + if ($first) { |
| 231 | + $first = false; |
| 232 | + } else { |
| 233 | + $result .= ";"; |
| 234 | + } |
211 | 235 | |
212 | | - public function getNumericValue() { |
213 | | - return false; |
214 | | - } |
| 236 | + if ($value) { |
| 237 | + $result .= $value->getWikiValue(); |
| 238 | + } |
| 239 | + } |
| 240 | + return $result; |
| 241 | + } |
215 | 242 | |
216 | | - public function getUnit() { |
217 | | - $units = Array(); |
218 | | - for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) { |
219 | | - $units[$i] = $this->m_values[$i]->getUnit(); |
220 | | - } |
221 | | - return implode(';', $units); |
222 | | - } |
| 243 | + public function getNumericValue() { |
| 244 | + return false; |
| 245 | + } |
223 | 246 | |
224 | | - public function getHash() { |
225 | | - $hash = ''; |
226 | | - foreach ($this->m_values as $value) { |
227 | | - $hash .= $value->getHash(); |
228 | | - /// FIXME: this is wrong (different value combinations yield same hash) |
229 | | - } |
230 | | - return $hash; |
231 | | - } |
| 247 | + public function getUnit() { |
| 248 | + $units = Array(); |
| 249 | + for ($i = 0; $i < sizeof($this->m_type->getTypeLabels()); $i++) { |
| 250 | + if ($this->m_values[$i]) { |
| 251 | + $units[$i] = $this->m_values[$i]->getUnit(); |
| 252 | + } |
| 253 | + } |
| 254 | + return implode(';', $units); |
| 255 | + } |
232 | 256 | |
233 | | - public function isNumeric() { |
234 | | - return false; // the n-ary is clearly non numeric (n-ary values cannot be ordered by numbers) |
235 | | - } |
| 257 | + public function getHash() { |
| 258 | + $hash = ''; |
| 259 | + foreach ($this->m_values as $value) { |
| 260 | + // is DV set? |
| 261 | + if ($value) { |
| 262 | + $hash .= $value->getHash(); |
| 263 | + } |
| 264 | + /// FIXME: this is wrong (different value combinations yield same hash) |
| 265 | + } |
| 266 | + return $hash; |
| 267 | + } |
236 | 268 | |
237 | | - // |
238 | | - // custom functions for n-ary attributes |
239 | | - // |
| 269 | + public function isNumeric() { |
| 270 | + return false; // the n-ary is clearly non numeric (n-ary values cannot be ordered by numbers) |
| 271 | + } |
240 | 272 | |
241 | | - public function getDVTypeIDs() { |
242 | | - return implode(';', $this->m_type->getTypeLabels()); |
243 | | - } |
| 273 | + // |
| 274 | + // custom functions for n-ary attributes |
| 275 | + // |
244 | 276 | |
245 | | - public function getType() { |
246 | | - return $this->m_type; |
247 | | - } |
| 277 | + public function getDVTypeIDs() { |
| 278 | + return implode(';', $this->m_type->getTypeLabels()); |
| 279 | + } |
248 | 280 | |
249 | | - public function getDVs() { |
250 | | - return $this->isValid() ? $this->m_values : null; |
251 | | - } |
| 281 | + public function getType() { |
| 282 | + return $this->m_type; |
| 283 | + } |
252 | 284 | |
| 285 | + public function getDVs() { |
| 286 | + return $this->isValid() ? $this->m_values : null; |
| 287 | + } |
| 288 | + |
| 289 | + public function setDVs($datavalues) { |
| 290 | + // less values specified... |
| 291 | + $typelabels = $this->m_type->getTypeLabels(); |
| 292 | + if (sizeof($datavalues) < sizeof($typelabels)) { |
| 293 | + for ($i = 0; $i < sizeof($datavalues); $i++) { |
| 294 | + if ($datavalues[$i]) { |
| 295 | + if ($datavalues[$i]->getTypeID() == SMWTypesValue::findTypeID($typelabels[$i])) { |
| 296 | + $this->m_values[$i] = $datavalues[$i]; |
| 297 | + } else { |
| 298 | + $this->m_values[$i] = null; |
| 299 | + } |
| 300 | + } |
| 301 | + } |
| 302 | + } else if (sizeof($datavalues) == sizeof($typelabels)) { |
| 303 | + // everything specified as desired |
| 304 | + for ($i = 0; $i < sizeof($typelabels); $i++) { |
| 305 | + if ($datavalues[$i]) { |
| 306 | + // check if dv matches expected one |
| 307 | + if ($datavalues[$i]->getTypeID() == SMWTypesValue::findTypeID($typelabels[$i])) { |
| 308 | + $this->m_values[$i] = $datavalues[$i]; |
| 309 | + } else { |
| 310 | + $this->m_values[$i] = null; |
| 311 | + } |
| 312 | + } |
| 313 | + } |
| 314 | + } |
| 315 | + } |
| 316 | + |
253 | 317 | } |
254 | 318 | |