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 if (trimmed.indexOf('[[Media:' + filename + '|') !== -1 ||
trimmed.indexOf('[[File:' + filename + '|') !== -1) {
next = text;
} 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 = {
action: 'upload',
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
file: file,
format: 'json'
};
};
api.postWithToken('csrf', uploadParams, {
 
contentType: 'multipart/form-data',
function doUpload() {
timeout: 0,
/* Multipart + XHR progress (api.upload alone often has no progress events). */
xhr: function () {
return api.postWithToken('csrf', $.extend({
var xhr = $.ajaxSettings.xhr();
action: 'upload',
if (xhr.upload) {
file: file,
xhr.upload.addEventListener('progress', function (evt) {
format: 'json'
if (evt.lengthComputable) {
}, uploadParams), {
setProgress(evt.loaded, evt.total);
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;
}
}
return xhr;
});
}
}
}).then(function (data) {
 
var uploaded = data.upload && data.upload.filename;
doUpload().then(function (data) {
if (!uploaded) {
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');
$progressFill.css('width', '100%');
$progressLabel.text('Finishing…');
$progressLabel.text('Finishing…');
setStatus('Adding to Library list…');
setStatus('Adding to Library list…');
return appendToLibrary(api, uploaded, displayTitle, note);
return appendToLibrary(api, uploadedName, displayTitle, note);
}).done(function () {
}).done(function () {
setProgress(file.size, file.size);
$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 && data.error && data.error.info) ||
var err = failMessage(code, data);
(data && data.exception && data.exception.message) ||
if (uploadedName) {
'';
err = 'File uploaded as “' + uploadedName + '”, but the Library list was not updated: ' + err +
if (!err || err === 'http' || err === 'error' || err === 'timeout') {
' Refresh this page; if it is missing from the list, say so and we can add it.';
err = 'Upload failed (connection dropped or timed out). For large PDFs, wait and retry; if it keeps failing, try a smaller file or Special:Upload.';
}
}
setStatus(String(err), true);
setStatus(String(err), true);