Index: trunk/extensions/Wikidata/WiktionaryZ/WiktionaryZ.php |
— | — | @@ -7,6 +7,100 @@ |
8 | 8 | require_once('type.php'); |
9 | 9 | require_once('languages.php'); |
10 | 10 | |
| 11 | +function getLatestRevisionForDefinedMeaning($definedMeaningId) { |
| 12 | + $dbr =& wfGetDB(DB_SLAVE); |
| 13 | + $sql = "SELECT revision_id from uw_defined_meaning where defined_meaning_id=$definedMeaningId and is_latest_ver=1 limit 1"; |
| 14 | + $queryResult = $dbr->query($sql); |
| 15 | + |
| 16 | + return $dbr->fetchObject($queryResult)->revision_id; |
| 17 | +} |
| 18 | + |
| 19 | +function relationExists($setId, $definedMeaning1Id, $relationTypeId, $definedMeaning2Id) { |
| 20 | + $dbr =& wfGetDB(DB_SLAVE); |
| 21 | + $queryResult = $dbr->query("SELECT * FROM uw_meaning_relations WHERE set_id=$setId AND meaning1_mid=$definedMeaning1Id AND meaning2_mid=$definedMeaning2Id AND relationtype_mid=$relationTypeId AND is_latest_set=1"); |
| 22 | + |
| 23 | + return $dbr->numRows($queryResult) > 0; |
| 24 | +} |
| 25 | + |
| 26 | +function getSetIdForDefinedMeaningRelations($definedMeaningId) { |
| 27 | + $dbr =& wfGetDB(DB_SLAVE); |
| 28 | + $sql = "SELECT set_id from uw_meaning_relations where meaning1_mid=$definedMeaningId and is_latest_set=1 limit 1"; |
| 29 | + $queryResult = $dbr->query($sql); |
| 30 | + |
| 31 | + $setId = $dbr->fetchObject($queryResult)->set_id; |
| 32 | + |
| 33 | + if (!$setId) { |
| 34 | + $sql = "SELECT max(set_id) as max_id from uw_meaning_relations"; |
| 35 | + $queryResult = $dbr->query($sql); |
| 36 | + $setId = $dbr->fetchObject($queryResult)->max_id + 1; |
| 37 | + } |
| 38 | + |
| 39 | + return $setId; |
| 40 | +} |
| 41 | + |
| 42 | +function addRelation($definedMeaning1Id, $relationTypeId, $definedMeaning2Id) { |
| 43 | + $setId = getSetIdForDefinedMeaningRelations($definedMeaning1Id); |
| 44 | + $revisionId = getLatestRevisionForDefinedMeaning($definedMeaning1Id); |
| 45 | + |
| 46 | + if (!relationExists($setId, $definedMeaning1Id, $relationTypeId, $definedMeaning2Id)) { |
| 47 | + $dbr =& wfGetDB(DB_MASTER); |
| 48 | + $sql = "insert into uw_meaning_relations(set_id, meaning1_mid, meaning2_mid, relationtype_mid, is_latest_set, first_set, revision_id) " . |
| 49 | + "values($setId, $definedMeaning1Id, $definedMeaning2Id, $relationTypeId, 1, $setId, $revisionId)"; |
| 50 | + $dbr->query($sql); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +class SynonymTranslationController implements PageElementController { |
| 56 | + protected $definedMeaningId; |
| 57 | + |
| 58 | + public function __construct($definedMeaningId) { |
| 59 | + $this->definedMeaningId = $definedMeaningId; |
| 60 | + } |
| 61 | + |
| 62 | + public function add($values) { |
| 63 | + $languageId = $values[0]; |
| 64 | + $spelling = $values[1]; |
| 65 | + $endemicMeaning = $values[2]; |
| 66 | + |
| 67 | + if ($spelling != '') { |
| 68 | + $expression = findOrCreateExpression($spelling, $languageId); |
| 69 | + $expression->assureIsBoundToDefinedMeaning($this->definedMeaningId, $endemicMeaning); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class DefinedMeaningRelationController implements PageElementController { |
| 75 | + protected $definedMeaningId; |
| 76 | + |
| 77 | + public function __construct($definedMeaningId) { |
| 78 | + $this->definedMeaningId = $definedMeaningId; |
| 79 | + } |
| 80 | + |
| 81 | + public function add($values) { |
| 82 | + $relationTypeId = $values[0]; |
| 83 | + $otherDefinedMeaningId = $values[1]; |
| 84 | + |
| 85 | + if ($relationTypeId != 0 && $otherDefinedMeaningId != 0) |
| 86 | + addRelation($this->definedMeaningId, $relationTypeId, $otherDefinedMeaningId); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class DefinedMeaningAttributeController implements PageElementController { |
| 91 | + protected $definedMeaningId; |
| 92 | + |
| 93 | + public function __construct($definedMeaningId) { |
| 94 | + $this->definedMeaningId = $definedMeaningId; |
| 95 | + } |
| 96 | + |
| 97 | + public function add($values) { |
| 98 | + $attributeId = $values[0]; |
| 99 | + |
| 100 | + if ($attributeId != 0) |
| 101 | + addRelation($this->definedMeaningId, 0, $attributeId); |
| 102 | + } |
| 103 | +} |
| 104 | + |
11 | 105 | /** |
12 | 106 | * Renders a content page from WiktionaryZ based on the GEMET database. |
13 | 107 | * @package MediaWiki |
— | — | @@ -26,13 +120,11 @@ |
27 | 121 | |
28 | 122 | $wgOut->addHTML("Your user interface language preference: <b>$userlang</b> - " . $skin->makeLink("Special:Preferences", "set your preferences")); |
29 | 123 | |
30 | | - # Get entry record from GEMET namespace |
31 | 124 | $res=$dbr->query("SELECT * from uw_expression_ns WHERE spelling=BINARY ".$dbr->addQuotes($wgTitle->getText())); |
32 | 125 | |
33 | 126 | while($row=$dbr->fetchObject($res)) { |
34 | 127 | $expressionId = $row->expression_id; |
35 | 128 | $definedMeaningIds = $this->getDefinedMeaningsForExpression($expressionId); |
36 | | - $synonymAndTranslationRelations = $this->getSynonymAndTranslationRelations($definedMeaningIds, $expressionId); |
37 | 129 | $definedMeaningTexts = $this->getDefinedMeaningTexts($definedMeaningIds); |
38 | 130 | $alternativeMeaningTexts = $this->getAlternativeMeaningTexts($definedMeaningIds); |
39 | 131 | |
— | — | @@ -53,9 +145,10 @@ |
54 | 146 | } |
55 | 147 | |
56 | 148 | $wgOut->addHTML('<div class="wiki-data-blocks">'); |
57 | | - addWikiDataBlock("Translations and synonyms", getRelationAsHTML($synonymAndTranslationRelations[$definedMeaningId])); |
58 | | - addWikiDataBlock("Relations", getRelationAsHTML($this->getDefinedMeaningRelationsRelation($definedMeaningId))); |
59 | | - addWikiDataBlock("Attributes", getRelationAsHTML($this->getDefinedMeaningAttributesRelation($definedMeaningId))); |
| 149 | + $pageElements = $this->getDefinedMeaningPageElements($definedMeaningId, $expressionId); |
| 150 | + |
| 151 | + foreach($pageElements as $pageElement) |
| 152 | + $this->displayPageElement($pageElement); |
60 | 153 | $wgOut->addHTML('</div>'); |
61 | 154 | |
62 | 155 | $wgOut->addHTML('<div class="clear-float"/>'); |
— | — | @@ -68,6 +161,81 @@ |
69 | 162 | # $wgOut->setPageTitleArray($this->mTitle->getTitleArray()); |
70 | 163 | } |
71 | 164 | |
| 165 | + function displayPageElement($pageElement) { |
| 166 | + addWikiDataBlock($pageElement->getCaption(), getRelationAsHTML($pageElement->getRelationModel())); |
| 167 | + } |
| 168 | + |
| 169 | + function editPageElement($pageElement) { |
| 170 | + $inputRow = array(); |
| 171 | + $addId = "add-".$pageElement->getId(); |
| 172 | + |
| 173 | + foreach($pageElement->getRelationModel()->getAttributes() as $attribute) |
| 174 | + $inputRow[] = getInputFieldForType($addId . "-" . $attribute->id, $attribute->type, ""); |
| 175 | + |
| 176 | + addWikiDataBlock($pageElement->getCaption(), getRelationAsEditHTML($pageElement->getRelationModel(), $addId, |
| 177 | + $inputRow, $pageElement->repeatInput())); |
| 178 | + } |
| 179 | + |
| 180 | + function addRowForPageElement($pageElement, $postFix) { |
| 181 | + $addId = "add-".$pageElement->getId(); |
| 182 | + $attributes = $pageElement->getRelationModel()->getAttributes(); |
| 183 | + |
| 184 | + if (array_key_exists($addId . "-" . $attributes[0]->id . $postFix, $_POST)) { |
| 185 | + $values = array(); |
| 186 | + |
| 187 | + foreach($attributes as $attribute) |
| 188 | + $values[] = getFieldValueForType($addId . "-". $attribute->id . $postFix, $attribute->type); |
| 189 | + |
| 190 | + $pageElement->getController()->add($values); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + function savePageElement($pageElement) { |
| 195 | + global |
| 196 | + $wgRequest; |
| 197 | + |
| 198 | + if ($pageElement->getController()) { |
| 199 | + $addId = "add-" . $pageElement->getId(); |
| 200 | + $inputRow = array(); |
| 201 | + $rowCount = $wgRequest->getInt($addId . '-RC'); |
| 202 | + |
| 203 | + $this->addRowForPageElement($pageElement, ""); |
| 204 | + |
| 205 | + for ($i = 2; $i <= $rowCount; $i++) |
| 206 | + $this->addRowForPageElement($pageElement, '-' . $i); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + function getSynonymsAndTranslationsPageElement($definedMeaningId, $expressionId) { |
| 211 | + return new DefaultPageElement("synonym-translation-$definedMeaningId", "Translations and synonyms", |
| 212 | + $this->getSynonymAndTranslationRelation($definedMeaningId, $expressionId), |
| 213 | + false, |
| 214 | + true, |
| 215 | + new SynonymTranslationController($definedMeaningId)); |
| 216 | + } |
| 217 | + |
| 218 | + function getDefinedMeaningRelationsPageElement($definedMeaningId) { |
| 219 | + return new DefaultPageElement("defined-meaning-relation-$definedMeaningId", "Relations", |
| 220 | + $this->getDefinedMeaningRelationsRelation($definedMeaningId), |
| 221 | + false, |
| 222 | + false, |
| 223 | + new DefinedMeaningRelationController($definedMeaningId)); |
| 224 | + } |
| 225 | + |
| 226 | + function getDefinedMeaningAttributesPageElement($definedMeaningId) { |
| 227 | + return new DefaultPageElement("defined-meaning-attribute-$definedMeaningId", "Attributes", |
| 228 | + $this->getDefinedMeaningAttributesRelation($definedMeaningId), |
| 229 | + false, |
| 230 | + false, |
| 231 | + new DefinedMeaningAttributeController($definedMeaningId)); |
| 232 | + } |
| 233 | + |
| 234 | + function getDefinedMeaningPageElements($definedMeaningId, $expressionId) { |
| 235 | + return array($this->getSynonymsAndTranslationsPageElement($definedMeaningId, $expressionId), |
| 236 | + $this->getDefinedMeaningRelationsPageElement($definedMeaningId), |
| 237 | + $this->getDefinedMeaningAttributesPageElement($definedMeaningId)); |
| 238 | + } |
| 239 | + |
72 | 240 | function viewTranslatedContent($setId) { |
73 | 241 | global |
74 | 242 | $wgOut, $wgLanguageNames; |
— | — | @@ -80,18 +248,6 @@ |
81 | 249 | } |
82 | 250 | } |
83 | 251 | |
84 | | - function getRelationTypeSuggest($definedMeaningId) { |
85 | | - return getSuggest("new-relation-type-$definedMeaningId", "relation-type"); |
86 | | - } |
87 | | - |
88 | | - function getAttributeSuggest($definedMeaningId) { |
89 | | - return getSuggest("new-attribute-$definedMeaningId", "attribute"); |
90 | | - } |
91 | | - |
92 | | - function getDefinedMeaningSuggest($definedMeaningId) { |
93 | | - return getSuggest("new-relation-other-meaning-$definedMeaningId", "defined-meaning"); |
94 | | - } |
95 | | - |
96 | 252 | function getRelationTypes() { |
97 | 253 | $relationtypes=array(); |
98 | 254 | $reltypecollections=$this->getReltypeCollections(); |
— | — | @@ -170,9 +326,10 @@ |
171 | 327 | if (count($translatedContents)) |
172 | 328 | $this->addTranslatedDefinitionFromRequest($definedMeaningId, $definedMeaningTextId, getRevisionForExpressionId($expressionId), array_keys($translatedContents)); |
173 | 329 | |
174 | | - $this->addSynonymsOrTranslationsFromRequest($definedMeaningId); |
175 | | - $this->addRelationFromRequest($definedMeaningId); |
176 | | - $this->addAttributeFromRequest($definedMeaningId); |
| 330 | + $pageElements = $this->getDefinedMeaningPageElements($definedMeaningId, $expressionId); |
| 331 | + |
| 332 | + foreach($pageElements as $pageElement) |
| 333 | + $this->savePageElement($pageElement); |
177 | 334 | } |
178 | 335 | |
179 | 336 | function edit() { |
— | — | @@ -241,19 +398,18 @@ |
242 | 399 | |
243 | 400 | $wgOut->addHTML("<h2><i>Spelling:</i>" . $spelling . " - <i>Language:</i> ".$wgLanguageNames[$languageId]."</h2>"); |
244 | 401 | |
245 | | - $synonymAndTranslationRelations = $this->getSynonymAndTranslationRelations($definedMeaningIds, $expressionId); |
246 | 402 | $definedMeaningTexts = $this->getDefinedMeaningTexts($definedMeaningIds); |
247 | 403 | |
248 | 404 | $wgOut->addHTML('<ul>'); |
249 | 405 | foreach ($definedMeaningIds as $definedMeaningId) { |
250 | 406 | $wgOut->addHTML('<li>'); |
251 | | - $this->displayDefinedMeaningEditForm($definedMeaningId, $synonymAndTranslationRelations[$definedMeaningId], $definedMeaningTexts[$definedMeaningId]); |
| 407 | + $this->displayDefinedMeaningEditForm($definedMeaningId, $expressionId, $this->getSynonymAndTranslationRelation($definedMeaningId, $expressionId), $definedMeaningTexts[$definedMeaningId]); |
252 | 408 | $wgOut->addHTML('</li>'); |
253 | 409 | } |
254 | 410 | $wgOut->addHTML('</ul>'); |
255 | 411 | } |
256 | 412 | |
257 | | - function displayDefinedMeaningEditForm($definedMeaningId, $synonymAndTranslationTable, $definedMeaningTextId) { |
| 413 | + function displayDefinedMeaningEditForm($definedMeaningId, $expressionId, $synonymAndTranslationTable, $definedMeaningTextId) { |
258 | 414 | global |
259 | 415 | $wgOut, $wgLanguageNames; |
260 | 416 | |
— | — | @@ -267,14 +423,16 @@ |
268 | 424 | |
269 | 425 | if (count($translatedContents) > 0) { |
270 | 426 | $wgOut->addHTML('<div><i>Translate into</i>: '. |
271 | | - $this->getLanguageSelect("translated-definition-language-$definedMeaningId", array_keys($translatedContents)).'</div>'. |
| 427 | + getLanguageSelect("translated-definition-language-$definedMeaningId", array_keys($translatedContents)).'</div>'. |
272 | 428 | getTextArea("translated-definition-$definedMeaningId")); |
273 | 429 | } |
274 | 430 | |
275 | 431 | $wgOut->addHTML('<div class="wiki-data-blocks">'); |
276 | | - addWikiDataBlock("Translations and synonyms", getRelationAsEditHTML($synonymAndTranslationTable, "add-translation-synonym-$definedMeaningId", $this->getAddTranslationsAndSynonymsRowFields($definedMeaningId), true)); |
277 | | - addWikiDataBlock("Relations", getRelationAsEditHTML($this->getDefinedMeaningRelationsRelation($definedMeaningId), "", array($this->getRelationTypeSuggest($definedMeaningId), $this->getDefinedMeaningSuggest($definedMeaningId)), false)); |
278 | | - addWikiDataBlock("Attributes", getRelationAsEditHTML($this->getDefinedMeaningAttributesRelation($definedMeaningId), "", array($this->getAttributeSuggest($definedMeaningId)), false)); |
| 432 | + $pageElements = $this->getDefinedMeaningPageElements($definedMeaningId, $expressionId); |
| 433 | + |
| 434 | + foreach($pageElements as $pageElement) |
| 435 | + $this->editPageElement($pageElement); |
| 436 | + |
279 | 437 | $wgOut->addHTML('</div>'); |
280 | 438 | |
281 | 439 | $wgOut->addHTML('<div class="clear-float"/>'); |
— | — | @@ -364,30 +522,24 @@ |
365 | 523 | return $synonymAndTranslationIds; |
366 | 524 | } |
367 | 525 | |
368 | | - function getSynonymAndTranslationRelations($definedMeaningIds, $skippedExpressionId) { |
| 526 | + function getSynonymAndTranslationRelation($definedMeaningId, $skippedExpressionId) { |
369 | 527 | $dbr =& wfGetDB(DB_SLAVE); |
370 | | - $result = array(); |
371 | | - $attributes = array(new Attribute("Language", "language"), new Attribute("Spelling", "spelling"), new Attribute("Identical meaning?", "boolean")); |
| 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"); |
| 531 | + |
| 532 | + while($synonymOrTranslation = $dbr->fetchObject($queryResult)) { |
| 533 | + $spellingAndLanguage = $this->getSpellingAndLanguageForExpression($synonymOrTranslation->expression_id); |
| 534 | + |
| 535 | + foreach($spellingAndLanguage as $languageId => $spelling) |
| 536 | + $relation->addTuple(array($languageId, $spelling, $synonymOrTranslation->endemic_meaning)); |
| 537 | + } |
372 | 538 | |
373 | | - foreach($definedMeaningIds as $definedMeaningId) { |
374 | | - $table = new ArrayRelation($attributes); |
375 | | - $queryResult = $dbr->query("SELECT expression_id, endemic_meaning from uw_syntrans where defined_meaning_id=$definedMeaningId and expression_id!=$skippedExpressionId"); |
376 | | - |
377 | | - while($synonymOrTranslation = $dbr->fetchObject($queryResult)) { |
378 | | - $spellingAndLanguage = $this->getSpellingAndLanguageForExpression($synonymOrTranslation->expression_id); |
379 | | - |
380 | | - foreach($spellingAndLanguage as $languageId => $spelling) |
381 | | - $table->addTuple(array($languageId, $spelling, $synonymOrTranslation->endemic_meaning)); |
382 | | - } |
383 | | - |
384 | | - $result[$definedMeaningId] = $table; |
385 | | - } |
386 | | - |
387 | | - return $result; |
| 539 | + return $relation; |
388 | 540 | } |
389 | 541 | |
390 | 542 | function getDefinedMeaningRelationsRelation($definedMeaningId) { |
391 | | - $attributes = array(new Attribute("Relation type", "defined-meaning"), new Attribute("Other defined meaning", "defined-meaning")); |
| 543 | + $attributes = array(new Attribute("relation-type", "Relation type", "relation-type"), new Attribute("other-defined_meaning", "Other defined meaning", "defined-meaning")); |
392 | 544 | $relation = new ArrayRelation($attributes); |
393 | 545 | |
394 | 546 | $dbr =& wfGetDB(DB_SLAVE); |
— | — | @@ -395,13 +547,13 @@ |
396 | 548 | |
397 | 549 | while($definedMeaningRelation = $dbr->fetchObject($queryResult)) |
398 | 550 | $relation->addTuple(array($this->getExpressionForMeaningId($definedMeaningRelation->relationtype_mid, 85), |
399 | | - $this->getExpressionForMeaningId($definedMeaningRelation->meaning2_mid, 85))); |
| 551 | + $this->getExpressionForMeaningId($definedMeaningRelation->meaning2_mid, 85))); |
400 | 552 | |
401 | 553 | return $relation; |
402 | 554 | } |
403 | 555 | |
404 | 556 | function getDefinedMeaningAttributesRelation($definedMeaningId) { |
405 | | - $attributes = array(new Attribute("Attribute", "defined-meaning")); |
| 557 | + $attributes = array(new Attribute("attribute", "Attribute", "attribute")); |
406 | 558 | $relation = new ArrayRelation($attributes); |
407 | 559 | |
408 | 560 | $dbr =& wfGetDB(DB_SLAVE); |
— | — | @@ -459,41 +611,11 @@ |
460 | 612 | return $translatedContents; |
461 | 613 | } |
462 | 614 | |
463 | | - function getAddTranslationsAndSynonymsRowFields($definedMeaningId) { |
464 | | - return array($this->getLanguageSelect("language-$definedMeaningId"), getTextBox("spelling-$definedMeaningId"), getCheckBox("endemic-meaning-$definedMeaningId", true)); |
465 | | - } |
466 | | - |
467 | 615 | function getTranslationIdsForDefinedMeaning($definedMeaningId) { |
468 | 616 | $dbr =& wfGetDB(DB_SLAVE); |
469 | 617 | $queryResult = $dbr->query("SELECT * from text where old_id=$textId"); |
470 | 618 | } |
471 | 619 | |
472 | | - function getLanguageOptions($languageIdsToExclude = array()) { |
473 | | - global |
474 | | - $wgUser; |
475 | | - |
476 | | - $userLanguage = $wgUser->getOption('language'); |
477 | | - $idNameIndex = getLangNames($userLanguage); |
478 | | - |
479 | | - $result = array(); |
480 | | - |
481 | | - foreach($idNameIndex as $id => $name) |
482 | | - if (!in_array($id, $languageIdsToExclude)) |
483 | | - $result[$id] = $name; |
484 | | - |
485 | | - return $result; |
486 | | - } |
487 | | - |
488 | | - function getLanguageSelect($name, $languageIdsToExclude = array()) { |
489 | | - global |
490 | | - $wgUser; |
491 | | - |
492 | | - $userLanguage = $wgUser->getOption('language'); |
493 | | - $userLanguageId = getLanguageIdForCode($userLanguage); |
494 | | - |
495 | | - return getSelect($name, $this->getLanguageOptions($languageIdsToExclude), $userLanguageId); |
496 | | - } |
497 | | - |
498 | 620 | function getText($textId) { |
499 | 621 | $dbr =& wfGetDB(DB_SLAVE); |
500 | 622 | $queryResult = $dbr->query("SELECT old_text from text where old_id=$textId"); |
— | — | @@ -520,35 +642,6 @@ |
521 | 643 | return $dbr->insertId(); |
522 | 644 | } |
523 | 645 | |
524 | | - function addSynonymOrTranslation($spelling, $languageId, $definedMeaningId, $endemicMeaning) { |
525 | | - $expression = findOrCreateExpression($spelling, $languageId); |
526 | | - $expression->assureIsBoundToDefinedMeaning($definedMeaningId, $endemicMeaning); |
527 | | - } |
528 | | - |
529 | | - function addSynonymOrTranslationFromRequest($definedMeaningId, $postFix) { |
530 | | - global |
531 | | - $wgRequest; |
532 | | - |
533 | | - if (array_key_exists('language-'. $postFix, $_POST)) { |
534 | | - $languageId = $wgRequest->getInt('language-'. $postFix); |
535 | | - $spelling = trim($wgRequest->getText('spelling-'. $postFix)); |
536 | | - $endemicMeaning = $wgRequest->getCheck('endemic-meaning-'.$postFix); |
537 | | - |
538 | | - if ($spelling != '') |
539 | | - $this->addSynonymOrTranslation($spelling, $languageId, $definedMeaningId, $endemicMeaning); |
540 | | - } |
541 | | - } |
542 | | - |
543 | | - function addSynonymsOrTranslationsFromRequest($definedMeaningId) { |
544 | | - global |
545 | | - $wgRequest; |
546 | | - |
547 | | - $this->addSynonymOrTranslationFromRequest($definedMeaningId, $definedMeaningId); |
548 | | - |
549 | | - for ($i = 2; $i <= $wgRequest->getInt('add-translation-synonym-'. $definedMeaningId . '-RC'); $i++) |
550 | | - $this->addSynonymOrTranslationFromRequest($definedMeaningId, $definedMeaningId . '-' . $i); |
551 | | - } |
552 | | - |
553 | 646 | function createTranslatedContent($setId, $languageId, $textId, $revisionId) { |
554 | 647 | $dbr = &wfGetDB(DB_MASTER); |
555 | 648 | $sql = "insert into translated_content(set_id,language_id,text_id,first_set,revision_id) values($setId, $languageId, $textId, $setId, $revisionId)"; |
— | — | @@ -615,78 +708,15 @@ |
616 | 709 | # Should be expressions in the language of preference, with an appropriate fallback scheme |
617 | 710 | function getExpressionsForDefinedMeaningIds($definedMeaningIds) { |
618 | 711 | $dbr =& wfGetDB(DB_SLAVE); |
619 | | - $queryResult = $dbr->query("SELECT defined_meaning_id, spelling from uw_syntrans, uw_expression_ns where defined_meaning_id in (". implode(",", $definedMeaningIds) . ") and uw_expression_ns.expression_id=uw_syntrans.expression_id and uw_expression_ns.language_id=85"); |
| 712 | + $queryResult = $dbr->query("SELECT defined_meaning_id, spelling from uw_syntrans, uw_expression_ns where defined_meaning_id in (". implode(",", $definedMeaningIds) . ") and uw_expression_ns.expression_id=uw_syntrans.expression_id and uw_expression_ns.language_id=85 and uw_syntrans.endemic_meaning=1"); |
620 | 713 | $expressions = array(); |
621 | 714 | |
622 | | - while ($expression = $dbr->fetchObject($queryResult)) |
623 | | - $expressions[$expression->defined_meaning_id] = $expression->spelling; |
| 715 | + while ($expression = $dbr->fetchObject($queryResult)) |
| 716 | + if (!array_key_exists($expression->defined_meaning_id, $expressions)) |
| 717 | + $expressions[$expression->defined_meaning_id] = $expression->spelling; |
624 | 718 | |
625 | 719 | return $expressions; |
626 | 720 | } |
627 | | - |
628 | | - function addRelationFromRequest($definedMeaningId) { |
629 | | - global |
630 | | - $wgRequest; |
631 | | - |
632 | | - $relationTypeId = $wgRequest->getInt("new-relation-type-$definedMeaningId"); |
633 | | - $otherDefinedMeaningId = $wgRequest->getInt("new-relation-other-meaning-$definedMeaningId"); |
634 | | - |
635 | | - if ($relationTypeId != 0 && $otherDefinedMeaningId != 0) |
636 | | - $this->addRelation($definedMeaningId, $relationTypeId, $otherDefinedMeaningId); |
637 | | - } |
638 | | - |
639 | | - function addAttributeFromRequest($definedMeaningId) { |
640 | | - global |
641 | | - $wgRequest; |
642 | | - |
643 | | - $attributeId = $wgRequest->getInt("new-attribute-$definedMeaningId"); |
644 | | - |
645 | | - if ($attributeId != 0) |
646 | | - $this->addRelation($definedMeaningId, 0, $attributeId); |
647 | | - } |
648 | | - |
649 | | - function getSetIdForDefinedMeaningRelations($definedMeaningId) { |
650 | | - $dbr =& wfGetDB(DB_SLAVE); |
651 | | - $sql = "SELECT set_id from uw_meaning_relations where meaning1_mid=$definedMeaningId and is_latest_set=1 limit 1"; |
652 | | - $queryResult = $dbr->query($sql); |
653 | | - |
654 | | - $setId = $dbr->fetchObject($queryResult)->set_id; |
655 | | - |
656 | | - if (!$setId) { |
657 | | - $sql = "SELECT max(set_id) as max_id from uw_meaning_relations"; |
658 | | - $queryResult = $dbr->query($sql); |
659 | | - $setId = $dbr->fetchObject($queryResult)->max_id + 1; |
660 | | - } |
661 | | - |
662 | | - return $setId; |
663 | | - } |
664 | | - |
665 | | - function getLatestRevisionForDefinedMeaning($definedMeaningId) { |
666 | | - $dbr =& wfGetDB(DB_SLAVE); |
667 | | - $sql = "SELECT revision_id from uw_defined_meaning where defined_meaning_id=$definedMeaningId and is_latest_ver=1 limit 1"; |
668 | | - $queryResult = $dbr->query($sql); |
669 | | - |
670 | | - return $dbr->fetchObject($queryResult)->revision_id; |
671 | | - } |
672 | | - |
673 | | - function relationExists($setId, $definedMeaning1Id, $relationTypeId, $definedMeaning2Id) { |
674 | | - $dbr =& wfGetDB(DB_MASTER); |
675 | | - $queryResult = $dbr->query("SELECT * FROM uw_meaning_relations WHERE set_id=$setId AND meaning1_mid=$definedMeaning1Id AND meaning2_mid=$definedMeaning2Id AND relationtype_mid=$relationTypeId AND is_latest_set=1"); |
676 | | - |
677 | | - return $dbr->numRows($queryResult) > 0; |
678 | | - } |
679 | | - |
680 | | - function addRelation($definedMeaning1Id, $relationTypeId, $definedMeaning2Id) { |
681 | | - $setId = $this->getSetIdForDefinedMeaningRelations($definedMeaning1Id); |
682 | | - $revisionId = $this->getLatestRevisionForDefinedMeaning($definedMeaning1Id); |
683 | | - |
684 | | - if (!$this->relationExists($setId, $definedMeaning1Id, $relationTypeId, $definedMeaning2Id)) { |
685 | | - $dbr =& wfGetDB(DB_MASTER); |
686 | | - $sql = "insert into uw_meaning_relations(set_id, meaning1_mid, meaning2_mid, relationtype_mid, is_latest_set, first_set, revision_id) " . |
687 | | - "values($setId, $definedMeaning1Id, $definedMeaning2Id, $relationTypeId, 1, $setId, $revisionId)"; |
688 | | - $dbr->query($sql); |
689 | | - } |
690 | | - } |
691 | 721 | } |
692 | 722 | |
693 | 723 | ?> |
Index: trunk/extensions/Wikidata/WiktionaryZ/relation.php |
— | — | @@ -1,10 +1,12 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class Attribute { |
| 5 | + public $id = ""; |
5 | 6 | public $name = ""; |
6 | 7 | public $type = ""; |
7 | 8 | |
8 | | - public function __construct($name, $type) { |
| 9 | + public function __construct($id, $name, $type) { |
| 10 | + $this->id = $id; |
9 | 11 | $this->name = $name; |
10 | 12 | $this->type = $type; |
11 | 13 | } |
Index: trunk/extensions/Wikidata/WiktionaryZ/wikidata.php |
— | — | @@ -1,11 +1,68 @@ |
2 | 2 | <?php |
3 | | - function addWikiDataBlock($title, $content) { |
4 | | - global |
5 | | - $wgOut; |
6 | | - |
7 | | - $wgOut->addHTML('<div class="wiki-data-block"> |
8 | | - <h4>'. $title . '</h4>'. |
9 | | - $content . |
10 | | - '</div>'); |
| 3 | +function addWikiDataBlock($title, $content) { |
| 4 | + global |
| 5 | + $wgOut; |
| 6 | + |
| 7 | + $wgOut->addHTML('<div class="wiki-data-block"> |
| 8 | + <h4>'. $title . '</h4>'. |
| 9 | + $content . |
| 10 | + '</div>'); |
| 11 | +} |
| 12 | + |
| 13 | +interface PageElement { |
| 14 | + public function getId(); |
| 15 | + public function getCaption(); |
| 16 | + public function getRelationModel(); |
| 17 | + public function allowRemove(); |
| 18 | + public function repeatInput(); |
| 19 | + public function getController(); |
| 20 | +} |
| 21 | + |
| 22 | +interface PageElementController { |
| 23 | + public function add($values); |
| 24 | +} |
| 25 | + |
| 26 | +class DefaultPageElement implements PageElement { |
| 27 | + public $id; |
| 28 | + public $caption; |
| 29 | + public $relationModel; |
| 30 | + public $allowRemove; |
| 31 | + public $inputRow; |
| 32 | + public $repeatInput; |
| 33 | + public $controller; |
| 34 | + |
| 35 | + public function __construct($id, $caption, $relationModel, $allowRemove, $repeatInput, $controller) { |
| 36 | + $this->id = $id; |
| 37 | + $this->caption = $caption; |
| 38 | + $this->relationModel = $relationModel; |
| 39 | + $this->allowRemove = $allowRemove; |
| 40 | + $this->repeatInput = $repeatInput; |
| 41 | + $this->controller = $controller; |
11 | 42 | } |
| 43 | + |
| 44 | + public function getId() { |
| 45 | + return $this->id; |
| 46 | + } |
| 47 | + |
| 48 | + public function getCaption() { |
| 49 | + return $this->caption; |
| 50 | + } |
| 51 | + |
| 52 | + public function getRelationModel() { |
| 53 | + return $this->relationModel; |
| 54 | + } |
| 55 | + |
| 56 | + public function allowRemove() { |
| 57 | + return $this->allowRemove; |
| 58 | + } |
| 59 | + |
| 60 | + public function repeatInput() { |
| 61 | + return $this->repeatInput; |
| 62 | + } |
| 63 | + |
| 64 | + public function getController() { |
| 65 | + return $this->controller; |
| 66 | + } |
| 67 | +} |
| 68 | + |
12 | 69 | ?> |
Index: trunk/extensions/Wikidata/WiktionaryZ/Expression.php |
— | — | @@ -190,6 +190,11 @@ |
191 | 191 | $queryResult = $dbr->query($sql);
|
192 | 192 | }
|
193 | 193 |
|
| 194 | +function addSynonymOrTranslation($spelling, $languageId, $definedMeaningId, $endemicMeaning) {
|
| 195 | + $expression = findOrCreateExpression($spelling, $languageId);
|
| 196 | + $expression->assureIsBoundToDefinedMeaning($definedMeaningId, $endemicMeaning);
|
| 197 | +}
|
| 198 | +
|
194 | 199 | function getMaximum($field, $table) {
|
195 | 200 | $dbr = &wfGetDB(DB_SLAVE);
|
196 | 201 | $sql = "select max($field) as maximum from $table";
|
Index: trunk/extensions/Wikidata/WiktionaryZ/type.php |
— | — | @@ -1,6 +1,7 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | require_once('languages.php'); |
| 5 | +require_once('forms.php'); |
5 | 6 | |
6 | 7 | function booleanAsText($value) { |
7 | 8 | if ($value) |
— | — | @@ -35,9 +36,37 @@ |
36 | 37 | case "boolean": return booleanAsHTML($value); |
37 | 38 | case "spelling": return spellingAsLink($value); |
38 | 39 | case "defined-meaning": return spellingAsLink($value); |
| 40 | + case "relation-type": return spellingAsLink($value); |
| 41 | + case "attribute": return spellingAsLink($value); |
39 | 42 | case "language": return languageIdAsText($value); |
40 | 43 | default: return $value; |
41 | 44 | } |
42 | 45 | } |
43 | 46 | |
| 47 | +function getInputFieldForType($name, $type, $value) { |
| 48 | + switch($type) { |
| 49 | + case "language": return getLanguageSelect($name); |
| 50 | + case "spelling": return getTextBox($name); |
| 51 | + case "boolean": return getCheckBox($name, true); |
| 52 | + case "defined-meaning": return getSuggest($name, "defined-meaning"); |
| 53 | + case "relation-type": return getSuggest($name, "relation-type"); |
| 54 | + case "attribute": return getSuggest($name, "attribute"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function getFieldValueForType($name, $type) { |
| 59 | + global |
| 60 | + $wgRequest; |
| 61 | + |
| 62 | + switch($type) { |
| 63 | + case "language": return $wgRequest->getInt($name); |
| 64 | + case "spelling": return trim($wgRequest->getText($name)); |
| 65 | + case "boolean": return $wgRequest->getCheck($name); |
| 66 | + case "defined-meaning": return $wgRequest->getInt($name); |
| 67 | + case "relation-type": return $wgRequest->getInt($name); |
| 68 | + case "attribute": return $wgRequest->getInt($name); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | + |
44 | 73 | ?> |
Index: trunk/extensions/Wikidata/WiktionaryZ/forms.php |
— | — | @@ -1,4 +1,7 @@ |
2 | 2 | <?php |
| 3 | + |
| 4 | +require_once('languages.php'); |
| 5 | + |
3 | 6 | function getTextBox($name, $maximumLength = 255, $value = "") { |
4 | 7 | return '<input type="text" name="'. $name .'" value="'. $value .'" maxlength="'. $maximumLength .'"/>'; |
5 | 8 | } |
— | — | @@ -47,6 +50,32 @@ |
48 | 51 | return $result; |
49 | 52 | } |
50 | 53 | |
| 54 | +function getLanguageOptions($languageIdsToExclude = array()) { |
| 55 | + global |
| 56 | + $wgUser; |
| 57 | + |
| 58 | + $userLanguage = $wgUser->getOption('language'); |
| 59 | + $idNameIndex = getLangNames($userLanguage); |
| 60 | + |
| 61 | + $result = array(); |
| 62 | + |
| 63 | + foreach($idNameIndex as $id => $name) |
| 64 | + if (!in_array($id, $languageIdsToExclude)) |
| 65 | + $result[$id] = $name; |
| 66 | + |
| 67 | + return $result; |
| 68 | +} |
| 69 | + |
| 70 | +function getLanguageSelect($name, $languageIdsToExclude = array()) { |
| 71 | + global |
| 72 | + $wgUser; |
| 73 | + |
| 74 | + $userLanguage = $wgUser->getOption('language'); |
| 75 | + $userLanguageId = getLanguageIdForCode($userLanguage); |
| 76 | + |
| 77 | + return getSelect($name, getLanguageOptions($languageIdsToExclude), $userLanguageId); |
| 78 | +} |
| 79 | + |
51 | 80 | function getSubmitButton($name, $value) { |
52 | 81 | return '<input type="submit" name="'. $name .'" value="'. $value .'"/>'; |
53 | 82 | } |