r14934 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r14933‎ | r14934 | r14935 >
Date:11:06, 23 June 2006
Author:proes
Status:old
Tags:
Comment:
Refactoring: Introduced interface Tuple
Modified paths:
  • /trunk/extensions/Wikidata/WiktionaryZ/WiktionaryZ.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/attribute.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/converter.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/relation.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/tuple.php (added) (history)

Diff [purge]

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 @@
44 require_once('wikidata.php');
55 require_once('Expression.php');
66 require_once('forms.php');
 7+require_once('attribute.php');
 8+require_once('tuple.php');
79 require_once('relation.php');
810 require_once('type.php');
911 require_once('languages.php');
1012
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 -
2113 function getLatestRevisionForDefinedMeaning($definedMeaningId) {
2214 $dbr =& wfGetDB(DB_SLAVE);
2315 $sql = "SELECT revision_id from uw_defined_meaning where defined_meaning_id=$definedMeaningId and is_latest_ver=1 limit 1";
@@ -227,12 +219,18 @@
228220 }
229221
230222 public function remove($tuple) {
231 - $languageId = $tuple['language'];
 223+ global
 224+ $languageAttribute;
 225+
 226+ $languageId = $tuple->getAttributeValue($languageAttribute);
232227 removeDefinedMeaningDefinition($this->definedMeaningId, $languageId);
233228 }
234229
235230 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']);
237235 }
238236 }
239237
@@ -254,12 +252,18 @@
255253 }
256254
257255 public function remove($tuple) {
258 - $languageId = $tuple['language'];
 256+ global
 257+ $languageAttribute;
 258+
 259+ $languageId = $tuple->getAttributeValue($languageAttribute);
259260 removeTranslatedDefinition($this->alternativeDefinitionId, $languageId);
260261 }
261262
262263 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']);
264268 }
265269 }
266270
@@ -282,13 +286,18 @@
283287 }
284288
285289 public function remove($tuple) {
286 - $expressionId = $tuple['expression'];
287 - $endemicMeaning = $tuple['endemic-meaning'];
 290+ global
 291+ $expressionAttribute;
 292+
 293+ $expressionId = $tuple->getAttributeValue($expressionAttribute);
288294 removeSynonymOrTranslation($this->definedMeaningId, $expressionId);
289295 }
290296
291297 public function update($tuple, $updatedValues) {
292 - $expressionId = $tuple['expression'];
 298+ global
 299+ $expressionAttribute;
 300+
 301+ $expressionId = $tuple->getAttributeValue($expressionAttribute);
293302 $identicalMeaning = $updatedValues['endemic-meaning'];
294303 updateSynonymOrTranslation($this->definedMeaningId, $expressionId, $identicalMeaning);
295304 }
@@ -310,7 +319,10 @@
311320 }
312321
313322 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));
315327 }
316328
317329 public function update($tuple, $updatedValues) {
@@ -332,7 +344,10 @@
333345 }
334346
335347 public function remove($tuple) {
336 - removeRelation($this->definedMeaningId, 0, $tuple['attribute']);
 348+ global
 349+ $attributeAttribute;
 350+
 351+ removeRelation($this->definedMeaningId, 0, $tuple->getAttributeValue($attributeAttribute));
337352 }
338353
339354 public function update($tuple, $updatedValues) {
@@ -357,11 +372,17 @@
358373 }
359374
360375 public function remove($tuple) {
361 - removeDefinedMeaningFromCollection($this->definedMeaningId, $tuple['collection']);
 376+ global
 377+ $collectionAttribute;
 378+
 379+ removeDefinedMeaningFromCollection($this->definedMeaningId, $tuple->getAttributeValue($collectionAttribute));
362380 }
363381
364382 public function update($tuple, $updatedValues) {
365 - $collectionId = $tuple["collection"];
 383+ global
 384+ $collectionAttribute;
 385+
 386+ $collectionId = $tuple->getAttributeValue($collectionAttribute);
366387 $internalId = $updatedValues["internal-id"];
367388
368389 if ($internalId != "")
@@ -507,7 +528,7 @@
508529 $values = getFieldValuesForAttribute($updateId . $tupleKeyName . '-', $attribute, "");
509530 $value = $values[0];
510531
511 - if ($value != $tuple[$attribute->id])
 532+ if ($value != $tuple->getAttributeValue($attribute))
512533 $updatedValues[$attribute->id] = $value;
513534 }
514535
@@ -812,11 +833,10 @@
813834
814835 function getSynonymAndTranslationRelation($definedMeaningId, $skippedExpressionId) {
815836 global
816 - $identicalMeaningAttribute;
 837+ $expressionAttribute, $identicalMeaningAttribute;
817838
818839 $dbr =& wfGetDB(DB_SLAVE);
819 - $heading = new Heading(array(new Attribute("expression", "Expression", "expression"),
820 - $identicalMeaningAttribute));
 840+ $heading = new Heading(array($expressionAttribute, $identicalMeaningAttribute));
821841
822842 $relation = new ArrayRelation($heading, $heading);
823843 $queryResult = $dbr->query("SELECT expression_id, endemic_meaning FROM uw_syntrans WHERE defined_meaning_id=$definedMeaningId AND expression_id!=$skippedExpressionId");
@@ -828,7 +848,10 @@
829849 }
830850
831851 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));
833856 $relation = new ArrayRelation($heading, $heading);
834857
835858 $dbr =& wfGetDB(DB_SLAVE);
@@ -857,7 +880,10 @@
858881 }
859882
860883 function getDefinedMeaningAttributesRelation($definedMeaningId) {
861 - $heading = new Heading(array(new Attribute("attribute", "Attribute", "attribute")));
 884+ global
 885+ $attributeAttribute;
 886+
 887+ $heading = new Heading(array($attributeAttribute));
862888 $relation = new ArrayRelation($heading, $heading);
863889
864890 $dbr =& wfGetDB(DB_SLAVE);
Index: trunk/extensions/Wikidata/WiktionaryZ/attribute.php
@@ -20,4 +20,21 @@
2121 }
2222 }
2323
 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+
2441 ?>
Index: trunk/extensions/Wikidata/WiktionaryZ/converter.php
@@ -5,44 +5,57 @@
66
77 interface Converter {
88 public function convert($value);
9 - public function getAttributes();
 9+ public function getHeading();
1010 }
1111
1212 class IdentityConverter {
1313 protected $attribute;
 14+ protected $heading;
1415
1516 public function __construct($attribute) {
1617 $this->attribute = $attribute;
 18+ $this->heading = new Heading(array($attribute));
1719 }
1820
1921 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;
2126 }
2227
23 - public function getAttributes() {
24 - return array($this->attribute);
 28+ public function getHeading() {
 29+ return $this->heading;
2530 }
2631 }
2732
2833 class DefaultConverter implements Converter {
2934 protected $attribute;
 35+ protected $heading;
3036
3137 public function __construct($attribute) {
3238 $this->attribute = $attribute;
 39+ $this->heading = new Heading(array($attribute));
3340 }
3441
3542 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;
3747 }
3848
39 - public function getAttributes() {
40 - return array($this->attribute);
 49+ public function getHeading() {
 50+ return $this->heading;
4151 }
4252 }
4353
4454 class DefiningExpressionConverter extends DefaultConverter {
4555 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;
4760 }
4861 }
4962
@@ -50,22 +63,31 @@
5164 protected $attributes = array();
5265
5366 public function __construct($attribute) {
 67+ global
 68+ $languageAttribute, $spellingAttribute;
 69+
5470 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));
5772 }
5873
59 - public function getAttributes() {
60 - return $this->attributes;
 74+ public function getHeading() {
 75+ return $this->heading;
6176 }
6277
6378 public function convert($tuple) {
 79+ global
 80+ $languageAttribute, $spellingAttribute;
 81+
6482 $dbr =& wfGetDB(DB_SLAVE);
65 - $expressionId = $tuple[$this->attribute->id];
 83+ $expressionId = $tuple->getAttributeValue($this->attribute);
6684 $queryResult = $dbr->query("SELECT language_id, spelling from uw_expression_ns WHERE expression_id=$expressionId");
6785 $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));
6890
69 - return array(languageIdAsText($expression->language_id), spellingAsLink($expression->spelling));
 91+ return $result;
7092 }
7193 }
7294
Index: trunk/extensions/Wikidata/WiktionaryZ/relation.php
@@ -3,6 +3,7 @@
44 require_once('forms.php');
55 require_once('converter.php');
66 require_once('attribute.php');
 7+require_once('tuple.php');
78
89 interface RelationModel {
910 public function getHeading();
@@ -22,11 +23,9 @@
2324 }
2425
2526 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+
3130 $this->tuples[] = $tuple;
3231 }
3332
@@ -50,6 +49,7 @@
5150 class HTMLRelation implements RelationModel {
5251 protected $relationModel;
5352 protected $converters = array();
 53+ protected $heading;
5454
5555 public function __construct($relationModel, $updatableHeading) {
5656 $this->relationModel = $relationModel;
@@ -70,10 +70,10 @@
7171
7272 public function getTuple($index) {
7373 $tuple = $this->relationModel->getTuple($index);
74 - $result = array();
 74+ $result = new ArrayTuple($this->heading);
7575
7676 foreach ($this->converters as $converter)
77 - $result = array_merge($result, $converter->convert($tuple));
 77+ $result->setSubTuple($converter->convert($tuple));
7878
7979 return $result;
8080 }
@@ -94,7 +94,7 @@
9595 }
9696
9797 $this->converters[] = $converter;
98 - $attributes = array_merge($attributes, $converter->getAttributes());
 98+ $attributes = array_merge($attributes, $converter->getHeading()->attributes);
9999 }
100100
101101 $this->heading = new Heading($attributes);
@@ -105,7 +105,7 @@
106106 $attributes = $this->relationModel->getHeading()->attributes;
107107
108108 foreach ($attributes as $attribute)
109 - $result[$attribute->id] = convertToHTML($tuple[$attribute->id], $attribute->type);
 109+ $result[$attribute->id] = convertToHTML($tuple->getAttributeValue($attribute), $attribute->type);
110110
111111 return $result;
112112 }
@@ -198,8 +198,15 @@
199199
200200 $result .= '</tr>';
201201
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+ }
204211
205212 $result .= '</table>';
206213
@@ -229,7 +236,7 @@
230237 $ids = array();
231238
232239 foreach($key->attributes as $attribute)
233 - $ids[] = $tuple[$attribute->id];
 240+ $ids[] = $tuple->getAttributeValue($attribute);
234241
235242 return implode("-", $ids);
236243 }
@@ -259,8 +266,14 @@
260267 if ($allowRemove)
261268 $result .= '<td class="remove">' . getRemoveCheckBox($removeId . $tupleKeyName) . '</td>';
262269
263 - $result .= getTableCellsAsEditHTML($attributes, $htmlRelation->getTuple($i), $updateId . $tupleKeyName . '-', $updatableHeading);
 270+ $htmlTuple = $htmlRelation->getTuple($i);
 271+ $values = array();
264272
 273+ foreach($attributes as $attribute)
 274+ $values[] = $htmlTuple->getAttributeValue($attribute);
 275+
 276+ $result .= getTableCellsAsEditHTML($attributes, $values, $updateId . $tupleKeyName . '-', $updatableHeading);
 277+
265278 if ($repeatInput)
266279 $result .= '<td/>';
267280