-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
361 lines (318 loc) · 14.1 KB
/
index.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Global User Contributions</title>
<!--
- favicon
-->
<link rel="shortcut icon" href="./assets/images/favicon.ico" type="image/x-icon">
<!--
- custom css link
-->
<link rel="stylesheet" href="./assets/css/style.css">
</head>
<body>
<main>
<h1 class="header">Global Wiki Contributions</h1>
<form id="editCountsForm">
<input type="text" id="username" name="username" placeholder="Wiki Username" required>
<label for="namespace">Namespace:</label>
<select id="namespace" name="namespace">
<option value="all">All</option>
<option value="0">Main</option>
<option value="1">Talk</option>
<option value="2">User</option>
<option value="3">User Talk</option>
<option value="4">Project</option>
<option value="5">Project Talk</option>
<option value="6">File</option>
<option value="7">File Talk</option>
<option value="8">MediaWiki</option>
<option value="9">MediaWiki Talk</option>
<option value="10">Template</option>
<option value="11">Template Talk</option>
<option value="12">Help</option>
<option value="13">Help Talk</option>
<option value="14">Category</option>
<option value="15">Category Talk</option>
<option value="100">Portal</option>
<option value="101">Portal Talk</option>
<option value="118">Draft</option>
<option value="119">Draft Talk</option>
<option value="120">Property</option>
<option value="121">Property Talk</option>
<option value="120">Lexeme</option>
<option value="121">Lexeme Talk</option>
<option value="460">Campaign</option>
<option value="461">Campaign Talk</option>
<option value="486">Data</option>
<option value="487">Data Talk</option>
<option value="828">Module</option>
<option value="829">Module Talk</option>
<option value="1198">Translations</option>
<option value="1199">Translations Talk</option>
</select>
<div class="date-inputs">
<label for="startDate">Start Date:</label>
<input type="date" id="startDate" name="startDate" required>
<label for="endDate">End Date:</label>
<input type="date" id="endDate" name="endDate" required>
</div>
<button type="submit">Get Contributions</button>
<button id="downloadCSVButton" type="button" style="display: none;">Download CSV</button>
</form>
<div id="apiRequestsInfoContainer"></div>
<table id="editCounts" style="display: none;">
<thead>
<tr id="usernameHeaderRow">
</tr>
<tr>
<th>Wiki Project</th>
<th>Live Edit Count</th>
<th>Deleted Edit Count</th>
<th>User Rights</th>
</tr>
</thead>
<tbody id="editCountsBody"></tbody>
<tr id="footerRow">
</table>
<p id="noUsernameMessage" style="display: none; color: red;">Username not found</p>
<div id="noteContainer" style="display: none; color: green;">
<p id="projectNote" style="display: none; color: red;"></p>
</div>
</main>
<script>
let totalApiRequests = 0;
let successfulApiRequests = 0;
let allApiRequestsInitiated = false;
// Function to fetch user global info from MediaWiki API
async function fetchUserGlobalInfo(username) {
const apiUrl = `https://www.mediawiki.org/w/api.php?action=query&format=json&list=&meta=globaluserinfo&utf8=1&formatversion=2&guiuser=${username}&guiprop=editcount|rights|merged|groups|unattached&origin=*`;
try {
const response = await fetch(apiUrl);
if (response.ok) {
updateApiRequestsInfo();
}
return await response.json();
} catch (error) {
console.error('Error fetching user global info:', error);
return null;
}
}
// Function to fetch edit counts from XTools API
async function fetchEditCounts(wikiUrl, username, namespace, startDate, endDate) {
const apiUrl = `https://xtools.wmcloud.org/api/user/simple_editcount/${wikiUrl}/${username}/${getNamespaceParam(namespace)}/${startDate}/${endDate}`;
totalApiRequests++;
try {
const response = await fetch(apiUrl);
if (response.ok) {
successfulApiRequests++;
updateApiRequestsInfo();
}
return await response.json();
} catch (error) {
console.error('Error fetching edit counts:', error);
return null;
}
}
// Function to get user edit counts
async function getUserEditCounts(username, namespace, startDate, endDate) {
const globalUserInfo = await fetchUserGlobalInfo(username);
if (!globalUserInfo || (globalUserInfo.query.globaluserinfo && globalUserInfo.query.globaluserinfo.missing)) {
document.getElementById('noUsernameMessage').style.display = 'block';
return [];
}
const editCountsPromises = globalUserInfo.query.globaluserinfo.merged
.filter(contribution => contribution.editcount > 0)
.map(async contribution => {
const wikiUrl = contribution.url.replace('https://', '').replace(/\/$/, '');
const editCounts = await fetchEditCounts(wikiUrl, username, namespace, startDate, endDate);
return {
wiki: wikiUrl,
liveEditCount: editCounts.live_edit_count,
deletedEditCount: editCounts.deleted_edit_count,
userGroups: editCounts.user_groups || [],
warning: editCounts.warning
};
});
const editCounts = await Promise.all(editCountsPromises);
return editCounts.filter(counts => counts !== null);
}
// Function to get namespace parameter
function getNamespaceParam(namespace) {
if (namespace === 'all' || namespace === null) {
return 'all';
} else {
return `${namespace}`;
}
}
// Function to display user edit counts
async function displayUserEditCounts(username, namespace, startDate, endDate) {
console.log('Displaying user edit counts for:', username, namespace, startDate, endDate);
const editCounts = await getUserEditCounts(username, namespace, startDate, endDate);
console.log('Edit counts:', editCounts);
const editCountsBody = document.getElementById('editCountsBody');
const noteContainer = document.getElementById('noteContainer');
const projectNote = document.getElementById('projectNote');
const usernameHeaderRow = document.getElementById('usernameHeaderRow');
const footerRow = document.getElementById('footerRow');
usernameHeaderRow.innerHTML = `<th colspan="4">[[ User:${username} ]]</th>`;
footerRow.innerHTML = `<th colspan="4" style="font-size: 12px; color: #808080; background-color: #f2f2f2;">The data is sourced from <a href="https://www.mediawiki.org/w/api.php">MediaWiki API</a> and <a href="https://xtools.wmcloud.org/api">Xtools API</th>`;
if (editCountsBody) {
editCountsBody.innerHTML = '';
}
if (noteContainer) {
noteContainer.innerHTML = '';
}
if (projectNote) {
projectNote.innerHTML = '';
}
if (editCounts.length === 0) {
// No contributions found
console.log('No contributions found');
document.getElementById('noContributionsMessage').style.display = 'block';
document.getElementById('editCounts').style.display = 'none'; // Hide the table
return;
}
let totalLiveEditCount = 0;
let totalDeletedEditCount = 0;
let hasHighEditCount = false;
let highEditCountProjects = [];
editCounts.forEach(counts => {
totalLiveEditCount += counts.liveEditCount;
totalDeletedEditCount += counts.deletedEditCount;
if (counts.liveEditCount > 0 || counts.deletedEditCount > 0) {
const row = document.createElement('tr');
const wikiURL = `https://${counts.wiki}/w/index.php?title=Special%3AContributions&target=${username}&namespace=${namespace}&tagfilter=&start=${startDate}&end=${endDate}&limit=500`;
const liveLink = document.createElement('a');
liveLink.setAttribute('href', wikiURL);
liveLink.setAttribute('target', '_blank');
liveLink.textContent = counts.liveEditCount;
liveLink.style.fontWeight = 'bold';
// Create a comma-separated list of user groups
const userGroupsText = counts.userGroups.join(', ');
const cellWikiURL = document.createElement('td');
const cellLiveEditCount = document.createElement('td');
const cellDeletedEditCount = document.createElement('td');
const cellUserGroups = document.createElement('td')
cellDeletedEditCount.textContent = counts.deletedEditCount;
cellWikiURL.textContent = counts.wiki;
cellLiveEditCount.appendChild(liveLink);
cellUserGroups.textContent = userGroupsText;
row.appendChild(cellWikiURL);
row.appendChild(cellLiveEditCount);
row.appendChild(cellDeletedEditCount);
row.appendChild(cellUserGroups);
editCountsBody.appendChild(row);
document.getElementById('editCounts').style.display = 'table';
}
if (counts.warning) {
hasHighEditCount = true;
highEditCountProjects.push(counts.wiki);
}
});
// Add total row if there are any rows displayed
if (editCountsBody.children.length > 0) {
const totalRow = document.createElement('tr');
totalRow.innerHTML = `
<td><strong>Total</strong></td>
<td>${totalLiveEditCount}</td>
<td>${totalDeletedEditCount}</td>
`;
editCountsBody.appendChild(totalRow);
}
// Display the note container if needed
if (hasHighEditCount || totalLiveEditCount === 0 && totalDeletedEditCount === 0) {
noteContainer.style.display = 'block';
const note = document.createElement('p');
if (hasHighEditCount) {
note.textContent = 'Note: This user has made a substantially high number of edits on some wikis.';
} else {
note.textContent = 'Note: No Contributions found during this time period.';
}
noteContainer.appendChild(note);
if (hasHighEditCount) {
if (highEditCountProjects.length > 0) {
const projectNote = document.createElement('p');
projectNote.textContent = `Projects: ${highEditCountProjects.join(', ')}`;
noteContainer.appendChild(projectNote);
}
}
} else {
noteContainer.style.display = 'none';
}
// Show the download button
document.getElementById('downloadCSVButton').style.display = 'inline';
}
function downloadCSV() {
const table = document.getElementById('editCounts');
const rows = table.querySelectorAll('tr');
let csvContent = 'data:text/csv;charset=utf-8,';
const headers = ['Wiki Project', 'Live Edit Count', 'Deleted Edit Count'].join(',') + '\r\n';
csvContent += headers;
rows.forEach((row, index) => {
if (index > 1) { // Skip row
const rowData = [];
row.querySelectorAll('td').forEach(cell => {
rowData.push(cell.textContent.trim());
});
csvContent += rowData.join(',') + '\r\n';
}
});
const username = document.getElementById('username').value;
const encodedUri = encodeURI(csvContent);
const link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download', `${username}_edit_counts.csv`);
document.body.appendChild(link); // Required for Firefox
link.click();
}
document.getElementById('downloadCSVButton').addEventListener('click', downloadCSV);
// Check if the URL contains the username, start date, and end date parameters
const urlParams = new URLSearchParams(window.location.search);
const usernameFromURL = urlParams.get('username');
const namespaceFromURL = urlParams.get('namespace');
const startDateFromURL = urlParams.get('startDate');
const endDateFromURL = urlParams.get('endDate');
if (usernameFromURL && startDateFromURL && endDateFromURL) {
// Fill the form fields with values from the URL
document.getElementById('username').value = usernameFromURL;
document.getElementById('startDate').value = startDateFromURL;
document.getElementById('endDate').value = endDateFromURL;
if (namespaceFromURL) {
document.getElementById('namespace').value = namespaceFromURL;
} else {
document.getElementById('namespace').value = 'all'; // Default to All
}
// Trigger the function to fetch user contributions
displayUserEditCounts(usernameFromURL, namespaceFromURL, startDateFromURL, endDateFromURL);
}
// Function to update API requests info
function updateApiRequestsInfo() {
const apiRequestsInfoContainer = document.getElementById('apiRequestsInfoContainer');
apiRequestsInfoContainer.textContent = `Total API Requests: ${totalApiRequests}, Successful API Requests: ${successfulApiRequests}`;
// Check if both total and successful API requests are non-zero
if (totalApiRequests !== 0 && totalApiRequests !== successfulApiRequests) {
const notification = document.createElement('p');
notification.textContent = 'API requests not completed successfully.';
// Create reload icon
const reloadIcon = document.createElement('span');
reloadIcon.innerHTML = '↻'; // Unicode character for reload symbol
reloadIcon.style.cursor = 'pointer';
reloadIcon.addEventListener('click', () => {
location.reload();
});
notification.appendChild(reloadIcon);
apiRequestsInfoContainer.appendChild(notification);
} else if (totalApiRequests !== 0 && totalApiRequests === successfulApiRequests) {
const notification = document.createElement('p');
notification.textContent = 'API requests completed successfully.';
apiRequestsInfoContainer.appendChild(notification);
}
}
updateApiRequestsInfo();
</script>
</body>
</html>