MediaWiki:Gadget-morebits.js: Difference between revisions
Content deleted Content added
Amorymeltzer (talk | contribs) Repo at c35f1fd9: stabilize api properly uses watchlist parameter; remove quickForm.element.autoNWSW, unused |
Amorymeltzer (talk | contribs) Repo at 540dcd6a: wait for 2 seconds after a connection error; fix error handling on no internet; Rename wikitext.template.parse to wikitext.parseTemplate |
||
Line 3,240:
// check for network or server error
} else if ((errorCode ===
// the error might be transient, so try again
ctx.statusElement.info('Save failed, retrying in 2 seconds ...');
--Morebits.wiki.numberOfActionsLeft; // allow for normal completion if retry succeeds
// wait for sometime for client to regain connnectivity
sleep(2000).then(function() {
ctx.saveApi.post(); // give it another go!
});
// hard error, give up
} else {
switch (errorCode) {
case 'protectedpage':
// non-admin attempting to edit a protected page - this gives a friendlier message than the default
ctx.statusElement.error('Failed to save edit: Page is protected');
break;
case 'abusefilter-disallowed':
ctx.statusElement.error('The edit was disallowed by the edit filter: "' + $(ctx.saveApi.getXML()).find('abusefilter').attr('description') + '".');
break;
case 'abusefilter-warning':
ctx.statusElement.error([ 'A warning was returned by the edit filter: "', $(ctx.saveApi.getXML()).find('abusefilter').attr('description'), '". If you wish to proceed with the edit, please carry it out again. This warning will not appear a second time.' ]);
// We should provide the user with a way to automatically retry the action if they so choose -
// I can't see how to do this without creating a UI dependency on Morebits.wiki.page though -- TTO
break;
case 'spamblacklist':
// .find('matches') returns an array in case multiple items are blacklisted, we only return the first
var spam = $(ctx.saveApi.getXML()).find('spamblacklist').find('matches').children()[0].textContent;
ctx.statusElement.error('Could not save the page because the URL ' + spam + ' is on the spam blacklist');
break;
ctx.statusElement.error('Failed to save edit: ' + ctx.saveApi.getErrorText());
}
ctx.editMode = 'all'; // cancel append/prepend/revert modes
if (ctx.onSaveFailure) {
Line 3,778 ⟶ 3,786:
ctx.stabilizeProcessApi.post();
};
var sleep = function(milliseconds) {
var deferred = $.Deferred();
setTimeout(deferred.resolve, milliseconds);
return deferred;
};
}; // end Morebits.wiki.page
Line 3,859 ⟶ 3,874:
Morebits.wikitext = {};
/**
* Get the value of every parameter found in a given template
* @param {string} text Wikitext containing a template
* @param {number} [start=0] Index noting where in the text the template begins
* @returns {Object} {name: templateName, parameters: {key: value}}
*/
Morebits.wikitext.parseTemplate = function(text, start) {
start = start || 0;
var count = -1;
var equals = -1;
var result = {
name: '',
parameters: {}
};
var key, value;
for (var i = start; i < text.length; ++i) {
var test3 = text.substr(i, 3);
if (test3 === '{{{') {
++level;
continue;
}
if (test3 === '}}}') {
--level;
var test2 = text.substr(i, 2);
if (test2 === '{{' || test2 === '[[') {
current += test2;
continue;
}
if (test2 === ']]') {
current += ']]';
++i;
--level;
continue;
}
if (test2 === '}}') {
current += test2;
++i;
--level;
if (
if (count === -1) {
result.name = current.substring(2).trim();
Line 3,930 ⟶ 3,932:
if (equals !== -1) {
key = current.substring(0, equals).trim();
value = current.substring(equals
result.parameters[key] = value;
equals = -1;
Line 3,938 ⟶ 3,940:
}
}
}
continue;
}
if (text.charAt(i) === '|' && level <= 0) {
if (count === -1) {
result.name = current.substring(2).trim();
++count;
} else {
if (equals !== -1) {
key = current.substring(0, equals).trim();
value = current.substring(equals + 1).trim();
result.parameters[key] = value;
equals = -1;
} else {
result.parameters[count] = current;
++count;
}
}
current = '';
} else if (equals === -1 && text.charAt(i) === '=' && level <= 0) {
equals = current.length;
current += text.charAt(i);
} else {
current += text.charAt(i);
}
}
return result;
};
|