Index: trunk/extensions/Wikidata/App.php |
— | — | @@ -39,6 +39,7 @@ |
40 | 40 | require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialNeedsTranslation.php"); |
41 | 41 | require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialImportLangNames.php"); |
42 | 42 | require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialAddCollection.php"); |
| 43 | +require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialConceptMapping.php"); |
43 | 44 | function addWikidataHeader() { |
44 | 45 | global $wgOut,$wgScriptPath; |
45 | 46 | $wgOut->addScript("<script type='text/javascript' src='{$wgScriptPath}/extensions/Wikidata/OmegaWiki/suggest.js'></script>"); |
Index: trunk/extensions/Wikidata/OmegaWiki/SpecialAddCollection.php |
— | — | @@ -1,6 +1,8 @@ |
2 | 2 | <?php |
3 | 3 | if (!defined('MEDIAWIKI')) die(); |
4 | 4 | |
| 5 | + require_once("WikiDataAPI.php"); // for bootstrapCollection |
| 6 | + |
5 | 7 | $wgAvailableRights[] = 'addcollection'; |
6 | 8 | $wgGroupPermissions['bureaucrat']['addcollection'] = true; |
7 | 9 | |
— | — | @@ -38,7 +40,7 @@ |
39 | 41 | array( |
40 | 42 | 'Collection name' => getTextBox('collection'), |
41 | 43 | 'Language of name' => getSuggest('language','language'), |
42 | | - 'Collection type' => getSelect('type',array('' => 'None','RELT' => 'RELT','LEVL' => 'LEVL','CLAS' => 'CLAS')) |
| 44 | + 'Collection type' => getSelect('type',array('' => 'None','RELT' => 'RELT','LEVL' => 'LEVL','CLAS' => 'CLAS', 'MAPP' => 'MAPP')) |
43 | 45 | ), |
44 | 46 | '',array('create' => 'Create') |
45 | 47 | )); |
Index: trunk/extensions/Wikidata/OmegaWiki/WikiDataAPI.php |
— | — | @@ -1056,4 +1056,133 @@ |
1057 | 1057 | return $result; |
1058 | 1058 | } |
1059 | 1059 | |
| 1060 | + |
| 1061 | +/** Write a concept mapping to db |
| 1062 | + * supply mapping as a valid |
| 1063 | + * array("dataset_prefix"=>defined_meaning_id,...) |
| 1064 | + */ |
| 1065 | + |
| 1066 | +function createConceptMapping($concepts) { |
| 1067 | + $uuid= getUUID(); |
| 1068 | + foreach ($concepts as $dc => $dm_id) { |
| 1069 | + $collid=getCollectionIdForDC($dc); |
| 1070 | + writeDmToCollection($dc, $collid, $uuid, $dm_id); |
| 1071 | + } |
| 1072 | +} |
| 1073 | + |
| 1074 | +/** ask db to provide a universally unique id */ |
| 1075 | + |
| 1076 | +function getUUID() { |
| 1077 | + $dbr = & wfGetDB(DB_SLAVE); |
| 1078 | + $query="SELECT uuid() AS id"; |
| 1079 | + $queryResult = $dbr->query($query); |
| 1080 | + $row=$dbr->fetchObject($queryResult); |
| 1081 | + return isset($row->id) ? $row->id : null; |
| 1082 | +} |
| 1083 | + |
| 1084 | +/** this funtion assumes that there is only a single mapping collection */ |
| 1085 | + |
| 1086 | +function getCollectionIdForDC($dc) { |
| 1087 | + $dbr = & wfGetDB(DB_SLAVE); |
| 1088 | + $query=" |
| 1089 | + SELECT collection_id FROM {$dc}_collection_ns |
| 1090 | + WHERE collection_type=\"MAPP\" |
| 1091 | + AND ". getLatestTransactionRestriction("{$dc}_collection_ns") ." |
| 1092 | + LIMIT 1 |
| 1093 | + "; |
| 1094 | + $queryResult = $dbr->query($query); |
| 1095 | + $row=$dbr->fetchObject($queryResult); |
| 1096 | + return isset($row->collection_id) ? $row->collection_id : null; |
| 1097 | +} |
| 1098 | + |
| 1099 | +/** Write the dm to the correct collection for a particular dc */ |
| 1100 | + |
| 1101 | +function writeDmToCollection($dc, $collid, $uuid, $dm_id) { |
| 1102 | + global |
| 1103 | + $wgUser; |
| 1104 | + //if(is_null($dc)) { |
| 1105 | + // $dc=wdGetDataSetContext(); |
| 1106 | + //} |
| 1107 | + $dbr = & wfGetDB(DB_SLAVE); |
| 1108 | + |
| 1109 | + $collection_contents="{$dc}_collection_contents"; |
| 1110 | + $collid=$dbr->addQuotes($collid); |
| 1111 | + $uuid=$dbr->addQuotes($uuid); |
| 1112 | + $dm_id=$dbr->addQuotes($dm_id); |
| 1113 | + |
| 1114 | + startNewTransaction($wgUser->getId(), wfGetIP(), "inserting collection $collid", $dc); |
| 1115 | + $add_transaction_id=getUpdateTransactionId(); |
| 1116 | + |
| 1117 | + $sql=" |
| 1118 | + INSERT INTO $collection_contents |
| 1119 | + SET collection_id=$collid, |
| 1120 | + internal_member_id=$uuid, |
| 1121 | + member_mid=$dm_id, |
| 1122 | + add_transaction_id=$add_transaction_id |
| 1123 | + "; |
| 1124 | + global $wgOut; |
| 1125 | + $wgOut->addWikiText($sql); |
| 1126 | + $dbr->query($sql); |
| 1127 | +} |
| 1128 | + |
| 1129 | +/**read a ConceptMapping from the database |
| 1130 | + * map is in the form; |
| 1131 | + * array("dataset_prefix"=>defined_meaning_id,...) |
| 1132 | + * (possibly to rename $map or $concepts, to remain consistent) |
| 1133 | + * note that we are using collection_contents.internal_member_id |
| 1134 | + * as our ConceptMap ID. |
| 1135 | + * see also: createConceptMapping($concepts) |
| 1136 | + */ |
| 1137 | +function &readConceptMapping($concept_id) { |
| 1138 | + $dbr = & wfGetDB(DB_SLAVE); |
| 1139 | + $sets=wdGetDataSets(); |
| 1140 | + $map=array(); |
| 1141 | + $concept_id=$dbr->addQuotes($concept_id); |
| 1142 | + foreach ($sets as $key => $set) { |
| 1143 | + #wfdebug ("$key => $set"); |
| 1144 | + $dc=$set->getPrefix(); |
| 1145 | + $collection_id=getCollectionIdForDC($dc); |
| 1146 | + $collection_id=$dbr->addQuotes($collection_id); |
| 1147 | + $collection_contents="{$dc}_collection_contents"; |
| 1148 | + |
| 1149 | + $query=" |
| 1150 | + SELECT member_mid FROM $collection_contents |
| 1151 | + WHERE collection_id = $collection_id |
| 1152 | + AND internal_member_id=$concept_id |
| 1153 | + "; |
| 1154 | + wfDebug($query); |
| 1155 | + $queryResult = $dbr->query($query); |
| 1156 | + $row=$dbr->fetchObject($queryResult); |
| 1157 | + if (isset($row->member_mid)) { |
| 1158 | + wfDebug("do we have ".$row->member_mid."\n"); |
| 1159 | + $map[$dc]=$row->member_mid; |
| 1160 | + } |
| 1161 | + } |
| 1162 | + return $map; |
| 1163 | +} |
| 1164 | + |
| 1165 | +function getConceptId($dm,$dc){ |
| 1166 | + if(is_null($dc)) { |
| 1167 | + $dc=wdGetDataSetContext(); |
| 1168 | + } |
| 1169 | + $dbr = & wfGetDB(DB_SLAVE); |
| 1170 | + $dm=$dbr->addQuotes($dm); |
| 1171 | + $query = " |
| 1172 | + SELECT internal_member_id AS concept_id |
| 1173 | + FROM {$dc}_collection_contents |
| 1174 | + WHERE member_mid=$dm |
| 1175 | + "; |
| 1176 | + wfDebug($query); |
| 1177 | + $queryResult = $dbr->query($query); |
| 1178 | + $row=$dbr->fetchObject($queryResult); |
| 1179 | + return isset($row->concept_id) ? $row->concept_id : null; |
| 1180 | +} |
| 1181 | + |
| 1182 | +function &getAssociatedByConcept($dm, $dc) { |
| 1183 | + $dbr = & wfGetDB(DB_SLAVE); |
| 1184 | + $concept_id=getConceptId($dm,$dc); |
| 1185 | + wfDebug("concept id:".$concept_id."\n"); |
| 1186 | + return readConceptMapping($concept_id); |
| 1187 | +} |
| 1188 | + |
1060 | 1189 | ?> |
Index: trunk/extensions/Wikidata/OmegaWiki/SpecialConceptMapping.php |
— | — | @@ -0,0 +1,134 @@ |
| 2 | +<?php |
| 3 | +if (!defined('MEDIAWIKI')) die(); |
| 4 | +/** |
| 5 | + * A Special Page extension to add languages, runnable by users with the 'addlanguage' right. |
| 6 | + * @addtogroup Extensions |
| 7 | + * |
| 8 | + * @author Erik Moeller <Eloquence@gmail.com> |
| 9 | + * @author Kim Bruning <kim@bruning.xs4all.nl> |
| 10 | + * @license public domain |
| 11 | + */ |
| 12 | + |
| 13 | + |
| 14 | +$wgExtensionFunctions[] = 'wfSpecialConceptMapping'; |
| 15 | +$wgExtensionCredits['specialpage'][] = array( |
| 16 | + 'name' => 'foo', |
| 17 | + 'author' => 'Kim Bruning', |
| 18 | +); |
| 19 | + |
| 20 | +define(help_text, |
| 21 | + "<p>possible actions: <ul> |
| 22 | + <li>&action=insert&<data_context_prefix>=<defined_id>&... insert a mapping</li> |
| 23 | + <li>&action=get&concept=<concept_id> read a mapping back</li> |
| 24 | + <li>&action=list_sets return a list of possible data context prefixes and what they refer to.</li> |
| 25 | + <li>&action=get_associated&dm=<defined_meaning_id>&dc=<dataset_context_prefix> for one defined meaning in a concept, return all others</li> |
| 26 | + <li>&action=help Show helpful help.</li> |
| 27 | + </ul></p>"); #brief help message |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +function wfSpecialConceptMapping() { |
| 33 | + # Add messages |
| 34 | + #require_once "$IP/includes/SpecialPage.php"; |
| 35 | + |
| 36 | + require_once("Wikidata.php"); |
| 37 | + require_once("WikiDataAPI.php"); |
| 38 | + class SpecialConceptMapping extends SpecialPage { |
| 39 | + |
| 40 | + function SpecialConceptMapping() { |
| 41 | + SpecialPage::SpecialPage( 'ConceptMapping' ); |
| 42 | + } |
| 43 | + |
| 44 | + function execute( $par ) { |
| 45 | + global $wgOut, $wgRequest, $wgTitle, $wgUser; |
| 46 | + $wgOut->setPageTitle("ConceptMapping"); |
| 47 | + $action=$wgRequest->getText('action'); |
| 48 | + if(!$action) { |
| 49 | + $wgOut->addWikiText("You didn't set an action. Now kicking back and being lazy.<br>"); |
| 50 | + $wgOut->addWikiText(help_text); |
| 51 | + } elseif ($action=="insert"){ |
| 52 | + $this->insert(); |
| 53 | + } elseif ($action=="get"){ |
| 54 | + $this->get(); |
| 55 | + } elseif ($action=="list_sets"){ |
| 56 | + $this->list_sets(); |
| 57 | + } elseif ($action=="help"){ |
| 58 | + $this->help(); |
| 59 | + } elseif ($action=="get_associated"){ |
| 60 | + $this->get_associated(); |
| 61 | + } else { |
| 62 | + $wgOut->addWikiText("Apologies, I don't know how to '$action'."); |
| 63 | + $wgOut->addWikiText(help_text); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected function help() { |
| 68 | + global $wgOut; |
| 69 | + $wgOut->addWikiText("<h2>Help</h2>"); |
| 70 | + $wgOut->addWikiText(help_text); |
| 71 | + } |
| 72 | + |
| 73 | + protected function insert() { |
| 74 | + global |
| 75 | + $wgRequest, $wgOut; |
| 76 | + |
| 77 | + # $wgRequest->getText( 'page' ); |
| 78 | + $sets=wdGetDataSets(); |
| 79 | + #$requests=$wgRequest->getValues(); |
| 80 | + $wgOut->addWikiText("<h2>Will insert the following:</h2>"); |
| 81 | + $map=array(); |
| 82 | + foreach ($sets as $key => $set) { |
| 83 | + $dc=$set->getPrefix(); |
| 84 | + $dm_id=$wgRequest->getText($dc); |
| 85 | + $name=$set->fetchName(); |
| 86 | + |
| 87 | + $dm_id_ui=$dm_id; # Only for teh purdy |
| 88 | + if ($dm_id_ui==null) |
| 89 | + $dm_id_ui="unset"; |
| 90 | + $wgOut->addWikiText("$name ->$dm_id_ui"); |
| 91 | + $map[$dc]=$dm_id; |
| 92 | + #$dbr=&wfGetDB(DB_MASTER); |
| 93 | + } |
| 94 | + createConceptMapping($map); |
| 95 | + } |
| 96 | + |
| 97 | + protected function get() { |
| 98 | + global |
| 99 | + $wgOut, $wgRequest; |
| 100 | + $concept_id=$wgRequest->getText("concept"); |
| 101 | + $wgOut->addWikiText("<h2>Contents of mapping:</h2>"); |
| 102 | + $map=readConceptMapping($concept_id); |
| 103 | + #$sets=wdGetDataSets(); |
| 104 | + |
| 105 | + foreach ($map as $dc => $dm_id) { |
| 106 | + $wgOut->addWikiText("$dc -> $dm_id"); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + protected function list_sets() { |
| 111 | + global $wgOut; |
| 112 | + $wgOut->addWikiText("<h2>available contexts</h2>"); |
| 113 | + $sets=wdGetDataSets(); |
| 114 | + foreach ($sets as $key => $set) { |
| 115 | + $name=$set->fetchName(); |
| 116 | + $wgOut->addWikiText("$key => $name"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + protected function get_associated() { |
| 121 | + global $wgOut, $wgRequest; |
| 122 | + $dm_id=$wgRequest->getText("dm"); |
| 123 | + $dc=$wgRequest->getText("dc"); |
| 124 | + $map=getAssociatedByConcept($dm_id, $dc); |
| 125 | + foreach ($map as $dc => $dm_id) { |
| 126 | + $wgOut->addWikiText("$dc -> $dm_id"); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + SpecialPage::addPage( new SpecialConceptMapping ); |
| 133 | + |
| 134 | +} |
| 135 | +?> |
Index: trunk/extensions/Wikidata/OmegaWiki/Transaction.php |
— | — | @@ -266,11 +266,15 @@ |
267 | 267 | global |
268 | 268 | $updateTransactionId; |
269 | 269 | |
270 | | -function startNewTransaction($userID, $userIP, $comment) { |
| 270 | +function startNewTransaction($userID, $userIP, $comment, $dc=null) { |
271 | 271 | |
272 | 272 | global |
273 | 273 | $updateTransactionId; |
274 | | - $dc=wdGetDataSetContext(); |
| 274 | + |
| 275 | + if(is_null($dc)) { |
| 276 | + $dc=wdGetDataSetContext(); |
| 277 | + } |
| 278 | + |
275 | 279 | $dbr =& wfGetDB(DB_MASTER); |
276 | 280 | $timestamp = wfTimestampNow(); |
277 | 281 | |