errorType = 4;
break;
}
}
}
}
warn = window.fuw.warn;
warn.NFArticleNotFound = (errorType==1);
warn.NFArticleNotMainspace = (errorType==2);
warn.UserspaceDraft = ((errorType==2) && userspace);
warn.NFArticleDab = (errorType==4);
warn.NFArticleOK = (errorType==0);  
// fix links in error messages:
if (warn.NFArticleNotFound) {
fuwMakeWikilink(fuwGet('warningNFArticleNotFound').getElementsByTagName('A')[0], target, true);
}
else if (warn.NFArticleNotMainspace) {
fuwMakeWikilink(fuwGet('warningNFArticleNotMainspace').getElementsByTagName('A')[0], target);
}
else if (warn.NFArticleDab) {
fuwMakeWikilink(fuwGet('warningNFArticleDab').getElementsByTagName('A')[0], target);
}
else if (warn.NFArticleOK) {
fuwMakeWikilink(fuwGet('NFArticleOK').getElementsByTagName('A')[0], target);
}  
if (errorType==3) {
// automatically replace title with redirect target
window.fuw.ScriptForm.NFArticle.value = target;
// need to recursively call validation again now
//if (confirm(name + " is a redirect. Follow it to " + target + "?")) {
fuwValidateNFArticle;
//}
}
else {
fuwUpdateOptions;
}
}
});
};  
// ================================================
// manually reload script (just for testing)
// ================================================
function fuwReload {
mw.loader.load( 'http://localhost/script/uploadscript.js' );
fuwReset;
}  
// ================================================
// reset forms
// TO DO: add a button that actually triggers this.
// ================================================
function fuwReset {
var forms = mw.util.$content[0].getElementsByTagName('form');
for (i = 0; i < forms.length; i++) {
forms[i].reset;
window.fuw.warn = { };
window.fuw.opts = { };
}
fuwSetVisible('UploadScriptArea', true);
fuwSetVisible('fuwSuccess', false);
fuwSetVisible('fuwWaiting', false);
fuwUpdateOptions;
}  
// ===============================================
// convenience functions for string handling
// ===============================================
function fuwAppendLines(parts) {
// assemble a string from an array of strings.
// treat every second element as a conditional
// separator that will be included only if
// surrounding elements are non-empty.
var build = "";
for (var i = 0; i < parts.length; i += 2) {
if (parts[i]) {
if (build) build += parts[i - 1];
build += parts[i];
}
}
return build;
}
function fuwSurroundString(prefix, content, suffix) {
// put a prefix and a suffix on a string,
// if the input string is non-empty.
if (content)
return (prefix ? prefix : "") + content + (suffix ? suffix : "");
else return "";
}  
// ========================================================
// convenience function for accessing the contents of the
// dummy TargetIFrame
// ========================================================
function fuwGetDocumentFromIFrame(iframe) {
var doc = (iframe.contentDocument ? iframe.contentDocument : iframe.contentWindow.document);
if (doc.XMLDocument) {
doc = doc.XMLDocument;
}
return doc;
}
// ========================================================
// event handler for the dummy TargetIFrame's onload event.
// TO DO: expand stub to add real notification of success,
// link to new file page, instructions about how to include
// file in articles, etc.
// ========================================================
function fuwUploadCompleted {
var doc = fuwGetDocumentFromIFrame(fuwGet('TargetIFrame'));
if (doc) {
//alert(doc);
fuwSetVisible('successThumb', false);  
var fuw = window.fuw;
var name = fuw.opts.InputName;  
var uploads = doc.getElementsByTagName('upload');
var success = false;
for (i = uploads.length-1; i>=0; i--) {
if (uploads[i].getAttribute('result') == 'Success') {
success = true;
// need to get the real resulting filename here; might be different from the requested one in some cases.
name = uploads[i].getAttribute('filename');
break;
}
}
if (success) {  
// need another ajax call to check the file is actually there,
// and to retrieve its direct img url:
$.ajax({
url : mw.util.wikiScript( 'api' ),
type : 'GET',
dataType: 'xml',
traditional : true,
data: {
format: 'xml',
action: 'query',
titles: 'File:' + name,
prop : 'imageinfo',
iiprop: 'url',
iiurlwidth: 120
},
success: function(resp) {
// callback function, called when API query has succeeded:
// see if the request has returned info from an existing image:  
var foundImg = resp.getElementsByTagName('ii')[0];
if (foundImg) {  
// need this data for creating our own image link
var width = foundImg.getAttribute('thumbwidth');
var height = foundImg.getAttribute('thumbheight');
var thumbURL = foundImg.getAttribute('thumburl');
var lastUser = foundImg.getAttribute('user');
var descURL = foundImg.getAttribute('descriptionurl');  
// build the in the success message:
thumbDiv = fuwGet('successThumb');  
// make link point to description page:
var thumbA = thumbDiv.getElementsByTagName('a')[0];
thumbA.href = descURL;  
// insert the image itself:
var thumbImg = thumbDiv.getElementsByTagName('img')[0];
thumbImg.setAttribute('src', thumbURL);
thumbImg.setAttribute('width', width);
thumbImg.setAttribute('height', height);  
fuwSetVisible(thumbDiv, true);
}
}
});
fuwMakeWikilink(
fuwGet('fuwSuccessLink2').getElementsByTagName('a')[0],
'File:' + name);
fuwGet('placeholderExFilename1').innerHTML = name;
fuwGet('placeholderExFilename2').innerHTML = name;
fuwSetVisible('fuwSuccess', true);
fuwSetVisible('fuwWaiting', false);
}
else {
var err = doc.getElementsByTagName('error');
if (err) {
var info = err[0].getAttribute('info');
var details = err[0].getElementsByTagName('detail');
var add = "";
for (i = 0; i < details.length; i++) {
if (add.length > 0) add += ", ";
add += details[i].textContent;
}
if (add) {
info = info + " (" + add + ")";
}
alert("Upload failed: " + info);
}
else {
alert("Unknown error: upload may have failed.");
}
}
}
}  
// ========================================================
// clean out dummy IFrame before submitting a request
// ========================================================
function fuwResetTargetIFrame {
var doc = fuwGetDocumentFromIFrame(fuwGet('TargetIFrame'));
if (doc) {
while (doc.hasChildNodes) {
doc.removeChild(doc.firstChild);
}
}
}  
// ========================================================
// Event handler for the real submit button
// ========================================================
function fuwSubmitUpload {  
var fuw = window.fuw;
var frm = fuw.TargetForm;  
fuw.collectInput;
fuw.formatOutput(false);  
// we will use the iframe's onload event to trigger a function that
// adds success notification etc.
fuwResetTargetIFrame;
var ifr = fuwGet('TargetIFrame');
if (ifr.attachEvent) {
// workaround for IE, according to
// http://www.nczonline.net/blog/2009/09/15/iframes-onload-and-documentdomain/
ifr.attachEvent("onload", fuwUploadCompleted);
}
else {
// all other browsers
ifr.onload = fuwUploadCompleted;
}  
if (fuwTesting) {
fuwSetVisible('placeholderTestForm', false);
}
fuwSetVisible('UploadScriptArea', false);  
fuwMakeWikilink(
fuwGet('fuwSuccessLink').getElementsByTagName('a')[0], 'File:' + fuw.opts.InputName);
fuwSetVisible('fuwWaiting', true);  
frm.submit;
var opts = window.fuw.opts;
// the API won't overwrite the description page text while overwriting
// a file, which is really, really, really annoying and stupid.
// So in the opts.OverwriteDifferent scenario, we need to edit
// the description page through a separate ajax call. Dang.
if (opts.OverwriteDifferent) {
$.ajax({
url : mw.util.wikiScript('api'),
type : 'POST',
dataType : 'xml',
data : {
format : 'xml',
action : 'edit',
title : 'File:' + opts.InputName,
token : mw.user.tokens.get('csrfToken'),
summary : opts.EditSummary,
text : ($('#TargetForm .[name="text"]')[0]).value  
}
});
}
}  
// =======================================================
// Event handler for the Commons submit button
// =======================================================
function fuwSubmitCommons {
var fuw = window.fuw;
fuw.collectInput;
var url = fuw.formatOutput(true);