User:Wikidudeman/fluff.js

/** Twinklefluff revert and antivandalism utillity // If TwinkleConfig aint exist. if( typeof( TwinkleConfig ) == 'undefined' ) { TwinkleConfig = {}; } /** TwinkleConfig.revertMaxRevisions (int) defines how many revision to query maximum, maximum possible is 50, default is 50 if( typeof( TwinkleConfig.revertMaxRevisions ) == 'undefined' ) { TwinkleConfig.revertMaxRevisions = 50; } /** TwinkleConfig.userTalkPageMode may take arguments: 'window': open a new window, remmenber the opened window 'tab': opens in a new tab, if possible. 'blank': force open in a new window, even if a such window exist if( typeof( TwinkleConfig.userTalkPageMode ) == 'undefined' ) { TwinkleConfig.userTalkPageMode = 'window'; } /** TwinkleConfig.openTalkPage (array) What types of actions that should result in opening of talk page if( typeof( TwinkleConfig.openTalkPage ) == 'undefined' ) { TwinkleConfig.openTalkPage = [ 'agf', 'norm', 'vand' ]; } /** TwinkleConfig.openTalkPageOnAutoRevert (bool) Defines if talk page should be opened when canling revert from contrib page, this because from there, actions may be multiple, and opening talk page not suitable. If set to true, openTalkPage defines then if talk page will be opened. if( typeof( TwinkleConfig.openTalkPageOnAutoRevert ) == 'undefined' ) { TwinkleConfig.openTalkPageOnAutoRevert = false; } /** TwinkleConfig.openAOLAnonTalkPage may take arguments: true: to open Anon AOL talk pages on revert false: to not open them if( typeof( TwinkleConfig.openAOLAnonTalkPage ) == 'undefined' ) { TwinkleConfig.openAOLAnonTalkPage = false; } /** TwinkleConfig.summaryAd (string) If ad should be added or not to summary, default TWINKLE if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) { TwinkleConfig.summaryAd = " using TW"; } /** TwinkleConfig.markRevertedPagesAsMinor (array) What types of actions that should result in marking edit as minor if( typeof( TwinkleConfig.markRevertedPagesAsMinor ) == 'undefined' ) { TwinkleConfig.markRevertedPagesAsMinor = [ 'agf', 'norm', 'vand', 'torev' ]; } /** TwinkleConfig.watchRevertedPages (array) What types of actions that should result in forced addition to watchlist if( typeof( TwinkleConfig.watchRevertedPages ) == 'undefined' ) { TwinkleConfig.watchRevertedPages = [ 'agf', 'norm', 'vand', 'torev' ]; } /** TwinkleConfig.offerReasonOnNormalRevert (boolean) If to offer a promt for extra summary reason for normal reverts, default to true if( typeof( TwinkleConfig.offerReasonOnNormalRevert ) == 'undefined' ) { TwinkleConfig.offerReasonOnNormalRevert = true; } // 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', 'HBC AIV helperbot2', 'HBC AIV helperbot3', ] var revertXML; var contentXML; var contentDoc; var editXML; var vandal; var type; var goodRev; var nbrOfRevisions; var curStatus; var curVersion = true; addOnloadHook( function {		if( QueryString.exists( 'twinklerevert' ) ) {			twinkleAutoRevert;		} else {			addRevertButtons;		}	} ); function twinkleAutoRevert { if( QueryString.get( 'oldid' ) != wgCurRevisionId ) { // not latest revision return; }	var ntitle = getElementsByClassName( document.getElementById('bodyContent'), 'td', 'diff-ntitle' )[0]; if( ntitle.getElementsByTagName('a')[0].firstChild.nodeValue != 'Current revision' ) { // not latest revision return; }	vandal = ntitle.getElementsByTagName('a')[3].firstChild.nodeValue.replace("'", "\\'"); if( !TwinkleConfig.openTalkPageOnAutoRevert ) { TwinkleConfig.openTalkPage = []; }	return revertPage( QueryString.get( 'twinklerevert' ), vandal ); } function addRevertButtons { var spanTag = function( color, content ) { var span = document.createElement( 'span' ); span.style.color = color; span.appendChild( document.createTextNode( content ) ); return span; }	if( wgNamespaceNumber == -1 && wgCanonicalSpecialPageName == "Contributions" ) { var list = document.getElementById('bodyContent').getElementsByTagName( 'ul' )[0].getElementsByTagName( 'li' ); var vandal = document.getElementById('contentSub').getElementsByTagName( 'a' )[0].getAttribute( 'title' ).replace(/^User( talk)?:/, '').replace("'", "\\'"); var revNode = document.createElement('strong'); var revLink = document.createElement('a'); revLink.appendChild( spanTag( 'Black', ' [' ) ); revLink.appendChild( spanTag( 'SteelBlue', 'rollback' ) ); revLink.appendChild( spanTag( 'Black', ']' ) ); revNode.appendChild(revLink); var revVandNode = document.createElement('strong'); var revVandLink = document.createElement('a'); revVandLink.appendChild( spanTag( 'Black', ' [' ) ); revVandLink.appendChild( spanTag( 'Red', 'vandalism' ) ); revVandLink.appendChild( spanTag( 'Black', ']' ) ); revVandNode.appendChild(revVandLink); for(var i = 0; i < list.length; ++i ) { var item = list[i].lastChild; if ( !item ) { continue; }			if( userIsInGroup( 'sysop' ) ) { item = item.previousSibling; }			if( item.nodeName != 'STRONG' ) { continue }			var href = list[i].getElementsByTagName( 'a' )[1].getAttribute( 'href' ); var tmpNode = revNode.cloneNode( true ); tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'norm' } ) ); list[i].appendChild( tmpNode ); var tmpNode = revVandNode.cloneNode( true ); tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'vand' } ) ); list[i].appendChild( tmpNode ); }	} else { 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 query = new QueryString( decodeURI( otitle.getElementsByTagName( 'a' )[0].getAttribute( 'href' ).split( '?', 2 )[1] ) ); var oldrev = query.get( 'oldid' ); var oldEditNode = document.createElement('strong'); var oldEditLink = document.createElement('a'); oldEditLink.href = "javascript:revertToRevision('" + oldrev + "')"; 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.replace("'", "\\'"); 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); } } function revertPage( pType, pVandal, rev, page ) { wgPageName = page || wgPageName; wgCurRevisionId = rev || wgCurRevisionId; 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': TwinkleConfig.revertMaxRevisions, 'rvprop': [ 'ids', 'timestamp', 'user', 'comment' ], 'format': 'xml' }		Status.status( 'Querying revisions' ); revertXML.onreadystatechange = revertPageCallback; revertXML.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( query ), true ); revertXML.send( null ); } catch(e) { if( e instanceof Exception ) { Status.error( 'Error: ' + e.what ); } else { Status.error( 'Error: ' + e ); }	} } function revertPageCallback { if ( revertXML.readyState != 4 ){ Status.progress('.'); return; } 	if( revertXML.status != 200 ){ Status.error('Bad status, bailing out'); return; }	var doc = revertXML.responseXML.documentElement; if( !doc ) { Status.error( 'Possible failure in recieving document, will abort.' ); return; }	var revisions = doc.getElementsByTagName('rev'); var top = revisions[0]; if( top.getAttribute( 'revid' ) < wgCurRevisionId ) { Status.error( [ 'The recieved top revision id ', htmlNode( 'strong', top.getAttribute('revid') ), ' is less than our current revision id, this could indicate that the current revision has been deleted, the server is lagging, or that bad data has been recieved. Will stop proceeding at this point.' ] ); return; }	if( !top ) { Status.error( 'No top revision found, this could indicate that the page has been deleted, or that a problem in the transmittion has occoured, will abort reversion '); return; }	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; }	} 	if( WHITELIST.indexOf( vandal ) != -1 ) { switch( type ) { case 'vand': 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' ); break; case 'agf': 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; break; case 'norm': default: var cont = confirm( 'Normal revert is choosen, but the top user (' + vandal + ') is a whitelisted bot, do you want to revert the revision before instead?' ); if( cont ) { Status.info( [ 'Normal revert is choosen on ', htmlNode( 'strong', vandal ), ', as this is a whitelisted bot, and per confirm, we\'ll revert the previous revision instead.' ] ); top = top.nextSibling; vandal = top.getAttribute( 'user' ); } else { Status.warn( [ 'Normal revert is choosen on ', htmlNode( 'strong', vandal ), ', this is a whitelisted bot, bet per confirm, revert will proceed.' ] ); }			break; }	}	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 ' + TwinkleConfig.revertMaxRevisions + ' 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 = sajax_init_object; revertXML.overrideMimeType('text/xml'); revertXML.onreadystatechange = revertCallback2; revertXML.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( query ), true ); revertXML.send( null ); } function revertCallback2 { if ( revertXML.readyState != 4 ){ Status.progress( '.' ); return; } 	if( revertXML.status != 200 ){ Status.error( 'Bad status, bailing out' ); return; }	contentDoc = revertXML.responseXML.documentElement; if( !contentDoc ) { Status.error( 'Failed to recieve revision to revert to, will abort.'); return; }	Status.status( 'Grabbing edit form' ); revertXML = sajax_init_object; revertXML.overrideMimeType('text/xml'); revertXML.onreadystatechange = revertCallback3; var query = { 'title': wgPageName, 'action': 'submit' };	revertXML.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), true ); revertXML.send( null ); } function revertCallback3 { if ( revertXML.readyState != 4 ){ Status.progress( '.' ); return; } 	if( revertXML.status != 200 ){ Status.error( 'Bad status, bailing out' ); return; }	Status.status( 'Updating the textbox...' ); var doc = revertXML.responseXML; var form = doc.getElementById( 'editform' ); if( !form ) { Status.error( 'couldn\'t grab element "editform", aborting, this could indicate failed respons from the server' ); return; }	form.style.display = 'none'; var content = contentDoc.getElementsByTagName('rev')[0]; if( !content ) { Status.error( 'we recieved no revision, something is wrong, bailing out!' ); return; }	var textbox = doc.getElementById( 'wpTextbox1' ); textbox.value = ""; var cn = content.childNodes; for( var i = 0; i < cn.length; ++i ) { textbox.value += cn[i].nodeValue ? cn[i].nodeValue : ''; }	Status.status( 'Updating the summary...' ); var summary; var extra_summary = ''; switch( type ) { case 'agf': extra_summary = prompt( "An optional comment for the edit summary:" ); summary = "Reverted good faith edits by " + vandal + "" + ( extra_summary ? "; " + extra_summary.toUpperCaseFirstChar : '' ) + "." + TwinkleConfig.summaryAd; break; case 'vand': summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + " by " + vandal + " identified as vandalism to last revision by " + goodRev.getAttribute( 'user' ) + "." + TwinkleConfig.summaryAd; break; case 'norm': if( TwinkleConfig.offerReasonOnNormalRevert ) { extra_summary = prompt( "An optional comment for the edit summary:" ); }		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" :  ) + " by " + vandal + " to last revision by  " + goodRev.getAttribute( 'user' ) + "" + ( extra_summary ? "; " + extra_summary.toUpperCaseFirstChar :  ) + '.' + TwinkleConfig.summaryAd; }	doc.getElementById( 'wpSummary' ).value = summary; if( TwinkleConfig.markRevertedPagesAsMinor.indexOf( type ) != -1 ) { doc.getElementById( 'wpMinoredit' ).checked = true; }	if( TwinkleConfig.watchRevertedPages.indexOf( type ) != -1 ) { doc.getElementById( 'wpWatchthis' ).checked = true; }	Status.status( [ 'Open user talk page edit form for user ', htmlNode( 'strong', vandal ) ]); var opentalk = true; if( TwinkleConfig.openTalkPage.indexOf( type ) != -1 ) { if( isIPAddress( vandal ) ) { 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 ) } )) { if( TwinkleConfig.openAOLAnonTalkPage ) { Status.info( [ htmlNode( 'strong', vandal ), ' is an AOL address. Per configuration, we will open talk page anyway' ] ); } else { Status.warn( [ htmlNode( 'strong', vandal ), ' is an AOL address. will not open a edit form for the user talk page because AOL addresses are randomly assigned' ] ); opentalk = false; }			} else { Status.info( [ htmlNode( 'strong', vandal ), ' is an normal ip-address, opening user talk page' ] ); }		}		if( opentalk ) { var query = { 'title': 'User talk:' + vandal, 'action': 'edit', 'vanarticle': wgPageName.replace(/_/g, ' '), 'vanarticlerevid': wgCurRevisionId, 'vanarticlegoodrevid': goodRev.getAttribute( 'revid' ), 'type': type, 'count': nbrOfRevisions }			switch( TwinkleConfig.userTalkPageMode ) { case 'tab': window.open( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), '_tab' ); break; case 'blank': window.open( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), '_blank', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' ); break; case 'window': default : window.open( mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), 'twinklewarnwindow', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' ); break; }		}	}	document.getElementById('globalWrapper').appendChild( form ); Status.status( 'Submitting the form...' ); form.submit; } function revertToRevision( oldrev ) { try { Status.init( document.getElementById('bodyContent') ); var query = { 'action': 'query', 'prop': 'revisions', 'titles': wgPageName, 'rvlimit': 1, 'rvstartid': oldrev, 'rvprop': [ 'ids', 'timestamp', 'user', 'comment', 'content' ], 'format': 'xml' }		Status.status( 'Querying revision' ); var xmlhttp = sajax_init_object; xmlhttp.overrideMimeType('text/xml'); xmlhttp.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?' + QueryString.create( query ), true ); xmlhttp.onload = function(e) { Status.status( 'Grabbing edit form' ); var xmlhttp = sajax_init_object; xmlhttp.lastGoodRevision = this.responseXML.documentElement.getElementsByTagName('rev')[0]; xmlhttp.query = { 'title': wgPageName, 'action': 'submit' }			xmlhttp.overrideMimeType('text/xml'); xmlhttp.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( xmlhttp.query ), true ); xmlhttp.onload = function(e) { Status.status( 'Parsing the text of the old revision...' ); var form = this.responseXML.getElementById( 'editform' ); var text; if( !form ) { Status.error( 'couldn\'t grab element "editform", aborting, this could indicate failed respons from the server' ); return; }				text = this.lastGoodRevision.textContent; Status.status( 'Updating the summary...' ); var optional_summary = prompt( "Please, if possible, specify a reason for the revert" ); var summary = 'Reverted to revision ' + this.lastGoodRevision.getAttribute( 'revid' ) + ' by ' + this.lastGoodRevision.getAttribute( 'user' ) +  + ( optional_summary ? "; " + optional_summary :  ) + '.' + TwinkleConfig.summaryAd; var postData = { 'wpMinoredit': TwinkleConfig.markRevertedPagesAsMinor.indexOf( 'torev' ) != -1 ? true : form.wpMinoredit.checked ? '': undefined, 'wpWatchthis': TwinkleConfig.watchRevertedPages.indexOf( 'torev' ) != -1 ? true : form.wpWatchthis.checked ? '': undefined, 'wpStarttime': form.wpStarttime.value, 'wpEdittime': form.wpEdittime.value, 'wpAutoSummary': form.wpAutoSummary.value, 'wpEditToken': form.wpEditToken.value, 'wpSummary': summary, 'wpTextbox1': text };				Status.status( 'Submitting the form...' ); var xmlhttp = sajax_init_object; xmlhttp.overrideMimeType('text/xml'); xmlhttp.open( 'POST', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( this.query ), true); xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); xmlhttp.onload = function { window.location = mw.config.get('wgServer') + mw.config.get('wgArticlePath').replace( '$1', wgPageName.replace( '_', ' ' ) ); } xmlhttp.send( QueryString.create( postData ) ); }			xmlhttp.send( null ); }		xmlhttp.send( null ); } catch(e) { if( e instanceof Exception ) { Status.error( 'Error: ' + e.what ); } else { Status.error( 'Error: ' + e ); }	} }