Skip to content

Commit

Permalink
morebits: deletePage: return promises and overhaul callback arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Dec 10, 2019
1 parent 43d36e7 commit 50c1814
Showing 1 changed file with 29 additions and 32 deletions.
61 changes: 29 additions & 32 deletions morebits.js
Expand Up @@ -2343,25 +2343,29 @@ Morebits.wiki.page = function(pageName, currentAction) {
* Deletes a page (for admins only)
* @param {Function} [onSuccess] - callback function to run on success (optional)
* @param {Function} [onFailure] - callback function to run on failure (optional)
* @returns {promise} resolved or rejected with the Morebits.wiki.page object, errorCode
* and errorText (the latter two being undefined on success)
*/
this.deletePage = function(onSuccess, onFailure) {
ctx.onDeleteSuccess = onSuccess;
ctx.onDeleteSuccess = onSuccess || emptyFunction;
ctx.onDeleteFailure = onFailure || emptyFunction;

var fnDeleteReturnError = function (errorCode, errorText) {
ctx.statusElement.error(errorText);
ctx.onDeleteFailure.call(this, this, errorCode, errorText);
return $.Deferred().rejectWith(this, [this, errorCode, errorText]);
};

// if a non-admin tries to do this, don't bother
if (!Morebits.userIsInGroup('sysop')) {
ctx.statusElement.error('Cannot delete page: only admins can do that');
ctx.onDeleteFailure(this);
return;
return fnDeleteReturnError.call(this, 'permissiondenied', 'Cannot delete page: only admins can do that');
}
if (!ctx.editSummary) {
ctx.statusElement.error('Internal error: delete reason not set before delete (use setEditSummary function)!');
ctx.onDeleteFailure(this);
return;
return fnDeleteReturnError.call(this, 'int-noreason', 'Internal error: delete reason not set before delete (use setEditSummary function)!');
}

if (fnCanUseMwUserToken('delete')) {
fnProcessDelete.call(this, this);
return fnProcessDelete.call(this).catch(fnDeleteReturnError);
} else {
var query = {
action: 'query',
Expand All @@ -2374,9 +2378,10 @@ Morebits.wiki.page = function(pageName, currentAction) {
query.redirects = ''; // follow all redirects
}

ctx.deleteApi = new Morebits.wiki.api('retrieving delete token...', query, fnProcessDelete, ctx.statusElement, ctx.onDeleteFailure);
ctx.deleteApi = new Morebits.wiki.api('retrieving delete token...', query);
ctx.deleteApi.setStatusElement(ctx.statusElement);
ctx.deleteApi.setParent(this);
ctx.deleteApi.post();
return ctx.deleteApi.post().then(fnProcessDelete).catch(fnDeleteReturnError);
}
};

Expand Down Expand Up @@ -2887,9 +2892,7 @@ Morebits.wiki.page = function(pageName, currentAction) {
var xml = ctx.deleteApi.getXML();

if ($(xml).find('page').attr('missing') === '') {
ctx.statusElement.error('Cannot delete the page, because it no longer exists');
ctx.onDeleteFailure(this);
return;
return $.Deferred().rejectWith(this, ['missingtitle', 'Cannot delete the page, because it no longer exists']);
}

// extract protection info
Expand All @@ -2898,16 +2901,12 @@ Morebits.wiki.page = function(pageName, currentAction) {
!confirm('You are about to delete the fully protected page "' + ctx.pageName +
(editprot.attr('expiry') === 'infinity' ? '" (protected indefinitely)' : '" (protection expiring ' + editprot.attr('expiry') + ')') +
'. \n\nClick OK to proceed with the deletion, or Cancel to skip this deletion.')) {
ctx.statusElement.error('Deletion of fully protected page was aborted.');
ctx.onDeleteFailure(this);
return;
return $.Deferred().rejectWith(this, ['int-aborted', 'Deletion of fully protected page was aborted.']);
}

token = $(xml).find('page').attr('deletetoken');
if (!token) {
ctx.statusElement.error('Failed to retrieve delete token.');
ctx.onDeleteFailure(this);
return;
return $.Deferred().rejectWith(this, ['notoken', 'Failed to retrieve delete token']);
}

pageTitle = $(xml).find('page').attr('title');
Expand All @@ -2923,9 +2922,11 @@ Morebits.wiki.page = function(pageName, currentAction) {
query.watch = 'true';
}

ctx.deleteProcessApi = new Morebits.wiki.api('deleting page...', query, ctx.onDeleteSuccess, ctx.statusElement, fnProcessDeleteError);
ctx.deleteProcessApi = new Morebits.wiki.api('deleting page...', query, function() {
ctx.onDeleteSuccess.call(this, this);
}, ctx.statusElement);
ctx.deleteProcessApi.setParent(this);
ctx.deleteProcessApi.post();
return ctx.deleteProcessApi.post().catch(fnProcessDeleteError);
};

// callback from deleteProcessApi.post()
Expand All @@ -2937,25 +2938,21 @@ Morebits.wiki.page = function(pageName, currentAction) {
if (errorCode === 'internal_api_error_DBQueryError' && ctx.retries++ < ctx.maxRetries) {
ctx.statusElement.info('Database query error, retrying');
--Morebits.wiki.numberOfActionsLeft; // allow for normal completion if retry succeeds
ctx.deleteProcessApi.post(); // give it another go!
return ctx.deleteProcessApi.post().catch(fnProcessDeleteError); // give it another go!
} else if ((errorCode === 'badtoken' || errorCode === 'notoken') && ctx.retries++ < ctx.maxRetries) {
ctx.statusElement.info('Invalid token, retrying');
--Morebits.wiki.numberOfActionsLeft;
fnGetToken().then(function(token) {
return fnGetToken().then(function(token) {
ctx.deleteProcessApi.query.token = token;
ctx.deleteProcessApi.post();
return ctx.deleteProcessApi.post().catch(fnProcessDeleteError);
});
} else if (errorCode === 'missingtitle') {
ctx.statusElement.error('Cannot delete the page, because it no longer exists');
if (ctx.onDeleteFailure) {
ctx.onDeleteFailure.call(this, ctx.deleteProcessApi); // invoke callback
}
return $.Deferred().rejectWith(this, ['missingtitle', 'Cannot delete the page, because it no longer exists']);

// hard error, give up
} else {
ctx.statusElement.error('Failed to delete the page: ' + ctx.deleteProcessApi.getErrorText());
if (ctx.onDeleteFailure) {
ctx.onDeleteFailure.call(this, ctx.deleteProcessApi); // invoke callback
}
return $.Deferred().rejectWith(this, [errorCode, 'Failed to delete the page: ' +
ctx.deleteProcessApi.getErrorText()]);
}
};

Expand Down

0 comments on commit 50c1814

Please sign in to comment.