-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmwwdprop.js
272 lines (238 loc) · 10.2 KB
/
mwwdprop.js
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const endpointUrl = 'https://www.wikidata.org/w/api.php';
function showMediaWikiQuery(fullurl, divId) {
let queryLink = document.getElementById(divId + "Query");
if (queryLink != null) {
let a = document.createElement("a");
a.setAttribute('href', fullurl);
let text = document.createTextNode("Run Query using Wikidata Mediawiki API. ");
a.appendChild(text);
queryLink.appendChild(a);
}
}
function queryMediaWiki(queryparams, func, divId, url) {
var div = document.getElementById(divId);
var fetchText = document.createElement("h4");
fetchText.innerHTML = "Fetching data...";
div.append(fetchText);
fullUrl = endpointUrl + '?action=' + queryparams + "&format=json";
showMediaWikiQuery(fullUrl, divId);
fetch(fullUrl, {}).then(body => body.json()).then(json => {
div.removeChild(fetchText);
func(divId, json, url)
});
}
function createDivLanguage(divId, json, url) {
xml = json.parse["parsetree"]["*"];
var languagesDiv = document.getElementById(divId);
var count = 0;
var regexp = /<name>(.+?)<\/name>/g;
var languages = document.createElement("div");
while (true) {
match = regexp.exec(xml);
if (match == null) {
break;
}
var languageText = match[1].replace(/\s/g, "")
if (languageText == "lang" || languageText == "#default" ||
languageText == "templatedata") {
continue;
}
count++;
var language = document.createElement("div");
language.setAttribute('class', "language");
var a = document.createElement("a");
a.setAttribute('href', url);
var text = document.createTextNode(languageText);
a.appendChild(text);
language.appendChild(a);
languages.appendChild(language);
}
var total = document.createElement("h4");
total.innerHTML = "Template translated in total " + count + " languages";
languagesDiv.appendChild(total);
languagesDiv.appendChild(languages);
}
function fetchWikidataPage(property, language) {
url = "https://www.wikidata.org/w/api.php?action=parse&page=Property:" +
property +
"&prop=wikitext&format=json&origin=*";
fetch(url, {}).then(body => body.json()).then(json => {
result = json.parse.wikitext["*"];
parsedResult = JSON.parse(result);
let aliases = [];
if (language in parsedResult.aliases) {
for (let i = 0; i < parsedResult.aliases[language].length; i++) {
aliases.push(parsedResult.aliases[language][i]["value"]);
}
}
aliasesString = aliases.join(", ");
link = document.getElementById("wikidatalabel");
link.setAttribute('href', "https://www.wikidata.org/entity/" + property)
link.innerHTML = parsedResult.labels[language]["value"];
link = document.getElementById("wikidatadescription");
link.setAttribute('href', "https://www.wikidata.org/entity/" + property)
link.innerHTML = parsedResult.descriptions[language]["value"];
link = document.getElementById("wikidataalias");
link.setAttribute('href', "https://www.wikidata.org/entity/" + property)
link.innerHTML = aliasesString;
link = document.getElementById("wikidatadatatype");
link.setAttribute('href', "https://www.wikidata.org/entity/" + property)
link.innerHTML = parsedResult.datatype;
link = document.getElementById("wikidatastatements");
link.setAttribute('href', "https://www.wikidata.org/entity/" + property)
link.innerHTML = String(Object.keys(parsedResult.claims).length);
link = document.getElementById("wikidataconstraints");
link.setAttribute('href', "https://www.wikidata.org/entity/" + property)
if ('P2302' in parsedResult.claims) {
link.innerHTML = String(Object.keys(parsedResult.claims['P2302']).length);
}
});
}
function getTemplateTranslationStatistics() {
var queryparams = "parse&page=Template:Support&prop=parsetree&origin=*";
queryMediaWiki(queryparams, createDivLanguage,
"translatedTemplateSupport",
"https://www.wikidata.org/wiki/Template:Support");
var queryparams = "parse&page=Template:Oppose&prop=parsetree&origin=*";
queryMediaWiki(queryparams, createDivLanguage,
"translatedTemplateOppose",
"https://www.wikidata.org/wiki/Template:Oppose");
var queryparams = "parse&page=Template:Neutral&prop=parsetree&origin=*";
queryMediaWiki(queryparams, createDivLanguage,
"translatedTemplateNeutral",
"https://www.wikidata.org/wiki/Template:Neutral");
var queryparams = "parse&page=Template:Comment&prop=parsetree&origin=*";
queryMediaWiki(queryparams, createDivLanguage,
"translatedTemplateComment",
"https://www.wikidata.org/wiki/Template:Comment");
}
function createDivWikprojectsWithProperty(divId, json) {
let wikiprojects = document.getElementById(divId);
let total = document.createElement("h3");
let count = 0;
let table = document.createElement("table");
table.setAttribute("class", "alternate");
let th = document.createElement("tr");
let td = document.createElement("th");
td.innerHTML = "Projects";
th.appendChild(td);
table.appendChild(th);
td = document.createElement("th");
td.innerHTML = "Link";
th.appendChild(td);
table.appendChild(th);
let tr = "";
for (const page of Object.keys(json.query.search)) {
if (!json.query.search[page]["title"].startsWith("Wikidata:WikiProject")) {
continue;
}
tr = document.createElement("tr");
td = document.createElement("td");
let a = document.createElement("a");
a.setAttribute('href', "https://www.wikidata.org/wiki/" + json.query.search[page]["title"]);
let title = json.query.search[page]["title"].replace("Wikidata:WikiProject", "");
let text = document.createTextNode(title);
a.appendChild(text);
td.appendChild(a);
tr.appendChild(td);
td = document.createElement("td");
let wdproject = document.createElement("a");
let link = "wikiproject.html?project=" + json.query.search[page]["title"];
wdproject.setAttribute('href', link);
text = document.createTextNode(link);
wdproject.appendChild(text);
td.appendChild(wdproject);
tr.appendChild(td);
table.appendChild(tr);
}
wikiprojects.append(table);
}
function updateCreationDate(property, language) {
url = "https://www.wikidata.org/w/api.php?action=query&prop=revisions&titles=Property:" +
property +
"&rvlimit=1&rvprop=timestamp&rvdir=newer" +
"&origin=*&format=json";
fetch(url, {}).then(body => body.json()).then(json => {
let creationDate = document.getElementById("wikidatapropertycreationdate");
creationDate.innerHTML = json.query.pages[Object.keys(json.query.pages)[0]].revisions[0]["timestamp"];
});
}
function updateModificationDate(property, language) {
url = "https://www.wikidata.org/w/api.php?action=query&prop=revisions&titles=Property:" +
property +
"&rvlimit=1&rvprop=timestamp&rvdir=older" +
"&origin=*&format=json";
fetch(url, {}).then(body => body.json()).then(json => {
let modificationDate = document.getElementById("wikidatapropertylastmodified");
modificationDate.innerHTML = json.query.pages[Object.keys(json.query.pages)[0]].revisions[0]["timestamp"];
});
}
function createDivWikprojectProperties(divId, json) {
let properties = document.getElementById(divId);
let total = document.createElement("h3");
let count = 0;
let wdproperties = [];
for (const page of Object.keys(json.query.pages)) {
for (const result of json.query.pages[page].links) {
if (!result.title.startsWith("Property:")) {
continue;
}
let text = result.title.replace("Property", "wd");
wdproperties = wdproperties + " " + text + " ";
count++;
}
}
total.innerHTML = "Total " + count + " properties";
addDivPropertyLabels(divId, wdproperties);
}
function createDivPropertyList(divId, json, url) {
var properties = document.getElementById(divId);
var total = document.createElement("h3");
var count = 0;
properties.appendChild(total);
for (const page of Object.keys(json.query.pages)) {
var div = document.getElementById("WikiProject");
div.innerHTML = json.query.pages[page].title;
for (const result of json.query.pages[page].links) {
if (result.title.indexOf("Property:") !== -1 && result.title !== "Property:P") {
var property = document.createElement("div");
property.setAttribute('class', "property");
var a = document.createElement("a");
propertyid = result.title.replace("Property:", "");
a.setAttribute('href', "property.html?property=" + propertyid);
var text = document.createTextNode(propertyid);
a.appendChild(text);
property.appendChild(a);
properties.appendChild(property);
count++;
}
}
}
total.innerHTML = "Total " + count + " properties";
}
function showWikiProjectProperties(project, divId) {
var queryparams = "query&prop=links&pllimit=500&origin=*&titles=" + project;
queryMediaWiki(queryparams, createDivWikprojectProperties,
divId,
"");
}
function showWikiProjectsWithProperty(property, divId) {
var queryparams = "query&list=search&origin=*&srnamespace=4&srsearch=Wikidata:WikiProject%20haswbstatement=" +
property + "&srlimit=500&format=json";
queryMediaWiki(queryparams, createDivWikprojectsWithProperty,
divId,
"");
}
function showWikiProjectOnLoad() {
limit = 500;
offset = 500;
var project = 'Wikidata:WikiProject Properties';
if (window.location.search.length > 0) {
var reg = new RegExp("project=([^&#=]*)");
var value = reg.exec(window.location.search);
if (value != null) {
project = decodeURIComponent(value[1]);
}
}
showWikiProjectProperties(project, "allProperties");
}