Content added Content deleted
TheDemon08 (talk | contribs) No edit summary Tag: Reverted |
TheDemon08 (talk | contribs) m (Undo revision 1643 by TheDemon08 (talk)) Tag: Undo |
||
Line 1: | Line 1: | ||
/* @authors Gguigui1, KhangND, Universal Omega */ |
|||
/** |
|||
mw.loader.using([ |
|||
* Name: MultiUploadJS |
|||
'mediawiki.user', |
|||
* Author: Fujimaru-kun |
|||
'mediawiki.api', |
|||
* Based on: MultiUpload (by Gguigui1 and KhangND), Gadget-multiupload.js (by Pcj) |
|||
'jquery.client', |
|||
* Description: Allows upload of multiple files at the same time |
|||
'jquery.spinner', |
|||
*/ |
|||
'ext.LinkSuggest' |
|||
mw.loader.using(['mediawiki.api', 'mediawiki.util']).then(function () { |
|||
], function() { |
|||
'use strict'; |
|||
var |
var groups = mw.config.get('wgUserGroups').join(), |
||
config = mw.config.get([ |
|||
'wgCanonicalSpecialPageName', |
'wgCanonicalSpecialPageName', |
||
'wgNamespaceNumber', |
|||
'wgTitle', |
|||
'wgUserLanguage', |
'wgUserLanguage', |
||
' |
'wgUserName' |
||
'wgVersion' |
|||
]); |
]); |
||
mw.loader.implement("mediawiki.legacy.ajax", function($) { |
|||
window.sajax_debug_mode = !1; |
|||
if ( |
|||
window.sajax_request_type = 'GET'; |
|||
window.sajax_debug = function(text) { |
|||
config.wgCanonicalSpecialPageName !== "Upload" || |
|||
if (!sajax_debug_mode) |
|||
$("#wpForReUpload").val() || //Disabling for Reupload |
|||
return false; |
|||
var e = document.getElementById('sajax_debug'); |
|||
) { |
|||
if (!e) { |
|||
e = document.createElement('p'); |
|||
e.className = 'sajax_debug'; |
|||
e.id = 'sajax_debug'; |
|||
var b = document.getElementsByTagName('body')[0]; |
|||
if (b.firstChild) { |
|||
b.insertBefore(e, b.firstChild); |
|||
} else { |
|||
b.appendChild(e); |
|||
} |
|||
} |
} |
||
var m = document.createElement('div'); |
|||
window.MultiUploadJSLoaded = true; |
|||
m.appendChild(document.createTextNode(text)); |
|||
e.appendChild(m); |
|||
return true; |
|||
} |
|||
token = mw.user.tokens.get('csrfToken'), |
|||
; |
|||
files = [], |
|||
window.sajax_init_object = function() { |
|||
stdmsgs = ['filedesc', 'license-header', 'fileexists-no-change', 'fileexists-duplicate-version', 'verification-error', 'fileexists-shared-forbidden', 'permissiondenied', 'watchthisupload', 'ignorewarnings', 'filewasdeleted'].join('|'), |
|||
sajax_debug('sajax_init_object() called..'); |
|||
limit = (window.MultiUploadoption && window.MultiUploadoption.max) ? window.MultiUploadoption.max : -1, |
|||
var A; |
|||
defaultlicense = (window.MultiUploadoption && window.MultiUploadoption.defaultlicense) ? window.MultiUploadoption.defaultlicense : '', |
|||
try { |
|||
A = new XMLHttpRequest(); |
|||
} catch (e) { |
|||
try { |
|||
A = new ActiveXObject('Msxml2.XMLHTTP'); |
|||
} catch (e) { |
|||
try { |
|||
$("#wpUploadFile").parent().parent().addClass("regularFileSelect"); |
|||
A = new ActiveXObject('Microsoft.XMLHTTP'); |
|||
$("tr.regularFileSelect").after('<tr class="multipleFileSelect"><td class="mw-label"> </td><td class="mw-input"><input type="file" id="multiupload" multiple /></td></tr>'); |
|||
} catch (oc) { |
|||
$("#mw-htmlform-source > tbody > tr.mw-htmlform-field-UploadSourceField.regularFileSelect").remove(); |
|||
A = null; |
|||
} |
|||
} |
|||
$("#mw-upload-form > fieldset:nth-of-type(2)").hide(); |
|||
$("span.mw-htmlform-submit-buttons").append('<input type="button" value="' + i18n['uploadfiles'] + '" class="multipleFileSelect" id="multiFileSubmit" />'); |
|||
$("span.mw-htmlform-submit-buttons").append('<input type="button" value="' + 'reset' + '" class="multipleFileSelect" id="multiFileReset" />'); |
|||
$("#multiupload").change(addFields); |
|||
$("#multiFileSubmit").click(uploadFiles); |
|||
$("#multiFileReset").click(reset); |
|||
} |
} |
||
if (!A) { |
|||
sajax_debug('Could not create connection object.'); |
|||
/* |
|||
} |
|||
* Set Maximum number of file the user can import at one time (can be overriden by a config var but its value is capped at 100) |
|||
return A; |
|||
} |
|||
function setLimit() { |
|||
; |
|||
if (limit < 0 || |
|||
window.sajax_do_call = function(func_name, args, target) { |
|||
limit > 100 || |
|||
var i, x, n; |
|||
var uri; |
|||
if (/staff|util|bot-global|wiki-specialist/.test(config.wgUserGroups.join())) { |
|||
var post_data; |
|||
uri = mw.util.wikiScript() + '?action=ajax'; |
|||
} else if (/bureaucrat|bot/.test(config.wgUserGroups.join())) { |
|||
if (sajax_request_type == 'GET') { |
|||
limit = 70; |
|||
if (uri.indexOf('?') == -1) { |
|||
uri = uri + '?rs=' + encodeURIComponent(func_name); |
|||
} else { |
|||
uri = uri + '&rs=' + encodeURIComponent(func_name); |
|||
} |
|||
for (i = 0; i < args.length; i++) { |
|||
uri = uri + '&rsargs[]=' + encodeURIComponent(args[i]); |
|||
} |
|||
post_data = null; |
|||
} else { |
|||
post_data = 'rs=' + encodeURIComponent(func_name); |
|||
for (i = 0; i < args.length; i++) { |
|||
post_data = post_data + '&rsargs[]=' + encodeURIComponent(args[i]); |
|||
} |
|||
} |
|||
x = sajax_init_object(); |
|||
if (!x) { |
|||
alert('AJAX not supported'); |
|||
return false; |
|||
} |
|||
try { |
|||
x.open(sajax_request_type, uri, true); |
|||
} catch (e) { |
|||
if (window.location.hostname == 'localhost') { |
|||
alert("Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing."); |
|||
} |
|||
throw e; |
|||
} |
|||
if (sajax_request_type == 'POST') { |
|||
x.setRequestHeader('Method', 'POST ' + uri + ' HTTP/1.1'); |
|||
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
|||
} |
|||
x.setRequestHeader('Pragma', 'cache=yes'); |
|||
x.setRequestHeader('Cache-Control', 'no-transform'); |
|||
x.onreadystatechange = function() { |
|||
if (x.readyState != 4) { |
|||
return; |
|||
} |
|||
sajax_debug('received (' + x.status + ' ' + x.statusText + ') ' + x.responseText); |
|||
if (typeof (target) == 'function') { |
|||
target(x); |
|||
} else if (typeof (target) == 'object') { |
|||
if (target.tagName == 'INPUT') { |
|||
if (x.status == 200) { |
|||
target.value = x.responseText; |
|||
} |
|||
} else { |
} else { |
||
if (x.status == 200) { |
|||
target.innerHTML = x.responseText; |
|||
} else { |
|||
target.innerHTML = '<div class="error">Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')</div>'; |
|||
} |
|||
} |
} |
||
} else { |
|||
alert('bad target for sajax_do_call: not a function or object: ' + target); |
|||
} |
} |
||
} |
} |
||
; |
|||
sajax_debug(func_name + ' uri = ' + uri + ' / post = ' + post_data); |
|||
x.send(post_data); |
|||
sajax_debug(func_name + ' waiting..'); |
|||
delete x; |
|||
return true; |
|||
} |
|||
; |
|||
window.wfSupportsAjax = function() { |
|||
var request = sajax_init_object(); |
|||
var supportsAjax = request ? true : !1; |
|||
delete request; |
|||
return supportsAjax; |
|||
} |
|||
; |
|||
; |
|||
}, {}, {}); |
|||
function getLicensePreview(num) { |
|||
window.licenseSelectorCheck = function () { |
|||
var selector = document.getElementById("license" + num); |
|||
var selection = selector.options[selector.selectedIndex].value; |
|||
if (selector.selectedIndex > 0) { |
|||
if (selection == "") { |
|||
// Option disabled, but browser is broken and doesn't respect this |
|||
selector.selectedIndex = 0; |
|||
} |
|||
} |
|||
var UploadLicenseObj = { |
|||
'responseCache': { |
|||
'': '' |
|||
* Add Fields to change the filename, description and license of each file to DOM |
|||
}, |
|||
function addFields() { |
|||
'fetchPreview': function (license) { |
|||
if (!mw.config.get('wgAjaxLicensePreview')) |
|||
files = $("#multiupload")[0].files; |
|||
return; |
|||
for (cached in this.responseCache) { |
|||
var watchuploads = mw.user.options.get('watchuploads') == "1" ? 'checked=\"checked\"' : ''; |
|||
if (cached == license) { |
|||
this.showPreview(this.responseCache[license]); |
|||
return; |
|||
filedesc.attr("id", "file-" + index); |
|||
filedesc.children("legend").text('imagename' + (index + 1)); |
|||
filedesc.find("#mw-htmlform-description > tbody > tr.mw-htmlform-field-HTMLTextField > td.mw-label > label").attr("for", "wpDestFile" + index); |
|||
filedesc.find("#mw-htmlform-description > tbody > tr.mw-htmlform-field-HTMLTextAreaField > td.mw-label > label").attr("for", "wpUploadDescription" + index); |
|||
filedesc.find("#wpDestFile").attr("name", "wpDestFile" + index).attr("id", "wpDestFile" + index).val(element.name); |
|||
filedesc.find("#wpUploadDescription").attr("name", "wpUploadDescription" + index).attr("id", "wpUploadDescription" + index); |
|||
filedesc.find("#wpLicense").attr("name", "wpLicense" + index).attr("id", "wpLicense" + index).val(defaultlicense); |
|||
filedesc.append("<hr />"); |
|||
filedesc.append("<td class=\"mw-input\"><input name=\"wpWatchthis" + index + "\" type=\"checkbox\" value=\"1\" " + watchuploads + " id=\"wpWatchthis" + index + "\"> <label for=\"wpWatchthis" + index + "\">" + 'watchthisupload' + "</label></td>"); |
|||
filedesc.append("<td class=\"mw-input\"><input name=\"wpIgnoreWarning" + index + "\" type=\"checkbox\" value=\"1\" id=\"wpIgnoreWarning" + index + "\"> <label for=\"wpIgnoreWarning" + index + "\">" + 'ignorewarnings' + "</label></td>"); |
|||
$("#mw-upload-form > span").before(filedesc.show()); |
|||
} |
} |
||
} else { |
|||
alert('limit'); |
|||
$("#multiupload").val(""); |
|||
reset(); |
|||
} |
} |
||
$('#license' + num).injectSpinner('license' + num); |
|||
var title = document.getElementById('imagename' + num).value; |
|||
if (!title) |
|||
title = 'File:Sample.jpg'; |
|||
var url = mw.util.wikiScript('api') |
|||
+ '?action=parse&text={{' + encodeURIComponent(license) + '}}' |
|||
+ '&title=' + encodeURIComponent(title) |
|||
+ '&prop=text&pst&format=json'; |
|||
var req = sajax_init_object(); |
|||
req.onreadystatechange = function () { |
|||
if (req.readyState == 4 && req.status == 200) { |
|||
UploadLicenseObj.processResult(eval('(' + req.responseText + ')'), license); |
|||
} |
|||
}; |
|||
req.open('GET', url, true); |
|||
req.send(''); |
|||
}, |
|||
'processResult': function (result, license) { |
|||
$.removeSpinner('license' + num); |
|||
this.responseCache[license] = result['parse']['text']['*']; |
|||
this.showPreview(this.responseCache[license]); |
|||
}, |
|||
'showPreview': function (preview) { |
|||
var previewPanel = document.getElementById('mw-license-preview' + num); |
|||
if (previewPanel.innerHTML != preview) |
|||
previewPanel.innerHTML = preview; |
|||
} |
} |
||
}; |
|||
// We might show a preview |
|||
UploadLicenseObj.fetchPreview(selection); |
|||
*/ |
|||
}; |
|||
function reset() { |
|||
var wpLicense = document.getElementById('license' + num); |
|||
files = []; |
|||
$("#mw-upload-form > fieldset:nth-of-type(2) ~ fieldset").remove(); |
|||
if (mw.config.get('wgAjaxLicensePreview') && wpLicense) { |
|||
// License selector check |
|||
wpLicense.onchange = licenseSelectorCheck; |
|||
// License selector table row |
|||
var wpLicenseRow = wpLicense.parentNode.parentNode; |
|||
var wpLicenseTbody = wpLicenseRow.parentNode; |
|||
var row = document.createElement('tr'); |
|||
var td = document.createElement('td'); |
|||
row.appendChild(td); |
|||
td = document.createElement('td'); |
|||
td.id = 'mw-license-preview' + num; |
|||
row.appendChild(td); |
|||
wpLicenseTbody.insertBefore(row, wpLicenseRow.nextSibling); |
|||
} |
|||
} |
|||
// load protections |
|||
if(window.MultiUploadLoaded |
|||
|| config.wgUserName === null |
|||
|| ( !/autoconfirmed/.test(groups) && !/confirmed/.test(groups))) { |
|||
return; |
|||
} window.MultiUploadLoaded = true; |
|||
var storage = 'MultiUploadLicenses'; |
|||
var content = $('#mw-content-text'); |
|||
var allowTypes = $.map( mw.config.get("wgFileExtensions"), function(file_extensions) { |
|||
return('.' + file_extensions) |
|||
}); |
|||
var style = { |
|||
block: { |
|||
display: 'inline-block', |
|||
margin: '0 8px' |
|||
}, |
|||
textarea: { |
|||
width: '100%', |
|||
height: 150, |
|||
boxSizing: 'border-box', |
|||
resize: 'none' |
|||
} |
} |
||
}; |
|||
function loadMWMessagesIfMissing(messages) { |
|||
/* |
|||
var deferred = $.Deferred(), |
|||
* Get standard messages for supported errors |
|||
missingMessages = messages.filter(function (message) { return !mw.messages.exists(message); }); |
|||
*/ |
|||
if (!missingMessages.length) { |
|||
deferred.resolve(); |
|||
} else { |
|||
$.get(mw.util.wikiScript('api'), { |
|||
format: 'json', |
|||
action: 'query', |
action: 'query', |
||
meta: 'allmessages', |
meta: 'allmessages', |
||
ammessages: missingMessages.join('|'), |
|||
amlang: config.wgUserLanguage |
|||
}).then(function (data) { |
}).then(function (data) { |
||
if ($.isArray(data.query.allmessages)) { |
|||
$.each(data.query.allmessages, function (_, message) { |
|||
if (message.missing !== '') { |
|||
mw.messages.set(message.name, message['*']); |
|||
} |
|||
}); |
|||
} |
|||
deferred.resolve(); |
|||
}, function () { |
|||
// Silently swallow failures; we don't want error reporting to stall just because we failed to fetch some messages. |
|||
deferred.resolve(); |
|||
}); |
}); |
||
return i18n; |
|||
} |
} |
||
return deferred; |
|||
} |
|||
MultiUpload = { |
|||
input: $(), // available on init |
|||
editor: $(), // available on init |
|||
button: $(), // available on init |
|||
fileCount: 0,// available on create |
|||
preload: function(load) { |
|||
$.extend(this, window.MultiUploadoption || {}); |
|||
this.load=load; |
|||
// creates Special page |
|||
if( |
|||
(config.wgNamespaceNumber === -1 && config.wgTitle === 'MultiUpload') || |
|||
return false; |
|||
(config.wgCanonicalSpecialPageName === 'Blankpage' && /MultiUpload/.test($.getUrlVar('blankspecial'))) |
|||
} else if (files.length > limit) { |
|||
) { |
|||
$('#firstHeading').text('Multi Upload'); |
|||
$('title').text('Multi Upload'); |
|||
this.init(); |
|||
} |
|||
}, |
|||
init: function() { |
|||
content.empty().append([ |
|||
$('<input>', { |
|||
id: 'fileinput', |
|||
type: 'file', |
|||
multiple: true, |
|||
accept: allowTypes.join(), |
|||
}), |
|||
$('<div>', { |
|||
id: 'editor', |
|||
css: { display: 'none' }, |
|||
}), |
|||
$('<button>', { |
|||
id: 'go', |
|||
text: 'Update the form', |
|||
click: $.proxy(this.getLicenses, this) |
|||
}) |
|||
]); |
|||
this.input = $('#fileinput'); |
|||
this.editor = $('#editor'); |
|||
this.button = $('#go'); |
|||
}, |
|||
notify: function(message, type) { |
|||
mw.notify(message, { type: type } ); |
|||
}, |
|||
getLicenses: function() { |
|||
var data = localStorage.getItem(storage); |
|||
if(data !== null) { |
|||
this.create(data); |
|||
var limit = this.max < this.fileCount ? this.max : this.fileCount; |
|||
for (i = 1; i <= limit; i++) { |
|||
getLicensePreview(i); |
|||
} |
|||
return; |
|||
} |
} |
||
$( |
$.get(mw.util.wikiScript('api'), { |
||
action: 'query', |
|||
meta: 'allmessages', |
|||
$("<h3>" + 'successful' + "</h3><ul id='multiUploadDone'></ul><div style='display:none;' id='multiUploadFailed'><h3>" + 'failed' + "</h3><ul></ul></div>").prependTo("#content"); |
|||
ammessages: 'Licenses', |
|||
format: 'json' |
|||
}).done( |
|||
$.proxy(this.create, this) |
|||
/* |
|||
).fail($.proxy(function(data) { |
|||
* Make API Requests to upload files |
|||
this.notify('Api Error : ' + data.error.info, 'error'); |
|||
*/ |
|||
}), this); |
|||
}, |
|||
if (curFile > files.length) { |
|||
create: function(data) { |
|||
$("<h3>" + 'done' + "</h3>").appendTo("#content"); |
|||
// conditions |
|||
var formatError = false; |
|||
this.fileCount = this.input[0].files.length; |
|||
$(this.input[0].files).each(function(i, file) { |
|||
if(!new RegExp(allowTypes.join('|\\'), 'i').test(file.name)) { |
|||
formatError = true; |
|||
return; |
|||
} |
|||
}); |
|||
if (formatError) { |
|||
this.notify('Unsupported file format', 'warn'); |
|||
return; |
return; |
||
} |
} |
||
if ( |
if (!this.input[0].files) { |
||
this.notify('This browser doesn't seem to support the `files` property of file inputs.', 'notify'); |
|||
curFile++; |
|||
apiUpload(); |
|||
return; |
return; |
||
} |
} |
||
if (this.fileCount === 0) { |
|||
var filename = $("#wpDestFile" + curFile).val() || files[curFile].name; |
|||
this.notify('You have to choose a file to upload it', 'warn'); |
|||
var license = $("#wpLicense" + curFile + " option:selected").prop("title") !== "{{}}" ? "== " + 'license-header' + " ==\n" + $("#wpLicense" + curFile + " option:selected").prop("title") : ""; |
|||
return; |
|||
var description = $("#wpUploadDescription" + curFile).val() ? "== " + i18n['filedesc'] + " ==\n" + $("#wpUploadDescription" + curFile).val() : ""; |
|||
} |
|||
var watch = $("#wpWatchthis" + curFile).is(":checked") ? 'watch' : 'nochange'; |
|||
if(!this.max |
|||
var warning = $("#wpIgnoreWarning" + curFile).is(":checked"); |
|||
|| this.max < 0 |
|||
|| this.max > 101 |
|||
|| typeof this.max !== 'number') { |
|||
if (/staff|helper|util|bot-global/.test(groups)) { |
|||
this.max = 200; |
|||
} else if (/bureaucrat|bot/.test(groups)) { |
|||
this.max = 70; |
|||
} |
} else if (/sysop/.test(groups)) { |
||
this.max = 50; |
|||
} else if (/rollback/.test(groups)) { |
|||
this.max = 30; |
|||
} else { |
|||
this.max = 20; |
|||
} |
|||
} |
|||
if (!this.max) { |
|||
this.notify('A problem occurred, upload cancelled', 'error'); |
|||
return; |
|||
} |
} |
||
// creates form |
|||
data = typeof data === 'object' |
|||
? data.query.allmessages[0]['*'].trim() |
|||
$("#multiUploadDone").append('<li><a href="' + d.upload.imageinfo.descriptionurl + '" target="_blank">' + d.upload.filename + '</a></li>'); |
|||
: data; |
|||
localStorage.setItem(storage, data); // retrieves licenses 1 time only |
|||
apiUpload(); |
|||
var licenses = data.split('\n'); |
|||
var limit = this.max < this.fileCount ? this.max : this.fileCount; |
|||
for (i = 1; i <= limit; i++) { |
|||
$("#multiUploadFailed ul").append('<li>' + filename + ': ' + d + "File:"+filename + '</li>'); |
|||
$('<fieldset>', { |
|||
id: 'field' + i, |
|||
appendTo: this.editor, |
|||
append: [ |
|||
$('<legend>', { |
|||
text: 'File n° ' + i |
|||
}), |
|||
$('<div>', { |
|||
css: style.block, |
|||
text: 'Name:', |
|||
append: $('<input>', { |
|||
type: 'text', |
|||
id: 'imagename' + i, |
|||
'class': 'imagename', |
|||
val: this.input[0].files[i - 1].name |
|||
}) |
|||
}), |
|||
$('<div>', { |
|||
css: style.block, |
|||
text: 'License:', |
|||
append: $('<select>', { |
|||
id: 'license' + i, |
|||
'class': 'license', |
|||
append: $('<option>', { |
|||
val: 'none', |
|||
text: 'None selected' |
|||
}) |
|||
}) |
|||
}), |
|||
$('<div>', { |
|||
id: 'progress' + i, |
|||
css: style.block |
|||
}), |
|||
] |
|||
}); |
|||
} |
|||
for (i = 0; i < licenses.length; i++) { |
|||
if (licenses[i].indexOf('**') === 0) { |
|||
var name = licenses[i].split('|')[0].replace('**', '').trim(), |
|||
text = licenses[i].split('|')[1]; |
|||
$('<option>', { |
|||
val: name, |
|||
text: text, |
|||
selected: name == this.defaultlicense, |
|||
appendTo: $('.license').find('optgroup:last-child') |
|||
}); |
|||
} else { |
} else { |
||
$ |
$('<optgroup>', { |
||
label: licenses[i].replace('*', '').trim(), |
|||
appendTo: $('.license') |
|||
}); |
|||
} |
} |
||
} |
|||
$('<div>', { |
|||
appendTo: this.editor, |
|||
text: 'Summaries:' |
|||
}), |
|||
$('<textarea>', { |
|||
appendTo: this.editor, |
|||
id: 'UploadDescription', |
|||
css: style.textarea, |
|||
}).linksuggest(); |
|||
$('.license option[value="none"').attr("disabled", "true"); |
|||
$('<button>', { |
|||
'class': 'secondary', |
|||
id: 'reset', |
|||
css: style.block, |
|||
text: 'Reset the form', |
|||
click: $.proxy(this.init, this), |
|||
appendTo: content |
|||
}), |
|||
$('<input>', { |
|||
type: 'checkbox', |
|||
id: 'ignorewarnings', |
|||
name: 'ignorewarnings', |
|||
appendTo: content |
|||
}), |
|||
$('<label>', { |
|||
'for': 'ignorewarnings', |
|||
text: 'Ignore warnings', |
|||
appendTo: content |
|||
}); |
}); |
||
jQuery(function ($) { |
|||
'use strict'; |
|||
init(); |
|||
if ($('#UploadDescription').val()) { |
|||
return; |
|||
} |
|||
var params = { |
|||
action: 'parse', |
|||
page: 'Template:Default Upload Summary', |
|||
format: 'json', |
|||
prop: 'wikitext' |
|||
}; |
|||
var api = new mw.Api(); |
|||
api.get(params).done(function(data) { |
|||
$('#UploadDescription').val(data.parse.wikitext['*']); |
|||
}); |
}); |
||
$('.license option[value="none"]').attr("disabled", "true"); |
|||
}); |
|||
license = $('#license' + i).find('option:selected').val(), |
|||
this.input.attr('disabled', true); |
|||
this.editor.show(); |
|||
this.button |
|||
.unbind('click') |
|||
.click($.proxy(this.upload, this)) |
|||
.text('Upload All'); |
|||
}, |
|||
upload: function() { |
|||
content.find('*').attr('disabled', true); |
|||
$('#reset').removeAttr('disabled'); |
|||
var obj = this; |
|||
var loop = function (i) { |
|||
if (i <= obj.fileCount) { |
|||
var file = obj.input[0].files[i - 1], |
|||
filename = $('#imagename' + i).val() || file.name, |
|||
license = $('#license' + i).find('option:selected').val(), |
|||
text = $('#UploadDescription').val(); |
|||
if(license == "none"){ |
|||
// alert('Licensing must be complete.'); |
|||
// $('*').removeAttr('disabled'); |
|||
// return; |
|||
} |
|||
if(license !== "none") text = '== Summary ==\n' + text + '\n\n== Licensing ==\n{{' + license + '}}\n'; |
|||
obj.uploadFile(file, filename, text, i).always(function(){ |
|||
loop(i+1); |
|||
}); |
|||
} |
|||
}; |
|||
loop(1); |
|||
}, |
|||
uploadFile: function(fileToUpload, fileName, text, index) { |
|||
// https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects |
|||
formdata = new FormData(); |
|||
formdata.append('action', 'upload'); |
|||
formdata.append('format', 'json'); |
|||
formdata.append('filename', fileName); |
|||
formdata.append('token', mw.user.tokens.get('csrfToken')); |
|||
formdata.append('file', fileToUpload); |
|||
formdata.append('text', text); |
|||
formdata.append( |
|||
$('#ignorewarnings').prop('checked') ? 'ignorewarnings' : '', '' |
|||
); |
|||
// https://stackoverflow.com/a/8244082 |
|||
return $.ajax({ |
|||
url: mw.util.wikiScript('api'), |
|||
contentType: false, |
|||
processData: false, |
|||
type: 'POST', |
|||
data: formdata, |
|||
dataType: 'json', |
|||
xhr: function() { // https://stackoverflow.com/a/27030092 |
|||
myXhr = $.ajaxSettings.xhr(); |
|||
myXhr.upload.addEventListener('progress', function(e) { |
|||
MultiUpload.progress(index, e); |
|||
}); |
|||
return myXhr; |
|||
}, |
|||
success: $.proxy(function(data) { |
|||
this.success(index, data); |
|||
}, this), |
|||
error: $.proxy(function(_, __, error) { |
|||
this.notify(error, 'error'); |
|||
}, this) |
|||
}); |
|||
}, |
|||
progress: function(index, e) { |
|||
var progress = e.loaded / e.total * 100; |
|||
$('#progress' + index).text(progress.toFixed(0) + '%'); |
|||
}, |
|||
success: function(index, data) { |
|||
// Error |
|||
if (data.error) { |
|||
var errorInfo, errorDetails; |
|||
function displayError() { |
|||
$('#progress' + index).html([ |
|||
$('<strong/>').text(errorInfo), |
|||
(errorDetails === undefined) ? null : $('<br/>'), |
|||
(errorDetails === undefined) ? null : $('<div/>').text(errorDetails) |
|||
]); |
|||
} |
|||
// Excluding variants of `unknownerror`, we're going to ignore `error.info` since it mightn't be localized. |
|||
// See <https://github.com/Wikia/app/blob/release-886.001/includes/upload/UploadBase.php> for special cases. |
|||
if (data.error.code.startsWith('unknownerror')) { |
|||
errorInfo = data.error.info; |
|||
displayError(); |
|||
} else { |
|||
var messages = [data.error.code]; |
|||
if (data.error.code === 'verification-error') { |
|||
messages.push(data.error.details[0]); |
|||
} |
|||
loadMWMessagesIfMissing(messages).then(function () { |
|||
errorInfo = mw.msg(data.error.code); |
|||
if (data.error.code === 'verification-error') { |
|||
errorDetails = mw.msg.apply(null, data.error.details); |
|||
} else if (data.error.code === 'hookaborted') { |
|||
errorDetails = data.error.error; // TODO: Determine whether it's safe to assume this'll always be properly escaped HTML. |
|||
} |
|||
displayError(); |
|||
}); |
|||
} |
|||
return; |
|||
} |
|||
// Success |
|||
if(data.upload.result === 'Success') { |
|||
$('#progress' + index).text('File uploaded successfully.'); |
|||
return; |
|||
} |
|||
// Warnings |
|||
var msg; |
|||
if(data.upload.warnings.hasOwnProperty('was-deleted')) { |
|||
msg = 'Deleted file'; |
|||
} else if(data.upload.warnings.hasOwnProperty('duplicate')) { |
|||
msg = 'Duplicated file'; |
|||
} else if(data.upload.warnings.hasOwnProperty('exists')) { |
|||
msg = 'Existing file'; |
|||
} else { |
|||
msg = 'File uploaded successfully.'; |
|||
} |
|||
$('#progress' + index).text(msg); |
|||
} |
|||
}; |
|||
$(function(load) { |
|||
$.proxy(MultiUpload.preload(), MultiUpload); |
|||
}); |
|||
}); |
Latest revision as of 00:57, 15 May 2024
/* @authors Gguigui1, KhangND, Universal Omega */
mw.loader.using([
'mediawiki.user',
'mediawiki.api',
'jquery.client',
'jquery.spinner',
'ext.LinkSuggest'
], function() {
var groups = mw.config.get('wgUserGroups').join(),
config = mw.config.get([
'wgCanonicalSpecialPageName',
'wgNamespaceNumber',
'wgTitle',
'wgUserLanguage',
'wgUserName'
]);
mw.loader.implement("mediawiki.legacy.ajax", function($) {
window.sajax_debug_mode = !1;
window.sajax_request_type = 'GET';
window.sajax_debug = function(text) {
if (!sajax_debug_mode)
return false;
var e = document.getElementById('sajax_debug');
if (!e) {
e = document.createElement('p');
e.className = 'sajax_debug';
e.id = 'sajax_debug';
var b = document.getElementsByTagName('body')[0];
if (b.firstChild) {
b.insertBefore(e, b.firstChild);
} else {
b.appendChild(e);
}
}
var m = document.createElement('div');
m.appendChild(document.createTextNode(text));
e.appendChild(m);
return true;
}
;
window.sajax_init_object = function() {
sajax_debug('sajax_init_object() called..');
var A;
try {
A = new XMLHttpRequest();
} catch (e) {
try {
A = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
A = new ActiveXObject('Microsoft.XMLHTTP');
} catch (oc) {
A = null;
}
}
}
if (!A) {
sajax_debug('Could not create connection object.');
}
return A;
}
;
window.sajax_do_call = function(func_name, args, target) {
var i, x, n;
var uri;
var post_data;
uri = mw.util.wikiScript() + '?action=ajax';
if (sajax_request_type == 'GET') {
if (uri.indexOf('?') == -1) {
uri = uri + '?rs=' + encodeURIComponent(func_name);
} else {
uri = uri + '&rs=' + encodeURIComponent(func_name);
}
for (i = 0; i < args.length; i++) {
uri = uri + '&rsargs[]=' + encodeURIComponent(args[i]);
}
post_data = null;
} else {
post_data = 'rs=' + encodeURIComponent(func_name);
for (i = 0; i < args.length; i++) {
post_data = post_data + '&rsargs[]=' + encodeURIComponent(args[i]);
}
}
x = sajax_init_object();
if (!x) {
alert('AJAX not supported');
return false;
}
try {
x.open(sajax_request_type, uri, true);
} catch (e) {
if (window.location.hostname == 'localhost') {
alert("Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing.");
}
throw e;
}
if (sajax_request_type == 'POST') {
x.setRequestHeader('Method', 'POST ' + uri + ' HTTP/1.1');
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
}
x.setRequestHeader('Pragma', 'cache=yes');
x.setRequestHeader('Cache-Control', 'no-transform');
x.onreadystatechange = function() {
if (x.readyState != 4) {
return;
}
sajax_debug('received (' + x.status + ' ' + x.statusText + ') ' + x.responseText);
if (typeof (target) == 'function') {
target(x);
} else if (typeof (target) == 'object') {
if (target.tagName == 'INPUT') {
if (x.status == 200) {
target.value = x.responseText;
}
} else {
if (x.status == 200) {
target.innerHTML = x.responseText;
} else {
target.innerHTML = '<div class="error">Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')</div>';
}
}
} else {
alert('bad target for sajax_do_call: not a function or object: ' + target);
}
}
;
sajax_debug(func_name + ' uri = ' + uri + ' / post = ' + post_data);
x.send(post_data);
sajax_debug(func_name + ' waiting..');
delete x;
return true;
}
;
window.wfSupportsAjax = function() {
var request = sajax_init_object();
var supportsAjax = request ? true : !1;
delete request;
return supportsAjax;
}
;
;
}, {}, {});
function getLicensePreview(num) {
window.licenseSelectorCheck = function () {
var selector = document.getElementById("license" + num);
var selection = selector.options[selector.selectedIndex].value;
if (selector.selectedIndex > 0) {
if (selection == "") {
// Option disabled, but browser is broken and doesn't respect this
selector.selectedIndex = 0;
}
}
var UploadLicenseObj = {
'responseCache': {
'': ''
},
'fetchPreview': function (license) {
if (!mw.config.get('wgAjaxLicensePreview'))
return;
for (cached in this.responseCache) {
if (cached == license) {
this.showPreview(this.responseCache[license]);
return;
}
}
$('#license' + num).injectSpinner('license' + num);
var title = document.getElementById('imagename' + num).value;
if (!title)
title = 'File:Sample.jpg';
var url = mw.util.wikiScript('api')
+ '?action=parse&text={{' + encodeURIComponent(license) + '}}'
+ '&title=' + encodeURIComponent(title)
+ '&prop=text&pst&format=json';
var req = sajax_init_object();
req.onreadystatechange = function () {
if (req.readyState == 4 && req.status == 200) {
UploadLicenseObj.processResult(eval('(' + req.responseText + ')'), license);
}
};
req.open('GET', url, true);
req.send('');
},
'processResult': function (result, license) {
$.removeSpinner('license' + num);
this.responseCache[license] = result['parse']['text']['*'];
this.showPreview(this.responseCache[license]);
},
'showPreview': function (preview) {
var previewPanel = document.getElementById('mw-license-preview' + num);
if (previewPanel.innerHTML != preview)
previewPanel.innerHTML = preview;
}
};
// We might show a preview
UploadLicenseObj.fetchPreview(selection);
};
var wpLicense = document.getElementById('license' + num);
if (mw.config.get('wgAjaxLicensePreview') && wpLicense) {
// License selector check
wpLicense.onchange = licenseSelectorCheck;
// License selector table row
var wpLicenseRow = wpLicense.parentNode.parentNode;
var wpLicenseTbody = wpLicenseRow.parentNode;
var row = document.createElement('tr');
var td = document.createElement('td');
row.appendChild(td);
td = document.createElement('td');
td.id = 'mw-license-preview' + num;
row.appendChild(td);
wpLicenseTbody.insertBefore(row, wpLicenseRow.nextSibling);
}
}
// load protections
if(window.MultiUploadLoaded
|| config.wgUserName === null
|| ( !/autoconfirmed/.test(groups) && !/confirmed/.test(groups))) {
return;
} window.MultiUploadLoaded = true;
var storage = 'MultiUploadLicenses';
var content = $('#mw-content-text');
var allowTypes = $.map( mw.config.get("wgFileExtensions"), function(file_extensions) {
return('.' + file_extensions)
});
var style = {
block: {
display: 'inline-block',
margin: '0 8px'
},
textarea: {
width: '100%',
height: 150,
boxSizing: 'border-box',
resize: 'none'
}
};
function loadMWMessagesIfMissing(messages) {
var deferred = $.Deferred(),
missingMessages = messages.filter(function (message) { return !mw.messages.exists(message); });
if (!missingMessages.length) {
deferred.resolve();
} else {
$.get(mw.util.wikiScript('api'), {
format: 'json',
action: 'query',
meta: 'allmessages',
ammessages: missingMessages.join('|'),
amlang: config.wgUserLanguage
}).then(function (data) {
if ($.isArray(data.query.allmessages)) {
$.each(data.query.allmessages, function (_, message) {
if (message.missing !== '') {
mw.messages.set(message.name, message['*']);
}
});
}
deferred.resolve();
}, function () {
// Silently swallow failures; we don't want error reporting to stall just because we failed to fetch some messages.
deferred.resolve();
});
}
return deferred;
}
MultiUpload = {
input: $(), // available on init
editor: $(), // available on init
button: $(), // available on init
fileCount: 0,// available on create
preload: function(load) {
$.extend(this, window.MultiUploadoption || {});
this.load=load;
// creates Special page
if(
(config.wgNamespaceNumber === -1 && config.wgTitle === 'MultiUpload') ||
(config.wgCanonicalSpecialPageName === 'Blankpage' && /MultiUpload/.test($.getUrlVar('blankspecial')))
) {
$('#firstHeading').text('Multi Upload');
$('title').text('Multi Upload');
this.init();
}
},
init: function() {
content.empty().append([
$('<input>', {
id: 'fileinput',
type: 'file',
multiple: true,
accept: allowTypes.join(),
}),
$('<div>', {
id: 'editor',
css: { display: 'none' },
}),
$('<button>', {
id: 'go',
text: 'Update the form',
click: $.proxy(this.getLicenses, this)
})
]);
this.input = $('#fileinput');
this.editor = $('#editor');
this.button = $('#go');
},
notify: function(message, type) {
mw.notify(message, { type: type } );
},
getLicenses: function() {
var data = localStorage.getItem(storage);
if(data !== null) {
this.create(data);
var limit = this.max < this.fileCount ? this.max : this.fileCount;
for (i = 1; i <= limit; i++) {
getLicensePreview(i);
}
return;
}
$.get(mw.util.wikiScript('api'), {
action: 'query',
meta: 'allmessages',
ammessages: 'Licenses',
format: 'json'
}).done(
$.proxy(this.create, this)
).fail($.proxy(function(data) {
this.notify('Api Error : ' + data.error.info, 'error');
}), this);
},
create: function(data) {
// conditions
var formatError = false;
this.fileCount = this.input[0].files.length;
$(this.input[0].files).each(function(i, file) {
if(!new RegExp(allowTypes.join('|\\'), 'i').test(file.name)) {
formatError = true;
return;
}
});
if (formatError) {
this.notify('Unsupported file format', 'warn');
return;
}
if (!this.input[0].files) {
this.notify('This browser doesn't seem to support the `files` property of file inputs.', 'notify');
return;
}
if (this.fileCount === 0) {
this.notify('You have to choose a file to upload it', 'warn');
return;
}
if(!this.max
|| this.max < 0
|| this.max > 101
|| typeof this.max !== 'number') {
if (/staff|helper|util|bot-global/.test(groups)) {
this.max = 200;
} else if (/bureaucrat|bot/.test(groups)) {
this.max = 70;
} else if (/sysop/.test(groups)) {
this.max = 50;
} else if (/rollback/.test(groups)) {
this.max = 30;
} else {
this.max = 20;
}
}
if (!this.max) {
this.notify('A problem occurred, upload cancelled', 'error');
return;
}
// creates form
data = typeof data === 'object'
? data.query.allmessages[0]['*'].trim()
: data;
localStorage.setItem(storage, data); // retrieves licenses 1 time only
var licenses = data.split('\n');
var limit = this.max < this.fileCount ? this.max : this.fileCount;
for (i = 1; i <= limit; i++) {
$('<fieldset>', {
id: 'field' + i,
appendTo: this.editor,
append: [
$('<legend>', {
text: 'File n° ' + i
}),
$('<div>', {
css: style.block,
text: 'Name:',
append: $('<input>', {
type: 'text',
id: 'imagename' + i,
'class': 'imagename',
val: this.input[0].files[i - 1].name
})
}),
$('<div>', {
css: style.block,
text: 'License:',
append: $('<select>', {
id: 'license' + i,
'class': 'license',
append: $('<option>', {
val: 'none',
text: 'None selected'
})
})
}),
$('<div>', {
id: 'progress' + i,
css: style.block
}),
]
});
}
for (i = 0; i < licenses.length; i++) {
if (licenses[i].indexOf('**') === 0) {
var name = licenses[i].split('|')[0].replace('**', '').trim(),
text = licenses[i].split('|')[1];
$('<option>', {
val: name,
text: text,
selected: name == this.defaultlicense,
appendTo: $('.license').find('optgroup:last-child')
});
} else {
$('<optgroup>', {
label: licenses[i].replace('*', '').trim(),
appendTo: $('.license')
});
}
}
$('<div>', {
appendTo: this.editor,
text: 'Summaries:'
}),
$('<textarea>', {
appendTo: this.editor,
id: 'UploadDescription',
css: style.textarea,
}).linksuggest();
$('.license option[value="none"').attr("disabled", "true");
$('<button>', {
'class': 'secondary',
id: 'reset',
css: style.block,
text: 'Reset the form',
click: $.proxy(this.init, this),
appendTo: content
}),
$('<input>', {
type: 'checkbox',
id: 'ignorewarnings',
name: 'ignorewarnings',
appendTo: content
}),
$('<label>', {
'for': 'ignorewarnings',
text: 'Ignore warnings',
appendTo: content
});
jQuery(function ($) {
'use strict';
if ($('#UploadDescription').val()) {
return;
}
var params = {
action: 'parse',
page: 'Template:Default Upload Summary',
format: 'json',
prop: 'wikitext'
};
var api = new mw.Api();
api.get(params).done(function(data) {
$('#UploadDescription').val(data.parse.wikitext['*']);
});
$('.license option[value="none"]').attr("disabled", "true");
});
license = $('#license' + i).find('option:selected').val(),
this.input.attr('disabled', true);
this.editor.show();
this.button
.unbind('click')
.click($.proxy(this.upload, this))
.text('Upload All');
},
upload: function() {
content.find('*').attr('disabled', true);
$('#reset').removeAttr('disabled');
var obj = this;
var loop = function (i) {
if (i <= obj.fileCount) {
var file = obj.input[0].files[i - 1],
filename = $('#imagename' + i).val() || file.name,
license = $('#license' + i).find('option:selected').val(),
text = $('#UploadDescription').val();
if(license == "none"){
// alert('Licensing must be complete.');
// $('*').removeAttr('disabled');
// return;
}
if(license !== "none") text = '== Summary ==\n' + text + '\n\n== Licensing ==\n{{' + license + '}}\n';
obj.uploadFile(file, filename, text, i).always(function(){
loop(i+1);
});
}
};
loop(1);
},
uploadFile: function(fileToUpload, fileName, text, index) {
// https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
formdata = new FormData();
formdata.append('action', 'upload');
formdata.append('format', 'json');
formdata.append('filename', fileName);
formdata.append('token', mw.user.tokens.get('csrfToken'));
formdata.append('file', fileToUpload);
formdata.append('text', text);
formdata.append(
$('#ignorewarnings').prop('checked') ? 'ignorewarnings' : '', ''
);
// https://stackoverflow.com/a/8244082
return $.ajax({
url: mw.util.wikiScript('api'),
contentType: false,
processData: false,
type: 'POST',
data: formdata,
dataType: 'json',
xhr: function() { // https://stackoverflow.com/a/27030092
myXhr = $.ajaxSettings.xhr();
myXhr.upload.addEventListener('progress', function(e) {
MultiUpload.progress(index, e);
});
return myXhr;
},
success: $.proxy(function(data) {
this.success(index, data);
}, this),
error: $.proxy(function(_, __, error) {
this.notify(error, 'error');
}, this)
});
},
progress: function(index, e) {
var progress = e.loaded / e.total * 100;
$('#progress' + index).text(progress.toFixed(0) + '%');
},
success: function(index, data) {
// Error
if (data.error) {
var errorInfo, errorDetails;
function displayError() {
$('#progress' + index).html([
$('<strong/>').text(errorInfo),
(errorDetails === undefined) ? null : $('<br/>'),
(errorDetails === undefined) ? null : $('<div/>').text(errorDetails)
]);
}
// Excluding variants of `unknownerror`, we're going to ignore `error.info` since it mightn't be localized.
// See <https://github.com/Wikia/app/blob/release-886.001/includes/upload/UploadBase.php> for special cases.
if (data.error.code.startsWith('unknownerror')) {
errorInfo = data.error.info;
displayError();
} else {
var messages = [data.error.code];
if (data.error.code === 'verification-error') {
messages.push(data.error.details[0]);
}
loadMWMessagesIfMissing(messages).then(function () {
errorInfo = mw.msg(data.error.code);
if (data.error.code === 'verification-error') {
errorDetails = mw.msg.apply(null, data.error.details);
} else if (data.error.code === 'hookaborted') {
errorDetails = data.error.error; // TODO: Determine whether it's safe to assume this'll always be properly escaped HTML.
}
displayError();
});
}
return;
}
// Success
if(data.upload.result === 'Success') {
$('#progress' + index).text('File uploaded successfully.');
return;
}
// Warnings
var msg;
if(data.upload.warnings.hasOwnProperty('was-deleted')) {
msg = 'Deleted file';
} else if(data.upload.warnings.hasOwnProperty('duplicate')) {
msg = 'Duplicated file';
} else if(data.upload.warnings.hasOwnProperty('exists')) {
msg = 'Existing file';
} else {
msg = 'File uploaded successfully.';
}
$('#progress' + index).text(msg);
}
};
$(function(load) {
$.proxy(MultiUpload.preload(), MultiUpload);
});
});