User:CAPTAIN MEDUSA/Redirect Helper.js

// This is a copy and paste of User Talk:PhantomTech's script'.

importScript( "MediaWiki:Gadget-morebits.js" ); importStylesheet( "MediaWiki:Gadget-morebits.css" );

( function ( $, mw ) {	'use strict';	$( document ).ready( function {

function replyButton( sectionID ) { var window = new Morebits.simpleWindow( 900, 900 ); window.setTitle( 'Response to the Redirect' ); window.setScriptName( 'Redirect Helper' ); window.addFooterLink( 'Help', 'User:CAPTAIN MEDUSA/Redirect Helper' );

var form = new Morebits.quickForm( submitForm ); form.append( {				type: 'hidden',				name: 'sectionID',				value: sectionID			} ); form.append( {				type: 'radio',				name: 'acceptRadio',				list: [					{ name: 'accept', label: 'Accept', value: true },					{ name: 'accept', label: 'Decline', value: false, checked: true }				],				event: function ( event ) {					updateDynamicArea( event.target.form );				}			} ); form.append( {				type: 'div',				name: 'dynamic_area',				label: 'Something broke...'			} ); form.append( {				type: 'submit',				label: 'Submit'			} ); var renderedForm = form.render;

window.setContent( renderedForm ); window.display; updateDynamicArea( renderedForm ); }

function updateDynamicArea( form ) { var accept = Morebits.quickForm.getElements( form, "accept" )[ 0 ].checked;

var dynamic_area = new Morebits.quickForm.element( {				type: 'div',				name: 'dynamic_area'			} );

if ( !accept ) { dynamic_area.append( {					type: 'header',					label: 'Decline request'				} ); dynamic_area.append( {					type: 'select',					name: 'declineOptions',					label: 'Decline reason: ',					list: [						{ type: 'option', name: 'declineOptions', value: 'unlikely', label: 'Unlikely redirect' },						{ type: 'option', name: 'declineOptions', value: 'exists', label: 'Redirect alredy exists' },						{ type: 'option', name: 'declineOptions', value: 'empty', label: 'Empty request' },						{ type: 'option', name: 'declineOptions', value: 'notarget', label: 'Target does not exist' },						{							type: 'option',							name: 'declineOptions',							value: 'externallink',							label: 'Target is external link'						},						{							type: 'option',							name: 'declineOptions',							value: 'notredirect',							label: 'Request is not for a redirect'						},						{							type: 'option',							name: 'declineOptions',							value: 'editrequest',							label: 'Request is an edit request' },						{							type: 'option', name: 'declineOptions', value: 'notenglish', label: 'Request not in english' },						{ type: 'option', name: 'declineOptions', value: 'decline', label: 'Custom reason' } ]				} );				dynamic_area.append( { type: 'input', name: 'comment', label: 'Provide custom reason if it is selected: ' } );			} else {				dynamic_area.append( { type: 'header', label: 'Accept request' } );				dynamic_area.append( { type: 'div', name: 'redirectTypeInstructions', label: 'Enter "none" for none, "to acronym" for {' + '{R' + ' to acronym}}, etc.' } );				dynamic_area.append( { type: 'input', name: 'redirectTypes', label: 'Redirect type: ', value: 'none' } );				dynamic_area.append( { type: 'input', name: 'redirTarget', label: 'Redirect target: ' } );				dynamic_area.append( { type: 'input', name: 'comment', label: 'Comments: ', value: 'All of the redirects you requested were created. Thank you for your contribution to Wikipedia!' } );				dynamic_area.append( { type: 'dyninput', name: 'redirectList', label: 'List of redirects to be created', min: 1, max: 30, sublabel: 'Name: ' } );			}

form.replaceChild( dynamic_area.render, Morebits.quickForm.getElements( form, "dynamic_area" )[ 0 ] ); }

function submitForm( e ) { // Check if we're still on the current version of the page var redirectPage = new Morebits.wiki.page( mw.config.get( 'wgPageName' ) ); redirectPage.load( function {				if ( parseInt( redirectPage.getCurrentID ) !== mw.config.get( 'wgRevisionId' ) ) {					alert( 'Changes have been made to this page since the version you\'re viewing' );					return;				}

// If version check passes, start making changes Morebits.simpleWindow.setButtonsEnabled( false );

var redirectType, target, redirects; var form = e.target; var sectionID = parseInt( form.sectionID.value ); var accept = form.accept[ 0 ].checked; var comment = form.comment.value; var redirectTemplateCode = 'accept'; var page = new Morebits.wiki.page( mw.config.get( 'wgPageName' ) ); if ( accept ) { redirectType = form.redirectTypes.value; if ( redirectType !== 'none' ) { redirectType = '\n{' + '{R ' + redirectType + '}}'; } else { redirectType = ''; }					target = form.redirTarget.value; redirects = []; for ( var i = 0; i < form.redirectList.length; i++ ) { if ( form.redirectList[ i ].value.length > 0 ) { redirects.push( form.redirectList[ i ].value ); }					}					if (redirects.length === 0) { redirects.push(form.redirectList.value); }				} else { redirectTemplateCode = form.declineOptions.value; }

Morebits.status.init( form ); var requestStatus = new Morebits.status( 'Marking request on page as ' + (accept ? 'accepted' : 'denied' ) );

page.setPageSection( sectionID ); requestStatus.info( 'Loading request page...' );

if ( accept ) { // Get WikiProjects from target's talk page var targetPageTalkPage = new Morebits.wiki.page( 'Talk:' + target ); targetPageTalkPage.load( function {						var talkPageText = '{' + '{sub' + 'st:WPAFC/redirect}}';						if ( targetPageTalkPage.getPageText.indexOf("{" + "{Disambig" + "Project}}") > -1 || targetPageTalkPage.getPageText.indexOf("{" + "{ Disambig" + "Project }}") > -1 ) {							talkPageText += '\n{' + '{WikiProject ' + 'Disambiguation|class=redirect}}';						}						var match = targetPageTalkPage.getPageText.match( /\{\{ ?((W|w)ikiProject |(W|w)P)(?!Articles for creation)(.*?)(?=\n|\||}})/g );						if ( match !== null ) {							for ( var t = 0; t < match.length; t++ ) {								talkPageText += '\n' + match[ t ] + '|class=redirect}}';							}						}

// Get ready to do some crazy recursion to get around making functions // in loops without really solving the problem... var index = -1; var createRedirectFunction = function { index++; var redirectPage = new Morebits.wiki.page( redirects[ index ] ); redirectPage.load( function { // Load redirect								// Create redirect page								redirectPage.setPageText( '#REDIRECT ' + target + '' + redirectType );								redirectPage.setEditSummary( 'Creating redirect per request - Using AFC/R HS' );								redirectPage.setCreateOption( 'createonly' );								redirectPage.setMaxRetries(5);								redirectPage.save( function  { // Save redirect // When that's done... var redirectTalkPage = new Morebits.wiki.page( 'Talk:' + redirects[ index ] ); redirectTalkPage.load( function { // Load redirect talk										// Create redirect talk page										redirectTalkPage.setPageText( talkPageText );										redirectTalkPage.setEditSummary( 'Creating redirect talk page per request - Using AFC/R HS' );										redirectPage.setMaxRetries(5);										redirectTalkPage.save( function  { // Save redirect talk // When that's done... Do it all over again! if ( index + 1 < redirects.length ) { createRedirectFunction; //setTimeout(createRedirectFunction, 1000); } else { page.load( function { // Edit Request													var text = page.getPageText;													var match = text.match( /(==.*?==\n)((.|\n)*)/ );

text = match[ 1 ] + '{' + '{afc-c|' + ( accept ? 'a' : 'd' ) + '}}\n' + match[ 2 ] + '\n:{' + '{su' + 'bst:afc redirect|' + redirectTemplateCode + '|' + comment + '}} ' + '\n{' + '{afc' + '-c|b}}';

page.setPageSection( sectionID ); page.setPageText( text ); page.setMaxConflictRetries( 0 ); page.setEditSummary( 'Responding to redirect request with AFC/R HS' ); page.save; } );											}										} );									} );								} );							} );						};

createRedirectFunction; //setTimeout(createRedirectFunction, 1000); } );				} else {					page.load( function { // Edit request var text = page.getPageText; var match = text.match( /(==.*?==\n)((.|\n)*)/ );

text = match[ 1 ] + '{' + '{afc-c|' + ( accept ? 'a' : 'd' ) + '}}\n' + match[ 2 ] + '\n:{' + '{su' + 'bst:afc redirect|' + redirectTemplateCode + '|' + comment + '}} ' + '\n{' + '{afc' + '-c|b}}';

page.setPageSection( sectionID ); page.setPageText( text ); page.setMaxConflictRetries( 0 ); page.setEditSummary( 'Responding to redirect request with AFC/R HS' ); page.save; } );				}			} );		}

// Only start on the right page and if we're working with the current version if ( mw.config.get( 'wgPageName' ) === 'Wikipedia:Articles_for_creation/Redirects' && mw.config.get( 'wgRevisionId' ) === mw.config.get( 'wgCurRevisionId' ) ) { mw.loader.using( 'jquery.ui' ); mw.loader.using( 'jquery.tipsy' );

// Check if user is approved var checkPage = new Morebits.wiki.page( 'Wikipedia:WikiProject Articles for creation/Participants' ); checkPage.setPageSection( 1 ); checkPage.load( function {				var approvedUsers = checkPage.getPageText.match( /(?:\n\* ?\{\{user2\|(.*?)}})/g );				if ( approvedUsers === null ) {					alert( 'Failed to load checkpage' );				} else if ( $.inArray( mw.user.getName, approvedUsers ) ) {					// Add links next to section headings					$( 'h2:has(.mw-headline:contains("Redirect request"))' ).each( function  { var id = $( this ).children.last.find( 'a' ).attr( "href" ).match( /&section=(\d+)/i )[ 1 ]; var $replyLink = $( ' [Respond] ' ); $replyLink.click( function {							replyButton( id );						} );

$( this ).append( $replyLink ); } );				} else {					alert( 'You aren\'t on the checkpage at WikiProject Articles for creation/Participants' );				}			} ); }	} ); }( jQuery, mediaWiki ) );