User:Lectrician1/qualifier-constraint-usage.js
Jump to navigation
Jump to search
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.
// Shows what properies this qualifier is a required and allowed qualifier on at the bottom of the qualifier page
class List {
constructor(query, id, headings) {
this.query = query
this.id = id
this.headings = headings
}
}
mw.hook("wikibase.entityPage.entityLoaded").add(async function(e) {
if (e.type == "property") {
var userLang = mw.config.get('wgContentLanguage')
// From User:Nikki/ExMusica.js
let langs = [...new Set([
// If the UI language is English, the chain is only ["en"] and
// using slice() to remove the last array element will result in
// an empty array. To avoid that, we use shift() to get the first
// element and slice() to get anything between the first and last
// elements separately.
mw.language.getFallbackLanguageChain().shift(),
mw.language.getFallbackLanguageChain().slice(1, -1),
mw.config.get("wgULSBabelLanguages") || [],
mw.config.get("wgULSAcceptLanguageList") || [],
"en"
].flat().map(function(x) {
return x.toLowerCase();
}))]
function getQuery(propertyConstraintID) {
return `SELECT ?statement ?propertyLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "${langs.join(",")}, en". }
?property p:P2302 ?statement.
?statement ps:P2302 wd:${propertyConstraintID}.
?statement pq:P2306 wd:${e.id}.
}`
}
let selectorToAppendListsTo = '.wikibase-entityview-main'
let listsID = 'constraints-list'
$(selectorToAppendListsTo).after(`
<div id="${listsID}"></div>`)
let lists = [
new List(getQuery('Q21510856'), 'requiredQualifier', {
en: 'Properties this property is required as a qualifier on:'
}),
new List(getQuery('Q21510851'), 'allowedQualifier', {
en: 'Properties this property is allowed as a qualifier on:'
})
]
for (let list of lists) {
let listResults = await $.post("https://query.wikidata.org/sparql?format=json", {
query: list.query
})
$(`#${listsID}`).append(`
<div id="${list.id}">
<ul></ul>
</div>`)
for (let lang of langs) {
if (lang in list.headings) {
$(`#${list.id}`).prepend(`<h3>${list.headings[lang]}</h3>`)
break
}
}
for (let result of listResults.results.bindings) {
let statementLink = mw.html.escape(result.statement.value)
$(`#${list.id} > ul`).append(`
<li><a href="${statementLink}">${result.propertyLabel.value}</a></li>
`)
}
}
}
})