MediaWiki:Gadget-autobp.js

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

Note: After saving, you have to bypass your browser's cache to see the changes.

  • Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
  • Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
  • Opera, Konqueror and Safari users can just click the Reload button.
  • Chrome: press Ctrl+F5 or Shift+F5
// <nowiki>
bp = {};

bp.add = function() {
	if ( wgCanonicalNamespace == 'User' || wgCanonicalNamespace == 'User_talk' ) {
		bp.user = encodeURIComponent( wgTitle );
	} else if ( wgCanonicalSpecialPageName == 'Contributions' ) {
		bp.user = document.forms[0].target.value;
	}
	if ( bp.user != undefined ) {
		addPortletLink(
			'p-cactions',
			'javascript:bp.go()',
			'Ban Patrol',
			'td-bp',
			'Add user to Ban Patrol'
		);
	}
}

bp.debugOutput = function( str ) {
	bp.output += str;
	bp.panel.setBody( bp.output );
}

bp.debugStatus = function( i ) {
	var s = ( i ) ? 'green;"> [OK]' : 'red;"> [FAILED]';
	bp.debugOutput( '<span style="font-weight: bold;color:' + s + '</span>' );
}

bp.go = function() {
	bp.comment = prompt( 'Comment (required):', '' );
	if ( bp.comment == null ) {
		return;
	}
	bp.panel = new YAHOO.widget.Panel(
		'bp',
		{
			width: '240px',
			fixedcenter: true,
			draggable: true,
			zindex: 10,
			modal: false,
			visible: false
		}
	);
	bp.panel.setHeader( 'Listing on Ban Patrol, please wait...' );
	bp.panel.setFooter( '<br /><hr><small>By <a href="/wiki/User:Spang">Spang</a></small>' );
	bp.output = '';
	bp.debugOutput( '<br />Loading Ban Patrol page...' );
	bp.panel.render( document.body );
	bp.panel.show();
	YAHOO.util.Connect.asyncRequest(
		'GET',
		'/api.php?action=query&titles=Uncyclopedia:Ban_Patrol&prop=revisions|info|links&pllimit=500&rvprop=content&rvlimit=1&rvsection=1&format=xml&intoken=edit',
		bp.update,
		null
	);
}

bp.update = {
	success: function( o ) {
		bp.debugStatus( 1 );
		bp.debugOutput( '<br />Checking Ban Patrol for selected user...' );
		var titles = o.responseXML.getElementsByTagName( 'pl' );
		for ( lk in titles ) {
			try {
				if ( titles[lk].getAttribute( 'title' ) == 'User:' + bp.user.replace( /%20/ig, ' ' ).replace( /%2F/ig, '/' ).replace( /%3A/ig, ':' ) ) {
					bp.debugStatus( 0 );
					bp.debugOutput( '<br />User already on Ban Patrol!<br /><a href="javascript:bp.panel.hide()">Close</a>' );
					return;
				}
			} catch( e ) {};
		}
		bp.debugStatus( 1 );
		bp.debugOutput( '<br />User not listed, adding them now...' );
		try {
			var sectionText = o.responseXML.getElementsByTagName( 'rev' )[0].firstChild.nodeValue;
			var lines = sectionText.split( '\n' );
			var ip = ( bp.user.match( /^\b(?:\d{1,3}\.){3}\d{1,3}\b$/g ) ) ? 1 : 0;
			if ( ip ) {
				lines.splice( 1, 0, '* {{IP|' + bp.user + '}} - ' + bp.comment + ' ~~~' );
			} else {
				// delicious copypasta from wikibits.js, function importScript
				// bug reported by Lyrithya on IRC on 14 February 2011 --Jack Phoenix
				lines.splice( 1, 0, '*{{User|' + bp.user.replace( /%20/ig, ' ' ).replace( /%2F/ig, '/' ).replace( /%3A/ig, ':' ) + '}} - ' + bp.comment + ' ~~~' );
			};
			sectionText = lines.join( '\n' );
		} catch( e ) {
			bp.debugStatus( 0 );
			bp.debugOutput( '<br />Could not add text to the page!<br /><a href="javascript:bp.panel.hide()">Close</a>' );
			return;
		}
		bp.debugStatus( 1 );
		bp.debugOutput( '<br />Saving page...' );

		var token = o.responseXML.getElementsByTagName( 'page' )[0].getAttribute( 'edittoken' );
		var post = 'title=Uncyclopedia:Ban_Patrol&section=1&token=' +
			encodeURIComponent( token ) + '&summary=%2B[[User:' +
			encodeURIComponent( bp.user ) + ']]&text=' +
			encodeURIComponent( sectionText );
		YAHOO.util.Connect.asyncRequest(
			'POST',
			'/api.php?action=edit&format=xml',
			bp.saved,
			post
		);
	},
	failure: function() {
		bp.debugStatus( 0 );
		bp.debugOutput( '<br />Couldn\'t connect to Ban Patrol page!<br /><a href="javascript:bp.panel.hide()">Close</a>' );
	}
}

bp.saved = {
	success: function( o ) {
		try {
			result = ( o.responseXML.getElementsByTagName( 'edit' )[0].getAttribute( 'result' ) == 'Success' ) ? 1 : 0;
		} catch( e ) {
			result = 0;
		}
		if ( result ) {
			bp.debugStatus( 1 );
			bp.debugOutput( '<br /><b>Saved!</b><br /><a href="/wiki/Uncyclopedia:Ban_Patrol">Go to Ban Patrol</a> or <br /><a href="javascript:bp.panel.hide()">close</a>' );
		} else {
			with( o.responseXML.getElementsByTagName( 'error' )[0] ) {
				bp.debugStatus( 0 );
				bp.debugOutput(
					'<br /><b>Saving failed :(</b><br />Error: ' +
					getAttribute( 'code' ) + ' - ' +
					getAttribute( 'info' ) +
					'<br /><a href="/wiki/Uncyclopedia:Ban Patrol">Add manually</a> or <br /><a href="javascript:bp.panel.hide()">close</a>'
				);
			}
		}
	},
	failure: function( o ) {
		bp.debugStatus( 0 );
		bp.debugOutput(
			'<br /><b>Connection failed :(</b><br /><a href="/wiki/Uncyclopedia:Ban Patrol">Add manually</a> or <br /><a href="javascript:bp.panel.hide()">close</a>'
		);
	}
}

YAHOO.util.Event.onContentReady('p-cactions', bp.add);
// </nowiki>