Jump to content

User:Voice of All/Revert.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Aaron Schulz (talk | contribs) at 17:22, 28 January 2007 (m). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
//<pre><nowiki>

//////AOL RANGES////////
var AOL_RANGES = /^(64\.12\.|149\.174|152\.163\.|195\.93\.[0-9]\d{0,1}\.|195\.93\.1[0-1][0-9]\.|195\.93\.12[0-7]\.|198\.81\.[0-9]\.|198\.81\.[1-2][0-9]\.|198\.81\.3[01]\.|202\.67\.6[4-9]\.|202\.67\.[7-9][0-9]\.|202\.67\.1[01]|202\.67\.12[0-7]|205\.188\.|207\.200\.6[4-9]\.|207\.200\.[7-9][0-9]\.|207\.200\.1[01]|207\.200\.12[0-7])/;
//all ranges
var AOL_RANGES_ALL = /^(64\.12\.|149\.174|152\.163\.|195\.93\.[0-9]\d{0,1}\.|195\.93\.1[0-1][0-9]\.|195\.93\.12[0-7]\.|198\.81\.[0-9]\.|198\.81\.[1-2][0-9]\.|198\.81\.3[01]\.|202\.67\.6[4-9]\.|202\.67\.[7-9][0-9]\.|202\.67\.1[01]|202\.67\.12[0-7]|205\.188\.|207\.200\.6[4-9]\.|207\.200\.[7-9][0-9]\.|207\.200\.1[01]|207\.200\.12[0-7]|172\.12[0-8]|172\.1[3-9][0-9]|172\.20[0-9]|172\.21[0-6])/;

//has some stuff, alternate AOL range system
importScript('User:AzaToth/morebits.js');

//////
/**
"Twinklefluff" revert and antivandalism utillity
By AzaToth, modified a bit, added by request
*/

var VERSION = '1.0a';
var MAXREV = 8; // maximum number of revision to lookup

// a list of usernames, usually only bots, that vandalism revert is jumped over, that is
// if vandalism revert is choosen on such username, then it's target in on the revision before.
// This is for handeling quick bots that makes edits seconds after the original edit is made.
// This only affect vandalism rollback, for good faith rollback, it will stop, indicating a bot 
// has no faith, and for normal rollback, it will rollback that edit.
var WHITELIST = [
	'HagermanBot',
	'HBC AIV helperbot'
]

var revertXML;
var contentXML;
var contentDoc;
var editXML;
var vandal;
var type;
var goodRev;
var nbrOfRevisions;
var curStatus;
var curVersion = true;

function addRevertButtons() {

	var spanTag = function( color, content ) {
		var span = document.createElement( 'span' );
		span.style.color = color;
		span.appendChild( document.createTextNode( content ) );
		return span;
	}

	var otitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-otitle' )[0];
	var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td' , 'diff-ntitle' )[0];

	if( !ntitle ) {
		// Nothing to see here, move along...
		return;
	}

	if( !otitle.getElementsByTagName('a')[0] ) {
		// no previous revision available
		return;
	}

	// Lets first add a [edit this revision] link

	var oldrev = QueryString.get( 'oldid', decodeURI( otitle.getElementsByTagName( 'a' )[0].getAttribute( 'href' ).split( '&', 2 )[1] ) );
	var query = {
		'action': 'edit',
		'title': wgPageName,
		'oldid': oldrev,
	};

	var oldEditNode = document.createElement('strong');

	var oldEditLink = document.createElement('a');
	oldEditLink.href = wgServer + wgScriptPath + '/index.php?' + QueryString.create( query );
	oldEditLink.appendChild( spanTag( 'Black', '[' ) );
	oldEditLink.appendChild( spanTag( 'SaddleBrown', 'restore this version' ) );
	oldEditLink.appendChild( spanTag( 'Black', ']' ) );
	oldEditNode.appendChild(oldEditLink);

	var cur = otitle.insertBefore(oldEditNode, otitle.firstChild);
	otitle.insertBefore(document.createElement('br'), cur.nextSibling);

	if( ntitle.getElementsByTagName('a')[0].firstChild.nodeValue != 'Current revision' ) {
		// not latest revision
		curVersion = false;
		return;
	}

	vandal = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue;

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

	var agfLink = document.createElement('a');
	var vandLink = document.createElement('a');
	var normLink = document.createElement('a');

	agfLink.href = "javascript:revertPage('agf' , '" + vandal + "')"; 
	vandLink.href = "javascript:revertPage('vand' , '" + vandal + "')"; 
	normLink.href = "javascript:revertPage('norm' , '" + vandal + "')"; 

	agfLink.appendChild( spanTag( 'Black', '[' ) );
	agfLink.appendChild( spanTag( 'DarkOliveGreen', 'rollback (AGF)' ) );
	agfLink.appendChild( spanTag( 'Black', ']' ) );

	vandLink.appendChild( spanTag( 'Black', '[' ) );
	vandLink.appendChild( spanTag( 'Red', 'rollback (VANDAL)' ) );
	vandLink.appendChild( spanTag( 'Black', ']' ) );

	normLink.appendChild( spanTag( 'Black', '[' ) );
	normLink.appendChild( spanTag( 'SteelBlue', 'rollback' ) );
	normLink.appendChild( spanTag( 'Black', ']' ) );

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

	var cur = ntitle.insertBefore(agfNode, ntitle.firstChild);
	cur = ntitle.insertBefore(document.createTextNode(' || '), cur.nextSibling);
	cur = ntitle.insertBefore(normNode, cur.nextSibling);
	cur = ntitle.insertBefore(document.createTextNode(' || '), cur.nextSibling);
	cur = ntitle.insertBefore(vandNode, cur.nextSibling);
	cur = ntitle.insertBefore(document.createElement('br'), cur.nextSibling);

}
addOnloadHook(addRevertButtons);

function revertPage( pType, pVandal ) {

	try {
		vandal = pVandal;
		type = pType;
		Status.init( document.getElementById('bodyContent') );

		revertXML = sajax_init_object();
		revertXML.overrideMimeType('text/xml');

		var query = {
			'action': 'query',
			'prop': 'revisions',
			'titles': wgPageName,
			'rvlimit': MAXREV,
			'rvprop': [ 'timestamp', 'user', 'comment' ],
			'format': 'xml'
		}

		Status.status( 'Querying revisions' );
		revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ) );
		revertXML.onreadystatechange = revertPageCallback;
		revertXML.send( null );
	} catch(e) {
		alert( e.what() );
	}

}
function revertPageCallback() {

	if ( revertXML.readyState != 4 ){
		Status.progress('.');
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error('Bad status , bailing out');
		return;
	}

	Status.progress(' Done');

	var doc = revertXML.responseXML.documentElement;
	var revisions = doc.getElementsByTagName('rev');
	var top = revisions[0];

	// Not top revision

	Status.status( [ 'Evaluating revisions to see if ', htmlNode( 'strong', vandal), ' is the last contributor...' ] );
	if( wgCurRevisionId != top.getAttribute('revid') ) {
		Status.warn( [ 'Latest revision ', htmlNode( 'strong', top.getAttribute('revid') ), ' doesn\'t equals our revision ', htmlNode( 'strong', wgCurRevisionId) ] )
		if( top.getAttribute( 'user' ) == vandal ) {
			switch( type ) {
				case 'vand':
				Status.info( [ 'Latest revision is made by ', htmlNode( 'strong', vandal ) , ', as we assume vandalism, we continue to revert' ]);
				break;
				case 'afg':
				Status.warn( [ 'Latest revision is made by ', htmlNode( 'strong', vandal ) , ', as we assume good faith, we stop reverting, as the problem might have been fixed.' ]);
				return;
				default:
				Status.warn( [ 'Latest revision is made by ', htmlNode( 'strong', vandal ) , ', but we will stop reverting anyway.' ] );
				return;
			}
		} else if( 
			type == 'vand' && 
			WHITELIST.indexOf( top.getAttribute( 'user' ) ) != -1 && 
			top.nextSibling.getAttribute( 'pageId' ) == wgCurRevisionId 
		) {
			Status.info( [ 'Latest revision is made by ', htmlNode( 'strong', top.getAttribute( 'user' ) ), ', a trusted bot, and the revision before was made by our vandal, so we proceed with the revert.' ] );
			top = top.nextSibling;
		} else {
			Status.error( [ 'Latest revision is made by ', htmlNode( 'strong', top.getAttribute( 'user' ) ), ', so it might already been reverted, stopping  reverting.'] );
			return;
		}
	} else {
		Status.progress( ' Done' );
	}

	if( type == 'vand' && WHITELIST.indexOf( vandal ) != -1 ) {
		Status.info( [ 'Vandalism revert is choosen on ', htmlNode( 'strong', vandal ), ', as this is a whitelisted bot, we assume you wanted to revert vandalism made by the previous user instead.' ] );
		top = top.nextSibling;
		vandal = top.getAttribute( 'user' );
	} else if( type == 'agf'  && WHITELIST.indexOf( vandal ) != -1 ) {
		Status.warn( [ 'Good faith revert is choosen on ', htmlNode( 'strong', vandal ), ', as this is a whitelisted bot, it makes no sense at all to revert it as a good faith edit, will stop reverting.' ] );
		return;
	}

	Status.status( 'Finding last good revision...' );

	goodRev = top;
	nbrOfRevisions = 0;

	while( goodRev.getAttribute('user') == vandal ) {

		goodRev = goodRev.nextSibling;

		nbrOfRevisions++;

		if( goodRev == null ) {
			Status.error( [ 'No previous revision found, perhaps ', htmlNode( 'strong', vandal ), ' is the only contributor, or that the user has made more than ' + MAXREV + ' edits in a row.' ] );
			return;
		}
	}

	if( nbrOfRevisions == 0 ) {
		Status.error( "We where to revert zero revisions. As that makes no sense, we'll stop reverting this time. It could be that the edit already have been reverted, but the revision id was still the same." );
		return;
	}

	if( 
		type != 'vand' && 
		nbrOfRevisions > 1  && 
		!confirm( vandal + ' has done ' + nbrOfRevisions + ' edits in a row. Are you sure you want to revert them all?' ) 
	) {
		Status.info( 'Stopping reverting per user input' );
		return;
	}

	Status.progress( [ ' revision ', htmlNode( 'strong', goodRev.getAttribute( 'revid' ) ), ' that was made ', htmlNode( 'strong', nbrOfRevisions ), ' revisions ago by ', htmlNode( 'strong', goodRev.getAttribute( 'user' ) ) ] );

	Status.status( [ 'Getting content for revision ', htmlNode( 'strong', goodRev.getAttribute( 'revid' ) ) ] );
	var query = {
		'action': 'query',
		'prop': 'revisions',
		'titles': wgPageName,
		'rvlimit': 1,
		'rvprop': 'content',
		'rvstartid': goodRev.getAttribute( 'revid' ),
		'format': 'xml'
	}

	// getting the content for the last good revision
	revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ));
	revertXML.onreadystatechange = revertCallback2;
	revertXML.send( null );

}

function revertCallback2() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error( 'Bad status , bailing out' );
		return;
	}

	Status.progress( ' Done' );

	contentDoc = revertXML.responseXML.documentElement;

	Status.status( 'Grabbing edit form' );

	revertXML.open( 'GET' , wgServer + wgScriptPath + '/index.php?' + QueryString.create( { 'title': wgPageName, 'action': 'submit' } ));
	revertXML.onreadystatechange = revertCallback3;
	revertXML.send( null );
}

function revertCallback3() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

	if( revertXML.status != 200 ){
		Status.error( 'Bad status , bailing out' );
		return;
	}

	Status.progress( ' Done' );

	Status.status( 'Updating the textbox...' );

	var doc = revertXML.responseXML;

	var form = doc.getElementById( 'editform' );
	form.style.display = 'none';


	var content = contentDoc.getElementsByTagName('rev')[0];

	var textbox = doc.getElementById( 'wpTextbox1' );

	textbox.value = "";

	var cn =  content.childNodes;

	for( var i in cn ) {
		textbox.value += cn[i].nodeValue ? cn[i].nodeValue : '';
	}

	Status.progress( ' Done' );

	Status.status( 'Updating the summary...' );
	var summary;



	switch( type ) {
		case 'agf':
		summary = "Reverted [[WP:AGF|good faith]] edits by [[Special:Contributions/" + vandal + "|" + vandal + "]] per policy concerns. Please read up on [[WP:POL#Key_policies|policies and guidelines]]. Thanks!";
		break;
		case 'vand':
		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + " by [[Special:Contributions/" + vandal + "|" + vandal + "]] identified as [[WP:VAND|vandalism]] to last revision made by [[User:" + goodRev.getAttribute( 'user' ) + "|" + goodRev.getAttribute( 'user' ) + "]].";
		break;
		case 'norm':
		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + "  by [[Special:Contributions/" + vandal + "|" + vandal + "]]  to last revision by  [[User:" + goodRev.getAttribute( 'user' ) + "|" + goodRev.getAttribute( 'user' ) + "]].";
	}
	doc.getElementById( 'wpSummary' ).value = summary;
	var minor = doc.getElementById( 'wpMinoredit' );
	if( minor ) {
		minor.value = 1;
	}

	Status.progress( ' Done' );

	Status.status( [ 'Open user talk page edit form for user ', htmlNode( 'strong', vandal ) ]);

	var opentalk = true;

	if( vandal.match( /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/ ) ) {
		Status.info( [ htmlNode( 'strong', vandal ), ' is an ip-address, checking if it\'s inside the AOL range' ] );

		if( AOLNetworks.some( function( net ) { return isInNetwork( vandal, net ) } )) {
			Status.warn( [ htmlNode( 'strong', vandal ), ' is an AOL address. will not open a edit form for the user talk page because AOL addresses change rapidly' ] );
			opentalk = false;
		} else {
			Status.info( [ htmlNode( 'strong', vandal ), ' is an normal ip-address, opening user talk page' ] );
		}

	} else {
		Status.progress( ' Done' );
	}
	document.getElementById('globalWrapper').appendChild( form );
	if( opentalk ) {
		var query = {
			'title': 'User talk:' + vandal,
			'action': 'edit',
			'vanarticle': wgPageName.replace(/_/g, ' '),
			'type': type,
			'count': nbrOfRevisions,
		}
		window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), 'vandalwarn', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
	}

	Status.status( 'Sumbitting the form...' );
	form.submit();
	Status.progress( ' Done' );


}

//END</nowiki></pre>