Index: trunk/extensions/Wikidata/WiktionaryZ/tuple.php |
— | — | @@ -0,0 +1,50 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +require_once('attribute.php'); |
| 5 | + |
| 6 | +interface Tuple { |
| 7 | + public function getHeading(); |
| 8 | + public function getAttributeValue($attribute); |
| 9 | + public function project($heading); |
| 10 | +} |
| 11 | + |
| 12 | +class ArrayTuple implements Tuple { |
| 13 | + protected $heading; |
| 14 | + protected $values = array(); |
| 15 | + |
| 16 | + public function __construct($heading) { |
| 17 | + $this->heading = $heading; |
| 18 | + } |
| 19 | + |
| 20 | + public function getHeading() { |
| 21 | + return $this->heading; |
| 22 | + } |
| 23 | + |
| 24 | + public function getAttributeValue($attribute) { |
| 25 | + return $this->values[$attribute->id]; |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public function project($heading) { |
| 30 | + $result = new ArrayTuple($heading); |
| 31 | + |
| 32 | + foreach($heading->attributes as $attribute) |
| 33 | + $result->setAttributeValue($attribute, $this->getAttributeValue($attribute)); |
| 34 | + } |
| 35 | + |
| 36 | + public function setAttributeValue($attribute, $value) { |
| 37 | + $this->values[$attribute->id] = $value; |
| 38 | + } |
| 39 | + |
| 40 | + public function setAttributeValuesByOrder($values) { |
| 41 | + for ($i = 0; $i < count($this->heading->attributes); $i++) |
| 42 | + $this->values[$this->heading->attributes[$i]->id] = $values[$i]; |
| 43 | + } |
| 44 | + |
| 45 | + public function setSubTuple($tuple) { |
| 46 | + foreach($tuple->getHeading()->attributes as $attribute) |
| 47 | + $this->values[$attribute->id] = $tuple->getAttributeValue($attribute); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +?> |
Index: trunk/extensions/Wikidata/WiktionaryZ/WiktionaryZ.php |
— | — | @@ -3,20 +3,12 @@ |
4 | 4 | require_once('wikidata.php'); |
5 | 5 | require_once('Expression.php'); |
6 | 6 | require_once('forms.php'); |
| 7 | +require_once('attribute.php'); |
| 8 | +require_once('tuple.php'); |
7 | 9 | require_once('relation.php'); |
8 | 10 | require_once('type.php'); |
9 | 11 | require_once('languages.php'); |
10 | 12 | |
11 | | -global |
12 | | - $languageAttribute, $textAttribute, $identicalMeaningAttribute, $internalIdAttribute, $collectionAttribute; |
13 | | - |
14 | | -$languageAttribute = new Attribute("language", "Language", "language"); |
15 | | -$textAttribute = new Attribute("text", "Text", "text"); |
16 | | - |
17 | | -$identicalMeaningAttribute = new Attribute("endemic-meaning", "Identical meaning?", "boolean"); |
18 | | -$collectionAttribute = new Attribute("collection", "Collection", "collection"); |
19 | | -$internalIdAttribute = new Attribute("internal-id", "Internal ID", "short-text"); |
20 | | - |
21 | 13 | function getLatestRevisionForDefinedMeaning($definedMeaningId) { |
22 | 14 | $dbr =& wfGetDB(DB_SLAVE); |
23 | 15 | $sql = "SELECT revision_id from uw_defined_meaning where defined_meaning_id=$definedMeaningId and is_latest_ver=1 limit 1"; |
— | — | @@ -227,12 +219,18 @@ |
228 | 220 | } |
229 | 221 | |
230 | 222 | public function remove($tuple) { |
231 | | - $languageId = $tuple['language']; |
| 223 | + global |
| 224 | + $languageAttribute; |
| 225 | + |
| 226 | + $languageId = $tuple->getAttributeValue($languageAttribute); |
232 | 227 | removeDefinedMeaningDefinition($this->definedMeaningId, $languageId); |
233 | 228 | } |
234 | 229 | |
235 | 230 | public function update($tuple, $updatedValues) { |
236 | | - updateDefinedMeaningDefinition($this->definedMeaningId, $tuple['language'], $updatedValues['text']); |
| 231 | + global |
| 232 | + $languageAttribute; |
| 233 | + |
| 234 | + updateDefinedMeaningDefinition($this->definedMeaningId, $tuple->getAttributeValue($languageAttribute), $updatedValues['text']); |
237 | 235 | } |
238 | 236 | } |
239 | 237 | |
— | — | @@ -254,12 +252,18 @@ |
255 | 253 | } |
256 | 254 | |
257 | 255 | public function remove($tuple) { |
258 | | - $languageId = $tuple['language']; |
| 256 | + global |
| 257 | + $languageAttribute; |
| 258 | + |
| 259 | + $languageId = $tuple->getAttributeValue($languageAttribute); |
259 | 260 | removeTranslatedDefinition($this->alternativeDefinitionId, $languageId); |
260 | 261 | } |
261 | 262 | |
262 | 263 | public function update($tuple, $updatedValues) { |
263 | | - updateDefinedMeaningAlternativeDefinition($this->alternativeDefinitionId, $tuple['language'], $updatedValues['text']); |
| 264 | + global |
| 265 | + $languageAttribute; |
| 266 | + |
| 267 | + updateDefinedMeaningAlternativeDefinition($this->alternativeDefinitionId, $tuple->getAttributeValue($languageAttribute), $updatedValues['text']); |
264 | 268 | } |
265 | 269 | } |
266 | 270 | |
— | — | @@ -282,13 +286,18 @@ |
283 | 287 | } |
284 | 288 | |
285 | 289 | public function remove($tuple) { |
286 | | - $expressionId = $tuple['expression']; |
287 | | - $endemicMeaning = $tuple['endemic-meaning']; |
| 290 | + global |
| 291 | + $expressionAttribute; |
| 292 | + |
| 293 | + $expressionId = $tuple->getAttributeValue($expressionAttribute); |
288 | 294 | removeSynonymOrTranslation($this->definedMeaningId, $expressionId); |
289 | 295 | } |
290 | 296 | |
291 | 297 | public function update($tuple, $updatedValues) { |
292 | | - $expressionId = $tuple['expression']; |
| 298 | + global |
| 299 | + $expressionAttribute; |
| 300 | + |
| 301 | + $expressionId = $tuple->getAttributeValue($expressionAttribute); |
293 | 302 | $identicalMeaning = $updatedValues['endemic-meaning']; |
294 | 303 | updateSynonymOrTranslation($this->definedMeaningId, $expressionId, $identicalMeaning); |
295 | 304 | } |
— | — | @@ -310,7 +319,10 @@ |
311 | 320 | } |
312 | 321 | |
313 | 322 | public function remove($tuple) { |
314 | | - removeRelation($this->definedMeaningId, $tuple['relation-type'], $tuple['other-defined-meaning']); |
| 323 | + global |
| 324 | + $relationTypeAttribute, $otherDefinedMeaningAttribute; |
| 325 | + |
| 326 | + removeRelation($this->definedMeaningId, $tuple->getAttributeValue($relationTypeAttribute), $tuple->getAttributeValue($otherDefinedMeaningAttribute)); |
315 | 327 | } |
316 | 328 | |
317 | 329 | public function update($tuple, $updatedValues) { |
— | — | @@ -332,7 +344,10 @@ |
333 | 345 | } |
334 | 346 | |
335 | 347 | public function remove($tuple) { |
336 | | - removeRelation($this->definedMeaningId, 0, $tuple['attribute']); |
| 348 | + global |
| 349 | + $attributeAttribute; |
| 350 | + |
| 351 | + removeRelation($this->definedMeaningId, 0, $tuple->getAttributeValue($attributeAttribute)); |
337 | 352 | } |
338 | 353 | |
339 | 354 | public function update($tuple, $updatedValues) { |
— | — | @@ -357,11 +372,17 @@ |
358 | 373 | } |
359 | 374 | |
360 | 375 | public function remove($tuple) { |
361 | | - removeDefinedMeaningFromCollection($this->definedMeaningId, $tuple['collection']); |
| 376 | + global |
| 377 | + $collectionAttribute; |
| 378 | + |
| 379 | + removeDefinedMeaningFromCollection($this->definedMeaningId, $tuple->getAttributeValue($collectionAttribute)); |
362 | 380 | } |
363 | 381 | |
364 | 382 | public function update($tuple, $updatedValues) { |
365 | | - $collectionId = $tuple["collection"]; |
| 383 | + global |
| 384 | + $collectionAttribute; |
| 385 | + |
| 386 | + $collectionId = $tuple->getAttributeValue($collectionAttribute); |
366 | 387 | $internalId = $updatedValues["internal-id"]; |
367 | 388 | |
368 | 389 | if ($internalId != "") |
— | — | @@ -507,7 +528,7 @@ |
508 | 529 | $values = getFieldValuesForAttribute($updateId . $tupleKeyName . '-', $attribute, ""); |
509 | 530 | $value = $values[0]; |
510 | 531 | |
511 | | - if ($value != $tuple[$attribute->id]) |
| 532 | + if ($value != $tuple->getAttributeValue($attribute)) |
512 | 533 | $updatedValues[$attribute->id] = $value; |
513 | 534 | } |
514 | 535 | |
— | — | @@ -812,11 +833,10 @@ |
813 | 834 | |
814 | 835 | function getSynonymAndTranslationRelation($definedMeaningId, $skippedExpressionId) { |
815 | 836 | global |
816 | | - $identicalMeaningAttribute; |
| 837 | + $expressionAttribute, $identicalMeaningAttribute; |
817 | 838 | |
818 | 839 | $dbr =& wfGetDB(DB_SLAVE); |
819 | | - $heading = new Heading(array(new Attribute("expression", "Expression", "expression"), |
820 | | - $identicalMeaningAttribute)); |
| 840 | + $heading = new Heading(array($expressionAttribute, $identicalMeaningAttribute)); |
821 | 841 | |
822 | 842 | $relation = new ArrayRelation($heading, $heading); |
823 | 843 | $queryResult = $dbr->query("SELECT expression_id, endemic_meaning FROM uw_syntrans WHERE defined_meaning_id=$definedMeaningId AND expression_id!=$skippedExpressionId"); |
— | — | @@ -828,7 +848,10 @@ |
829 | 849 | } |
830 | 850 | |
831 | 851 | function getDefinedMeaningRelationsRelation($definedMeaningId) { |
832 | | - $heading = new Heading(array(new Attribute("relation-type", "Relation type", "relation-type"), new Attribute("other-defined-meaning", "Other defined meaning", "defining-expression"))); |
| 852 | + global |
| 853 | + $relationTypeAttribute, $otherDefinedMeaningAttribute; |
| 854 | + |
| 855 | + $heading = new Heading(array($relationTypeAttribute, $otherDefinedMeaningAttribute)); |
833 | 856 | $relation = new ArrayRelation($heading, $heading); |
834 | 857 | |
835 | 858 | $dbr =& wfGetDB(DB_SLAVE); |
— | — | @@ -857,7 +880,10 @@ |
858 | 881 | } |
859 | 882 | |
860 | 883 | function getDefinedMeaningAttributesRelation($definedMeaningId) { |
861 | | - $heading = new Heading(array(new Attribute("attribute", "Attribute", "attribute"))); |
| 884 | + global |
| 885 | + $attributeAttribute; |
| 886 | + |
| 887 | + $heading = new Heading(array($attributeAttribute)); |
862 | 888 | $relation = new ArrayRelation($heading, $heading); |
863 | 889 | |
864 | 890 | $dbr =& wfGetDB(DB_SLAVE); |
Index: trunk/extensions/Wikidata/WiktionaryZ/attribute.php |
— | — | @@ -20,4 +20,21 @@ |
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
| 24 | +global |
| 25 | + $languageAttribute, $spellingAttribute, $textAttribute, $identicalMeaningAttribute, $internalIdAttribute, |
| 26 | + $collectionAttribute, $relationTypeAttribute, $otherDefinedMeaningAttribute, $expressionAttribute, $attributeAttribute; |
| 27 | + |
| 28 | +$expressionAttribute = new Attribute("expression", "Expression", "expression"); |
| 29 | +$languageAttribute = new Attribute("language", "Language", "language"); |
| 30 | +$spellingAttribute = new Attribute("spelling", "Spelling", "spelling"); |
| 31 | +$textAttribute = new Attribute("text", "Text", "text"); |
| 32 | + |
| 33 | +$identicalMeaningAttribute = new Attribute("endemic-meaning", "Identical meaning?", "boolean"); |
| 34 | +$collectionAttribute = new Attribute("collection", "Collection", "collection"); |
| 35 | +$internalIdAttribute = new Attribute("internal-id", "Internal ID", "short-text"); |
| 36 | + |
| 37 | +$attributeAttribute = new Attribute("attribute", "Attribute", "attribute"); |
| 38 | +$relationTypeAttribute = new Attribute("relation-type", "Relation type", "relation-type"); |
| 39 | +$otherDefinedMeaningAttribute = new Attribute("other-defined-meaning", "Other defined meaning", "defining-expression"); |
| 40 | + |
24 | 41 | ?> |
Index: trunk/extensions/Wikidata/WiktionaryZ/converter.php |
— | — | @@ -5,44 +5,57 @@ |
6 | 6 | |
7 | 7 | interface Converter { |
8 | 8 | public function convert($value); |
9 | | - public function getAttributes(); |
| 9 | + public function getHeading(); |
10 | 10 | } |
11 | 11 | |
12 | 12 | class IdentityConverter { |
13 | 13 | protected $attribute; |
| 14 | + protected $heading; |
14 | 15 | |
15 | 16 | public function __construct($attribute) { |
16 | 17 | $this->attribute = $attribute; |
| 18 | + $this->heading = new Heading(array($attribute)); |
17 | 19 | } |
18 | 20 | |
19 | 21 | public function convert($tuple) { |
20 | | - return array($tuple[$this->attribute->id]); |
| 22 | + $result = new ArrayTuple($this->heading); |
| 23 | + $result->setAttributeValue($this->attribute, $tuple->getAttributeValue($this->attribute)); |
| 24 | + |
| 25 | + return $result; |
21 | 26 | } |
22 | 27 | |
23 | | - public function getAttributes() { |
24 | | - return array($this->attribute); |
| 28 | + public function getHeading() { |
| 29 | + return $this->heading; |
25 | 30 | } |
26 | 31 | } |
27 | 32 | |
28 | 33 | class DefaultConverter implements Converter { |
29 | 34 | protected $attribute; |
| 35 | + protected $heading; |
30 | 36 | |
31 | 37 | public function __construct($attribute) { |
32 | 38 | $this->attribute = $attribute; |
| 39 | + $this->heading = new Heading(array($attribute)); |
33 | 40 | } |
34 | 41 | |
35 | 42 | public function convert($tuple) { |
36 | | - return array(convertToHTML($tuple[$this->attribute->id], $this->attribute->type)); |
| 43 | + $result = new ArrayTuple($this->heading); |
| 44 | + $result->setAttributeValue($this->attribute, convertToHTML($tuple->getAttributeValue($this->attribute), $this->attribute->type)); |
| 45 | + |
| 46 | + return $result; |
37 | 47 | } |
38 | 48 | |
39 | | - public function getAttributes() { |
40 | | - return array($this->attribute); |
| 49 | + public function getHeading() { |
| 50 | + return $this->heading; |
41 | 51 | } |
42 | 52 | } |
43 | 53 | |
44 | 54 | class DefiningExpressionConverter extends DefaultConverter { |
45 | 55 | public function convert($tuple) { |
46 | | - return array(definingExpressionAsLink($tuple[$this->attribute->id])); |
| 56 | + $result = new ArrayTuple($this->heading); |
| 57 | + $result->setAttributeValue($this->attribute, definingExpressionAsLink($tuple->getAttributeValue($this->attribute))); |
| 58 | + |
| 59 | + return $result; |
47 | 60 | } |
48 | 61 | } |
49 | 62 | |
— | — | @@ -50,22 +63,31 @@ |
51 | 64 | protected $attributes = array(); |
52 | 65 | |
53 | 66 | public function __construct($attribute) { |
| 67 | + global |
| 68 | + $languageAttribute, $spellingAttribute; |
| 69 | + |
54 | 70 | parent::__construct($attribute); |
55 | | - $this->attributes[] = new Attribute("language", "Language", "language"); |
56 | | - $this->attributes[] = new Attribute("spelling", "Spelling", "spelling"); |
| 71 | + $this->heading = new Heading(array($languageAttribute, $spellingAttribute)); |
57 | 72 | } |
58 | 73 | |
59 | | - public function getAttributes() { |
60 | | - return $this->attributes; |
| 74 | + public function getHeading() { |
| 75 | + return $this->heading; |
61 | 76 | } |
62 | 77 | |
63 | 78 | public function convert($tuple) { |
| 79 | + global |
| 80 | + $languageAttribute, $spellingAttribute; |
| 81 | + |
64 | 82 | $dbr =& wfGetDB(DB_SLAVE); |
65 | | - $expressionId = $tuple[$this->attribute->id]; |
| 83 | + $expressionId = $tuple->getAttributeValue($this->attribute); |
66 | 84 | $queryResult = $dbr->query("SELECT language_id, spelling from uw_expression_ns WHERE expression_id=$expressionId"); |
67 | 85 | $expression = $dbr->fetchObject($queryResult); |
| 86 | + |
| 87 | + $result = new ArrayTuple($this->heading); |
| 88 | + $result->setAttributeValue($languageAttribute, languageIdAsText($expression->language_id)); |
| 89 | + $result->setAttributeValue($spellingAttribute, spellingAsLink($expression->spelling)); |
68 | 90 | |
69 | | - return array(languageIdAsText($expression->language_id), spellingAsLink($expression->spelling)); |
| 91 | + return $result; |
70 | 92 | } |
71 | 93 | } |
72 | 94 | |
Index: trunk/extensions/Wikidata/WiktionaryZ/relation.php |
— | — | @@ -3,6 +3,7 @@ |
4 | 4 | require_once('forms.php'); |
5 | 5 | require_once('converter.php'); |
6 | 6 | require_once('attribute.php'); |
| 7 | +require_once('tuple.php'); |
7 | 8 | |
8 | 9 | interface RelationModel { |
9 | 10 | public function getHeading(); |
— | — | @@ -22,11 +23,9 @@ |
23 | 24 | } |
24 | 25 | |
25 | 26 | public function addTuple($values) { |
26 | | - $tuple = array(); |
27 | | - |
28 | | - for ($i = 0; $i < count($this->heading->attributes); $i++) |
29 | | - $tuple[$this->heading->attributes[$i]->id] = $values[$i]; |
30 | | - |
| 27 | + $tuple = new ArrayTuple($this->heading); |
| 28 | + $tuple->setAttributeValuesByOrder($values); |
| 29 | + |
31 | 30 | $this->tuples[] = $tuple; |
32 | 31 | } |
33 | 32 | |
— | — | @@ -50,6 +49,7 @@ |
51 | 50 | class HTMLRelation implements RelationModel { |
52 | 51 | protected $relationModel; |
53 | 52 | protected $converters = array(); |
| 53 | + protected $heading; |
54 | 54 | |
55 | 55 | public function __construct($relationModel, $updatableHeading) { |
56 | 56 | $this->relationModel = $relationModel; |
— | — | @@ -70,10 +70,10 @@ |
71 | 71 | |
72 | 72 | public function getTuple($index) { |
73 | 73 | $tuple = $this->relationModel->getTuple($index); |
74 | | - $result = array(); |
| 74 | + $result = new ArrayTuple($this->heading); |
75 | 75 | |
76 | 76 | foreach ($this->converters as $converter) |
77 | | - $result = array_merge($result, $converter->convert($tuple)); |
| 77 | + $result->setSubTuple($converter->convert($tuple)); |
78 | 78 | |
79 | 79 | return $result; |
80 | 80 | } |
— | — | @@ -94,7 +94,7 @@ |
95 | 95 | } |
96 | 96 | |
97 | 97 | $this->converters[] = $converter; |
98 | | - $attributes = array_merge($attributes, $converter->getAttributes()); |
| 98 | + $attributes = array_merge($attributes, $converter->getHeading()->attributes); |
99 | 99 | } |
100 | 100 | |
101 | 101 | $this->heading = new Heading($attributes); |
— | — | @@ -105,7 +105,7 @@ |
106 | 106 | $attributes = $this->relationModel->getHeading()->attributes; |
107 | 107 | |
108 | 108 | foreach ($attributes as $attribute) |
109 | | - $result[$attribute->id] = convertToHTML($tuple[$attribute->id], $attribute->type); |
| 109 | + $result[$attribute->id] = convertToHTML($tuple->getAttributeValue($attribute), $attribute->type); |
110 | 110 | |
111 | 111 | return $result; |
112 | 112 | } |
— | — | @@ -198,8 +198,15 @@ |
199 | 199 | |
200 | 200 | $result .= '</tr>'; |
201 | 201 | |
202 | | - for($i = 0; $i < $relationModel->getTupleCount(); $i++) |
203 | | - $result .= '<tr>' . getTableCellsAsHTML($attributes, $relationModel->getTuple($i)) .'</tr>'; |
| 202 | + for($i = 0; $i < $relationModel->getTupleCount(); $i++) { |
| 203 | + $tuple = $relationModel->getTuple($i); |
| 204 | + $values = array(); |
| 205 | + |
| 206 | + foreach($attributes as $attribute) |
| 207 | + $values[] = $tuple->getAttributeValue($attribute); |
| 208 | + |
| 209 | + $result .= '<tr>' . getTableCellsAsHTML($attributes, $values) .'</tr>'; |
| 210 | + } |
204 | 211 | |
205 | 212 | $result .= '</table>'; |
206 | 213 | |
— | — | @@ -229,7 +236,7 @@ |
230 | 237 | $ids = array(); |
231 | 238 | |
232 | 239 | foreach($key->attributes as $attribute) |
233 | | - $ids[] = $tuple[$attribute->id]; |
| 240 | + $ids[] = $tuple->getAttributeValue($attribute); |
234 | 241 | |
235 | 242 | return implode("-", $ids); |
236 | 243 | } |
— | — | @@ -259,8 +266,14 @@ |
260 | 267 | if ($allowRemove) |
261 | 268 | $result .= '<td class="remove">' . getRemoveCheckBox($removeId . $tupleKeyName) . '</td>'; |
262 | 269 | |
263 | | - $result .= getTableCellsAsEditHTML($attributes, $htmlRelation->getTuple($i), $updateId . $tupleKeyName . '-', $updatableHeading); |
| 270 | + $htmlTuple = $htmlRelation->getTuple($i); |
| 271 | + $values = array(); |
264 | 272 | |
| 273 | + foreach($attributes as $attribute) |
| 274 | + $values[] = $htmlTuple->getAttributeValue($attribute); |
| 275 | + |
| 276 | + $result .= getTableCellsAsEditHTML($attributes, $values, $updateId . $tupleKeyName . '-', $updatableHeading); |
| 277 | + |
265 | 278 | if ($repeatInput) |
266 | 279 | $result .= '<td/>'; |
267 | 280 | |