MediaWiki:Common.js: Difference between revisions
Append uploads to Training materials/Library subpage |
Fix training-materials upload errors; library append messaging |
||
| Line 3,350: | Line 3,350: | ||
var next; | var next; | ||
var trimmed = $.trim(text); | var trimmed = $.trim(text); | ||
var already = trimmed.indexOf('[[Media:' + filename + '|') !== -1 || | |||
trimmed.indexOf('[[File:' + filename + '|') !== -1; | |||
if (already) { | |||
return { edit: { result: 'Success', nochange: true } }; | |||
} | |||
if (!trimmed || trimmed.indexOf(PLACEHOLDER) !== -1) { | if (!trimmed || trimmed.indexOf(PLACEHOLDER) !== -1) { | ||
next = bullet + '\n'; | next = bullet + '\n'; | ||
} else { | } else { | ||
next = trimmed + '\n' + bullet + '\n'; | next = trimmed + '\n' + bullet + '\n'; | ||
| Line 3,381: | Line 3,383: | ||
}); | }); | ||
}); | }); | ||
} | |||
function failMessage(code, data) { | |||
if (data && data.error && data.error.info) { | |||
return data.error.info; | |||
} | |||
if (data && data.exception && data.exception.message) { | |||
return data.exception.message; | |||
} | |||
if (code && typeof code === 'object' && code.message) { | |||
return code.message; | |||
} | |||
if (typeof code === 'string' && code && code !== 'http' && code !== 'error' && code !== 'timeout') { | |||
return code; | |||
} | |||
if (data && data.xhr && data.xhr.status) { | |||
return 'Upload failed (HTTP ' + data.xhr.status + '). Try again, or use Special:Upload.'; | |||
} | |||
return 'Upload failed (connection dropped or timed out). Try again, or use Special:Upload.'; | |||
} | } | ||
| Line 3,406: | Line 3,427: | ||
setStatus('Uploading…'); | setStatus('Uploading…'); | ||
var api = new mw.Api(); | var api = new mw.Api({ ajax: { timeout: 0 } }); | ||
var fileText = 'Camp Ritchie / MITC training material.'; | var fileText = 'Camp Ritchie / MITC training material.'; | ||
var uploadedName = null; | |||
var uploadParams = { | var uploadParams = { | ||
filename: file.name, | filename: file.name, | ||
comment: 'Uploaded to Training materials library', | comment: 'Uploaded to Training materials library', | ||
text: fileText, | text: fileText, | ||
ignorewarnings: true | ignorewarnings: true | ||
}; | }; | ||
api.postWithToken('csrf', uploadParams, { | |||
function doUpload() { | |||
/* Multipart + XHR progress (api.upload alone often has no progress events). */ | |||
return api.postWithToken('csrf', $.extend({ | |||
action: 'upload', | |||
file: file, | |||
format: 'json' | |||
}, uploadParams), { | |||
contentType: 'multipart/form-data', | |||
} | timeout: 0, | ||
} | xhr: function () { | ||
var xhr = $.ajaxSettings.xhr(); | |||
if (xhr.upload) { | |||
xhr.upload.addEventListener('progress', function (ev) { | |||
if (ev.lengthComputable) { | |||
setProgress(ev.loaded, ev.total); | |||
} | |||
}); | |||
} | |||
return xhr; | |||
} | } | ||
}); | |||
} | |||
doUpload().then(function (data) { | |||
if (! | uploadedName = data.upload && data.upload.filename; | ||
if (!uploadedName) { | |||
throw new Error((data.error && data.error.info) || 'Upload failed'); | throw new Error((data.error && data.error.info) || 'Upload failed'); | ||
} | } | ||
setProgress(file.size, file.size); | |||
$progress.addClass('is-indeterminate'); | $progress.addClass('is-indeterminate'); | ||
$progressLabel.text('Finishing…'); | $progressLabel.text('Finishing…'); | ||
setStatus('Adding to Library list…'); | setStatus('Adding to Library list…'); | ||
return appendToLibrary(api, | return appendToLibrary(api, uploadedName, displayTitle, note); | ||
}).done(function () { | }).done(function () { | ||
$progress.removeClass('is-indeterminate'); | $progress.removeClass('is-indeterminate'); | ||
$progressLabel.text('100% · done'); | $progressLabel.text('100% · done'); | ||
| Line 3,448: | Line 3,476: | ||
window.location.reload(); | window.location.reload(); | ||
}).fail(function (code, data) { | }).fail(function (code, data) { | ||
var err = (data | var err = failMessage(code, data); | ||
if (uploadedName) { | |||
err = 'File uploaded as “' + uploadedName + '”, but the Library list was not updated: ' + err + | |||
' Refresh this page; if it is missing from the list, say so and we can add it.'; | |||
} | } | ||
setStatus(String(err), true); | setStatus(String(err), true); | ||