Index: trunk/extensions/Wikidata/App.php |
— | — | @@ -46,7 +46,7 @@ |
47 | 47 | # The site prefix allows us to have multiple sets of customized |
48 | 48 | # messages (for different, typically site-specific UIs) |
49 | 49 | # in a single database. |
50 | | -$wdSiteContext="ow"; |
| 50 | +if(!isset($wdSiteContext)) $wdSiteContext="ow"; |
51 | 51 | |
52 | 52 | require_once("{$IP}/extensions/Wikidata/AddPrefs.php"); |
53 | 53 | require_once("{$IP}/extensions/Wikidata/SpecialLanguages.php"); |
— | — | @@ -60,6 +60,7 @@ |
61 | 61 | require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialConceptMapping.php"); |
62 | 62 | require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialCopy.php"); |
63 | 63 | require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialExportTSV.php"); |
| 64 | +require_once("{$IP}/extensions/Wikidata/OmegaWiki/SpecialImportTSV.php"); |
64 | 65 | require_once("{$IP}/extensions/Wikidata/LocalApp.php"); |
65 | 66 | |
66 | 67 | function addWikidataHeader() { |
Index: trunk/extensions/Wikidata/OmegaWiki/forms.php |
— | — | @@ -57,6 +57,16 @@ |
58 | 58 | return $result . '</select>'; |
59 | 59 | } |
60 | 60 | |
| 61 | +function getFileField($name, $onChangeHandler = "") { |
| 62 | + if ($onChangeHandler != "") |
| 63 | + $onChangeAttribute = ' onchange="'. $onChangeHandler . '"'; |
| 64 | + else |
| 65 | + $onChangeAttribute = ''; |
| 66 | + |
| 67 | + return '<input type="file" id="'. $name .'" name="'. $name . '"' . $onChangeAttribute . ' style="width: 100%; padding: 0px; margin: 0px;"/>'; |
| 68 | +} |
| 69 | + |
| 70 | + |
61 | 71 | /** |
62 | 72 | * |
63 | 73 | * Returns HTML for an autocompleted form field. |
— | — | @@ -198,3 +208,33 @@ |
199 | 209 | return $result; |
200 | 210 | } |
201 | 211 | |
| 212 | +function getOptionPanelForFileUpload($fields, $action = '', $buttons = array("upload" => "Upload")) { |
| 213 | + global |
| 214 | + $wgTitle; |
| 215 | + |
| 216 | + $result = |
| 217 | + '<div class="option-panel">' . |
| 218 | + '<form method="POST" enctype="multipart/form-data" action="">' . |
| 219 | + '<table cellpadding="0" cellspacing="0">' . |
| 220 | + '<input type="hidden" name="title" value="' . $wgTitle->getNsText() . ':' . htmlspecialchars($wgTitle->getText()) . '"/>'; |
| 221 | + |
| 222 | + if ($action && $action != '') |
| 223 | + $result .= '<input type="hidden" name="action" value="' . $action . '"/>'; |
| 224 | + |
| 225 | + foreach($fields as $caption => $field) |
| 226 | + $result .= '<tr><th>' . $caption . ':</th><td class="option-field">' . $field . '</td></tr>'; |
| 227 | + |
| 228 | + $buttonHTML = ""; |
| 229 | + |
| 230 | + foreach ($buttons as $name => $caption) |
| 231 | + $buttonHTML .= getSubmitButton($name, $caption); |
| 232 | + |
| 233 | + $result .= |
| 234 | + '<tr><th/><td>' . $buttonHTML . '</td></tr>' . |
| 235 | + '</table>' . |
| 236 | + '</form>' . |
| 237 | + '</div>'; |
| 238 | + |
| 239 | + return $result; |
| 240 | +} |
| 241 | + |
Index: trunk/extensions/Wikidata/OmegaWiki/languages.php |
— | — | @@ -45,6 +45,24 @@ |
46 | 46 | |
47 | 47 | } |
48 | 48 | |
| 49 | +function getLanguageIdForIso639_3($code) { |
| 50 | + |
| 51 | + static $languages=null; |
| 52 | + if(is_null($languages)) { |
| 53 | + $dbr =& wfGetDB( DB_SLAVE ); |
| 54 | + $id_res=$dbr->query("select language_id,iso639_3 from language"); |
| 55 | + while($id_row=$dbr->fetchObject($id_res)) { |
| 56 | + $languages[$id_row->iso639_3]=$id_row->language_id; |
| 57 | + } |
| 58 | + } |
| 59 | + if(is_array($languages) && array_key_exists($code,$languages)) { |
| 60 | + return $languages[$code]; |
| 61 | + } else { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
49 | 67 | /* Return SQL query string for fetching language names. */ |
50 | 68 | function getSQLForLanguageNames($lang_code) { |
51 | 69 | /* Use a simpler query if the user's language is English. */ |
Index: trunk/extensions/Wikidata/OmegaWiki/SpecialImportTSV.php |
— | — | @@ -0,0 +1,179 @@ |
| 2 | +<?php |
| 3 | + if (!defined('MEDIAWIKI')) die(); |
| 4 | + |
| 5 | + require_once("WikiDataAPI.php"); // for bootstrapCollection |
| 6 | + require_once("Utilities.php"); |
| 7 | + |
| 8 | + $wgAvailableRights[] = 'importtsv'; |
| 9 | + $wgGroupPermissions['bureaucrat']['importtsv'] = true; |
| 10 | + $wgExtensionFunctions[] = 'wfSpecialImportTSV'; |
| 11 | + |
| 12 | + function wfSpecialImportTSV() { |
| 13 | + global $wgMessageCache; |
| 14 | + $wgMessageCache->addMessages(array('importtsv'=>'Wikidata: Import TSV'),'en'); |
| 15 | + |
| 16 | + class SpecialImportTSV extends SpecialPage { |
| 17 | + |
| 18 | + function SpecialImportTSV() { |
| 19 | + SpecialPage::SpecialPage('ImportTSV'); |
| 20 | + } |
| 21 | + |
| 22 | + function execute($par) { |
| 23 | + |
| 24 | + global $wgOut, $wgUser, $wgRequest; |
| 25 | + |
| 26 | + if (!$wgUser->isAllowed('importtsv')) { |
| 27 | + $wgOut->addHTML('You do not have permission to do a tsv import.'); |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + $dbr =& wfGetDB(DB_MASTER); |
| 32 | + $dc = wdGetDataSetcontext(); |
| 33 | + |
| 34 | + if ($wgRequest->getFileName('tsvfile')) { |
| 35 | + // process tsv |
| 36 | + |
| 37 | + require_once('WikiDataAPI.php'); |
| 38 | + require_once('Transaction.php'); |
| 39 | + $file = fopen($wgRequest->getFileTempname('tsvfile'),'r'); |
| 40 | + $columns = fgetcsv($file, 1024, "\t"); |
| 41 | + |
| 42 | + // somehow testing for $columns[0] fails sometimes. Byte Order Mark? |
| 43 | +// if (!$columns || count($columns) <= 2 || $columns[1] != "defining expression") { |
| 44 | +// $wgOut->addHTML("<p>This does not appear to be a valid tsv file.</p>"); |
| 45 | +// return; |
| 46 | +// } |
| 47 | + |
| 48 | + // TODO verify column names |
| 49 | + // TODO verify the max line length |
| 50 | + |
| 51 | + startNewTransaction($wgUser->getID(), wfGetIP(), "Bulk import via SpecialImportTSV", $dc); |
| 52 | + |
| 53 | + $row = ""; |
| 54 | + $line = 1; // actually 2, 1 was the header, but increased at the start of while |
| 55 | + $definitions = 0; // definitions added |
| 56 | + $translations = 0; // translations added |
| 57 | + |
| 58 | + while($row = fgetcsv($file, 8196, "\t")) { |
| 59 | + $line++; |
| 60 | + if (count($row) != count($columns)) { |
| 61 | + $wgOut->addHTML("Skipped line $line: incorrect column count.<br/>"); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $dmid = $row[0]; |
| 66 | + $exp = $row[1]; |
| 67 | + |
| 68 | + // find the defined meaning record |
| 69 | + $qry = "SELECT dm.meaning_text_tcid, exp.spelling, dm.remove_transaction_id "; |
| 70 | + $qry .= "FROM {$dc}_defined_meaning dm INNER JOIN {$dc}_expression exp ON dm.expression_id=exp.expression_id "; |
| 71 | + $qry .= "WHERE dm.defined_meaning_id=$dmid"; |
| 72 | + |
| 73 | + $dmResult = $dbr->query($qry); |
| 74 | + $dmRecord = null; |
| 75 | + // perfomr some tests |
| 76 | + if ($dmRecord = $dbr->fetchRow($dmResult)) { |
| 77 | + if ($dmRecord['spelling'] != $exp) { |
| 78 | + $wgOut->addHTML("Skipped line $line: defined meaning id $dmid does not match defining expression. Should be '$exp', found '{$dmRecord['spelling']}'.<br/>"); |
| 79 | + continue; |
| 80 | + } |
| 81 | + if ($dmRecord['remove_transaction_id']) { |
| 82 | + $wgOut->addHTML("Skipped line $line: defined meaning has been removed.<br/>"); |
| 83 | + continue; |
| 84 | + } |
| 85 | + } |
| 86 | + else { |
| 87 | + $wgOut->addHTML("Skipped line $line: incorrect defined meaning id $dmid.<br/>"); |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + // all is well. Get the translated content id |
| 93 | + $tcid = $dmRecord['meaning_text_tcid']; |
| 94 | + |
| 95 | + |
| 96 | + for ($columnIndex = 2; $columnIndex < count($columns); $columnIndex++) { |
| 97 | + |
| 98 | + $columnValue = $row[$columnIndex]; |
| 99 | + if (!$columnValue) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + $columnName = $columns[$columnIndex]; |
| 104 | + $langCode = substr($columnName, strrpos($columnName, '_')+1); |
| 105 | + $langId = getLanguageIdForIso639_3($langCode); |
| 106 | + if (strpos($columnName, 'definition') === 0) { |
| 107 | + if (!translatedTextExists($tcid, $langId)) { |
| 108 | + addTranslatedText($tcid, $langId, $columnValue); |
| 109 | + $wgOut->addHTML("Added definition for $exp ($dmid) in $langCode: $columnValue.<br/>"); |
| 110 | + $definitions++; |
| 111 | + } |
| 112 | + } |
| 113 | + if (strpos($columnName, 'translation') === 0) { |
| 114 | + $spellings = explode('|', $columnValue); |
| 115 | + foreach ($spellings as $spelling) { |
| 116 | + $spelling = trim($spelling); |
| 117 | + $expression = findExpression($spelling, $langId); |
| 118 | + if (!$expression) { // expression does not exist |
| 119 | + $expression = createExpression($spelling, $langId); |
| 120 | + $expression->bindToDefinedMeaning($dmid, 1); |
| 121 | + |
| 122 | + // not nescesary to check page exists, createPage does that. |
| 123 | + $title = getPageTitle($spelling); |
| 124 | + createPage(16, $title); |
| 125 | + |
| 126 | + $wgOut->addHTML("Added translation for $exp ($dmid) in $langCode: $spelling. Also added a new page.<br/>"); |
| 127 | + $translations++; |
| 128 | + } |
| 129 | + else { // expression exists, but may not be bound to this defined meaning. |
| 130 | + if (!$expression->isBoundToDefinedMeaning($dmid)) { |
| 131 | + $expression->bindToDefinedMeaning($dmid, 1); |
| 132 | + |
| 133 | + $wgOut->addHTML("Added translation for $exp ($dmid) in $langCode: $spelling.<br/>"); |
| 134 | + $translations++; |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + if ($definitions == 0 && $translations == 0) { |
| 144 | + $wgOut->addHTML("<br/>Nothing added.<br/>"); |
| 145 | + } |
| 146 | + else { |
| 147 | + $wgOut->addHTML("<br/>Added $definitions definitions and $translations translations.<br/>"); |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + else { |
| 152 | + // render the page |
| 153 | + $wgOut->setPageTitle('Import a collection from tsv (tab separated values)'); |
| 154 | +// $wgOut->addHTML('<p>Export a collection to a tab delimited text format that you can import in OpenOffice.org, Excel or other spreadsheet software.<br/>'); |
| 155 | +// $wgOut->addHTML('The collection is fixed right now, do not change it. In the languages text box, enter a comma separated list of '); |
| 156 | +// $wgOut->addHTML('ISO 639-3 languages codes. Start with the languages that you will be translating from (pick as many as you like) and '); |
| 157 | +// $wgOut->addHTML('finish with the ones you\'ll be translating to.</p>'); |
| 158 | + |
| 159 | + $wgOut->addHTML(getOptionPanelForFileUpload( |
| 160 | + array( |
| 161 | + 'TSV File' => getFileField('tsvfile') |
| 162 | + ), |
| 163 | + '',array('upload' => 'Upload') |
| 164 | + )); |
| 165 | + } |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | + /* HELPER METHODS START HERE */ |
| 171 | + |
| 172 | + function getLanguage($columnName) { |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + SpecialPage::addPage(new SpecialImportTSV); |
| 179 | + } |
| 180 | + |
Property changes on: trunk/extensions/Wikidata/OmegaWiki/SpecialImportTSV.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 181 | + native |