MediaWiki:Gadget-morebits.js: Difference between revisions

Content deleted Content added
Repo at c35f1fd9: stabilize api properly uses watchlist parameter; remove quickForm.element.autoNWSW, unused
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 === 'null || errorCode === undefined') && ctx.retries++ < ctx.maxRetries) {
 
// 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
 
ctx.saveApi.post(); // give it another go!
// 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) {
// non-admin attempting to edit a protected page - this gives a friendlier message than the default
 
if (errorCode === 'protectedpage') {
case 'protectedpage':
ctx.statusElement.error('Failed to save edit: Page is protected');
// non-admin attempting to edit a protected page - this gives a friendlier message than the default
// check for absuefilter hits: disallowed or warning
ctx.statusElement.error('Failed to save edit: Page is protected');
} else if (errorCode.indexOf('abusefilter') === 0) {
break;
var desc = $(ctx.saveApi.getXML()).find('abusefilter').attr('description');
 
if (errorCode === 'abusefilter-disallowed') {
case 'abusefilter-disallowed':
ctx.statusElement.error('The edit was disallowed by the edit filter: "' + desc + '".');
ctx.statusElement.error('The edit was disallowed by the edit filter: "' + $(ctx.saveApi.getXML()).find('abusefilter').attr('description') + '".');
} else if (errorCode === 'abusefilter-warning') {
break;
ctx.statusElement.error([ 'A warning was returned by the edit filter: "', desc, '". If you wish to proceed with the edit, please carry it out again. This warning will not appear a second time.' ]);
 
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;
} else { // shouldn't happen but...
 
ctx.statusElement.error('The edit was disallowed by the edit filter.');
case 'spamblacklist':
}
// .find('matches') returns an array in case multiple items are blacklisted, we only return the first
// check for blacklist hits
var spam = $(ctx.saveApi.getXML()).find('spamblacklist').find('matches').children()[0].textContent;
} else if (errorCode === 'spamblacklist') {
ctx.statusElement.error('Could not save the page because the URL ' + spam + ' is on the spam blacklist');
// .find('matches') returns an array in case multiple items are blacklisted, we only return the first
break;
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');
} else { default:
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 = {};
 
/**
Morebits.wikitext.template = {
* Get the value of every parameter found in a given template
parse: function(text, start) {
* @param {string} text Wikitext containing a template
var count = -1;
* @param {number} [start=0] Index noting where in the text the template begins
var level = -1;
* @returns {Object} {name: templateName, parameters: {key: value}}
var equals = -1;
*/
var current = '';
Morebits.wikitext.parseTemplate = function(text, start) {
var result = {
start = start || 0;
name: '',
parameters: {}
};
var key, value;
 
var count = -1;
for (var i = start; i < text.length; ++i) {
var test3level = text.substr(i, 3)-1;
var equals = -1;
if (test3 === '{{{') {
var current += '{{{';
var result = {
i += 2;
name: '',
++level;
parameters: {}
continue;
};
}
var key, value;
if (test3 === '}}}') {
current += '}}}';
i += 2;
--level;
continue;
}
var test2 = text.substr(i, 2);
if (test2 === '{{' || test2 === '[[') {
current += test2;
++i;
++level;
continue;
}
if (test2 === ']]') {
current += ']]';
++i;
--level;
continue;
}
if (test2 === '}}') {
current += test2;
++i;
--level;
 
for (var i = start; i < text.length; ++i) {
if (level <= 0) {
var test3 = text.substr(i, 3);
if (count === -1) {
if (test3 === '{{{') {
result.name = current.substring(2).trim();
current ++count= '{{{';
}i else+= {2;
++level;
if (equals !== -1) {
continue;
key = current.substring(0, equals).trim();
}
value = current.substring(equals).trim();
if (test3 === '}}}') {
result.parameters[key] = value;
equalscurrent += -1'}}}';
}i else+= {2;
--level;
result.parameters[count] = current;
++countcontinue;
}
var test2 = text.substr(i, 2);
}
if (test2 === '{{' || test2 === '[[') {
break;
current += test2;
}
continue++i;
}++level;
continue;
}
if (test2 === ']]') {
current += ']]';
++i;
--level;
continue;
}
if (test2 === '}}') {
current += test2;
++i;
--level;
 
if (text.charAt(i) === '|' && level <= 0) {
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 + 1).trim();
result.parameters[key] = value;
equals = -1;
Line 3,938 ⟶ 3,940:
}
}
current = ''break;
} else if (equals === -1 && text.charAt(i) === '=' && level <= 0) {
equals = current.length;
current += text.charAt(i);
} else {
current += text.charAt(i);
}
continue;
}
 
if (text.charAt(i) === '|' && level <= 0) {
return result;
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;
};