r14668 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r14667‎ | r14668 | r14669 >
Date:19:50, 8 June 2006
Author:proes
Status:old
Tags:
Comment:
Added the possibility to remove relations between defined meanings and to remove attributes from defined meanings.
Modified paths:
  • /trunk/extensions/Wikidata/WiktionaryZ/WiktionaryZ.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/relation.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/tmp/tables.css (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/type.php (modified) (history)
  • /trunk/extensions/Wikidata/WiktionaryZ/wikidata.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/WiktionaryZ/WiktionaryZ.php
@@ -50,6 +50,11 @@
5151 }
5252 }
5353
 54+function removeRelation($definedMeaning1Id, $relationTypeId, $definedMeaning2Id) {
 55+ $dbr =& wfGetDB(DB_MASTER);
 56+ $dbr->query("delete from uw_meaning_relations where meaning1_mid=$definedMeaning1Id and meaning2_mid=$definedMeaning2Id and ".
 57+ "relationtype_mid=$relationTypeId AND is_latest_set=1 LIMIT 1");
 58+}
5459
5560 class SynonymTranslationController implements PageElementController {
5661 protected $definedMeaningId;
@@ -68,6 +73,10 @@
6974 $expression->assureIsBoundToDefinedMeaning($this->definedMeaningId, $endemicMeaning);
7075 }
7176 }
 77+
 78+ public function remove($tuple) {
 79+
 80+ }
7281 }
7382
7483 class DefinedMeaningRelationController implements PageElementController {
@@ -84,6 +93,10 @@
8594 if ($relationTypeId != 0 && $otherDefinedMeaningId != 0)
8695 addRelation($this->definedMeaningId, $relationTypeId, $otherDefinedMeaningId);
8796 }
 97+
 98+ public function remove($tuple) {
 99+ removeRelation($this->definedMeaningId, $tuple['relation-type'], $tuple['other-defined-meaning']);
 100+ }
88101 }
89102
90103 class DefinedMeaningAttributeController implements PageElementController {
@@ -99,6 +112,10 @@
100113 if ($attributeId != 0)
101114 addRelation($this->definedMeaningId, 0, $attributeId);
102115 }
 116+
 117+ public function remove($tuple) {
 118+ removeRelation($this->definedMeaningId, 0, $tuple['attribute']);
 119+ }
103120 }
104121
105122 /**
@@ -168,17 +185,18 @@
169186 function editPageElement($pageElement) {
170187 $inputRow = array();
171188 $addId = "add-".$pageElement->getId();
 189+ $removeId = "remove-".$pageElement->getId()."-";
172190
173 - foreach($pageElement->getRelationModel()->getAttributes() as $attribute)
 191+ foreach($pageElement->getRelationModel()->getHeading()->attributes as $attribute)
174192 $inputRow[] = getInputFieldForType($addId . "-" . $attribute->id, $attribute->type, "");
175193
176 - addWikiDataBlock($pageElement->getCaption(), getRelationAsEditHTML($pageElement->getRelationModel(), $addId,
177 - $inputRow, $pageElement->repeatInput()));
 194+ addWikiDataBlock($pageElement->getCaption(), getRelationAsEditHTML($pageElement->getRelationModel(), $addId, $removeId,
 195+ $inputRow, $pageElement->repeatInput(), $pageElement->allowRemove()));
178196 }
179197
180198 function addRowForPageElement($pageElement, $postFix) {
181199 $addId = "add-".$pageElement->getId();
182 - $attributes = $pageElement->getRelationModel()->getAttributes();
 200+ $attributes = $pageElement->getRelationModel()->getHeading()->attributes;
183201
184202 if (array_key_exists($addId . "-" . $attributes[0]->id . $postFix, $_POST)) {
185203 $values = array();
@@ -194,7 +212,9 @@
195213 global
196214 $wgRequest;
197215
198 - if ($pageElement->getController()) {
 216+ $controller = $pageElement->getController();
 217+
 218+ if ($controller) {
199219 $addId = "add-" . $pageElement->getId();
200220 $inputRow = array();
201221 $rowCount = $wgRequest->getInt($addId . '-RC');
@@ -203,6 +223,17 @@
204224
205225 for ($i = 2; $i <= $rowCount; $i++)
206226 $this->addRowForPageElement($pageElement, '-' . $i);
 227+
 228+ $removeId = "remove-".$pageElement->getId()."-";
 229+ $relationModel = $pageElement->getRelationModel();
 230+ $key = $relationModel->getKey();
 231+
 232+ for ($i = 0; $i < $relationModel->getTupleCount(); $i++) {
 233+ $tuple = $relationModel->getTuple($i);
 234+
 235+ if ($wgRequest->getCheck(removeCheckBoxName($tuple, $key, $removeId)))
 236+ $controller->remove($tuple);
 237+ }
207238 }
208239 }
209240
@@ -217,7 +248,7 @@
218249 function getDefinedMeaningRelationsPageElement($definedMeaningId) {
219250 return new DefaultPageElement("defined-meaning-relation-$definedMeaningId", "Relations",
220251 $this->getDefinedMeaningRelationsRelation($definedMeaningId),
221 - false,
 252+ true,
222253 false,
223254 new DefinedMeaningRelationController($definedMeaningId));
224255 }
@@ -225,7 +256,7 @@
226257 function getDefinedMeaningAttributesPageElement($definedMeaningId) {
227258 return new DefaultPageElement("defined-meaning-attribute-$definedMeaningId", "Attributes",
228259 $this->getDefinedMeaningAttributesRelation($definedMeaningId),
229 - false,
 260+ true,
230261 false,
231262 new DefinedMeaningAttributeController($definedMeaningId));
232263 }
@@ -524,9 +555,11 @@
525556
526557 function getSynonymAndTranslationRelation($definedMeaningId, $skippedExpressionId) {
527558 $dbr =& wfGetDB(DB_SLAVE);
528 - $attributes = array(new Attribute("language", "Language", "language"), new Attribute("spelling", "Spelling", "spelling"), new Attribute("endemic_meaning", "Identical meaning?", "boolean"));
529 - $relation = new ArrayRelation($attributes);
530 - $queryResult = $dbr->query("SELECT expression_id, endemic_meaning from uw_syntrans where defined_meaning_id=$definedMeaningId and expression_id!=$skippedExpressionId");
 559+ $heading = new Heading(array(new Attribute("language", "Language", "language"),
 560+ new Attribute("spelling", "Spelling", "spelling"),
 561+ new Attribute("endemic_meaning", "Identical meaning?", "boolean")));
 562+ $relation = new ArrayRelation($heading, $heading);
 563+ $queryResult = $dbr->query("SELECT expression_id, endemic_meaning FROM uw_syntrans WHERE defined_meaning_id=$definedMeaningId AND expression_id!=$skippedExpressionId");
531564
532565 while($synonymOrTranslation = $dbr->fetchObject($queryResult)) {
533566 $spellingAndLanguage = $this->getSpellingAndLanguageForExpression($synonymOrTranslation->expression_id);
@@ -539,28 +572,27 @@
540573 }
541574
542575 function getDefinedMeaningRelationsRelation($definedMeaningId) {
543 - $attributes = array(new Attribute("relation-type", "Relation type", "relation-type"), new Attribute("other-defined_meaning", "Other defined meaning", "defined-meaning"));
544 - $relation = new ArrayRelation($attributes);
 576+ $heading = new Heading(array(new Attribute("relation-type", "Relation type", "relation-type"), new Attribute("other-defined-meaning", "Other defined meaning", "defining-expression")));
 577+ $relation = new ArrayRelation($heading, $heading);
545578
546579 $dbr =& wfGetDB(DB_SLAVE);
547580 $queryResult = $dbr->query("SELECT relationtype_mid, meaning2_mid from uw_meaning_relations where meaning1_mid=$definedMeaningId and relationtype_mid!=0 and is_latest_set=1 ORDER BY relationtype_mid");
548581
549582 while($definedMeaningRelation = $dbr->fetchObject($queryResult))
550 - $relation->addTuple(array($this->getExpressionForMeaningId($definedMeaningRelation->relationtype_mid, 85),
551 - $this->getDefiningExpressionForDefinedMeaningId($definedMeaningRelation->meaning2_mid)));
 583+ $relation->addTuple(array($definedMeaningRelation->relationtype_mid, $definedMeaningRelation->meaning2_mid));
552584
553585 return $relation;
554586 }
555587
556588 function getDefinedMeaningAttributesRelation($definedMeaningId) {
557 - $attributes = array(new Attribute("attribute", "Attribute", "attribute"));
558 - $relation = new ArrayRelation($attributes);
 589+ $heading = new Heading(array(new Attribute("attribute", "Attribute", "attribute")));
 590+ $relation = new ArrayRelation($heading, $heading);
559591
560592 $dbr =& wfGetDB(DB_SLAVE);
561593 $queryResult = $dbr->query("SELECT relationtype_mid, meaning2_mid from uw_meaning_relations where meaning1_mid=$definedMeaningId and relationtype_mid=0 and is_latest_set=1");
562594
563595 while($attribute = $dbr->fetchObject($queryResult))
564 - $relation->addTuple(array($this->getExpressionForMeaningId($attribute->meaning2_mid, 85)));
 596+ $relation->addTuple(array($attribute->meaning2_mid));
565597
566598 return $relation;
567599 }
Index: trunk/extensions/Wikidata/WiktionaryZ/tmp/tables.css
@@ -18,28 +18,39 @@
1919
2020 /* Wikidata tables */
2121
22 -.wiki-data-table td, .wiki-data-table th {
23 - padding-top: 0px;
24 - padding-bottom: 0px;
25 - padding-left: 2px;
26 - padding-right: 2px;
27 - margin: 0px;
28 - border: none;
29 -}
30 -
31 -.wiki-data-table th {
32 - background-color: #EEEEEE;
33 - text-align: left;
34 -}
35 -
36 -.wiki-data-table td.column-odd {
37 - background-color: #F6F6F6;
38 -}
39 -
40 -.boolean {
41 - text-align: center;
42 -}
43 -
44 -td.language {
45 - font-style: italic;
 22+.wiki-data-table td, .wiki-data-table th {
 23+ padding-top: 0px;
 24+ padding-bottom: 0px;
 25+ padding-left: 2px;
 26+ padding-right: 2px;
 27+ margin: 0px;
 28+ border: none;
 29+}
 30+
 31+.wiki-data-table th {
 32+ background-color: #EEEEEE;
 33+ text-align: left;
 34+}
 35+
 36+.wiki-data-table td.column-odd {
 37+ background-color: #F6F6F6;
 38+}
 39+
 40+.boolean {
 41+ text-align: center;
 42+}
 43+
 44+td.language {
 45+ font-style: italic;
 46+}
 47+
 48+.wiki-data-table td.remove {
 49+ text-align: center;
 50+ background-color: #EEEEEE;
 51+}
 52+
 53+.wiki-data-table th.remove, .wiki-data-table th.add {
 54+ font-weight: normal;
 55+ padding-left: 4px;
 56+ padding-right: 4px;
4657 }
\ No newline at end of file
Index: trunk/extensions/Wikidata/WiktionaryZ/relation.php
@@ -1,5 +1,7 @@
22 <?php
33
 4+require_once('forms.php');
 5+
46 class Attribute {
57 public $id = "";
68 public $name = "";
@@ -12,34 +14,54 @@
1315 }
1416 }
1517
 18+class Heading {
 19+ public $attributes;
 20+
 21+ public function __construct($attributes) {
 22+ $this->attributes = $attributes;
 23+ }
 24+}
 25+
1626 interface RelationModel {
17 - public function getAttributes();
 27+ public function getHeading();
 28+ public function getKey();
1829 public function getTupleCount();
1930 public function getTuple($tuple);
2031 }
2132
2233 class ArrayRelation implements RelationModel {
23 - protected $attributes = array();
24 - protected $cells = array();
 34+ protected $heading;
 35+ protected $key;
 36+ protected $tuples = array();
2537
26 - public function __construct($attributes) {
27 - $this->attributes = $attributes;
 38+ public function __construct($heading, $key) {
 39+ $this->heading = $heading;
 40+ $this->key = $key;
2841 }
2942
30 - public function addTuple($tuple) {
31 - $this->cells[] = $tuple;
 43+ public function addTuple($values) {
 44+ $tuple = array();
 45+
 46+ for ($i = 0; $i < count($this->heading->attributes); $i++)
 47+ $tuple[$this->heading->attributes[$i]->id] = $values[$i];
 48+
 49+ $this->tuples[] = $tuple;
3250 }
3351
34 - public function getAttributes() {
35 - return $this->attributes;
 52+ public function getHeading() {
 53+ return $this->heading;
3654 }
3755
 56+ public function getKey() {
 57+ return $this->key;
 58+ }
 59+
3860 public function getTupleCount() {
39 - return count($this->cells);
 61+ return count($this->tuples);
4062 }
4163
4264 public function getTuple($tuple) {
43 - return $this->cells[$tuple];
 65+ return $this->tuples[$tuple];
4466 }
4567 }
4668
@@ -53,7 +75,8 @@
5476 for ($i = 0; $i < $fieldCount; $i++)
5577 $attributes[] = new Attribute($dbr->fieldName($queryResult, $i), "Text");
5678
57 - $result = new ArrayRelation($attributes);
 79+ $heading = new Heading($attributes);
 80+ $result = new ArrayRelation($heading);
5881
5982 while ($row = $dbr->fetchRow($queryResult)) {
6083 $tuple = array();
@@ -101,7 +124,7 @@
102125
103126 function getRelationAsHTML($relationModel) {
104127 $result = '<table class="wiki-data-table"><tr>';
105 - $attributes = $relationModel->getAttributes();
 128+ $attributes = $relationModel->getHeading()->attributes;
106129
107130 foreach($attributes as $attribute)
108131 $result .= '<th class="'. $attribute->type .'">' . $attribute->name . '</th>';
@@ -116,43 +139,66 @@
117140 return $result;
118141 }
119142
120 -function getInputRowAsHTML($rowId, $attributes, $values, $repeatInput) {
 143+function getInputRowAsHTML($rowId, $attributes, $values, $repeatInput, $allowRemove) {
121144 if ($repeatInput)
122145 $rowClass = 'repeat';
123146 else
124147 $rowClass = '';
125148
126 - $result = '<tr id="'. $rowId. '" class="' . $rowClass . '">' .
127 - getTableCellsAsHTML($attributes, $values);
 149+ $result = '<tr id="'. $rowId. '" class="' . $rowClass . '">';
 150+
 151+ if ($allowRemove)
 152+ $result .= '<td/>';
 153+
 154+ $result .= getTableCellsAsHTML($attributes, $values);
128155
129156 if ($repeatInput)
130 - $result .= '<td/>';
 157+ $result .= '<td class="add"/>';
131158
132159 return $result . '</tr>';
133160 }
134161
135 -function getRelationAsEditHTML($relationModel, $inputRowId, $inputRowFields, $repeatInput) {
 162+function removeCheckBoxName($tuple, $key, $removeId) {
 163+ $ids = array();
 164+
 165+ foreach($key->attributes as $attribute)
 166+ $ids[] = $tuple[$attribute->id];
 167+
 168+ return $removeId . implode("-", $ids);
 169+}
 170+
 171+function getRelationAsEditHTML($relationModel, $inputRowId, $removeId, $inputRowFields, $repeatInput, $allowRemove) {
136172 $result = '<table class="wiki-data-table"><tr>';
137 - $attributes = $relationModel->getAttributes();
 173+ $attributes = $relationModel->getHeading()->attributes;
 174+ $key = $relationModel->getKey();
138175
 176+ if ($allowRemove)
 177+ $result .= '<th class="remove">Remove</th>';
 178+
139179 foreach($attributes as $attribute)
140180 $result .= '<th class="'. $attribute->type .'">' . $attribute->name . '</th>';
141181
142182 if ($repeatInput)
143 - $result .= '<th>Input rows</th>';
 183+ $result .= '<th class="add">Input rows</th>';
144184
145185 $result .= '</tr>';
146186
147 - for($i = 0; $i < $relationModel->getTupleCount(); $i++) {
148 - $result .= '<tr>' . getTableCellsAsHTML($attributes, convertValuesToHTML($attributes, $relationModel->getTuple($i)));
 187+ for ($i = 0; $i < $relationModel->getTupleCount(); $i++) {
 188+ $result .= '<tr>';
 189+ $tuple = $relationModel->getTuple($i);
149190
 191+ if ($allowRemove)
 192+ $result .= '<td class="remove">' . getCheckBox(removeCheckBoxName($tuple, $key, $removeId), false) . '</td>';
 193+
 194+ $result .= getTableCellsAsHTML($attributes, convertValuesToHTML($attributes, $tuple));
 195+
150196 if ($repeatInput)
151197 $result .= '<td/>';
152198
153199 $result .= '</tr>';
154200 }
155201
156 - $result .= getInputRowAsHTML($inputRowId, $attributes, $inputRowFields, $repeatInput) . '</table>';
 202+ $result .= getInputRowAsHTML($inputRowId, $attributes, $inputRowFields, $repeatInput, $allowRemove) . '</table>';
157203
158204 return $result;
159205 }
Index: trunk/extensions/Wikidata/WiktionaryZ/wikidata.php
@@ -20,6 +20,7 @@
2121
2222 interface PageElementController {
2323 public function add($values);
 24+ public function remove($tuple);
2425 }
2526
2627 class DefaultPageElement implements PageElement {
Index: trunk/extensions/Wikidata/WiktionaryZ/type.php
@@ -31,13 +31,40 @@
3232 return $wgLanguageNames[$languageId];
3333 }
3434
 35+function definingExpression($definedMeaningId) {
 36+ $dbr =& wfGetDB(DB_SLAVE);
 37+ $queryResult = $dbr->query("SELECT spelling from uw_defined_meaning, uw_expression_ns where uw_defined_meaning.defined_meaning_id=$definedMeaningId and uw_expression_ns.expression_id=uw_defined_meaning.expression_id and uw_defined_meaning.is_latest_ver=1 and uw_expression_ns.is_latest=1");
 38+
 39+ while ($spelling = $dbr->fetchObject($queryResult))
 40+ $result = $spelling->spelling;
 41+
 42+ return $result;
 43+}
 44+
 45+function definedMeaningExpression($definedMeaningId) {
 46+ $dbr =& wfGetDB(DB_SLAVE);
 47+ $queryResult = $dbr->query("SELECT spelling from uw_syntrans, uw_expression_ns where defined_meaning_id=$definedMeaningId and uw_expression_ns.expression_id=uw_syntrans.expression_id and uw_expression_ns.language_id=85 limit 1");
 48+ $expression = $dbr->fetchObject($queryResult);
 49+
 50+ return $expression->spelling;
 51+}
 52+
 53+function definingExpressionAsLink($definedMeaningId) {
 54+ return spellingAsLink(definingExpression($definedMeaningId));
 55+}
 56+
 57+function definedMeaningAsLink($definedMeaningId) {
 58+ return spellingAsLink(definedMeaningExpression($definedMeaningId));
 59+}
 60+
3561 function convertToHTML($value, $type) {
3662 switch($type) {
3763 case "boolean": return booleanAsHTML($value);
3864 case "spelling": return spellingAsLink($value);
39 - case "defined-meaning": return spellingAsLink($value);
40 - case "relation-type": return spellingAsLink($value);
41 - case "attribute": return spellingAsLink($value);
 65+ case "defined-meaning": return definedMeaningAsLink($value);
 66+ case "defining-expression": return definingExpressionAsLink($value);
 67+ case "relation-type": return definedMeaningAsLink($value);
 68+ case "attribute": return definedMeaningAsLink($value);
4269 case "language": return languageIdAsText($value);
4370 default: return $value;
4471 }
@@ -48,7 +75,9 @@
4976 case "language": return getLanguageSelect($name);
5077 case "spelling": return getTextBox($name);
5178 case "boolean": return getCheckBox($name, true);
52 - case "defined-meaning": return getSuggest($name, "defined-meaning");
 79+ case "defined-meaning":
 80+ case "defining-expression":
 81+ return getSuggest($name, "defined-meaning");
5382 case "relation-type": return getSuggest($name, "relation-type");
5483 case "attribute": return getSuggest($name, "attribute");
5584 }

Status & tagging log