Jump to content

User:Evad37/CFDcloser/sandbox.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//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("<span></span>");					// 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 = "<span style='margin-left:13px;'>[<a style='cursor:pointer;' onclick=close_cfd('" + cfd_section_e + "','" + nom_page + "','" + editsection + "','" + "close" + "') title='Close discussion'>Close</a>]</span>";
			$(this).append("<span id=XFDClosing_" + cfd_section_e + " style='font-size:85%;margin-left:13px;font-weight:normal;'>" + closing_options + "</span>");
		}
	});
});

});

/* == 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 = "<small>[[Wikipedia:NACD|(non-admin closure)]]</small> ~~" + "~~";
	}
	
	// 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 [[User:Evad37/CFDcloser|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(/{{closing}}/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");
	} );
}