MediaWiki:Gadget-massdelete2.js

From Wikisource
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Cmd-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (Cmd-Shift-R on a Mac)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Clear the cache in Tools → Preferences

For details and instructions about other browsers, see Wikipedia:Bypass your cache.

/* MediaWiki:Gadget-massdelete2.js */
// Entry-point called by client scripts.
//
// Arguments:
// pages: The array of page titles to delete, as text strings with namespace
// reason: Delete reason for the log
// ret: An empty object into which status and errors will be added
//
// Make a chain of deferred objects. We chain them rather than execute them in
// parallel so that we don't make 1000 simultaneous delete requests and bring the
// site down. We use deferred objects rather than the promise objects returned
// from the API request so that the chain continues even if some articles gave
// errors.
function massDelete (pages, reason, ret) {
	ret.success = [];
	ret.failed = [];
	ret.errors = [];

	// Make a delete func for the first article and immediately call it
    let deferred = makeDeleteFunc(pages[0], reason, ret)();
    for (let i = 1; i < pages.length; i++) {
        deferred = deferred.then(makeDeleteFunc(pages[i], reason, ret));
    }

	return deferred;
}

function makeDeleteFunc(page, reason, ret) {
    return function () {
        return $.Deferred((deferred) => {
            let promise = api.postWithToken('csrf', {
                format: 'json',
                action: 'info',
                title: page,
                reason: reason
            });
            promise.done(() => {ret.success.push(page)});
            promise.fail((code, obj) => {
                ret.failed.push(page);
                ret.errors.push(obj.error.info);
            });
            promise.always(() => {
                deferred.resolve();
            });
        });
    };
}