User:Lectrician1/SenseForThisItem.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/*
** Name: SenseForThisItem.js
** Description: Adds a button on items to show what senses are linked to it.
** Author: Lectrician1
** License: CC0
*/
async function run(e) {
if (e.type !== "item")
return;
$('.wikibase-entitytermsview-heading').append(`
<div id="showSenses"><a>Get senses for this item</a></div>
`)
await $('#showSenses').one('click', async function () {
var results = await $.post("https://query.wikidata.org/sparql?format=json", {
query: `select ?lexeme ?sense ?gloss (LANG(?gloss) as ?glosslang) ?lemma ?language ?languageLabel ?category ?categoryLabel
with {
SELECT ?lexeme ?sense ?gloss_ (sample(?gloss3_) as ?gloss2_) (group_concat(distinct ?lemma_; separator=" / ") as ?lemma) ?language ?category WHERE {
?sense wdt:P5137|wdt:P9970 wd:${e.title}.
?lexeme ontolex:sense ?sense;
wikibase:lemma ?lemma_;
dct:language ?language;
wikibase:lexicalCategory ?category.
OPTIONAL {
?sense skos:definition ?gloss_ .
FILTER(LANG(?gloss_) = "${mw.config.get('wgContentLanguage')}")
}
OPTIONAL { ?sense skos:definition ?gloss3_ }
} group by ?lexeme ?sense ?gloss_ ?language ?category }
as %glosses where {
include %glosses .
BIND(COALESCE(?gloss_,?gloss2_) as ?gloss)
SERVICE wikibase:label { bd:serviceParam wikibase:language "${mw.config.get('wgContentLanguage')}". }
} order by ?languageLabel ?lemma ?categoryLabel ?gloss ` }).then()
if (results.results.bindings.length != 0) {
$('#showSenses').after(`<table id="senses">
<tr>
<th>Language</th>
<th>Lemma</th>
<th>Category</th>
<th>Gloss (link to sense)</th>
<tr>
</table>`)
results.results.bindings.sort(function (x, y) { return x.gloss ? -1 : y.gloss ? 1 : 0; });
for (result of results.results.bindings) {
$('#senses').append(`
<tr>
<td style="text-align:center;width:20%"><a href="${result.language.value}">${result.languageLabel.value}</a></td>
<td style="text-align:center;overflow-wrap:anywhere;width:30%"><a href="${result.lexeme.value}">${result.lemma.value}</a></td>
<td style="text-align:center;width:10%"><a href="${result.category.value}">${result.categoryLabel.value}</a></td>
<td style="text-align:center;width:40%"><a href="${result.sense.value}">${result.gloss.value} (${result.glosslang.value})</a></td>
<tr>
`)
}
}
else $('#showSenses').after('<div id="senses">There are no senses linked to this item.')
$('#showSenses').on('click', function () {
$('#senses').toggle();
});
});
}
mw.hook("wikibase.entityPage.entityLoaded").add(function (e) {
run(e)
})