User:AzaToth/twinklefluff.js: Difference between revisions
Appearance
Content deleted Content added
more |
skip that one then |
||
Line 57: | Line 57: | ||
for(var i in list ) { |
for(var i in list ) { |
||
if( !list[i].lastChild || list[i].lastChild.nodeName != 'STRONG |
if( !list[i].lastChild || list[i].lastChild.nodeName != 'STRONG' ) { |
||
continue |
continue |
||
} |
} |
Revision as of 02:19, 31 January 2007
/**
Twinklefluff revert and antivandalism utillity
*/
var VERSION = '1.0';
var MAXREV = 50; // maximum number of revision to lookup
var twinklefluffForceOpenUserTalkPageInNewTab = twinklefluffForceOpenUserTalkPageInNewTab || false;
var twinklefluffForceOpenUserTalkPageInNewWindow = twinklefluffForceOpenUserTalkPageInNewWindow || false;
// 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;
}
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 in list ) {
if( !list[i].lastChild || list[i].lastChild.nodeName != 'STRONG' ) {
continue
}
var oldid = list[i].getElementsByTagName( 'a' )[1].getAttribute( 'href' ).split( 'oldid=' )[1];
var page = list[i].getElementsByTagName( 'a' )[2].getAttribute( 'title' ).replace("'", "\\'");
var tmpNode = revNode.cloneNode( true );
tmpNode.firstChild.setAttribute( 'href', "javascript:revertPage('norm' , '" + vandal + "', " + oldid + ", '" + page + "')" );
list[i].appendChild( tmpNode );
var tmpNode = revVandNode.cloneNode( true );
tmpNode.firstChild.setAttribute( 'href', "javascript:revertPage('vand' , '" + vandal + "', " + oldid + ", '" + page + "')" );
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 oldrev = QueryString.get( 'oldid', decodeURI( otitle.getElementsByTagName( 'a' )[0].getAttribute( 'href' ).split( '&', 2 )[1] ) );
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);
}
}
addOnloadHook(addRevertButtons);
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': MAXREV,
'rvprop': [ 'timestamp', 'user', 'comment' ],
'format': 'xml'
}
Status.status( 'Querying revisions' );
revertXML.onreadystatechange = revertPageCallback;
revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ), true );
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 = sajax_init_object();
revertXML.overrideMimeType('text/xml');
revertXML.onreadystatechange = revertCallback2;
revertXML.open( 'GET' , wgServer + 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;
}
Status.progress( ' Done' );
contentDoc = revertXML.responseXML.documentElement;
Status.status( 'Grabbing edit form' );
revertXML = sajax_init_object();
revertXML.overrideMimeType('text/xml');
revertXML.onreadystatechange = revertCallback3;
revertXML.open( 'GET' , wgServer + wgScriptPath + '/index.php?' + QueryString.create( { 'title': wgPageName, 'action': 'submit' } ), 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.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! ([[WP:TWINKLE|Twinklefluff " + VERSION + "]])";
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' ) + "]]. ([[WP:TWINKLE|Twinklefluff " + VERSION + "]])";
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' ) + "]]. ([[WP:TWINKLE|Twinklefluff " + VERSION + "]])";
}
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 are randomly' ] );
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,
}
if( twinklefluffForceOpenUserTalkPageInNewTab ) {
window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), '_tab' );
} else if( twinklefluffForceOpenUserTalkPageInNewWindow ) {
window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), '_blank', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
} else {
window.open( wgServer + wgScriptPath + '/index.php?' + QueryString.create( query ), 'twinklewarnwindow', 'location=no,toolbar=no,status=no,directories=no,scrollbars=yes,width=1200,height=800' );
}
}
Status.status( 'Sumbitting the form...' );
form.submit();
Status.progress( ' Done' );
}
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': [ 'timestamp', 'user', 'comment', 'content' ],
'format': 'xml'
}
Status.status( 'Querying revision' );
revertXML.onreadystatechange = revertToRevisionCallback;
revertXML.open( 'GET' , wgServer + wgScriptPath + '/api.php?' + QueryString.create( query ), true );
revertXML.send( null );
} catch(e) {
alert( e.what() );
}
}
function revertToRevisionCallback() {
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 = sajax_init_object();
revertXML.overrideMimeType('text/xml');
revertXML.onreadystatechange = revertToRevisionCallback2;
revertXML.open( 'GET' , wgServer + 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.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 = 'Reverted to revision ' + content.getAttribute( 'revid' ) + ' by [[User:' + content.getAttribute( 'user' ) + '|' + content.getAttribute( 'user' ) + ']] ([[WP:TWINKLE|Twinklefluff ' + VERSION + ']])';
doc.getElementById( 'wpSummary' ).value = summary;
var minor = doc.getElementById( 'wpMinoredit' );
if( minor ) {
minor.value = 1;
}
Status.progress( ' Done' );
document.getElementById('globalWrapper').appendChild( form );
Status.status( 'Sumbitting the form...' );
form.submit();
Status.progress( ' Done' );
}