Gebruiker:Waninge/twinklefluff.js

In dit artikel gaan we dieper in op het onderwerp Gebruiker:Waninge/twinklefluff.js en onderzoeken we de verschillende facetten en relevante aspecten ervan. Gebruiker:Waninge/twinklefluff.js is een breed en gevarieerd onderwerp dat de interesse en aandacht van veel mensen over de hele wereld heeft getrokken. Door middel van analyse en reflectie zullen we proberen licht te werpen op verschillende gezichtspunten en perspectieven met betrekking tot Gebruiker:Waninge/twinklefluff.js, met als doel een complete en verrijkende visie te bieden. Op dezelfde manier zullen we de historische evolutie van Gebruiker:Waninge/twinklefluff.js bespreken, de relevantie ervan vandaag en mogelijke implicaties voor de toekomst. Ga met ons mee op deze spannende verkenning van Gebruiker:Waninge/twinklefluff.js en ontdek alles wat dit thema te bieden heeft!
// Have debug on now.
//Status.debugLevel = 1;

/**
Twinklefluff revert and antivandalism utillity
*/
var VERSION = '1.0';

// 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 = ;
}

/**
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 ]
*/
if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) {
	TwinkleConfig.summaryAd = " using ]";
}

/**
TwinkleConfig.markRevertedPagesAsMinor (array)
What types of actions that should result in marking edit as minor
*/
if( typeof( TwinkleConfig.markRevertedPagesAsMinor ) == 'undefined' ) {
	TwinkleConfig.markRevertedPagesAsMinor = ;
}

/**
TwinkleConfig.watchRevertedPages (array)
What types of actions that should result in forced addition to watchlist
*/
if( typeof( TwinkleConfig.watchRevertedPages ) == 'undefined' ) {
	TwinkleConfig.watchRevertedPages = ;
}


// 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;

$( 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' );
	if( ntitle.getElementsByTagName('a').firstChild.nodeValue != 'Current revision' ) {
		// not latest revision
		return;
	}

	vandal = ntitle.getElementsByTagName('a').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' ).getElementsByTagName( 'li' );
		var vandal = document.getElementById('contentSub').getElementsByTagName( 'a' ).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 in list ) {
			var item = list.lastChild;
			if ( !item ) {
				continue;
			}
			if( userIsInGroup( 'sysop' ) ) {
				item = item.previousSibling;
			}
			if( item.nodeName != 'STRONG' ) {
				continue
			}

			var href = list.getElementsByTagName( 'a' ).getAttribute( 'href' );
			var tmpNode = revNode.cloneNode( true );
			tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'norm' } ) );
			list.appendChild( tmpNode );
			var tmpNode = revVandNode.cloneNode( true );
			tmpNode.firstChild.setAttribute( 'href', href + '&' + QueryString.create( { 'twinklerevert': 'vand' } ) );
			list.appendChild( tmpNode );
		}


	} else {

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

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

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

		// Lets first add a  link

		var oldrev = QueryString.get( 'oldid', decodeURI( otitle.getElementsByTagName( 'a' ).getAttribute( 'href' ).split( '&', 2 ) ) );

		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').firstChild.nodeValue != 'Current revision' ) {
			// not latest revision
			curVersion = false;
			return;
		}

		vandal = ntitle.getElementsByTagName('a').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();
		Status.debug( 'revertXML' + revertXML );
		revertXML.overrideMimeType('text/xml');

		var query = {
			'action': 'query',
			'prop': 'revisions',
			'titles': wgPageName,
			'rvlimit': TwinkleConfig.revertMaxRevisions,
			'rvprop': ,
			'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;
	Status.debug( 'revisions: ' + top );

	if( top.getAttribute( 'revid' ) < wgCurRevisionId ) {
		Status.error(  );
		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(  );
	Status.debug( 'wgCurRevisionId: ' + wgCurRevisionId + ', top.getAttribute(revid): ' + top.getAttribute('revid') );

	if( wgCurRevisionId != top.getAttribute('revid') ) {
		Status.warn(  );
		Status.debug( 'top.getAttribute(user): ' + top.getAttribute( 'user' ) );

		if( top.getAttribute( 'user' ) == vandal ) {
			switch( type ) {
				case 'vand':
				Status.info( );
				break;
				case 'afg':
				Status.warn( );
				return;
				default:
				Status.warn(  );
				return;
			}
		} else if( 
			type == 'vand' && 
			WHITELIST.indexOf( top.getAttribute( 'user' ) ) != -1 && 
			top.nextSibling.getAttribute( 'pageId' ) == wgCurRevisionId 
		) {
			Status.info(  );
			top = top.nextSibling;
		} else {
			Status.error(  );
			return;
		}
	} 

	if( WHITELIST.indexOf( vandal ) != -1  ) {
		switch( type ) {
			case 'vand':
			Status.info(  );
			top = top.nextSibling;
			vandal = top.getAttribute( 'user' );

			break;
			case 'agf':
			Status.warn(  );
			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(  );
				top = top.nextSibling;
				vandal = top.getAttribute( 'user' );
			} else {
				Status.warn(  );
			}
			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(  );
			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(  );

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

	Status.debug( 'query:' + query.toSource() );

	// 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'
	};

	Status.debug( 'query:' + query.toSource() );

	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' );
	Status.debug( 'editform: ' + form );
	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');
	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 in cn ) {
		textbox.value += cn.nodeValue ? cn.nodeValue : '';
	}

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



	switch( type ) {
		case 'agf':
		summary = "Reverted ] edits by ] per policy concerns. Please read up on ]. Thanks!" + TwinkleConfig.summaryAd;
		break;
		case 'vand':
		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + " by ] identified as ] to last revision by ]." + TwinkleConfig.summaryAd;
		break;
		case 'norm':
		summary = "Reverted " + nbrOfRevisions + " edit" + ( nbrOfRevisions > 1 ? "s" : '' ) + " by ]  to last revision by  ]." + 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( );

	var opentalk = true;

	if( TwinkleConfig.openTalkPage.indexOf( type ) != -1 ) {

		if( isIPAddress( vandal ) ) {
			Status.info(  );

			if( AOLNetworks.some( function( net ) { return isInNetwork( vandal, net ) } )) {
				if( TwinkleConfig.openAOLAnonTalkPage ) {
					Status.info(  );
				} else {
					Status.warn(  );
					opentalk = false;
				}
			} else {
				Status.info(  );
			}

		}

		if( opentalk ) {
			var query = {
				'title': 'User talk:' + vandal,
				'action': 'edit',
				'vanarticle': wgPageName.replace(/_/g, ' '),
				'vanarticlerevid': wgCurRevisionId,
				'vanarticlegoodrevid': goodRev.getAttribute( 'revid' ),
				'type': type,
				'count': nbrOfRevisions
			}

			Status.debug( 'query:' + query.toSource() );

			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') );

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

		var query = {
			'action': 'query',
			'prop': 'revisions',
			'titles': wgPageName,
			'rvlimit': 1,
			'rvstartid': oldrev,
			'rvprop': ,
			'format': 'xml'
		}

		Status.status( 'Querying revision' );
		revertXML.onreadystatechange = revertToRevisionCallback;
		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 revertToRevisionCallback() {
	if ( revertXML.readyState != 4 ){
		Status.progress( '.' );
		return;
	} 

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

	contentDoc = revertXML.responseXML.documentElement;

	Status.status( 'Grabbing edit form' );

	revertXML = sajax_init_object();
	revertXML.overrideMimeType('text/xml');
	revertXML.onreadystatechange = revertToRevisionCallback2;
	revertXML.open( 'GET' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( { 'title': wgPageName, 'action': 'submit' } ), true );
	revertXML.send( null );
}

function revertToRevisionCallback2() {
	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' );
	Status.debug( 'editform: ' + form );
	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');

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

	textbox.value = "";

	var cn =  content.childNodes;

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

	Status.status( 'Updating the summary...' );
	var summary = 'Reverted to revision ' + content.getAttribute( 'revid' ) + ' by ].' +TwinkleConfig.summaryAd;

	doc.getElementById( 'wpSummary' ).value = summary;

	if( TwinkleConfig.markRevertedPagesAsMinor.indexOf( 'torev' ) != -1 ) {
		doc.getElementById( 'wpMinoredit' ).checked = true;
	}

	if( TwinkleConfig. watchRevertedPages.indexOf( 'torev' ) != -1 ) {
		doc.getElementById( 'wpWatchthis' ).checked = true;
	}

	document.getElementById('globalWrapper').appendChild( form );

	Status.status( 'Submitting the form...' );
	form.submit();
}