Jump to content

User:Ainz Ooal Gown/mobilediffrollback.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//A script to add a rollback button to a diff page in mobile
if ( mw.config.get('wgCanonicalSpecialPageName') === 'MobileDiff' && mw.config.get('wgRelevantPageIsProbablyEditable') ) {
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'oojs-ui-windows', 'oojs-ui-core', 'oojs-ui.styles.icons-editing-core']).then( function () {
// Api variables and request call		
var api = new mw.Api(),
	callPromise = api.get({
	"action": "query",
	"format": "json",
	"prop": "revisions",
	"list": "users",
	"meta": "tokens",
	"titles": mw.config.get('wgRelevantPageName'),
	"formatversion": "2",
	"rvprop": "ids|user",
	"usprop": "rights",
	"ususers": mw.config.get( 'wgUserName' ),
	"type": "rollback"
});
// When the response is ready, then function
$.when( callPromise, $.ready ).then( function ( results ) {
	var rightsData, lastestRevisionUser, latestRevId, rollbackToken,
		response = results[ 0 ];
// Avoid loading the script if one of the details is not available
	if (
		response.query &&
		response.query.users &&
		response.query.users[0].rights &&
		response.query.tokens &&
		response.query.tokens.rollbacktoken &&
		response.query.pages[0] &&
		response.query.pages[0].revisions[0] &&
		response.query.pages[0].revisions[0].user &&
		response.query.pages[0].revisions[0].revid
		) {
//Get the revid from oldid link
			window.wgRevId = ($('#mw-mf-diffarea > div.mw-mf-diff-info > h3 > a').attr('href')).split('&oldid=')[1];
// Define data variables from API
			rightsData = response.query.users[0].rights;
			latestRevId = response.query.pages[0].revisions[0].revid;
			lastestRevisionUser = response.query.pages[0].revisions[0].user;
			rollbackToken = response.query.tokens.rollbacktoken;
		var mobilerollback = new OO.ui.ButtonWidget({
		label: 'Rollback',
		href: mw.util.getUrl( mw.config.get('wgRelevantPageName'), { action: 'rollback', token: rollbackToken, from: lastestRevisionUser }),
		icon: 'editUndo',
		id: 'mobileRollback',
		title: '"Rollback"  reverts the last contributor’s edit(s) to this page',
		classes: 'mw-rollback-link',
		flags: ['destructive', 'primary']
            });
// Only show the rollback button if the user has rollback right and if the specified revision is latest
			if ( wgRevId == latestRevId && response.query.users[0].rights.indexOf('rollback') !== -1 ) {
$('.minerva__subtitle').append( mobilerollback.$element );
			}
//Construct the confirmation prompt to ask for a summary
var rollbackSummaryPrompt = function(e) {
	e.preventDefault();
	OO.ui.prompt( 'Are you sure you want to rollback edits made to this page by this user?', { textInput: { placeholder: 'Edit summary (optional)' } } ).done( function ( result ) {
		if ( result !== null ) {
		location.href = mw.util.getUrl( mw.config.get('wgRelevantPageName'), { action: 'rollback', token: rollbackToken, from: lastestRevisionUser, summary: result });
		}
});
};
$('#mobileRollback').click(rollbackSummaryPrompt);
//Prompt end
		}
	});
});
}