User:DannyS712 test/chance.js

/** Config for this script ***/ var SC_config = { name: 'Second chance', version: 3.0, debug: true };

/*** Edit summary for using tool ***/ var SC_summary = { before: "Copied content from ",	after: " with " + SC_config.name + " (version " + SC_config.version + "); see that page's history for attribution.", all: "" };

/*** Add event-handler to trigger function ***/ mw.loader.using( 'mediawiki.util', function {    importScript('User:DannyS712 test/append.js');    $(document).ready( function  { var link = mw.util.addPortletLink( 'p-cactions', null, 'Second chance', 'ca-SC', 'Second chance'); $( link ).click( function ( event ) {           event.preventDefault;			if (SC_check_block) Second_chance;			else alert("You do not appear to be blocked");        } ); } ); } );

/*** Controlling main function ***/ function Second_chance{ // Only run on the user's talk page if (SC_check_page) { var page = SC_choose_page; SC_get_page( page ); }	// If not the user's talk page else alert("Error: You cannot edit this page, because you are blocked. Please use this script on your own user talk page. If you can't access that page, please see \"Wikipedia:Unblock Ticket Request System\""); }

/*** Get the name of the page that will be improved ***/ function SC_choose_page{ var page = SC_page_name; if (SC_config.debug) console.log( page ); SC_summary.all = 'Second chance: ' + SC_summary.before + page + SC_summary.after; return page; }

/*** Make sure that the page is in articlespace ***/ function SC_page_name{ var good_page = false; page = prompt("Please enter the page name", "Main page"); while (!good_page){ if (page.indexOf(":") > -1){ page = prompt("Please make sure you enter the name of an article, not a page in another namespace", "Main page"); }		else good_page = true; }	return page; }

/*** Actually instert the page ***/ function SC_get_page( page_name ){ console.log( page_name ); var page = { action: 'parse', page: page_name, prop: 'wikitext', format: 'json' };   if (SC_config.debug) console.log( page ); $.get( mw.config.get( 'wgScriptPath' ) + '/api.php', page, function( got_page ) {   	if (SC_config.debug) console.log ( got_page );    	if (got_page && got_page.parse && got_page.parse.wikitext && got_page.parse.wikitext["*"]){	    	var text = got_page.parse.wikitext["*"];	    	if (text.length > 50000) alert("Unfortunately, the page you chose was too big. Please try again with a shorter page.");	    	else if (text.toUpperCase.startsWith("#REDIRECT")) alert("The page you chose is a redirect to another page. Please try again with a page that is not a redirect.");	    	else if (text.search(/\{\{disambiguation/i) > -1) alert("The page you chose is a disambiguation page. Please try again with a page that is not a disambiguation page.");	    	else {		    	if (SC_config.debug) console.log( text );	    		var full = SC_full_text( text, page_name );	    		addNewSection( SC_summary.all, 'Second Chance', full ); }   	} else { var error = "Error, check the console for details"; if (got_page) console.log( got_page ); if (got_page && got_page.error && got_page.error.info) error = "Error: " + got_page.error.info; if (got_page && got_page.error) console.log( got_page.error ); alert(error); }   } ); }

/*** Clean up page * Currently removes: stub templates, category declarations, images/files, galleries (along with all content), infoboxes (allows up to 1 level of nested templates), and series templates * It also reduced the level of headings by 1 level each. ***/ function SC_edit_text ( text ){ if (SC_config.debug) console.log( text ); text = text.replace( /\{\{(\w|-)*stub\}\}/gi, '' ); text = text.replace( /\[\[category:(.)*\]\]/gi, ''); text = text.replace( /\[\[((file)|(image)):.*\]\]/gi, ''); text = text.replace( //gi, ''); text = text.replace( /\{\{infobox(([^\{\}]|\n)|\{\{([^\{\}]|\n)+\}\})*\}\}/gi, ''); text = text.replace( /\{\{([^\{\}])+series\}\}/gi, ''); text = text.replace( /(={2,})/g, '$1='); return text; }

/*** Add a "Guide" and "Finishing" section above the copied article ***/ function SC_top_explanation { var comment = SC_top_config; var pre = "Thank you for taking advantage of your second chance. The article that you have selected for improvement is pasted below."; pre = pre + "\n\n===Guide===\n"; pre = pre + "Please review the simplified ruleset, as well as the guide to article development. If you have any questions, please ask.\n\n"; pre = pre + "For a basic introduction to editing articles, see Tutorial. Note, however, that you will be editing here on your talk page, instead of the article itself.\n\n"; pre = pre + "To request help or ask an questions, add  to this section."; pre = pre + "\n\n===Finishing===\n"; pre = pre + "Once you have finished improving an article, add  to this section, and your work will be reviewed"; return comment + pre; } /*** Add tracking data about the script used ***/ function SC_top_config{ var comment = "Category" + ":Users testing DannyS712's second chance script\n\n"; return comment; }

/*** The full text to be added to the page ***/ function SC_full_text( text, page_name ){ var full = SC_top_explanation; full = full + '\n\n==Content of ' + page_name + '==\n\n'; text = SC_edit_text( text ); full = full + text; return full; }

/** Set up before fully running the script - check if the user is actually blocked ***/ function SC_check_block{ var SC_user = mw.config.get( 'wgRelevantUserName' ); if (SC_config.debug) console.log( SC_user ); var SC_is_blocked = { action: "query", list: "blocks", bkusers: SC_user, format: 'json' };	var is_blocked = false; var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php'; $.ajax({		url: scriptUrl,		type: 'get',		data: SC_is_blocked,		dataType: 'json',		async: false,		success: function(data) {			if (SC_config.debug) console.log( data );		   if (data && data.query && data.query.blocks && data.query.blocks["0"]) is_blocked=true;		} 	}); var is_blocked_text = is_blocked ? "is" : "is not"; if (SC_config.debug) console.log( "This user " + is_blocked_text + " currently blocked" ); return is_blocked; }

/** Check if the page to be edited is the user's talk page ***/ function SC_check_page { var page_name = mw.config.get( 'wgPageName' ); var new_name = page_name.replace( /_/g, ' ' ); var ideal_page = "User talk:" + mw.config.get( 'wgRelevantUserName' ); if (SC_config.debug) console.log(new_name, ideal_page); if ( new_name === ideal_page ) return true; return false; }