User:Evad37/CFDcloser/sandbox.js

//global vars var CFDpath = "Wikipedia:Categories for discussion/Log/";

/* == Main function == */ $( function($) {

// Only run in CFD/subpages, excluding holding cell var thispage = mw.config.get( 'wgPageName' ); if ( !thispage.includes("Categories_for_discussion") || thispage.includes("Categories_for_discussion/Working") ) { return; }

$.getScript('https://en.wikipedia.org/w/index.php?title=User:Evad37/XFDcloserCommon&action=raw&ctype=text/javascript', function { // Make sure User:Evad37/XFDcloserCommon.js is loaded!

setSysopStatus;

/* ##### Sandbox usage only (remove when updating main script) ##### */ if ( mw.config.get("wgUserName") === "Evad37" ) { //	isSysop = true; console.log("isSysop: " + isSysop); CFDpath = "User:Evad37/sandbox/Wikipedia:Categories for discussion/Log/"; } /* ##### End of sandbox usage only (remove when updating main script) ##### */

// Add links to actions $("h4 > span.mw-headline ").each(function( index ) {		$(".mw-headline-number", this).prependTo($(this).parent);		// fix for "Auto-number headings" preference		var cfd_section = $(this).text;		$(this).wrapInner(" ");					// wrap with span so it can be styled later		var cfd_section_e = mw.util.wikiUrlencode(cfd_section);			// "pretty"-encoded

//Check if already closed var discussion_element_class = $(this).parent.next.attr("class"); if ( discussion_element_class ) {					// only closed discussion have element with any class set $(this).parent.addClass("boilerplate cfd vfd xfd-closed");	// add classes to enable hiding of closed section headers } else { var sectionlink = $(this).next.find("a").attr('href'); var editsection = sectionlink.split("section=")[1]; if ( editsection.search("T")<0 ) {				// if true, we're on the subpage nom_page = thispage; } else {							// if false, section is transcluded from a subpage nom_page = sectionlink.split("title=")[1].split("&")[0]; editsection = editsection.substr(2);			// remove "T-" from section number }			if ( nom_page.includes("Categories_for_discussion/Working") ) return;

var closing_options = " [Close] "; $(this).append("" + closing_options + " "); }	}); });

});

/* == CLOSING-related functions == */

/* -- CloseCFD section (prompting for result/comment) -- */ function close_cfd(_cfd_section, _pagetitle, _sectionnum, _action) { var result_comment = prompt("Enter result/comment"); if ( result_comment !== null ) { process_close_tfd(_cfd_section, _pagetitle, _sectionnum, _action, result_comment); } }

/* -- Process the close -- */ function process_close_tfd(cfd_section, pagetitle, sectionnum, action, resultcomment) {

console.log("cfd_section = " + cfd_section);

var cfd_close_top, cfd_close_bottom, cfd_close_editsum, sig, bold1, bold2; var bolding_info = [];

deliverMsg( cfd_section, "Started closing...", "working" );

// Wikitext used for close if (isSysop) { sig = "" + ""; } else { sig = " (non-admin closure) " + ""; }	// Figure out what if/where bold markup should be added bolding_info = adjustCommentBolding(resultcomment); bold1 = bolding_info[0]; bold2 = bolding_info[1]; resultcomment = bolding_info[2]; var cfd_section_decoded = decodeURIComponent(cfd_section).replace(/_/g, " ");

cfd_close_top = "{" + "{subst:cfd top}} " + bold1 + resultcomment + bold2 + " " + sig + "\n"; cfd_close_bottom = "{" + "{subst:cfd bottom}" + "}"; cfd_close_editsum = "Closing discussion \"" + cfd_section_decoded + "\" as " + resultcomment.replace(/'''/g, "") + " (using CFDcloser)";

// Get page content and remove {Closing} template if present new mw.Api.get( {			action: 'query',			titles: pagetitle,			prop: [ 'revisions', 'info' ],			rvprop: 'content',			indexpageids: 1,			rawcontinue: ''		} ).done( function( result, jqXHR ) {			var p_id = result.query.pageids;			var p_contents = result.query.pages[ p_id ].revisions[ 0 ][ '*' ];			var sections_array = p_contents.split("====");			var section_heading = sections_array[((sectionnum-1)*2-1)];			var section_contents = sections_array[((sectionnum-1)*2)].replace(//gi, "");			var updated_section = "====" + section_heading + "====" + "\n" + cfd_close_top + section_contents + cfd_close_bottom;

// Perform edit to close TFD discussion new mw.Api.postWithToken( 'edit', {				action: 'edit',				title: pagetitle,				section: sectionnum,				text: updated_section,				summary: cfd_close_editsum			} ).done( function( result, jqXHR ) {				markClosed(cfd_section, resultcomment);			} ).fail( function( code, result ) {				var error_details = apifailed( code, result );				deliverMsg(cfd_section, "Could not close discussion " + error_details, "error");			} );

} ).fail( function( code, result ) { var error_details = apifailed( code, result ); deliverMsg(cfd_section, "Could not read contents of page " + pagetitle + "; could not close discussion " + error_details, "error"); } ); }