Difference between revisions of "MediaWiki:Gadget-BulkUpload/Runtime.js"
From Nookipedia, the Animal Crossing wiki
SuperHamster (talk | contribs) m (New line) |
SuperHamster (talk | contribs) (Fixing success/error detection. The FATALWARNINGS array holds warnings that always result in failure, even if ignorewarnings is checked...list might be incomplete.) |
||
Line 1: | Line 1: | ||
/** | /** | ||
* Turns Special:BulkUpload into a bulk upload form. Filenames are kept as-is and all files will have the same information. | * Turns Special:BulkUpload into a bulk upload form. Filenames are kept as-is and all files will have the same information. | ||
− | |||
* Created by SuperHamster on Nookipedia (https://nookipedia.com/wiki/User:SuperHamster) | * Created by SuperHamster on Nookipedia (https://nookipedia.com/wiki/User:SuperHamster) | ||
* Licensed under CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/) | * Licensed under CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/) | ||
**/ | **/ | ||
− | + | // Set title of page as it appears in the browser tab:: | |
− | + | const PAGETITLE = "Bulk upload form - Animal Crossing Wiki - Nookipedia"; | |
+ | |||
+ | // List of warnings that always result in upload failure, even if ignorewarnings is checked: | ||
+ | const FATALWARNINGS = ["fileexists-no-change", "abusefilter-disallowed"]; | ||
− | + | // Default file description that is loaded in the form: | |
+ | const DEFAULTDESCRIPTION = `== Summary == | ||
{{File Info | {{File Info | ||
|description = Describe the file (THIS IS NOT OPTIONAL) | |description = Describe the file (THIS IS NOT OPTIONAL) | ||
Line 21: | Line 24: | ||
{{Fair use}}`; | {{Fair use}}`; | ||
− | document.title = | + | (function() { |
+ | "use strict"; | ||
+ | |||
+ | document.title = PAGETITLE; | ||
$('#firstHeading').html('Bulk upload form'); | $('#firstHeading').html('Bulk upload form'); | ||
$('#bodyContent').empty(); | $('#bodyContent').empty(); | ||
Line 30: | Line 36: | ||
<input type="file" id="bulkUploadFiles" multiple/><br><br> | <input type="file" id="bulkUploadFiles" multiple/><br><br> | ||
<label for="bulkUploadInfo">File description</legend><br> | <label for="bulkUploadInfo">File description</legend><br> | ||
− | <textarea id="bulkUploadInfo" cols="80" rows="12">` + | + | <textarea id="bulkUploadInfo" cols="80" rows="12">` + DEFAULTDESCRIPTION + `</textarea><br><br> |
<input type="checkbox" id="bulkUploadIgnoreWarnings"><label for="bulkUploadIgnoreWarnings">Ignore warnings (check to override existing files)</label><br><br> | <input type="checkbox" id="bulkUploadIgnoreWarnings"><label for="bulkUploadIgnoreWarnings">Ignore warnings (check to override existing files)</label><br><br> | ||
<button id="bulkUploadButton" type="button">Upload files</button> | <button id="bulkUploadButton" type="button">Upload files</button> | ||
Line 49: | Line 55: | ||
function uploadFile(file, uploadParams, j) { | function uploadFile(file, uploadParams, j) { | ||
setTimeout(function() { | setTimeout(function() { | ||
− | api.upload(file, | + | api.upload(file, uploadParams).always(response => { |
− | + | completed++; | |
− | + | console.log(response); | |
− | + | console.log(typeof response); | |
− | + | if (typeof response === 'object') { | |
− | + | JSON.stringify(response); | |
− | + | if (response.hasOwnProperty('upload') && upload.result == 'Success') { | |
− | + | $('#bulkUploadStatus').append('Upload succeeded for ' + file.name + ' (' + completed + ' out of ' + files.length + ')<br>'); | |
− | + | } else { | |
+ | $('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' due to unknown error (' + completed + ' out of ' + files.length + ')<br>'); | ||
+ | } | ||
+ | } else if (typeof response === 'string') { | ||
+ | if(response.length > 0) { | ||
+ | if (FATALWARNINGS.includes(response)) { | ||
+ | $('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' with error "' + response + '" (' + completed + ' out of ' + files.length + ')<br>'); | ||
+ | } else { | ||
+ | if ($('#bulkUploadIgnoreWarnings').is(':checked')) { | ||
+ | $('#bulkUploadStatus').append('Upload succeeded for ' + file.name + ' with warning "' + response + '" (' + completed + ' out of ' + files.length + ')<br>'); | ||
+ | } else { | ||
+ | $('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' with error "' + response + '" (' + completed + ' out of ' + files.length + ')<br>'); | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | $('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' due to unknown error (' + completed + ' out of ' + files.length + ')<br>'); | ||
+ | } | ||
+ | } else { | ||
+ | $('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' due to unknown error (' + completed + ' out of ' + files.length + ')<br>'); | ||
+ | } | ||
+ | if (completed == files.length) { | ||
$('#bulkUploadStatus').append('<span style="color: green;">Done!</span>'); | $('#bulkUploadStatus').append('<span style="color: green;">Done!</span>'); | ||
document.getElementById('bulkUploadButton').disabled = false; | document.getElementById('bulkUploadButton').disabled = false; |
Revision as of 03:53, August 8, 2021
/**
* Turns Special:BulkUpload into a bulk upload form. Filenames are kept as-is and all files will have the same information.
* Created by SuperHamster on Nookipedia (https://nookipedia.com/wiki/User:SuperHamster)
* Licensed under CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0/)
**/
// Set title of page as it appears in the browser tab::
const PAGETITLE = "Bulk upload form - Animal Crossing Wiki - Nookipedia";
// List of warnings that always result in upload failure, even if ignorewarnings is checked:
const FATALWARNINGS = ["fileexists-no-change", "abusefilter-disallowed"];
// Default file description that is loaded in the form:
const DEFAULTDESCRIPTION = `== Summary ==
{{File Info
|description = Describe the file (THIS IS NOT OPTIONAL)
|source = Where you found the file (THIS IS NOT OPTIONAL)
|edits = Describe any edits made (if applicable)
|other-versions = Link to similar version(s) (if applicable)
|source-file-name = Name of the original source file (if applicable)
}}
== Licensing ==
{{Fair use}}`;
(function() {
"use strict";
document.title = PAGETITLE;
$('#firstHeading').html('Bulk upload form');
$('#bodyContent').empty();
$('#bodyContent').append('<p>Use this form to upload multiple files at once. Filenames are kept as-is and all files will have the same information. To cancel an upload that is in progress, simply navigate away from this page.</p>');
var htmlUploadForm = `<fieldset>
<legend>Files and info</legend>
<label for="bulkUploadFiles">Select files:</label><br>
<input type="file" id="bulkUploadFiles" multiple/><br><br>
<label for="bulkUploadInfo">File description</legend><br>
<textarea id="bulkUploadInfo" cols="80" rows="12">` + DEFAULTDESCRIPTION + `</textarea><br><br>
<input type="checkbox" id="bulkUploadIgnoreWarnings"><label for="bulkUploadIgnoreWarnings">Ignore warnings (check to override existing files)</label><br><br>
<button id="bulkUploadButton" type="button">Upload files</button>
</fieldset>
<br>
<fieldset>
<legend>Upload status</legend>
<span id="bulkUploadStatus">Waiting for user to start upload.</span>
</fieldset>
`;
$('#bodyContent').append(htmlUploadForm);
document.getElementById('bulkUploadButton').addEventListener("click", function () {
const api = new mw.Api();
var completed = 0;
document.getElementById('bulkUploadButton').disabled = true;
$('#bulkUploadStatus').empty();
function uploadFile(file, uploadParams, j) {
setTimeout(function() {
api.upload(file, uploadParams).always(response => {
completed++;
console.log(response);
console.log(typeof response);
if (typeof response === 'object') {
JSON.stringify(response);
if (response.hasOwnProperty('upload') && upload.result == 'Success') {
$('#bulkUploadStatus').append('Upload succeeded for ' + file.name + ' (' + completed + ' out of ' + files.length + ')<br>');
} else {
$('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' due to unknown error (' + completed + ' out of ' + files.length + ')<br>');
}
} else if (typeof response === 'string') {
if(response.length > 0) {
if (FATALWARNINGS.includes(response)) {
$('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' with error "' + response + '" (' + completed + ' out of ' + files.length + ')<br>');
} else {
if ($('#bulkUploadIgnoreWarnings').is(':checked')) {
$('#bulkUploadStatus').append('Upload succeeded for ' + file.name + ' with warning "' + response + '" (' + completed + ' out of ' + files.length + ')<br>');
} else {
$('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' with error "' + response + '" (' + completed + ' out of ' + files.length + ')<br>');
}
}
} else {
$('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' due to unknown error (' + completed + ' out of ' + files.length + ')<br>');
}
} else {
$('#bulkUploadStatus').append('Upload <span style="color: red;">failed</span> for ' + file.name + ' due to unknown error (' + completed + ' out of ' + files.length + ')<br>');
}
if (completed == files.length) {
$('#bulkUploadStatus').append('<span style="color: green;">Done!</span>');
document.getElementById('bulkUploadButton').disabled = false;
}
});
}, 1000 * j);
}
var files = $('#bulkUploadFiles')[0].files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var uploadParams = {};
if($('#bulkUploadIgnoreWarnings').is(':checked')) {
uploadParams = {
filename: file.name,
text: $('#bulkUploadInfo').val(),
ignorewarnings: "yes",
format: "json"
};
} else {
uploadParams = {
filename: file.name,
text: $('#bulkUploadInfo').val(),
format: "json"
};
}
uploadFile(file, uploadParams, i);
}
});
})();