Jump to content

MediaWiki:Gadget-twinklefluff.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Repo at 16e077bc: Partially revert type fixing from fefd1cbc
Repo at 6a11ff8: Add rollbackInPlace pref to not reload contribs/RC after reversion; Restructure twinklerevert links to use Twinkle.fluff.revert, remove auto function; Consolidate and genericize link creation functions; Don't say reversion completed if there's no change; Don't open the talkpage unless/until the reversion is complete; Add links to history pages, off by default; Add ability to enable rollback links on Recentchanges(linked); Use the provided diff url to get oldid
Line 9: Line 9:
*** twinklefluff.js: Revert/rollback module
*** twinklefluff.js: Revert/rollback module
****************************************
****************************************
* Mode of invocation: Links on history, contributions, and diff pages
* Mode of invocation: Links on contributions, recent changes, history, and diff pages
* Active on: Diff pages, history pages, contributions pages
* Active on: Diff pages, history pages, Special:RecentChanges(Linked),
and Special:Contributions
*/
*/


Line 28: Line 29:
];
];


if (mw.util.getParamValue('twinklerevert')) {
if (mw.config.get('wgIsProbablyEditable')) {
// Return if the user can't edit the page in question
if (!mw.config.get('wgIsProbablyEditable')) {
alert("Unable to edit the page, it's probably protected.");
} else {
Twinkle.fluff.auto();
}
} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Contributions') {
Twinkle.fluff.addLinks.contributions();
} else if (mw.config.get('wgIsProbablyEditable')) {
// Only proceed if the user can actually edit the page
// Only proceed if the user can actually edit the page
// in question (ignored for contributions, see #632).
// in question (ignored for contributions, see #632).
Line 50: Line 42:
} else if (mw.config.get('wgAction') === 'view' && mw.config.get('wgCurRevisionId') !== mw.config.get('wgRevisionId')) {
} else if (mw.config.get('wgAction') === 'view' && mw.config.get('wgCurRevisionId') !== mw.config.get('wgRevisionId')) {
Twinkle.fluff.addLinks.oldid();
Twinkle.fluff.addLinks.oldid();
} else if (mw.config.get('wgAction') === 'history') {
Twinkle.fluff.addLinks.history();
}
} else if (mw.config.get('wgNamespaceNumber') === -1) {
Twinkle.fluff.skipTalk = !Twinkle.getPref('openTalkPageOnAutoRevert');
Twinkle.fluff.rollbackInPlace = Twinkle.getPref('rollbackInPlace');

if (mw.config.get('wgCanonicalSpecialPageName') === 'Contributions') {
Twinkle.fluff.addLinks.contributions();
} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Recentchanges' || mw.config.get('wgCanonicalSpecialPageName') === 'Recentchangeslinked') {
// Reload with recent changes updates
// structuredChangeFilters.ui.initialized is just on load
mw.hook('wikipage.content').add(function(item) {
if (item.is('div')) {
Twinkle.fluff.addLinks.recentchanges();
}
});
}
}
}
}
};
};


Twinkle.fluff.skipTalk = null;
Twinkle.fluff.rollbackInPlace = null;


// Consolidated construction of fluff links
Twinkle.fluff.spanTag = function twinklefluffspanTag(color, content) {
Twinkle.fluff.linkBuilder = {
var span = document.createElement('span');
spanTag: function(color, content) {
span.style.color = color;
span.appendChild(document.createTextNode(content));
var span = document.createElement('span');
span.style.color = color;
return span;
span.appendChild(document.createTextNode(content));
};
return span;
},


Twinkle.fluff.buildLink = function twinklefluffbuildLink(color, text) {
buildLink: function(color, text) {
var link = document.createElement('a');
var link = document.createElement('a');
link.appendChild(Twinkle.fluff.spanTag('Black', '['));
link.appendChild(Twinkle.fluff.linkBuilder.spanTag('Black', '['));
link.appendChild(Twinkle.fluff.spanTag(color, text));
link.appendChild(Twinkle.fluff.linkBuilder.spanTag(color, text));
link.appendChild(Twinkle.fluff.spanTag('Black', ']'));
link.appendChild(Twinkle.fluff.linkBuilder.spanTag('Black', ']'));
return link;
link.href = '#';
return link;
};
},


/**
// Build [restore this revision] links
* @param {string} vandal - Username of the editor being reverted (required)
Twinkle.fluff.restoreThisRevision = function (element, revType) {
* @param {boolean} inline - True to create two links in a span, false
var revertToRevision = document.createElement('div');
* to create three links in a div (optional)
revertToRevision.setAttribute('id', 'tw-revert-to-' + (revType === 'wgDiffNewId' ? 'n' : 'o') + 'revision');
* @param {number|string} [rev=wgCurRevisionId] - Revision ID being reverted (optional)
revertToRevision.style.fontWeight = 'bold';
* @param {string} [page=wgPageName] - Page being reverted (optional)
*/
rollbackLinks: function(vandal, inline, rev, page) {
var elem = inline ? 'span' : 'div';
var revNode = document.createElement(elem);


rev = parseInt(rev, 10);
var revertToRevisionLink = Twinkle.fluff.buildLink('SaddleBrown', 'restore this version');
if (rev) {
revertToRevisionLink.href = '#';
revNode.setAttribute('id', 'tw-revert' + rev);
$(revertToRevisionLink).click(function() {
} else {
Twinkle.fluff.revertToRevision(mw.config.get(revType).toString());
revNode.setAttribute('id', 'tw-revert');
});
}
revertToRevision.appendChild(revertToRevisionLink);


var title = document.getElementById(element).parentNode;
var normNode = document.createElement('strong');
var vandNode = document.createElement('strong');
title.insertBefore(revertToRevision, title.firstChild);
};


var normLink = Twinkle.fluff.linkBuilder.buildLink('SteelBlue', 'rollback');
var vandLink = Twinkle.fluff.linkBuilder.buildLink('Red', 'vandalism');


Twinkle.fluff.auto = function twinklefluffauto() {
$(normLink).click(function() {
Twinkle.fluff.revert('norm', vandal, rev, page);
if (parseInt(mw.util.getParamValue('oldid'), 10) !== mw.config.get('wgCurRevisionId')) {
Twinkle.fluff.disableLinks(revNode);
// not latest revision
});
alert("Can't rollback, page has changed in the meantime.");
$(vandLink).click(function() {
return;
Twinkle.fluff.revert('vand', vandal, rev, page);
}
Twinkle.fluff.disableLinks(revNode);
});


vandNode.appendChild(vandLink);
var vandal = $('#mw-diff-ntitle2').find('a.mw-userlink').text();
normNode.appendChild(normLink);


var separator = inline ? ' ' : ' || ';
Twinkle.fluff.revert(mw.util.getParamValue('twinklerevert'), vandal, true);

if (!inline) {
var agfNode = document.createElement('strong');
var agfLink = Twinkle.fluff.linkBuilder.buildLink('DarkOliveGreen', 'rollback (AGF)');
$(agfLink).click(function() {
Twinkle.fluff.revert('agf', vandal, rev, page);
// Twinkle.fluff.disableLinks(revNode); // rollbackInPlace not relevant for any inline situations
});
agfNode.appendChild(agfLink);
revNode.appendChild(agfNode);
}
revNode.appendChild(document.createTextNode(separator));
revNode.appendChild(normNode);
revNode.appendChild(document.createTextNode(separator));
revNode.appendChild(vandNode);

return revNode;

},

// Build [restore this revision] links
restoreThisRevisionLink: function(revisionRef, inline) {
// If not a specific revision number, should be wgDiffNewId/wgDiffOldId/wgRevisionId
revisionRef = typeof revisionRef === 'number' ? revisionRef : mw.config.get(revisionRef);

var elem = inline ? 'span' : 'div';
var revertToRevisionNode = document.createElement(elem);

revertToRevisionNode.setAttribute('id', 'tw-revert-to-' + revisionRef);
revertToRevisionNode.style.fontWeight = 'bold';

var revertToRevisionLink = Twinkle.fluff.linkBuilder.buildLink('SaddleBrown', 'restore this version');
$(revertToRevisionLink).click(function() {
Twinkle.fluff.revertToRevision(revisionRef);
});

if (inline) {
revertToRevisionNode.appendChild(document.createTextNode(' '));
}
revertToRevisionNode.appendChild(revertToRevisionLink);
return revertToRevisionNode;
}
};
};



Twinkle.fluff.addLinks = {
Twinkle.fluff.addLinks = {
Line 112: Line 178:
(mw.config.get('wgUserName') === username && Twinkle.getPref('showRollbackLinks').indexOf('mine') !== -1)) {
(mw.config.get('wgUserName') === username && Twinkle.getPref('showRollbackLinks').indexOf('mine') !== -1)) {
var list = $('#mw-content-text').find('ul li:has(span.mw-uctop):has(.mw-changeslist-diff)');
var list = $('#mw-content-text').find('ul li:has(span.mw-uctop):has(.mw-changeslist-diff)');

var revNode = document.createElement('strong');
var revLink = Twinkle.fluff.buildLink('SteelBlue', 'rollback');
revNode.appendChild(revLink);

var revVandNode = document.createElement('strong');
var revVandLink = Twinkle.fluff.buildLink('Red', 'vandalism');
revVandNode.appendChild(revVandLink);


list.each(function(key, current) {
list.each(function(key, current) {
// revid is also available in the href of both
var href = $(current).find('.mw-changeslist-diff').attr('href');
// .mw-changeslist-date or .mw-changeslist-diff
current.appendChild(document.createTextNode(' '));
var tmpNode = revNode.cloneNode(true);
var page = $(current).find('.mw-contributions-title').text();
current.appendChild(Twinkle.fluff.linkBuilder.rollbackLinks(username, true, current.dataset.mwRevid, page));
tmpNode.firstChild.setAttribute('href', href + '&twinklerevert=norm');
current.appendChild(tmpNode);
current.appendChild(document.createTextNode(' '));
tmpNode = revVandNode.cloneNode(true);
tmpNode.firstChild.setAttribute('href', href + '&twinklerevert=vand');
current.appendChild(tmpNode);
});
});
}
}
}
},

recentchanges: function() {
if (Twinkle.getPref('showRollbackLinks').indexOf('recent') !== -1) {
// Latest and revertable (not page creations, logs, categorizations, etc.)
var list = $('.mw-changeslist .mw-changeslist-last.mw-changeslist-src-mw-edit');
// Exclude top-level header if "group changes" preference is used
// and find only individual lines or nested lines
list = list.not('.mw-rcfilters-ui-highlights-enhanced-toplevel').find('.mw-changeslist-line-inner, td.mw-enhanced-rc-nested');

list.each(function(key, current) {
var vandal = $(current).find('.mw-userlink').text();
var href = $(current).find('.mw-changeslist-diff').attr('href');
var rev = mw.util.getParamValue('diff', href);
var page = current.dataset.targetPage;
current.appendChild(Twinkle.fluff.linkBuilder.rollbackLinks(vandal, true, rev, page));
});
}
},

history: function() {
if (Twinkle.getPref('showRollbackLinks').indexOf('history') !== -1) {
// All revs
var histList = $('#pagehistory li').toArray();

// On first page of results, so add revert/rollback
// links to the top revision
if (!$('.mw-firstlink').length) {
var first = histList.shift();
var vandal = first.querySelector('.mw-userlink').text;

first.appendChild(Twinkle.fluff.linkBuilder.rollbackLinks(vandal, true));
}

// oldid
histList.forEach(function(rev) {
// From restoreThisRevision, non-transferable

var href = rev.querySelector('.mw-changeslist-date').href;
var oldid = parseInt(mw.util.getParamValue('oldid', href), 10);

rev.appendChild(Twinkle.fluff.linkBuilder.restoreThisRevisionLink(oldid, true));
});


}
}
},
},
Line 160: Line 259:
if (mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId'))) {
if (mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId'))) {
// Add a [restore this revision] link to the older revision
// Add a [restore this revision] link to the older revision
Twinkle.fluff.restoreThisRevision('mw-diff-otitle1', 'wgDiffOldId');
var oldTitle = document.getElementById('mw-diff-otitle1').parentNode;
oldTitle.insertBefore(Twinkle.fluff.linkBuilder.restoreThisRevisionLink('wgDiffOldId'), oldTitle.firstChild);
}
}


Line 169: Line 269:
if (document.getElementById('differences-nextlink')) {
if (document.getElementById('differences-nextlink')) {
// Not latest revision, add [restore this revision] link to newer revision
// Not latest revision, add [restore this revision] link to newer revision
Twinkle.fluff.restoreThisRevision('mw-diff-ntitle1', 'wgDiffNewId');
var newTitle = document.getElementById('mw-diff-ntitle1').parentNode;
newTitle.insertBefore(Twinkle.fluff.linkBuilder.restoreThisRevisionLink('wgDiffNewId'), newTitle.firstChild);
} else if (Twinkle.getPref('showRollbackLinks').indexOf('diff') !== -1 && mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId') || document.getElementById('differences-prevlink'))) {
} else if (Twinkle.getPref('showRollbackLinks').indexOf('diff') !== -1 && mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId') || document.getElementById('differences-prevlink'))) {
var vandal = $('#mw-diff-ntitle2').find('a').first().text();
var vandal = $('#mw-diff-ntitle2').find('a').first().text();
var ntitle = document.getElementById('mw-diff-ntitle1').parentNode;


ntitle.insertBefore(Twinkle.fluff.linkBuilder.rollbackLinks(vandal), ntitle.firstChild);
var revertNode = document.createElement('div');
revertNode.setAttribute('id', 'tw-revert');

var agfNode = document.createElement('strong');
var vandNode = document.createElement('strong');
var normNode = document.createElement('strong');

var agfLink = Twinkle.fluff.buildLink('DarkOliveGreen', 'rollback (AGF)');
var vandLink = Twinkle.fluff.buildLink('Red', 'rollback (VANDAL)');
var normLink = Twinkle.fluff.buildLink('SteelBlue', 'rollback');

agfLink.href = '#';
vandLink.href = '#';
normLink.href = '#';
$(agfLink).click(function() {
Twinkle.fluff.revert('agf', vandal);
});
$(vandLink).click(function() {
Twinkle.fluff.revert('vand', vandal);
});
$(normLink).click(function() {
Twinkle.fluff.revert('norm', vandal);
});

agfNode.appendChild(agfLink);
vandNode.appendChild(vandLink);
normNode.appendChild(normLink);

revertNode.appendChild(agfNode);
revertNode.appendChild(document.createTextNode(' || '));
revertNode.appendChild(normNode);
revertNode.appendChild(document.createTextNode(' || '));
revertNode.appendChild(vandNode);

var ntitle = document.getElementById('mw-diff-ntitle1').parentNode;
ntitle.insertBefore(revertNode, ntitle.firstChild);
}
}
},
},


oldid: function() { // Add a [restore this revision] link on old revisions
oldid: function() { // Add a [restore this revision] link on old revisions
Twinkle.fluff.restoreThisRevision('mw-revision-info', 'wgRevisionId');
var title = document.getElementById('mw-revision-info').parentNode;
title.insertBefore(Twinkle.fluff.linkBuilder.restoreThisRevisionLink('wgRevisionId'), title.firstChild);
}
}
};
};


Twinkle.fluff.disableLinks = function disablelinks(parentNode) {
// Array.from not available in IE11 :(
$(parentNode).children().each(function(_ix, node) {
node.innerHTML = node.textContent; // Feels like cheating
$(node).css('font-weight', 'normal').css('color', 'darkgray');
});
};



Twinkle.fluff.revert = function revertPage(type, vandal, autoRevert, rev, page) {
Twinkle.fluff.revert = function revertPage(type, vandal, rev, page) {
if (mw.util.isIPv6Address(vandal)) {
if (mw.util.isIPv6Address(vandal)) {
vandal = Morebits.sanitizeIPv6(vandal);
vandal = Morebits.sanitizeIPv6(vandal);
Line 226: Line 302:
var revid = rev || mw.config.get('wgCurRevisionId');
var revid = rev || mw.config.get('wgCurRevisionId');


if (Twinkle.fluff.rollbackInPlace) {
Morebits.status.init(document.getElementById('mw-content-text'));
var notifyStatus = document.createElement('span');
$('#catlinks').remove();
mw.notify(notifyStatus, {
autoHide: false,
title: 'Rollback on ' + page,
tag: 'twinklefluff_' + rev // Shouldn't be necessary given disableLink
});
Morebits.status.init(notifyStatus);
} else {
Morebits.status.init(document.getElementById('mw-content-text'));
$('#catlinks').remove();
}


var params = {
var params = {
Line 233: Line 319:
user: vandal,
user: vandal,
pagename: pagename,
pagename: pagename,
revid: revid,
revid: revid
autoRevert: !!autoRevert
};
};
var query = {
var query = {
Line 240: Line 325:
'prop': ['info', 'revisions', 'flagged'],
'prop': ['info', 'revisions', 'flagged'],
'titles': pagename,
'titles': pagename,
'intestactions': 'edit',
'rvlimit': 50, // intentionally limited
'rvlimit': 50, // intentionally limited
'rvprop': [ 'ids', 'timestamp', 'user', 'comment' ],
'rvprop': [ 'ids', 'timestamp', 'user', 'comment' ],
Line 267: Line 353:
'type': 'csrf'
'type': 'csrf'
};
};
var wikipedia_api = new Morebits.wiki.api('Grabbing data of the earlier revision', query, Twinkle.fluff.callbacks.toRevision.main);
var wikipedia_api = new Morebits.wiki.api('Grabbing data of the earlier revision', query, Twinkle.fluff.callbacks.toRevision);
wikipedia_api.params = { rev: oldrev };
wikipedia_api.params = { rev: oldrev };
wikipedia_api.post();
wikipedia_api.post();
Line 277: Line 363:


Twinkle.fluff.callbacks = {
Twinkle.fluff.callbacks = {
toRevision: {
toRevision: function(apiobj) {
var xmlDoc = apiobj.responseXML;
main: function(self) {
var xmlDoc = self.responseXML;


var lastrevid = parseInt($(xmlDoc).find('page').attr('lastrevid'), 10);
var lastrevid = parseInt($(xmlDoc).find('page').attr('lastrevid'), 10);
var touched = $(xmlDoc).find('page').attr('touched');
var touched = $(xmlDoc).find('page').attr('touched');
var loadtimestamp = $(xmlDoc).find('api').attr('curtimestamp');
var loadtimestamp = $(xmlDoc).find('api').attr('curtimestamp');
var csrftoken = $(xmlDoc).find('tokens').attr('csrftoken');
var csrftoken = $(xmlDoc).find('tokens').attr('csrftoken');
var revertToRevID = $(xmlDoc).find('rev').attr('revid');
var revertToRevID = parseInt($(xmlDoc).find('rev').attr('revid'), 10);
var revertToUser = $(xmlDoc).find('rev').attr('user');
var revertToUser = $(xmlDoc).find('rev').attr('user');


if (revertToRevID !== self.params.rev) {
if (revertToRevID !== apiobj.params.rev) {
self.statelem.error('The retrieved revision does not match the requested revision. Stopping revert.');
apiobj.statelem.error('The retrieved revision does not match the requested revision. Stopping revert.');
return;
return;
}
}


var optional_summary = prompt('Please specify a reason for the revert: ', ''); // padded out to widen prompt in Firefox
var optional_summary = prompt('Please specify a reason for the revert: ', ''); // padded out to widen prompt in Firefox
if (optional_summary === null) {
if (optional_summary === null) {
self.statelem.error('Aborted by user.');
apiobj.statelem.error('Aborted by user.');
return;
return;
}
}
var summary = Twinkle.fluff.formatSummary('Reverted to revision ' + revertToRevID + ' by $USER', revertToUser, optional_summary);
var summary = Twinkle.fluff.formatSummary('Reverted to revision ' + revertToRevID + ' by $USER', revertToUser, optional_summary);


var query = {
var query = {
'action': 'edit',
'action': 'edit',
'title': mw.config.get('wgPageName'),
'title': mw.config.get('wgPageName'),
'summary': summary,
'summary': summary,
'token': csrftoken,
'token': csrftoken,
'undo': lastrevid,
'undo': lastrevid,
'undoafter': revertToRevID,
'undoafter': revertToRevID,
'basetimestamp': touched,
'basetimestamp': touched,
'starttimestamp': loadtimestamp,
'starttimestamp': loadtimestamp,
'watchlist': Twinkle.getPref('watchRevertedPages').indexOf('torev') !== -1 ? 'watch' : undefined,
'watchlist': Twinkle.getPref('watchRevertedPages').indexOf('torev') !== -1 ? 'watch' : undefined,
'minor': Twinkle.getPref('markRevertedPagesAsMinor').indexOf('torev') !== -1 ? true : undefined
'minor': Twinkle.getPref('markRevertedPagesAsMinor').indexOf('torev') !== -1 ? true : undefined
};
};


Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName');
Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName');
Morebits.wiki.actionCompleted.notice = 'Reversion completed';
Morebits.wiki.actionCompleted.notice = 'Reversion completed';


var wikipedia_api = new Morebits.wiki.api('Saving reverted contents', query, Twinkle.fluff.callbacks.complete, self.statelem);
var wikipedia_api = new Morebits.wiki.api('Saving reverted contents', query, Twinkle.fluff.callbacks.complete, apiobj.statelem);
wikipedia_api.params = self.params;
wikipedia_api.params = apiobj.params;
wikipedia_api.post();
wikipedia_api.post();


}
},
},
main: function(self) {
main: function(apiobj) {
var xmlDoc = self.responseXML;
var xmlDoc = apiobj.responseXML;

if (typeof $(xmlDoc).find('actions').attr('edit') === 'undefined') {
self.statelem.error("Unable to edit the page, it's probably protected.");
return;
}


var lastrevid = parseInt($(xmlDoc).find('page').attr('lastrevid'), 10);
var lastrevid = parseInt($(xmlDoc).find('page').attr('lastrevid'), 10);
Line 332: Line 421:


var revs = $(xmlDoc).find('rev');
var revs = $(xmlDoc).find('rev');

var statelem = apiobj.statelem;
var params = apiobj.params;


if (revs.length < 1) {
if (revs.length < 1) {
self.statelem.error('We have less than one additional revision, thus impossible to revert.');
statelem.error('We have less than one additional revision, thus impossible to revert.');
return;
return;
}
}
var top = revs[0];
var top = revs[0];
if (lastrevid < self.params.revid) {
if (lastrevid < params.revid) {
Morebits.status.error('Error', [ 'The most recent revision ID received from the server, ', Morebits.htmlNode('strong', lastrevid), ', is less than the ID of the displayed revision. This could indicate that the current revision has been deleted, the server is lagging, or that bad data has been received. Stopping revert.' ]);
Morebits.status.error('Error', [ 'The most recent revision ID received from the server, ', Morebits.htmlNode('strong', lastrevid), ', is less than the ID of the displayed revision. This could indicate that the current revision has been deleted, the server is lagging, or that bad data has been received. Stopping revert.' ]);
return;
return;
}
}
var index = 1;
var index = 1;
if (self.params.revid !== lastrevid) {
if (params.revid !== lastrevid) {
Morebits.status.warn('Warning', [ 'Latest revision ', Morebits.htmlNode('strong', lastrevid), ' doesn\'t equal our revision ', Morebits.htmlNode('strong', self.params.revid) ]);
Morebits.status.warn('Warning', [ 'Latest revision ', Morebits.htmlNode('strong', lastrevid), ' doesn\'t equal our revision ', Morebits.htmlNode('strong', params.revid) ]);
if (lastuser === self.params.user) {
if (lastuser === params.user) {
switch (self.params.type) {
switch (params.type) {
case 'vand':
case 'vand':
Morebits.status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', self.params.user), '. As we assume vandalism, we will proceed to revert.' ]);
Morebits.status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', params.user), '. As we assume vandalism, we will proceed to revert.' ]);
break;
break;
case 'agf':
case 'agf':
Morebits.status.warn('Warning', [ 'Latest revision was made by ', Morebits.htmlNode('strong', self.params.user), '. As we assume good faith, we will stop the revert, as the problem might have been fixed.' ]);
Morebits.status.warn('Warning', [ 'Latest revision was made by ', Morebits.htmlNode('strong', params.user), '. As we assume good faith, we will stop the revert, as the problem might have been fixed.' ]);
return;
return;
default:
default:
Morebits.status.warn('Notice', [ 'Latest revision was made by ', Morebits.htmlNode('strong', self.params.user), ', but we will stop the revert.' ]);
Morebits.status.warn('Notice', [ 'Latest revision was made by ', Morebits.htmlNode('strong', params.user), ', but we will stop the revert.' ]);
return;
return;
}
}
} else if (self.params.type === 'vand' &&
} else if (params.type === 'vand' &&
Twinkle.fluff.whiteList.indexOf(top.getAttribute('user')) !== -1 && revs.length > 1 &&
Twinkle.fluff.whiteList.indexOf(top.getAttribute('user')) !== -1 && revs.length > 1 &&
revs[1].getAttribute('pageId') === self.params.revid) {
revs[1].getAttribute('pageId') === params.revid) {
Morebits.status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', lastuser), ', a trusted bot, and the revision before was made by our vandal, so we will proceed with the revert.' ]);
Morebits.status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', lastuser), ', a trusted bot, and the revision before was made by our vandal, so we will proceed with the revert.' ]);
index = 2;
index = 2;
Line 369: Line 461:
}
}


if (Twinkle.fluff.whiteList.indexOf(self.params.user) !== -1) {
if (Twinkle.fluff.whiteList.indexOf(params.user) !== -1) {
switch (self.params.type) {
switch (params.type) {
case 'vand':
case 'vand':
Morebits.status.info('Info', [ 'Vandalism revert was chosen on ', Morebits.htmlNode('strong', self.params.user), '. As this is a whitelisted bot, we assume you wanted to revert vandalism made by the previous user instead.' ]);
Morebits.status.info('Info', [ 'Vandalism revert was chosen on ', Morebits.htmlNode('strong', params.user), '. As this is a whitelisted bot, we assume you wanted to revert vandalism made by the previous user instead.' ]);
index = 2;
index = 2;
self.params.user = revs[1].getAttribute('user');
params.user = revs[1].getAttribute('user');
break;
break;
case 'agf':
case 'agf':
Morebits.status.warn('Notice', [ 'Good faith revert was chosen on ', Morebits.htmlNode('strong', self.params.user), '. This is a whitelisted bot and thus AGF rollback will not proceed.' ]);
Morebits.status.warn('Notice', [ 'Good faith revert was chosen on ', Morebits.htmlNode('strong', params.user), '. This is a whitelisted bot and thus AGF rollback will not proceed.' ]);
return;
return;
case 'norm':
case 'norm':
/* falls through */
/* falls through */
default:
default:
var cont = confirm('Normal revert was chosen, but the most recent edit was made by a whitelisted bot (' + self.params.user + '). Do you want to revert the revision before instead?');
var cont = confirm('Normal revert was chosen, but the most recent edit was made by a whitelisted bot (' + params.user + '). Do you want to revert the revision before instead?');
if (cont) {
if (cont) {
Morebits.status.info('Info', [ 'Normal revert was chosen on ', Morebits.htmlNode('strong', self.params.user), '. This is a whitelisted bot, and per confirmation, we\'ll revert the previous revision instead.' ]);
Morebits.status.info('Info', [ 'Normal revert was chosen on ', Morebits.htmlNode('strong', params.user), '. This is a whitelisted bot, and per confirmation, we\'ll revert the previous revision instead.' ]);
index = 2;
index = 2;
self.params.user = revs[1].getAttribute('user');
params.user = revs[1].getAttribute('user');
} else {
} else {
Morebits.status.warn('Notice', [ 'Normal revert was chosen on ', Morebits.htmlNode('strong', self.params.user), '. This is a whitelisted bot, but per confirmation, revert on selected revision will proceed.' ]);
Morebits.status.warn('Notice', [ 'Normal revert was chosen on ', Morebits.htmlNode('strong', params.user), '. This is a whitelisted bot, but per confirmation, revert on selected revision will proceed.' ]);
}
}
break;
break;
Line 398: Line 490:
for (var i = index; i < revs.length; ++i) {
for (var i = index; i < revs.length; ++i) {
++count;
++count;
if (revs[i].getAttribute('user') !== self.params.user) {
if (revs[i].getAttribute('user') !== params.user) {
found = i;
found = i;
break;
break;
Line 405: Line 497:


if (!found) {
if (!found) {
self.statelem.error([ 'No previous revision found. Perhaps ', Morebits.htmlNode('strong', self.params.user), ' is the only contributor, or that the user has made more than ' + Twinkle.getPref('revertMaxRevisions') + ' edits in a row.' ]);
statelem.error([ 'No previous revision found. Perhaps ', Morebits.htmlNode('strong', params.user), ' is the only contributor, or that the user has made more than ' + Twinkle.getPref('revertMaxRevisions') + ' edits in a row.' ]);
return;
return;
}
}
Line 416: Line 508:
var good_revision = revs[found];
var good_revision = revs[found];
var userHasAlreadyConfirmedAction = false;
var userHasAlreadyConfirmedAction = false;
if (self.params.type !== 'vand' && count > 1) {
if (params.type !== 'vand' && count > 1) {
if (!confirm(self.params.user + ' has made ' + count + ' edits in a row. Are you sure you want to revert them all?')) {
if (!confirm(params.user + ' has made ' + count + ' edits in a row. Are you sure you want to revert them all?')) {
Morebits.status.info('Notice', 'Stopping revert.');
Morebits.status.info('Notice', 'Stopping revert.');
return;
return;
Line 424: Line 516:
}
}


self.params.count = count;
params.count = count;


self.params.goodid = good_revision.getAttribute('revid');
params.goodid = good_revision.getAttribute('revid');
self.params.gooduser = good_revision.getAttribute('user');
params.gooduser = good_revision.getAttribute('user');


self.statelem.status([ ' revision ', Morebits.htmlNode('strong', self.params.goodid), ' that was made ', Morebits.htmlNode('strong', count), ' revisions ago by ', Morebits.htmlNode('strong', self.params.gooduser) ]);
statelem.status([ ' revision ', Morebits.htmlNode('strong', params.goodid), ' that was made ', Morebits.htmlNode('strong', count), ' revisions ago by ', Morebits.htmlNode('strong', params.gooduser) ]);


var summary, extra_summary;
var summary, extra_summary;
switch (self.params.type) {
switch (params.type) {
case 'agf':
case 'agf':
extra_summary = prompt('An optional comment for the edit summary: ', ''); // padded out to widen prompt in Firefox
extra_summary = prompt('An optional comment for the edit summary: ', ''); // padded out to widen prompt in Firefox
if (extra_summary === null) {
if (extra_summary === null) {
self.statelem.error('Aborted by user.');
statelem.error('Aborted by user.');
return;
return;
}
}
userHasAlreadyConfirmedAction = true;
userHasAlreadyConfirmedAction = true;


summary = Twinkle.fluff.formatSummary('Reverted [[WP:AGF|good faith]] edits by $USER', self.params.user, extra_summary);
summary = Twinkle.fluff.formatSummary('Reverted [[WP:AGF|good faith]] edits by $USER', params.user, extra_summary);
break;
break;


case 'vand':
case 'vand':


summary = 'Reverted ' + self.params.count + (self.params.count > 1 ? ' edits' : ' edit') + ' by [[Special:Contributions/' +
summary = 'Reverted ' + params.count + (params.count > 1 ? ' edits' : ' edit') + ' by [[Special:Contributions/' +
self.params.user + '|' + self.params.user + ']] ([[User talk:' + self.params.user + '|talk]]) to last revision by ' +
params.user + '|' + params.user + ']] ([[User talk:' + params.user + '|talk]]) to last revision by ' +
self.params.gooduser + Twinkle.getPref('summaryAd');
params.gooduser + Twinkle.getPref('summaryAd');
break;
break;


Line 457: Line 549:
extra_summary = prompt('An optional comment for the edit summary: ', ''); // padded out to widen prompt in Firefox
extra_summary = prompt('An optional comment for the edit summary: ', ''); // padded out to widen prompt in Firefox
if (extra_summary === null) {
if (extra_summary === null) {
self.statelem.error('Aborted by user.');
statelem.error('Aborted by user.');
return;
return;
}
}
Line 463: Line 555:
}
}


summary = Twinkle.fluff.formatSummary('Reverted ' + self.params.count + (self.params.count > 1 ? ' edits' : ' edit') +
summary = Twinkle.fluff.formatSummary('Reverted ' + params.count + (params.count > 1 ? ' edits' : ' edit') +
' by $USER', self.params.user, extra_summary);
' by $USER', params.user, extra_summary);
break;
break;
}
}


if (Twinkle.getPref('confirmOnFluff') && !userHasAlreadyConfirmedAction && !confirm('Reverting page: are you sure?')) {
if (Twinkle.getPref('confirmOnFluff') && !userHasAlreadyConfirmedAction && !confirm('Reverting page: are you sure?')) {
self.statelem.error('Aborted by user.');
statelem.error('Aborted by user.');
return;
return;
}
}


// Decide whether to notify the user on success
var query;
if ((!self.params.autoRevert || Twinkle.getPref('openTalkPageOnAutoRevert')) &&
if (!Twinkle.fluff.skipTalk && Twinkle.getPref('openTalkPage').indexOf(params.type) !== -1 &&
mw.config.get('wgUserName') !== params.user) {
Twinkle.getPref('openTalkPage').indexOf(self.params.type) !== -1 &&
params.notifyUser = true;
mw.config.get('wgUserName') !== self.params.user) {
Morebits.status.info('Info', [ 'Opening user talk page edit form for user ', Morebits.htmlNode('strong', self.params.user) ]);

query = {
'title': 'User talk:' + self.params.user,
'action': 'edit',
'preview': 'yes',
'vanarticle': self.params.pagename.replace(/_/g, ' '),
'vanarticlerevid': self.params.revid,
'vanarticlegoodrevid': self.params.goodid,
'type': self.params.type,
'count': self.params.count
};

switch (Twinkle.getPref('userTalkPageMode')) {
case 'tab':
window.open(mw.util.getUrl('', query), '_blank');
break;
case 'blank':
window.open(mw.util.getUrl('', query), '_blank',
'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800');
break;
case 'window':
/* falls through */
default:
window.open(mw.util.getUrl('', query),
window.name === 'twinklewarnwindow' ? '_blank' : 'twinklewarnwindow',
'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800');
break;
}
}
}


Line 512: Line 575:
if ((Morebits.userIsInGroup('reviewer') || Morebits.userIsSysop) &&
if ((Morebits.userIsInGroup('reviewer') || Morebits.userIsSysop) &&
$flagged.length &&
$flagged.length &&
$flagged.attr('stable_revid') >= self.params.goodid &&
$flagged.attr('stable_revid') >= params.goodid &&
$flagged.attr('pending_since')) {
$flagged.attr('pending_since')) {
self.params.reviewRevert = true;
params.reviewRevert = true;
self.params.csrftoken = csrftoken;
params.csrftoken = csrftoken;
}
}


query = {
var query = {
'action': 'edit',
'action': 'edit',
'title': self.params.pagename,
'title': params.pagename,
'summary': summary,
'summary': summary,
'token': csrftoken,
'token': csrftoken,
'undo': lastrevid,
'undo': lastrevid,
'undoafter': self.params.goodid,
'undoafter': params.goodid,
'basetimestamp': touched,
'basetimestamp': touched,
'starttimestamp': loadtimestamp,
'starttimestamp': loadtimestamp,
'watchlist': Twinkle.getPref('watchRevertedPages').indexOf(self.params.type) !== -1 ? 'watch' : undefined,
'watchlist': Twinkle.getPref('watchRevertedPages').indexOf(params.type) !== -1 ? 'watch' : undefined,
'minor': Twinkle.getPref('markRevertedPagesAsMinor').indexOf(self.params.type) !== -1 ? true : undefined
'minor': Twinkle.getPref('markRevertedPagesAsMinor').indexOf(params.type) !== -1 ? true : undefined
};
};


if (!Twinkle.fluff.rollbackInPlace) {
Morebits.wiki.actionCompleted.redirect = self.params.pagename;
Morebits.wiki.actionCompleted.redirect = params.pagename;
}
Morebits.wiki.actionCompleted.notice = 'Reversion completed';
Morebits.wiki.actionCompleted.notice = 'Reversion completed';


var wikipedia_api = new Morebits.wiki.api('Saving reverted contents', query, Twinkle.fluff.callbacks.complete, self.statelem);
var wikipedia_api = new Morebits.wiki.api('Saving reverted contents', query, Twinkle.fluff.callbacks.complete, statelem);
wikipedia_api.params = self.params;
wikipedia_api.params = params;
wikipedia_api.post();
wikipedia_api.post();


Line 547: Line 612:
apiobj.statelem.error('Could not rollback, because the wiki server wanted you to fill out a CAPTCHA.');
apiobj.statelem.error('Could not rollback, because the wiki server wanted you to fill out a CAPTCHA.');
} else if ($edit.attr('nochange') === '') {
} else if ($edit.attr('nochange') === '') {
apiobj.statelem.warn('Revision we are reverting to is identical to current revision, stopping revert.');
apiobj.statelem.error('Revision we are reverting to is identical to current revision, stopping revert.');
} else {
} else {
apiobj.statelem.info('done');
apiobj.statelem.info('done');
var params = apiobj.params;

if (params.notifyUser) { // Only from main, not from toRevision
Morebits.status.info('Info', [ 'Opening user talk page edit form for user ', Morebits.htmlNode('strong', params.user) ]);

var windowQuery = {
'title': 'User talk:' + params.user,
'action': 'edit',
'preview': 'yes',
'vanarticle': params.pagename.replace(/_/g, ' '),
'vanarticlerevid': params.revid,
'vanarticlegoodrevid': params.goodid,
'type': params.type,
'count': params.count
};

switch (Twinkle.getPref('userTalkPageMode')) {
case 'tab':
window.open(mw.util.getUrl('', windowQuery), '_blank');
break;
case 'blank':
window.open(mw.util.getUrl('', windowQuery), '_blank',
'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800');
break;
case 'window':
/* falls through */
default:
window.open(mw.util.getUrl('', windowQuery),
window.name === 'twinklewarnwindow' ? '_blank' : 'twinklewarnwindow',
'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800');
break;
}
}



// review the revert, if needed
// review the revert, if needed

Revision as of 18:05, 13 June 2020

// <nowiki>


(function($) {


/*
 ****************************************
 *** twinklefluff.js: Revert/rollback module
 ****************************************
 * Mode of invocation:     Links on contributions, recent changes, history, and diff pages
 * Active on:              Diff pages, history pages, Special:RecentChanges(Linked),
                           and Special:Contributions
 */

/**
 Twinklefluff revert and antivandalism utility
 */

Twinkle.fluff = function twinklefluff() {
	// A list of usernames, usually only bots, that vandalism revert is jumped over; that is,
	// if vandalism revert was chosen on such username, then its target is on the revision before.
	// This is for handling quick bots that makes edits seconds after the original edit is made.
	// This only affects vandalism rollback; for good faith rollback, it will stop, indicating a bot
	// has no faith, and for normal rollback, it will rollback that edit.
	Twinkle.fluff.whiteList = [
		'AnomieBOT',
		'SineBot'
	];

	if (mw.config.get('wgIsProbablyEditable')) {
		// Only proceed if the user can actually edit the page
		// in question (ignored for contributions, see #632).
		// wgIsProbablyEditable should take care of
		// namespace/contentModel restrictions as well as
		// explicit protections; it won't take care of
		// cascading or TitleBlacklist restrictions
		if (mw.config.get('wgDiffNewId') || mw.config.get('wgDiffOldId')) { // wgDiffOldId included for clarity in if else loop [[phab:T214985]]
			mw.hook('wikipage.diff').add(function () { // Reload alongside the revision slider
				Twinkle.fluff.addLinks.diff();
			});
		} else if (mw.config.get('wgAction') === 'view' && mw.config.get('wgCurRevisionId') !== mw.config.get('wgRevisionId')) {
			Twinkle.fluff.addLinks.oldid();
		} else if (mw.config.get('wgAction') === 'history') {
			Twinkle.fluff.addLinks.history();
		}
	} else if (mw.config.get('wgNamespaceNumber') === -1) {
		Twinkle.fluff.skipTalk = !Twinkle.getPref('openTalkPageOnAutoRevert');
		Twinkle.fluff.rollbackInPlace = Twinkle.getPref('rollbackInPlace');

		if (mw.config.get('wgCanonicalSpecialPageName') === 'Contributions') {
			Twinkle.fluff.addLinks.contributions();
		} else if (mw.config.get('wgCanonicalSpecialPageName') === 'Recentchanges' || mw.config.get('wgCanonicalSpecialPageName') === 'Recentchangeslinked') {
			// Reload with recent changes updates
			// structuredChangeFilters.ui.initialized is just on load
			mw.hook('wikipage.content').add(function(item) {
				if (item.is('div')) {
					Twinkle.fluff.addLinks.recentchanges();
				}
			});
		}
	}
};

Twinkle.fluff.skipTalk = null;
Twinkle.fluff.rollbackInPlace = null;

// Consolidated construction of fluff links
Twinkle.fluff.linkBuilder = {
	spanTag: function(color, content) {
		var span = document.createElement('span');
		span.style.color = color;
		span.appendChild(document.createTextNode(content));
		return span;
	},

	buildLink: function(color, text) {
		var link = document.createElement('a');
		link.appendChild(Twinkle.fluff.linkBuilder.spanTag('Black', '['));
		link.appendChild(Twinkle.fluff.linkBuilder.spanTag(color, text));
		link.appendChild(Twinkle.fluff.linkBuilder.spanTag('Black', ']'));
		link.href = '#';
		return link;
	},

	/**
	 * @param {string} vandal - Username of the editor being reverted (required)
	 * @param {boolean} inline - True to create two links in a span, false
	 * to create three links in a div (optional)
	 * @param {number|string} [rev=wgCurRevisionId] - Revision ID being reverted (optional)
	 * @param {string} [page=wgPageName] - Page being reverted (optional)
	 */
	rollbackLinks: function(vandal, inline, rev, page) {
		var elem = inline ? 'span' : 'div';
		var revNode = document.createElement(elem);

		rev = parseInt(rev, 10);
		if (rev) {
			revNode.setAttribute('id', 'tw-revert' + rev);
		} else {
			revNode.setAttribute('id', 'tw-revert');
		}

		var normNode = document.createElement('strong');
		var vandNode = document.createElement('strong');

		var normLink = Twinkle.fluff.linkBuilder.buildLink('SteelBlue', 'rollback');
		var vandLink = Twinkle.fluff.linkBuilder.buildLink('Red', 'vandalism');

		$(normLink).click(function() {
			Twinkle.fluff.revert('norm', vandal, rev, page);
			Twinkle.fluff.disableLinks(revNode);
		});
		$(vandLink).click(function() {
			Twinkle.fluff.revert('vand', vandal, rev, page);
			Twinkle.fluff.disableLinks(revNode);
		});

		vandNode.appendChild(vandLink);
		normNode.appendChild(normLink);

		var separator = inline ? ' ' : ' || ';

		if (!inline) {
			var agfNode = document.createElement('strong');
			var agfLink = Twinkle.fluff.linkBuilder.buildLink('DarkOliveGreen', 'rollback (AGF)');
			$(agfLink).click(function() {
				Twinkle.fluff.revert('agf', vandal, rev, page);
				// Twinkle.fluff.disableLinks(revNode); // rollbackInPlace not relevant for any inline situations
			});
			agfNode.appendChild(agfLink);
			revNode.appendChild(agfNode);
		}
		revNode.appendChild(document.createTextNode(separator));
		revNode.appendChild(normNode);
		revNode.appendChild(document.createTextNode(separator));
		revNode.appendChild(vandNode);

		return revNode;

	},

	// Build [restore this revision] links
	restoreThisRevisionLink: function(revisionRef, inline) {
		// If not a specific revision number, should be wgDiffNewId/wgDiffOldId/wgRevisionId
		revisionRef = typeof revisionRef === 'number' ? revisionRef : mw.config.get(revisionRef);

		var elem = inline ? 'span' : 'div';
		var revertToRevisionNode = document.createElement(elem);

		revertToRevisionNode.setAttribute('id', 'tw-revert-to-' + revisionRef);
		revertToRevisionNode.style.fontWeight = 'bold';

		var revertToRevisionLink = Twinkle.fluff.linkBuilder.buildLink('SaddleBrown', 'restore this version');
		$(revertToRevisionLink).click(function() {
			Twinkle.fluff.revertToRevision(revisionRef);
		});

		if (inline) {
			revertToRevisionNode.appendChild(document.createTextNode(' '));
		}
		revertToRevisionNode.appendChild(revertToRevisionLink);
		return revertToRevisionNode;
	}
};


Twinkle.fluff.addLinks = {
	contributions: function() {
		// $('sp-contributions-footer-anon-range') relies on the fmbox
		// id in [[MediaWiki:Sp-contributions-footer-anon-range]] and
		// is used to show rollback/vandalism links for IP ranges
		if (mw.config.exists('wgRelevantUserName') || !!$('#sp-contributions-footer-anon-range')[0]) {
			// Get the username these contributions are for
			var username = mw.config.get('wgRelevantUserName');
			if (Twinkle.getPref('showRollbackLinks').indexOf('contribs') !== -1 ||
				(mw.config.get('wgUserName') !== username && Twinkle.getPref('showRollbackLinks').indexOf('others') !== -1) ||
				(mw.config.get('wgUserName') === username && Twinkle.getPref('showRollbackLinks').indexOf('mine') !== -1)) {
				var list = $('#mw-content-text').find('ul li:has(span.mw-uctop):has(.mw-changeslist-diff)');

				list.each(function(key, current) {
					// revid is also available in the href of both
					// .mw-changeslist-date or .mw-changeslist-diff
					var page = $(current).find('.mw-contributions-title').text();
					current.appendChild(Twinkle.fluff.linkBuilder.rollbackLinks(username, true, current.dataset.mwRevid, page));
				});
			}
		}
	},

	recentchanges: function() {
		if (Twinkle.getPref('showRollbackLinks').indexOf('recent') !== -1) {
			// Latest and revertable (not page creations, logs, categorizations, etc.)
			var list = $('.mw-changeslist .mw-changeslist-last.mw-changeslist-src-mw-edit');
			// Exclude top-level header if "group changes" preference is used
			// and find only individual lines or nested lines
			list = list.not('.mw-rcfilters-ui-highlights-enhanced-toplevel').find('.mw-changeslist-line-inner, td.mw-enhanced-rc-nested');

			list.each(function(key, current) {
				var vandal = $(current).find('.mw-userlink').text();
				var href = $(current).find('.mw-changeslist-diff').attr('href');
				var rev = mw.util.getParamValue('diff', href);
				var page = current.dataset.targetPage;
				current.appendChild(Twinkle.fluff.linkBuilder.rollbackLinks(vandal, true, rev, page));
			});
		}
	},

	history: function() {
		if (Twinkle.getPref('showRollbackLinks').indexOf('history') !== -1) {
			// All revs
			var histList = $('#pagehistory li').toArray();

			// On first page of results, so add revert/rollback
			// links to the top revision
			if (!$('.mw-firstlink').length) {
				var first = histList.shift();
				var vandal = first.querySelector('.mw-userlink').text;

				first.appendChild(Twinkle.fluff.linkBuilder.rollbackLinks(vandal, true));
			}

			// oldid
			histList.forEach(function(rev) {
				// From restoreThisRevision, non-transferable

				var href = rev.querySelector('.mw-changeslist-date').href;
				var oldid = parseInt(mw.util.getParamValue('oldid', href), 10);

				rev.appendChild(Twinkle.fluff.linkBuilder.restoreThisRevisionLink(oldid, true));
			});


		}
	},

	diff: function() {
		// Autofill user talk links on diffs with vanarticle for easy warning, but don't autowarn
		var warnFromTalk = function(xtitle) {
			var talkLink = $('#mw-diff-' + xtitle + '2 .mw-usertoollinks a').first();
			if (talkLink.length) {
				var extraParams = 'vanarticle=' + mw.util.rawurlencode(Morebits.pageNameNorm) + '&' + 'noautowarn=true';
				// diffIDs for vanarticlerevid
				extraParams += '&vanarticlerevid=';
				extraParams += xtitle === 'otitle' ? mw.config.get('wgDiffOldId') : mw.config.get('wgDiffNewId');

				var href = talkLink.attr('href');
				if (href.indexOf('?') === -1) {
					talkLink.attr('href', href + '?' + extraParams);
				} else {
					talkLink.attr('href', href + '&' + extraParams);
				}
			}
		};

		// Older revision
		warnFromTalk('otitle'); // Add quick-warn link to user talk link
		// Don't load if there's a single revision or weird diff (cur on latest)
		if (mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId'))) {
			// Add a [restore this revision] link to the older revision
			var oldTitle = document.getElementById('mw-diff-otitle1').parentNode;
			oldTitle.insertBefore(Twinkle.fluff.linkBuilder.restoreThisRevisionLink('wgDiffOldId'), oldTitle.firstChild);
		}

		// Newer revision
		warnFromTalk('ntitle'); // Add quick-warn link to user talk link
		// Add either restore or rollback links to the newer revision
		// Don't show if there's a single revision or weird diff (prev on first)
		if (document.getElementById('differences-nextlink')) {
			// Not latest revision, add [restore this revision] link to newer revision
			var newTitle = document.getElementById('mw-diff-ntitle1').parentNode;
			newTitle.insertBefore(Twinkle.fluff.linkBuilder.restoreThisRevisionLink('wgDiffNewId'), newTitle.firstChild);
		} else if (Twinkle.getPref('showRollbackLinks').indexOf('diff') !== -1 && mw.config.get('wgDiffOldId') && (mw.config.get('wgDiffOldId') !== mw.config.get('wgDiffNewId') || document.getElementById('differences-prevlink'))) {
			var vandal = $('#mw-diff-ntitle2').find('a').first().text();
			var ntitle = document.getElementById('mw-diff-ntitle1').parentNode;

			ntitle.insertBefore(Twinkle.fluff.linkBuilder.rollbackLinks(vandal), ntitle.firstChild);
		}
	},

	oldid: function() { // Add a [restore this revision] link on old revisions
		var title = document.getElementById('mw-revision-info').parentNode;
		title.insertBefore(Twinkle.fluff.linkBuilder.restoreThisRevisionLink('wgRevisionId'), title.firstChild);
	}
};

Twinkle.fluff.disableLinks = function disablelinks(parentNode) {
	// Array.from not available in IE11 :(
	$(parentNode).children().each(function(_ix, node) {
		node.innerHTML = node.textContent; // Feels like cheating
		$(node).css('font-weight', 'normal').css('color', 'darkgray');
	});
};


Twinkle.fluff.revert = function revertPage(type, vandal, rev, page) {
	if (mw.util.isIPv6Address(vandal)) {
		vandal = Morebits.sanitizeIPv6(vandal);
	}

	var pagename = page || mw.config.get('wgPageName');
	var revid = rev || mw.config.get('wgCurRevisionId');

	if (Twinkle.fluff.rollbackInPlace) {
		var notifyStatus = document.createElement('span');
		mw.notify(notifyStatus, {
			autoHide: false,
			title: 'Rollback on ' + page,
			tag: 'twinklefluff_' + rev // Shouldn't be necessary given disableLink
		});
		Morebits.status.init(notifyStatus);
	} else {
		Morebits.status.init(document.getElementById('mw-content-text'));
		$('#catlinks').remove();
	}

	var params = {
		type: type,
		user: vandal,
		pagename: pagename,
		revid: revid
	};
	var query = {
		'action': 'query',
		'prop': ['info', 'revisions', 'flagged'],
		'titles': pagename,
		'intestactions': 'edit',
		'rvlimit': 50, // intentionally limited
		'rvprop': [ 'ids', 'timestamp', 'user', 'comment' ],
		'curtimestamp': '',
		'meta': 'tokens',
		'type': 'csrf'
	};
	var wikipedia_api = new Morebits.wiki.api('Grabbing data of earlier revisions', query, Twinkle.fluff.callbacks.main);
	wikipedia_api.params = params;
	wikipedia_api.post();
};

Twinkle.fluff.revertToRevision = function revertToRevision(oldrev) {

	Morebits.status.init(document.getElementById('mw-content-text'));

	var query = {
		'action': 'query',
		'prop': ['info', 'revisions'],
		'titles': mw.config.get('wgPageName'),
		'rvlimit': 1,
		'rvstartid': oldrev,
		'rvprop': [ 'ids', 'timestamp', 'user', 'comment' ],
		'format': 'xml',
		'curtimestamp': '',
		'meta': 'tokens',
		'type': 'csrf'
	};
	var wikipedia_api = new Morebits.wiki.api('Grabbing data of the earlier revision', query, Twinkle.fluff.callbacks.toRevision);
	wikipedia_api.params = { rev: oldrev };
	wikipedia_api.post();
};

Twinkle.fluff.userIpLink = function(user) {
	return (mw.util.isIPAddress(user) ? '[[Special:Contributions/' : '[[:User:') + user + '|' + user + ']]';
};

Twinkle.fluff.callbacks = {
	toRevision: function(apiobj) {
		var xmlDoc = apiobj.responseXML;

		var lastrevid = parseInt($(xmlDoc).find('page').attr('lastrevid'), 10);
		var touched = $(xmlDoc).find('page').attr('touched');
		var loadtimestamp = $(xmlDoc).find('api').attr('curtimestamp');
		var csrftoken = $(xmlDoc).find('tokens').attr('csrftoken');
		var revertToRevID = parseInt($(xmlDoc).find('rev').attr('revid'), 10);
		var revertToUser = $(xmlDoc).find('rev').attr('user');

		if (revertToRevID !== apiobj.params.rev) {
			apiobj.statelem.error('The retrieved revision does not match the requested revision. Stopping revert.');
			return;
		}

		var optional_summary = prompt('Please specify a reason for the revert:                                ', '');  // padded out to widen prompt in Firefox
		if (optional_summary === null) {
			apiobj.statelem.error('Aborted by user.');
			return;
		}
		var summary = Twinkle.fluff.formatSummary('Reverted to revision ' + revertToRevID + ' by $USER', revertToUser, optional_summary);

		var query = {
			'action': 'edit',
			'title': mw.config.get('wgPageName'),
			'summary': summary,
			'token': csrftoken,
			'undo': lastrevid,
			'undoafter': revertToRevID,
			'basetimestamp': touched,
			'starttimestamp': loadtimestamp,
			'watchlist': Twinkle.getPref('watchRevertedPages').indexOf('torev') !== -1 ? 'watch' : undefined,
			'minor': Twinkle.getPref('markRevertedPagesAsMinor').indexOf('torev') !== -1 ? true : undefined
		};

		Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName');
		Morebits.wiki.actionCompleted.notice = 'Reversion completed';

		var wikipedia_api = new Morebits.wiki.api('Saving reverted contents', query, Twinkle.fluff.callbacks.complete, apiobj.statelem);
		wikipedia_api.params = apiobj.params;
		wikipedia_api.post();

	},
	main: function(apiobj) {
		var xmlDoc = apiobj.responseXML;

		if (typeof $(xmlDoc).find('actions').attr('edit') === 'undefined') {
			self.statelem.error("Unable to edit the page, it's probably protected.");
			return;
		}

		var lastrevid = parseInt($(xmlDoc).find('page').attr('lastrevid'), 10);
		var touched = $(xmlDoc).find('page').attr('touched');
		var loadtimestamp = $(xmlDoc).find('api').attr('curtimestamp');
		var csrftoken = $(xmlDoc).find('tokens').attr('csrftoken');
		var lastuser = $(xmlDoc).find('rev').attr('user');

		var revs = $(xmlDoc).find('rev');

		var statelem = apiobj.statelem;
		var params = apiobj.params;

		if (revs.length < 1) {
			statelem.error('We have less than one additional revision, thus impossible to revert.');
			return;
		}
		var top = revs[0];
		if (lastrevid < params.revid) {
			Morebits.status.error('Error', [ 'The most recent revision ID received from the server, ', Morebits.htmlNode('strong', lastrevid), ', is less than the ID of the displayed revision. This could indicate that the current revision has been deleted, the server is lagging, or that bad data has been received. Stopping revert.' ]);
			return;
		}
		var index = 1;
		if (params.revid !== lastrevid) {
			Morebits.status.warn('Warning', [ 'Latest revision ', Morebits.htmlNode('strong', lastrevid), ' doesn\'t equal our revision ', Morebits.htmlNode('strong', params.revid) ]);
			if (lastuser === params.user) {
				switch (params.type) {
					case 'vand':
						Morebits.status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', params.user), '. As we assume vandalism, we will proceed to revert.' ]);
						break;
					case 'agf':
						Morebits.status.warn('Warning', [ 'Latest revision was made by ', Morebits.htmlNode('strong', params.user), '. As we assume good faith, we will stop the revert, as the problem might have been fixed.' ]);
						return;
					default:
						Morebits.status.warn('Notice', [ 'Latest revision was made by ', Morebits.htmlNode('strong', params.user), ', but we will stop the revert.' ]);
						return;
				}
			} else if (params.type === 'vand' &&
					Twinkle.fluff.whiteList.indexOf(top.getAttribute('user')) !== -1 && revs.length > 1 &&
					revs[1].getAttribute('pageId') === params.revid) {
				Morebits.status.info('Info', [ 'Latest revision was made by ', Morebits.htmlNode('strong', lastuser), ', a trusted bot, and the revision before was made by our vandal, so we will proceed with the revert.' ]);
				index = 2;
			} else {
				Morebits.status.error('Error', [ 'Latest revision was made by ', Morebits.htmlNode('strong', lastuser), ', so it might have already been reverted, we will stop the revert.']);
				return;
			}

		}

		if (Twinkle.fluff.whiteList.indexOf(params.user) !== -1) {
			switch (params.type) {
				case 'vand':
					Morebits.status.info('Info', [ 'Vandalism revert was chosen on ', Morebits.htmlNode('strong', params.user), '. As this is a whitelisted bot, we assume you wanted to revert vandalism made by the previous user instead.' ]);
					index = 2;
					params.user = revs[1].getAttribute('user');
					break;
				case 'agf':
					Morebits.status.warn('Notice', [ 'Good faith revert was chosen on ', Morebits.htmlNode('strong', params.user), '. This is a whitelisted bot and thus AGF rollback will not proceed.' ]);
					return;
				case 'norm':
				/* falls through */
				default:
					var cont = confirm('Normal revert was chosen, but the most recent edit was made by a whitelisted bot (' + params.user + '). Do you want to revert the revision before instead?');
					if (cont) {
						Morebits.status.info('Info', [ 'Normal revert was chosen on ', Morebits.htmlNode('strong', params.user), '. This is a whitelisted bot, and per confirmation, we\'ll revert the previous revision instead.' ]);
						index = 2;
						params.user = revs[1].getAttribute('user');
					} else {
						Morebits.status.warn('Notice', [ 'Normal revert was chosen on ', Morebits.htmlNode('strong', params.user), '. This is a whitelisted bot, but per confirmation, revert on selected revision will proceed.' ]);
					}
					break;
			}
		}
		var found = false;
		var count = 0;

		for (var i = index; i < revs.length; ++i) {
			++count;
			if (revs[i].getAttribute('user') !== params.user) {
				found = i;
				break;
			}
		}

		if (!found) {
			statelem.error([ 'No previous revision found. Perhaps ', Morebits.htmlNode('strong', params.user), ' is the only contributor, or that the user has made more than ' + Twinkle.getPref('revertMaxRevisions') + ' edits in a row.' ]);
			return;
		}

		if (!count) {
			Morebits.status.error('Error', 'As it is not possible to revert zero revisions, we will stop this revert. It could be that the edit has already been reverted, but the revision ID was still the same.');
			return;
		}

		var good_revision = revs[found];
		var userHasAlreadyConfirmedAction = false;
		if (params.type !== 'vand' && count > 1) {
			if (!confirm(params.user + ' has made ' + count + ' edits in a row. Are you sure you want to revert them all?')) {
				Morebits.status.info('Notice', 'Stopping revert.');
				return;
			}
			userHasAlreadyConfirmedAction = true;
		}

		params.count = count;

		params.goodid = good_revision.getAttribute('revid');
		params.gooduser = good_revision.getAttribute('user');

		statelem.status([ ' revision ', Morebits.htmlNode('strong', params.goodid), ' that was made ', Morebits.htmlNode('strong', count), ' revisions ago by ', Morebits.htmlNode('strong', params.gooduser) ]);

		var summary, extra_summary;
		switch (params.type) {
			case 'agf':
				extra_summary = prompt('An optional comment for the edit summary:                              ', '');  // padded out to widen prompt in Firefox
				if (extra_summary === null) {
					statelem.error('Aborted by user.');
					return;
				}
				userHasAlreadyConfirmedAction = true;

				summary = Twinkle.fluff.formatSummary('Reverted [[WP:AGF|good faith]] edits by $USER', params.user, extra_summary);
				break;

			case 'vand':

				summary = 'Reverted ' + params.count + (params.count > 1 ? ' edits' : ' edit') + ' by [[Special:Contributions/' +
				params.user + '|' + params.user + ']] ([[User talk:' + params.user + '|talk]]) to last revision by ' +
				params.gooduser + Twinkle.getPref('summaryAd');
				break;

			case 'norm':
			/* falls through */
			default:
				if (Twinkle.getPref('offerReasonOnNormalRevert')) {
					extra_summary = prompt('An optional comment for the edit summary:                              ', '');  // padded out to widen prompt in Firefox
					if (extra_summary === null) {
						statelem.error('Aborted by user.');
						return;
					}
					userHasAlreadyConfirmedAction = true;
				}

				summary = Twinkle.fluff.formatSummary('Reverted ' + params.count + (params.count > 1 ? ' edits' : ' edit') +
				' by $USER', params.user, extra_summary);
				break;
		}

		if (Twinkle.getPref('confirmOnFluff') && !userHasAlreadyConfirmedAction && !confirm('Reverting page: are you sure?')) {
			statelem.error('Aborted by user.');
			return;
		}

		// Decide whether to notify the user on success
		if (!Twinkle.fluff.skipTalk && Twinkle.getPref('openTalkPage').indexOf(params.type) !== -1 &&
				mw.config.get('wgUserName') !== params.user) {
			params.notifyUser = true;
		}

		// figure out whether we need to/can review the edit
		var $flagged = $(xmlDoc).find('flagged');
		if ((Morebits.userIsInGroup('reviewer') || Morebits.userIsSysop) &&
				$flagged.length &&
				$flagged.attr('stable_revid') >= params.goodid &&
				$flagged.attr('pending_since')) {
			params.reviewRevert = true;
			params.csrftoken = csrftoken;
		}

		var query = {
			'action': 'edit',
			'title': params.pagename,
			'summary': summary,
			'token': csrftoken,
			'undo': lastrevid,
			'undoafter': params.goodid,
			'basetimestamp': touched,
			'starttimestamp': loadtimestamp,
			'watchlist': Twinkle.getPref('watchRevertedPages').indexOf(params.type) !== -1 ? 'watch' : undefined,
			'minor': Twinkle.getPref('markRevertedPagesAsMinor').indexOf(params.type) !== -1 ? true : undefined
		};

		if (!Twinkle.fluff.rollbackInPlace) {
			Morebits.wiki.actionCompleted.redirect = params.pagename;
		}
		Morebits.wiki.actionCompleted.notice = 'Reversion completed';

		var wikipedia_api = new Morebits.wiki.api('Saving reverted contents', query, Twinkle.fluff.callbacks.complete, statelem);
		wikipedia_api.params = params;
		wikipedia_api.post();

	},
	complete: function (apiobj) {
		// TODO Most of this is copy-pasted from Morebits.wiki.page#fnSaveSuccess. Unify it
		var xml = apiobj.getXML();
		var $edit = $(xml).find('edit');

		if ($(xml).find('captcha').length > 0) {
			apiobj.statelem.error('Could not rollback, because the wiki server wanted you to fill out a CAPTCHA.');
		} else if ($edit.attr('nochange') === '') {
			apiobj.statelem.error('Revision we are reverting to is identical to current revision, stopping revert.');
		} else {
			apiobj.statelem.info('done');
			var params = apiobj.params;

			if (params.notifyUser) { // Only from main, not from toRevision
				Morebits.status.info('Info', [ 'Opening user talk page edit form for user ', Morebits.htmlNode('strong', params.user) ]);

				var windowQuery = {
					'title': 'User talk:' + params.user,
					'action': 'edit',
					'preview': 'yes',
					'vanarticle': params.pagename.replace(/_/g, ' '),
					'vanarticlerevid': params.revid,
					'vanarticlegoodrevid': params.goodid,
					'type': params.type,
					'count': params.count
				};

				switch (Twinkle.getPref('userTalkPageMode')) {
					case 'tab':
						window.open(mw.util.getUrl('', windowQuery), '_blank');
						break;
					case 'blank':
						window.open(mw.util.getUrl('', windowQuery), '_blank',
							'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800');
						break;
					case 'window':
					/* falls through */
					default:
						window.open(mw.util.getUrl('', windowQuery),
							window.name === 'twinklewarnwindow' ? '_blank' : 'twinklewarnwindow',
							'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800');
						break;
				}
			}


			// review the revert, if needed
			if (apiobj.params.reviewRevert) {
				var query = {
					'action': 'review',
					'revid': $edit.attr('newrevid'),
					'token': apiobj.params.csrftoken,
					'comment': Twinkle.getPref('summaryAd').trim()
				};
				var wikipedia_api = new Morebits.wiki.api('Automatically accepting your changes', query);
				wikipedia_api.post();
			}
		}
	}
};

// builtInString should contain the string "$USER", which will be replaced
// by an appropriate user link
Twinkle.fluff.formatSummary = function(builtInString, userName, userString) {
	var result = builtInString;

	// append user's custom reason
	if (userString) {
		result += ': ' + Morebits.string.toUpperCaseFirstChar(userString);
	}
	result += Twinkle.getPref('summaryAd');

	// find number of UTF-8 bytes the resulting string takes up, and possibly add
	// a contributions or contributions+talk link if it doesn't push the edit summary
	// over the 255-byte limit
	var resultLen = unescape(encodeURIComponent(result.replace('$USER', ''))).length;
	var contribsLink = '[[Special:Contributions/' + userName + '|' + userName + ']]';
	var contribsLen = unescape(encodeURIComponent(contribsLink)).length;
	if (resultLen + contribsLen <= 255) {
		var talkLink = ' ([[User talk:' + userName + '|talk]])';
		if (resultLen + contribsLen + unescape(encodeURIComponent(talkLink)).length <= 255) {
			result = Morebits.string.safeReplace(result, '$USER', contribsLink + talkLink);
		} else {
			result = Morebits.string.safeReplace(result, '$USER', contribsLink);
		}
	} else {
		result = Morebits.string.safeReplace(result, '$USER', userName);
	}

	return result;
};
})(jQuery);


// </nowiki>