User:Mercury~enwiki/AzaToth/twinkleprotect.js

// // If TwinkleConfig aint exist. if( typeof( TwinkleConfig ) == 'undefined' ) { TwinkleConfig = function {}; }

/** TwinkleConfig.summaryAd (string) If ad should be added or not to summary, default TWINKLE if( typeof( TwinkleConfig.summaryAd ) == 'undefined' ) { TwinkleConfig.summaryAd = " using TW"; }

/** TwinkleConfig.protectionSummaryAd (string) If ad should be added or not to protection summary, default TWINKLE if( typeof( TwinkleConfig.protectionSummaryAd ) == 'undefined' ) { TwinkleConfig.protectionSummaryAd = " using TW"; }

/** TwinkleConfig.toolboxButtons (string) If id defined in this array, the button of the action is located inthe toolbox instead of in the actions bar. if( typeof( TwinkleConfig.toolboxButtons ) == 'undefined' ) { TwinkleConfig.toolboxButtons = []; }

function twinkleProtectButton { if( wgNamespaceNumber < 0 || wgCurRevisionId == false ) { return; }

if( userIsInGroup( 'sysop' ) ) { mw.util.addPortletLink(chooseBox( 'tw-rpp', TwinkleConfig.toolboxButtons ), "javascript:twinkleprotect", "pp", "tw-rpp", "Protect page", ""); } else { mw.util.addPortletLink(chooseBox( 'tw-rpp', TwinkleConfig.toolboxButtons ), "javascript:twinkleprotect", "rpp", "tw-rpp", "Request page protection", ""); } } addOnloadHook(twinkleProtectButton);

function twinkleprotect { twinkleprotect.Window = new SimpleWindow( 600, 300 ); twinkleprotect.Window.setTitle( "Fill what protection is wanted" ); function createLabel( data ) { var label = document.createElement( 'label' ); label.setAttribute( 'for', data.target ); label.appendChild( document.createTextNode( data.label ) ); return label; }

function createOption( data ) { var option = document.createElement( 'option' ); option.setAttribute( 'value', data.value ); if( data.selected ) { option.setAttribute( 'selected', 'selected' ); }		option.setAttribute( 'label', data.label ); option.appendChild( document.createTextNode( data.label ) );

return option; }	function createOptGroup( label ) { var optgroup = document.createElement( 'optgroup' ); optgroup.setAttribute( 'label', label ); return optgroup; }

function createInput( data ) { var div = document.createElement( data.elem || 'span' ); var input = document.createElement( 'input' ); input.setAttribute( 'value', data.value ); input.setAttribute( 'name', data.name ); input.setAttribute( 'id', data.id ); input.setAttribute( 'type', data.type ); if( data.size ) { input.setAttribute( 'size', data.size ); }		if( data.checked ) { input.setAttribute( 'checked', 'checked' ); }		if( data.disabled ) { input.setAttribute( 'disabled', 'disabled' ); }		if( data.event && data.type == 'button' ){ input.addEventListener( 'click', data.event, true ); }		if( data.type == 'button' || data.type == 'submit' || data.type == 'reset' ) { div.appendChild( input ); } else { var label = document.createElement( 'label' ); label.setAttribute( 'for', data.id ); label.appendChild( document.createTextNode( data.text ) ); if( [ 'radio', 'checkbox' ].indexOf( data.type ) != -1 ) { div.appendChild( input ); div.appendChild( label ); } else { div.appendChild( label ); div.appendChild( input ); }		}

return div; }	twinkleprotect.form = document.createElement( 'form' ); twinkleprotect.form.setAttribute( 'action', 'javascript:void(0);'); twinkleprotect.form.addEventListener( 'submit',twinkleprotect.evaluate, true );

var main_choices = document.createElement( 'select' ); main_choices.setAttribute('name', 'main_choices'); main_choices.setAttribute('id', 'twProtMainChoices'); var full = createOptGroup( 'Full protection' ); full.appendChild( createOption( { label: 'Generic', value: 'full_generic' } ) ); full.appendChild( createOption( { label: 'Dispute', selected: true, value: 'full_dispute' } ) ); full.appendChild( createOption( { label: 'Vandalism', value: 'full_vandalism' } ) ); full.appendChild( createOption( { label: 'High visible template', value: 'full_template' } ) ); full.appendChild( createOption( { label: 'User talk of banned user', value: 'full_usertalk' } ) ); var semi = createOptGroup( 'Semi-protection' ); semi.appendChild( createOption( { label: 'Generic', value: 'semi_generic' } ) ); semi.appendChild( createOption( { label: 'Vandalism', value: 'semi_vandalism' } ) ); semi.appendChild( createOption( { label: 'High visible template', value: 'semi_template' } ) ); semi.appendChild( createOption( { label: 'User talk of banned user', value: 'semi_usertalk' } ) ); semi.appendChild( createOption( { label: 'Spambot target', value: 'semi_spambot' } ) ); var other = createOptGroup( 'Other' ); other.appendChild( createOption( { label: 'Move-protection', value: 'move' } ) ); other.appendChild( createOption( { label: 'Unprotection', value: 'un' } ) ); main_choices.appendChild( full ); main_choices.appendChild( semi ); main_choices.appendChild( other ); main_choices.addEventListener( 'change',twinkleprotect.defaultnoinclude, true );

var choices = document.createElement( 'fieldset' ); var choices_legend = document.createElement( 'legend' ); choices_legend.appendChild( document.createTextNode( 'Choice type of action' ) );

choices.appendChild( choices_legend ); choices.appendChild( main_choices ); choices.appendChild( document.createElement( 'br' ) ); choices.appendChild( createInput( { id:'twProtNoinclude', name:'noinclude', type:'checkbox', text:'Wrap template in tags?', checked:(wgNamespaceNumber==10), disabled:!userIsInGroup( 'sysop' ) }) );	choices.appendChild( document.createElement( 'br' ) ); choices.appendChild( createInput( { id:'twProtSmall', name:'small', type:'checkbox', text:'Show template small?', disabled:!userIsInGroup( 'sysop' ) }) );

choices.appendChild( document.createElement( 'br' ) ); choices.appendChild( createInput( { id:'twProtCascade', name:'cascade', type:'checkbox', text:'Enable cascade protection' }) );

var expiry = document.createElement( 'select' ); expiry.setAttribute('name', 'expiry'); expiry.setAttribute('id', 'twProtExpiry'); expiry.appendChild( createOption( { label: '1 hour', value: '1 hour' } ) ); expiry.appendChild( createOption( { label: '2 hours', value: '2 hours' } ) ); expiry.appendChild( createOption( { label: '3 hours', value: '3 hours' } ) ); expiry.appendChild( createOption( { label: '6 hours', value: '6 hours' } ) ); expiry.appendChild( createOption( { label: '12 hours', value: '12 hours' } ) ); expiry.appendChild( createOption( { label: '1 day', value: '1 day' } ) ); expiry.appendChild( createOption( { label: '2 days', value: '2 days' } ) ); expiry.appendChild( createOption( { label: '3 days', value: '3 days' } ) ); expiry.appendChild( createOption( { label: '4 days', value: '4 days' } ) ); expiry.appendChild( createOption( { label: '5 days', value: '5 days' } ) ); expiry.appendChild( createOption( { label: '6 days', value: '6 days' } ) ); expiry.appendChild( createOption( { label: '1 week', value: '1 week' } ) ); expiry.appendChild( createOption( { label: '2 weeks', value: '2 weeks' } ) ); expiry.appendChild( createOption( { label: '1 month', value: '1 month' } ) ); expiry.appendChild( createOption( { label: '2 months', value: '2 months' } ) ); expiry.appendChild( createOption( { label: '3 months', value: '3 months' } ) ); expiry.appendChild( createOption( { label: '6 months', value: '6 months' } ) ); expiry.appendChild( createOption( { label: '1 year', value: '1 year' } ) ); expiry.appendChild( createOption( { label: 'indefinite', selected: true, value:'indefinite' } ) ); choices.appendChild( document.createElement( 'br' ) ); choices.appendChild( createLabel( { target:'twProtExpiry', label:'Expiry' } ) ); choices.appendChild( expiry );

twinkleprotect.main_choices = main_choices;

twinkleprotect.form.appendChild( choices ); twinkleprotect.form.appendChild( createInput( { id:'twProtReason', value:'', text:'Reason ', type:'text', name:'rppreason', size:80 } ) );	twinkleprotect.form.appendChild( createInput( { id:'twProtSubmit', value:(userIsInGroup( 'sysop' ) ? 'Protect page' : 'Submit report'), name:'submit', type:'submit' } ) );

twinkleprotect.Window.setContent( twinkleprotect.form ); twinkleprotect.Window.display; }

twinkleprotect.defaultnoinclude = function(e) { if( twinkleprotect.form.main_choices.value == 'un' ) { twinkleprotect.form.noinclude.disabled = true; twinkleprotect.form.cascade.disabled = true; twinkleprotect.form.expiry.disabled = true; twinkleprotect.form.small.disabled = true; } else { twinkleprotect.form.noinclude.disabled = false; twinkleprotect.form.cascade.disabled = false; twinkleprotect.form.expiry.disabled = false; twinkleprotect.form.small.disabled = false; }

if( twinkleprotect.form.main_choices.value in {'full_template':,'semi_template':} ) { twinkleprotect.form.noinclude.checked = true; twinkleprotect.form.expiry.disabled = true; } else { twinkleprotect.form.noinclude.checked = false; }

} twinkleprotect.evaluate = function(e) {

twinkleprotect.type = twinkleprotect.form.main_choices.value;

if( twinkleprotect.type == null ) { Status.error( 'no action given' ); return; }

twinkleprotect.noinclude = twinkleprotect.form.noinclude.checked; twinkleprotect.cascade = twinkleprotect.form.cascade.checked; twinkleprotect.small = twinkleprotect.form.small.checked;

twinkleprotect.reason = twinkleprotect.form.rppreason.value; twinkleprotect.expiry = twinkleprotect.form.expiry.value;

Status.init( twinkleprotect.form );

if( userIsInGroup( 'sysop' ) ) { Status.status( 'Grabbing protection page' ); protXML = sajax_init_object; protXML.overrideMimeType('text/xml');

var query = { 'title': wgPageName, 'action': 'protect' }		protXML.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), true); protXML.onreadystatechange = twinkleprotect.sysopProtect1; protXML.send( null );

} else { Status.status( 'Grabbing WP:RFPP' );

editXML = sajax_init_object; editXML.overrideMimeType('text/xml'); var query = { 'title': 'Wikipedia:Requests for page protection', 'action': 'submit' };

editXML.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), true);

editXML.onreadystatechange = twinkleprotect.callback; editXML.send( null ); } }

twinkleprotect.sysopProtect1 = function { if ( protXML.readyState != 4 ){ Status.progress('.'); return; }

if( protXML.status != 200 ){ Status.error('Bad status, bailing out'); return; }	var doc = protXML.responseXML; var editToken = doc.getElementsByTagName('form')[0].wpEditToken.value;

Status.status( 'Protecting page' ); var edit, move, tag, reason; switch( twinkleprotect.type ) { case 'full_dispute': edit = 'sysop'; move = 'sysop'; tag = 'pp-dispute'; reason = 'Full protection: Dispute'; break; case 'full_vandalism': edit = 'sysop'; move = 'sysop'; tag = 'pp-vandalism'; reason = 'Full protection: Vandalism'; break; case 'full_template': edit = 'sysop'; move = 'sysop'; tag = 'pp-template'; reason = 'Full protection: High-visible template'; break; case 'full_usertalk': edit = 'sysop'; move = 'sysop'; tag = 'pp-usertalk'; reason = 'Full protection: User talk of banned user'; break; case 'full_generic': edit = 'sysop'; move = 'sysop'; tag = 'pp-protected'; if( twinkleprotect.reason ) { tag += '|reason=' + twinkleprotect.reason; }		reason = 'Full protection'; break; case 'semi_vandalism': edit = 'autoconfirmed'; move = 'autoconfirmed'; tag = 'pp-semi-vandalism'; reason = 'Semi-protection: Vandalism'; break; case 'semi_usertalk': edit = 'autoconfirmed'; move = 'autoconfirmed'; tag = 'pp-semi-usertalk'; reason = 'Semi-protection: User talk of banned user'; break; case 'semi_template': edit = 'autoconfirmed'; move = 'autoconfirmed'; tag = 'pp-semi-template'; reason = 'Semi-protection: High-visible template'; break; case 'semi_spambot': edit = 'autoconfirmed'; move = 'autoconfirmed'; tag = 'pp-semi-spambot'; reason = 'Semi-protection: Spambot target'; break; case 'semi_generic': edit = 'autoconfirmed'; move = 'autoconfirmed'; tag = 'pp-semi-protected'; if( twinkleprotect.reason ) { tag += '|reason=' + twinkleprotect.reason; }		reason = 'Semi-protection'; break; case 'move': edit = ''; move = 'sysop'; tag = 'pp-move'; reason = 'Move-protection'; break; case 'un': default: edit = ''; move = ''; reason = 'Unprotection'; break; }	twinkleprotect.tag = tag; if( twinkleprotect.reason ) { reason += ', ' + twinkleprotect.reason; }	reason += '.';

twinkleprotect.reason = reason; var postData = { 'wpEditToken': editToken, 'mwProtect-level-edit': edit, 'mwProtect-level-move': move, 'mwProtect-reason': reason + TwinkleConfig.protectionSummaryAd };

if( twinkleprotect.expiry != 'indefinite' && twinkleprotect.type != 'un') { twinkleprotect.tag += '|expiry='; postData['mwProtect-expiry'] = twinkleprotect.expiry; }

if( twinkleprotect.small && twinkleprotect.type != 'un') { twinkleprotect.tag += '|small=yes';

}

if( twinkleprotect.cascade && twinkleprotect.type != 'un') { postData['mwProtect-cascade'] = true; }

var query = { 'title': wgPageName, 'action': 'protect' }

protXML = sajax_init_object; protXML.overrideMimeType('text/xml'); protXML.open( 'POST', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), true); protXML.setRequestHeader('Content-type','application/x-www-form-urlencoded'); protXML.send( QueryString.create( postData ) ); Status.status( 'Adding appropriate template to protected page' );

query = { 'title': wgPageName, 'action': 'submit' }

editXML = sajax_init_object; editXML.overrideMimeType('text/xml'); editXML.open( 'GET', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), false); editXML.send( null );

var doc = editXML.responseXML;

var form = doc.getElementById( 'editform' );

form.style.display = 'none';

var text = form.wpTextbox1.value; var summary = form.wpSummary.value;

var checkre = /\s*(?: )?\s*\{\{\s*(pp-[^{}]*?|protected|(?:t|v|s|p-|usertalk-v|usertalk-s|sb|move)protected(?:2)?|protected template|privacy protection)\s*?\}\}\s*(?:<\/noinclude>)?\s*/gi;

text = text.replace( checkre, '' ); // Remove all old templates if( twinkleprotect.type == 'un' ) { summary = 'removing protection template' + TwinkleConfig.summaryAd; } else { if( twinkleprotect.noinclude ) { text = ' ' + text; } else { text = '' + "\n" + text; }		summary = 'adding ' + TwinkleConfig.summaryAd; }

var postData = { 'wpMinoredit': form.wpMinoredit.checked, 'wpWatchthis': form.wpWatchthis.checked, 'wpStarttime': form.wpStarttime.value, 'wpEdittime': form.wpEdittime.value, 'wpAutoSummary': form.wpAutoSummary.value, 'wpEditToken': form.wpEditToken.value, 'wpSummary': summary, 'wpTextbox1': text };	editXML = sajax_init_object; editXML.overrideMimeType('text/xml'); editXML.open( 'POST', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( query ), false); editXML.setRequestHeader('Content-type','application/x-www-form-urlencoded'); editXML.send( QueryString.create( postData ) );

Status.status('Protection and tagging done.');

window.location = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?' + QueryString.create( {'title':wgPageName,'action':'history'} ); }

twinkleprotect.callback = function { if ( editXML.readyState != 4 ){ Status.progress('.'); return; }

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

var doc = editXML.responseXML; var form;

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

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

var text = textbox.value;

Status.status( 'Checking for tags on the page...' );

var rppRe = new RegExp( '====.*?' + RegExp.escape( wgPageName, true ) + '.*?====', 'm' ); var tag = rppRe.exec( text );

if( tag ) { Status.warn( [ htmlNode( 'strong', tag[0] ), " is alread placed on the page." ] ) return false; }

var ns2tag	=	{ '0'	:	'la', '1'	:	'lat', '2'	:	'lu', '3'	:	'lut', '4'	:	'lw', '5'	:	'lwt', '6'	:	'li', '7'	:	'lit', '8'	:	'lm', '9'	:	'lmt', '10':	'lt', '11':	'ltt', '12':	'lh', '13':	'lht', '14':	'lc', '15':	'lct', '100':	'lp', '101':	'lpt' };

var type = twinkleprotect.type; if( type == null ) { Status.error( 'no type specified, aborting,'); return; }

var typename, reason; switch( type ) { case 'full_generic': case 'full_dispute': case 'full_vandalism': case 'full_template': case 'full_usertalk': typename = 'full protection'; break; case 'semi_generic': case 'semi_template': case 'semi_vandalism': case 'semi_usertalk': case 'semi_spambot': typename = 'semi-protection'; break; case 'move': typename = 'move-protection'; break; case 'un': typename = 'unprotection'; break; default: return; }	switch( type ) { case 'full_dispute': reason = 'Full protection: Dispute'; break; case 'full_vandalism': reason = 'Full protection: Vandalism'; break; case 'full_template': reason = 'Full protection: High-visible template'; break; case 'full_usertalk': reason = 'Full protection: User talk of banned user'; break; case 'full_generic': reason = 'Full protection'; break; case 'semi_generic': reason = 'Semi-protection'; break; case 'semi_vandalism': reason = 'Semi-protection: Vandalism'; break; case 'semi_template': reason = 'Semi-protection: High-visible template'; break; case 'semi_usertalk': reason = 'Semi-protection: User talk of banned user'; break; case 'semi_spambot': reason = 'Semi-protection: Spambot target'; break; case 'move': case 'un': default: reason = false; break; }

var newtag = (		'==== ====' +		"\n" +		typename +		" " +		( twinkleprotect.cascade ? " +cascade, " : '' ) +		( twinkleprotect.expiry != 'indefinite' ? " +expiry " + twinkleprotect.expiry + ", " : '' ) +		( reason ? reason + ', ' :  ) +		twinkleprotect.reason + 		' ' +			);

Status.info( newtag );

if( type == 'un' ) { var str = "==Current requests for unprotection==\n"; textbox.value = textbox.value.replace( str, str + "\n" + newtag + "\n" ); } else { var str = "==Current requests for protection==\n"; textbox.value = textbox.value.replace( str, str + "\n" + newtag + "\n" ); }

Status.status( 'Submitting form...' );

doc.getElementById( 'wpSummary' ).value = "Requesting " + typename + ' of ' + wgPageName.replace('_', ' ') + '.' + TwinkleConfig.summaryAd; doc.getElementById( 'wpMinoredit' ).checked = true;

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

} //