-
Notifications
You must be signed in to change notification settings - Fork 1
/
wiki.php
328 lines (310 loc) · 11.3 KB
/
wiki.php
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
<?php
require("script.php");
?><!DOCTYPE html>
<html>
<head>
<title>Area51 functions merge</title>
<style>
/* Tags */
input[type="button"], button {
vertical-align: top;
}
input[type="text"] {
width: 80%; max-width: 400px;
}
textarea {
width: 80%; max-width: 400px; min-height: 80px;
}
table {
border: 1px solid grey;
}
th, td {
border: 1px solid black;
}
a:link, a:hover, a:visited, a:active {
color: grey;
}
/* Class */
.inputInsert {
background: lightyellow;
}
.inputDisabled {
background: lightgrey;
}
/* IDs */
#latestDate {
max-width: 200px;
}
#debug {
border: 1px solid grey; width: 80%; max-width: 800px; min-height: 200px;
font-family: monospace;
}
</style>
</head>
<body>
<h2>Area51 functions merge <?php print (DEBUG ? "[DEBUG MODE]" : ""); ?></h2>
<label for="Area51">Area 51 page link:</label><br>
<input type="text" id="Area51" name="Area51" class="inputInsert" value="https://wiki.mudlet.org/w/Area_51"><br>
<label for="WikiBotUser">Wiki Bot User:</label><br>
<input type="text" id="WikiBotUser" name="WikiBotUser" class="inputInsert" value="<?php print WIKI_BOT_USER; ?>"><br>
<label for="WikiBotPass">Wiki Bot Pass:</label><br>
<input type="text" id="WikiBotPass" name="WikiBotPass" class="inputInsert" value="<?php print WIKI_BOT_PASS; ?>"><br>
<input id="btn-start" type="button" value="START">
<p>Just click the "Start" button and wait for function loading.</p>
<p>NOTE: Credential generated at https://wiki.mudlet.org/w/Special:BotPasswords, needed only "Edit pages" permission</p>
<h2>Output</h2>
<table id="tableMerge">
<thead>
<th>Section</th>
<th>Function</th>
<th>PR</th>
<th><input type="checkbox" class="btn-all" value="1" /> Merge</th>
<th><input type="checkbox" class="btn-all" value="1" /> Delete</th>
<th>Insert</th>
<th>Note</th>
<th>Content</th>
</thead>
<tbody>
</tbody>
</table>
<br>
<input id="btn-merge" type="button" value="MERGE" disabled="disabled">
<input id="btn-delete" type="button" value="DELETE" disabled="disabled">
<h2>Debug windows</h2>
<div id="debug">
</div>
<script src="js/jquery.min.js"></script>
<script>
const GITHUB_URL = '<?php print GITHUB_URL; ?>';
$(document).ready(function() {
// allow to select/deselect all the function for merging
$(".btn-all").click(function (e) {
var index = $(this).parent().index() + 1;
$("#tableMerge tbody tr td:nth-child(" + index + ")").find(':input').prop("checked", $(this).is(":checked"));
});
// retrive the area51 functions and try to match it with the official page
$("#btn-start").click(function (e) {
if ($("#Area51").val() == "") {
alert('Insert a valid Area51 url');
return;
} else {
$('#Area51, #btn-start').prop('disabled', true);
Area51Functions();
}
});
// do the real merge and overwrite the original page with new information
$("#btn-merge").click(function (e) {
var mergeable = [];
$('.mergeable:checked').each(function(i){
mergeable[i] = $(this).val();
});
if (mergeable.length == 0) {
alert("No functions to merge");
} else {
if ($("#WikiBotUser").val() == "" || $("#WikiBotPass").val() == "") {
alert("Wiki Bot User / Pass are mandatory for merging!");
} else if (confirm("Do you really want to merge this functions?")) {
$('#btn-merge, #btn-delete').prop('disabled', true);
Area51Insert(mergeable);
}
}
});
// do the real merge and overwrite the original page with new information
$("#btn-delete").click(function (e) {
var deletable = [];
$('.deletable:checked').each(function(i){
deletable[i] = $(this).val();
});
if (deletable.length == 0) {
alert("No functions to delete");
} else {
if ($("#WikiBotUser").val() == "" || $("#WikiBotPass").val() == "") {
alert("Wiki Bot User / Pass are mandatory for deleting!");
} else if (confirm("Do you really want to delete this functions from Area51?")) {
$('#btn-merge, #btn-delete').prop('disabled', true);
Area51Delete(deletable);
}
}
});
});
/*
* Request Wikimedia to get functions element
*/
function Area51Functions() {
makeRequest({
WikiBotUser: $("#WikiBotUser").val(), WikiBotPass: $("#WikiBotPass").val(),
action: "Area51", area51: $("#Area51").val()
}, function (status, data) {
switch (status) {
case "beforeSend":
writeOutput("Area51 functions started...");
break;
case "success":
var trg;
var kSections, vSections, kFunctions, vFunctions, arrFunctions;
if (data['data']['status'] == "OK") {
writeOutput("[INFO] functions found: populate table");
// Populate the table with functions to merge
trg = $("#tableMerge tbody");
trg.empty();
for (kSections in data['data']['table']) {
vSections = data['data']['table'][kSections];
for (kFunctions in vSections) {
vFunctions = vSections[kFunctions];
if (vFunctions["NAME"] != "STUBLAST") {
// Add link to wiki
if (vFunctions["LINK"] != "") {
vFunctions["LINK"] = '<a href="' + vFunctions["LINK"] + '" target="_blank">' + vFunctions["NAME"] + '</a>';
} else {
vFunctions["LINK"] = vFunctions["NAME"];
}
if (vFunctions["WIKI_LINK"] != "") {
vFunctions["WIKI_LINK"] = '<a href="' + vFunctions["WIKI_LINK"] + '" target="_blank">' + vFunctions["NOTE"] + '</a>';
} else {
vFunctions["WIKI_LINK"] = vFunctions["NOTE"];
}
// Add link to github
vFunctions["PR"] = vFunctions["PR"].replace(/#(\d+)/, '<a href="' + GITHUB_URL + '$1" target="_blank">#$1</a>');
// Print the table row
trg.append('<tr>' +
'<td>' + kSections + '</td>' + "\n" +
'<td>' + vFunctions["LINK"] + '</td>' + "\n" +
'<td>' + vFunctions["PR"] + '</td>' + "\n" +
'<td>' + (vFunctions['MERGE'] >= 1 ? '<input type="checkbox" class="mergeable" name="merge[]" value="' + vFunctions["ID"] + '" />' : '') + '</td>' + "\n" +
'<td>' + (vFunctions['MERGE'] >= 1 ? '<input type="checkbox" class="deletable" name="delete[]" value="' + vFunctions["ID"] + '" />' : '') + '</td>' + "\n" +
'<td>' + vFunctions["OFFSET_INSERT"] + '</td>' + "\n" +
'<td>' + vFunctions["WIKI_LINK"] + '</td>' + "\n" +
'<td>' + <?php print (DEBUG ? 'vFunctions["CONTENT"]' : '""'); ?> + '</td>' + "\n" +
'</tr>');
}
}
}
$('#btn-merge, #btn-delete').prop('disabled', false);
} else {
writeOutput("[ERROR] functions error [2]: " + data['data']['status']);
}
break;
case "error":
writeOutput("[ERROR] functions error [1]: " + data);
$('#Area51, #btn-start').prop('disabled', false);
break;
};
});
}
/*
* Insert wikimedia for selected functions
*/
function Area51Insert(merge) {
makeRequest({
WikiBotUser: $("#WikiBotUser").val(), WikiBotPass: $("#WikiBotPass").val(),
action: "Area51Insert", area51: $("#Area51").val(), merge51: merge
}, function (status, data) {
switch (status) {
case "beforeSend":
writeOutput("Area51 merge started...");
writeOutput("[INFO] functions list: " + merge);
break;
case "success":
var trg, id;
if (data['data']['status'] == "OK") {
// Highlight the merged functions rows
writeOutput("[INFO] insert complete: verify table");
for (id in data['data']['merged']) {
trg = $("#tableMerge tbody").find('.mergeable[value="' + data['data']['merged'][id] + '"]');
trg.parents('tr').css('background', 'lightgreen');
}
} else {
writeOutput("[ERROR] insert error [2]: " + data['data']['status']);
}
break;
case "error":
writeOutput("[ERROR] insert error [1]: " + data);
break;
};
});
}
/*
* Delete wikimedia for selected functions
*/
function Area51Delete(del) {
makeRequest({
WikiBotUser: $("#WikiBotUser").val(), WikiBotPass: $("#WikiBotPass").val(),
action: "Area51Delete", area51: $("#Area51").val(), delete51: del
}, function (status, data) {
switch (status) {
case "beforeSend":
writeOutput("Area51 delete started...");
writeOutput("[INFO] functions list: " + del);
break;
case "success":
var trg, id;
if (data['data']['status'] == "OK") {
// Highlight the merged functions rows
writeOutput("[INFO] delete complete: verify table");
for (id in data['data']['deleted']) {
trg = $("#tableMerge tbody").find('.deletable[value="' + data['data']['deleted'][id] + '"]');
trg.parents('tr').css('background', 'orange');
}
} else {
writeOutput("[ERROR] delete error [2]: " + data['data']['status']);
}
break;
case "error":
writeOutput("[ERROR] delete error [1]: " + data);
break;
};
});
}
/*
* Write to debug panel
*/
function writeOutput(message, style) {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
$("#debug").append((h < 10 ? '0' : '') + h + ':' + (m < 10 ? '0' : '') + m + ':' + (s < 10 ? '0' : '') + s + ': ' + message + "<br>");
}
/*
* Ajax request just a little better
*/
function makeRequest(reqParam, callBack) {
var req = {
"param": reqParam
};
$.ajax({
url: 'ajax.php',
type: 'post',
data: req,
success: function (data) {
var response, tmpHTML, elem, tmpObj, i;
try {
response = $.parseJSON(data);
} catch (err) {
_console(response);
}
switch (response['param']['type']) {
default:
if (callBack) callBack('success', response);
break;
}
},
beforeSend: function () {
switch (req['param']['type']) {
default:
if (callBack) callBack('beforeSend');
break;
}
},
error: function (xhr, text) {
switch (req['param']['type']) {
default:
if (callBack) callBack('error', text);
break;
}
}
});
}
</script>
</body>
</html>