-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwir_randomized_user_script.js
More file actions
136 lines (116 loc) · 3.48 KB
/
Copy pathwir_randomized_user_script.js
File metadata and controls
136 lines (116 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Script: Women in Red randomizer tool item
*
*/
messages = {
'en': {
'wir-title': 'Hello!',
'wir-greeting': 'Welcome, $1!',
'wir-info': 'The following items have no page yet, you can create them:',
'wir-link': 'List of Women in Red',
'wir-tooltip': 'Shows Women in Red Lists'
},
'tr': {
'wir-title': 'Merhaba!',
'wir-greeting': 'Hoş geldin, $1!',
'wir-info': 'Aşağıdaki öğelerin henüz sayfası yok, sen oluşturabilirsin:',
'wir-link': 'Kırmızılı Kadınlar Listesi',
'wir-tooltip': 'Sayfası eksik olan kadın biyografilerini gösterir'
}
};
mw.messages.set(messages['en']);
var lang = mw.config.get('wgUserLanguage');
if (lang && lang != 'en' && lang in messages) {
mw.messages.set(messages[lang]);
}
// Import the jQuery dialog plugin before starting the rest of this script
mw.loader.using(['jquery.ui'], function () {
const sparqlEndpoint = 'https://query.wikidata.org/sparql';
// SPARQL query
const sparqlQuery = `
SELECT ?item ?itemLabel ?linkcount WHERE {
?item wdt:P31 wd:Q5 .
?item wdt:P21 wd:Q6581072 .
?item wdt:P106 wd:Q5716684 .
?item wikibase:sitelinks ?linkcount .
FILTER (?linkcount >= 1) . # only include items with 1 or more sitelinks
FILTER NOT EXISTS {
?article schema:about ?item .
?article schema:inLanguage "tr" .
?article schema:isPartOf <https://tr.wikipedia.org/>
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de,es,ar,fr" }
}
ORDER BY DESC(?linkcount)
LIMIT 300
`;
function renderQuickRCDialog(pageLinks) {
var $dialog = $('<div></div>')
.html(
'<strong>' +
mw.message('wir-greeting', mw.user.getName()).escaped() +
'</strong> ' +
mw.message('wir-info').escaped() +
'<br/><ul><li>' +
pageLinks.join('<br /><li>') + '</ul>'
)
.dialog({
autoOpen: true,
title: mw.message('wir-title').plain(),
width: '70%',
modal: true
});
}
// Function to execute the SPARQL query
function executeSPARQLQuery(query) {
const url = sparqlEndpoint + '?query=' + encodeURIComponent(query);
const headers = {
'Accept': 'application/sparql-results+json'
};
fetch(url, { headers })
.then(response => response.json())
.then(data => handleSparqlResult(data))
.catch(error => console.error('Error executing SPARQL query:', error));
}
// Function to handle the SPARQL result
function handleSparqlResult(data) {
var myPageLinks = [];
var myTitles = [];
const results = data.results.bindings;
console.log('SPARQL query results:', results);
// Process and display the results as needed
results.forEach(result => {
const itemLink = result.item.value;
const nameSurname = result.itemLabel.value;
console.log(`Item: ${itemLink}, Label: ${nameSurname}`);
myPageLinks.push(
mw.html.element(
'a', { href: mw.util.getUrl( itemLink ) }, nameSurname
)
);
myTitles.push(nameSurname);
});
renderQuickRCDialog(myPageLinks);
}
$(document).ready(function () {
// Add a link to the toolbox
var link = mw.util.addPortletLink(
'p-tb',
'#',
mw.message('wir-link').plain(),
't-prettylinkwidget',
mw.message('wir-tooltip').plain(),
'/',
'#t-whatlinkshere'
);
// Create a jQuery object for this link so that we get
// to use jQuery awesomeness like .click() for binding functions to events
// and methods like e.preventDefault();
$(link).click(function (e) {
// Avoid the browser going to '#'
e.preventDefault();
// generate women in red list!
executeSPARQLQuery(sparqlQuery);
});
});
});