User:Riceissa/scripts/AFCRHS.js

// This script is meant to help with Articles for creation/Redirects. The GUI // was put together pretty messily and some of the code isn't too pretty either, but it works. // To use it, add a new line with // importScript( 'User:PhantomTech/scripts/AFCRHS.js' ); // to your common.js and add yourself to the Articles for Creation // checkpage at WikiProject Articles for creation/Participants // // For help using the script, to report issues or to request changes use User Talk:PhantomTech.

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( 750, 400 ); window.setTitle( 'Response Interface' ); window.setScriptName( 'AFC/R HS' ); window.addFooterLink( 'Source', 'User:PhantomTech/scripts/AFCRHS.js' );

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 title' },						{ type: 'option', name: 'declineOptions', value: 'exists', label: 'Suggested title exists' },						{ type: 'option', name: 'declineOptions', value: 'empty', label: 'Submission is empty' },						{ 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 ) ) {					console.log(mw.user.getName);					console.log(approvedUsers);					// Ummm yeah, so this returns a -1 when the username isn't in the array, and a -1 in javascript passes a if-test, so there you go, all this checking for nothing?					console.log($.inArray( mw.user.getName, approvedUsers));					// Add links next to section headings					$( 'h2:has(.mw-headline:contains("Redirect request"))' ).each( function  { var id = $( this ).children; id = id.last; //console.log("A1", id); var id_links = id.children; //console.log("A2", id_links); var section_num = -1; $.each(id_links, function(key, value) {							if (value.nodeName.toLowerCase == "a") {								//console.log(key, value);								//console.log(value.nodeName);								//console.log(value.getAttribute('href'));								//console.log($(value).attr('href'));								var temp = $(value).attr('href').match( /&section=(\d+)/i );								if (temp) {									section_num = temp[1];									console.log("SECTION NUM", section_num);								}							}						}); // id = id.match( /&section=(\d+)/i )[ 1 ]; if (section_num < 0) { console.log("something went wrong"); }						var $replyLink = $( ' [Respond] ' ); $replyLink.click( function {							replyButton( section_num );						} );

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