User:Armbrust/closeFPC.js

// //This is a modification of User:Jujutacular/closeFPC.js, to remove an incrementation step no longer needed. // See documentation at User:Jujutacular/closeFPC // This code is largely based on User:Mr.Z-man/closeAFD (function($, mw) {

var currentPageName = mw.config.get( 'wgPageName' ).replace(/_/g, ' '); var AJAXnumber = 0;

function closeFPC_init(event) { // The clickhandler shouldn't do anything but this action event.preventDefault; var isDelist = currentPageName.replace(/^.*\b(delist)\b.*$/,"$1"); if (isDelist === "delist") { errorMessage('Delist nominations are not supported by this script.'); return; }

var form = ''+ ' Closing '+currentPageName+' '+ ' For promotion: '+ 'Nominator (without "User:"):  '+ 'Nominator 2 (if applicable):  '+ //'Creator (if non-Wikipedian):  '+ 'Creator (if Wikipedian):  '+ 'Image editor/Restorer:  '+ 'Image editor/Restorer 2:  '+ 'Promoted file (without "File:"):  '+ 'Name of nomination: <input type="text" id="closeFPC_name" name="closeFPC_name" /> '+ '<label for="closeFPC_cat">Main category: <input type="text" id="closeFPC_cat" name="closeFPC_cat" /> '+ //'<label for="closeFPC_subcat">Subcategory: <input type="text" id="closeFPC_subcat" name="closeFPC_subcat" /> '+ '<label for="closeFPC_reason">Additional closing rationale (optional): <input type="text" id="closeFPC_reason" name="closeFPC_reason" /> '+ '<input type="button" id="closeFPC_promote_button" name="closeFPC_promote_button" value="Promote" />'+ '<input type="button" id="closeFPC_nopromote_button" name="closeFPC_nopromote_button" value="No Promote" /> '; var $htmlElements = $( $.parseHTML( form )[0] ); $htmlElements.find( '#closeFPC_promote_button' ).click( closeFPC_promote ); $htmlElements.find( '#closeFPC_nopromote_button' ).click( closeFPC_nopromote ); displayMessage($htmlElements); }

function closeFPC_promote { closeFPC_close('promote'); }

function closeFPC_nopromote { closeFPC_close('nopromote'); }

function closeFPC_close(close) { var closetype = close; var nominator = document.getElementById('closeFPC_nominator').value; var nominator2 = document.getElementById('closeFPC_nominator2').value; //var creator_nonwiki = document.getElementById('closeFPC_creator_nonwiki').value; var creator_wiki = document.getElementById('closeFPC_creator_wiki').value; var editor = document.getElementById('closeFPC_editor').value; var editor2 = document.getElementById('closeFPC_editor2').value; var promoted_file = document.getElementById('closeFPC_promoted_file').value; var nom_name = document.getElementById('closeFPC_name').value; var cat = document.getElementById('closeFPC_cat').value; //var subcat = document.getElementById('closeFPC_subcat').value; var reason = document.getElementById('closeFPC_reason').value; var FPClink = +currentPageName+;

var today = new Date; var monthnames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var month = monthnames[today.getUTCMonth]; var day = today.getUTCDate; var year = today.getUTCFullYear; var abbr_month; if (month == "January") { abbr_month = "Jan"; }	else if (month === "February") { abbr_month = "Feb"; }	else if (month === "March") { abbr_month = "Mar"; }	else if (month === "April") { abbr_month = "Apr"; }	else if (month === "May") { abbr_month = "May"; }	else if (month === "June") { abbr_month = "Jun"; }	else if (month === "July") { abbr_month = "Jul"; }	else if (month === "August") { abbr_month = "Aug"; }	else if (month === "September") { abbr_month = "Sep"; }	else if (month === "October") { abbr_month = "Oct"; }	else if (month === "November") { abbr_month = "Nov"; }	else if (month === "December") { abbr_month = "Dec"; }

var PageName_actual = currentPageName.replace(/Wikipedia:Featured picture candidates\//,''); if (reason !== '') { reason = "\n*"+reason+" "+""; }	displayMessage('<ul id="closeFPC_status"></ul><ul id="closeFPC_finish"></ul>'); document.getElementById('closeFPC_finish').innerHTML += '<span id="closeFPC_finished_wrapper"><span id="closeFPC_finished_main" style="display:none"><li id="closeFPC_done">Done (<a href="/wiki/'+encodeURI(currentPageName)+'?action=purge" title="'+currentPageName+'">Reload page</a>)</li> '; var FPCpage = closeFPC_getPageText(currentPageName); var isClosed = FPCpage.match(/{{FPCresult\|/); if (isClosed !== null) { errorMessage('It seems that this nomination has already been closed.'); return; }	var token = closeFPC_getToken(true); if (closetype === "promote") {

// Checks for missing information if (nominator === '') { errorMessage('A nominator has not been specified. A nominator is required for promotion.'); return; }		//if (creator_nonwiki ==  && creator_wiki == ) { // errorMessage('A creator has not been specified. A creator is required for promotion. Use "Unknown" under non-Wikipedian if creator is unknown.'); // return; //}		if (promoted_file === '') { errorMessage('A file name has not been specified. A file name is required for promotion.'); return; }		var goodFile = promoted_file.match(/\./); if (goodFile === null) { errorMessage('Specified file name is formatted incorrectly.'); return; }		if (nom_name === '') { errorMessage('A name for the nomination has not been specified. A name is required for promotion.'); return; }		if (cat === '') { errorMessage('A category for the nomination has not been specified. A category is required for promotion.'); return; }		var promoted_file_error = promoted_file.match(/File:/); if (promoted_file_error !== null) { errorMessage('Error: filename entered contains "File:".'); return; }		var username_error1 = nominator.match(/User:/); var username_error2 = nominator2.match(/User:/); var username_error3 = creator_wiki.match(/User:/); var username_error4 = editor.match(/User:/); var username_error5 = editor2.match(/User:/); if (username_error1 !== null || username_error2 !== null || username_error3 !== null || username_error4 !== null || username_error5 !== null) { errorMessage('Error: username entered contains "User:".'); return; }

// Increments count, adds image, and removes older image at WP:FP var FP = "Wikipedia:Featured pictures"; var FPpage = closeFPC_getPageText(FP); if (cat === "Animals") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Artwork") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Culture, entertainment, and lifestyle") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Currency") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Diagrams, drawings, and maps") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Engineering and technology") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Food and drink") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Fungi") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "History") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Natural phenomena") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "People") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Photographic techniques, terms, and equipment") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Places") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Plants") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Sciences") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Space") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Vehicles") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Other lifeforms") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else if (cat === "Other") { FPpage = FPpage.replace(//, "\n|"); FPpage = FPpage.replace(/\n\|\/,""); } else { errorMessage('An invalid category has been specified.'); return; }		closeFPC_editPage(FP, FPpage, token, "Add to "+cat);

// Adds image to WP:GO var GO = "Wikipedia:Goings-on"; var GOpage = closeFPC_getPageText(GO); GOpage = GOpage.replace(/\n\n\[\[Wikipedia:Featured topics\|Topics]]/,"\n* "+nom_name+" ("+day+" "+abbr_month+")\n\nTopics"); closeFPC_editPage(GO, GOpage, token, "+ to pictures");

// Adds Featured Picture template to file description page var promoted_file_page = "File:"+promoted_file; var promoted_file_pageText = closeFPC_getPageText(promoted_file_page); if (promoted_file_pageText == "undefined") { promoted_file_pageText = "{{"+"FeaturedPicture|"+PageName_actual+"}}"; }		else { promoted_file_pageText = "{{"+"FeaturedPicture|"+PageName_actual+"}}\n"+promoted_file_pageText; }		closeFPC_editPage(promoted_file_page, promoted_file_pageText, token, "Featured picture, per "+FPClink);

// Closes nomination as promote FPCpage = FPCpage.replace(/\n/, "\n{"+"{FPCresult|Promoted|File:"+promoted_file+"}} --"+""+reason+"\n"); closeFPC_editPage(currentPageName, FPCpage, token, "Closed, promoted File:"+promoted_file+"");

// Adds image to New featured content var NFC = "Template:Announcements/New featured content"; var NFCpage = closeFPC_getPageText(NFC); NFCpage = NFCpage.replace(//,"\n* "+nom_name+""); NFCpage = NFCpage.replace(/\n\* \] ?/," "); closeFPC_editPage(NFC, NFCpage, token, "+ to FPs");

// Adds image to FPC thumbs var thumbs = "Wikipedia:Featured pictures thumbs"; var currentthumbs = closeFPC_getPageText(thumbs); currentthumbs = currentthumbs.replace(/#REDIRECT \[\[/,''); currentthumbs = currentthumbs.replace(/]]/,''); var currentthumbs_page = closeFPC_getPageText(currentthumbs); currentthumbs_page = currentthumbs_page.replace(/ /," \nFile:"+promoted_file+"|"+nom_name); closeFPC_editPage(currentthumbs, currentthumbs_page, token, "+");

// Notifies nominator of promotion if (nominator != creator_wiki && nominator != editor && nominator != editor2) { var nominator_talk = "User talk:"+nominator; var nomtalkpage = closeFPC_getPageText(nominator_talk); nomtalkpage = nomtalkpage + "\n\n=="+currentPageName+"==\n{"+"{subst:PromotedFPC|File:"+promoted_file+"}}"; closeFPC_editPage(nominator_talk, nomtalkpage, token, FPClink); }

// Notifies nominator2 of promotion if (nominator2 !== '' && nominator2 !== creator_wiki && nominator2 !== editor && nominator2 !== editor2) { var nominator2_talk = "User talk:"+nominator2; var nom2talkpage = closeFPC_getPageText(nominator2_talk); nom2talkpage = nom2talkpage + "\n\n=="+currentPageName+"==\n{"+"{subst:PromotedFPC|File:"+promoted_file+"}}"; closeFPC_editPage(nominator2_talk, nom2talkpage, token, FPClink); }

// Notifies creator of promotion if (creator_wiki !== '') { var creator_talk = "User talk:"+creator_wiki; var creatortalkpage = closeFPC_getPageText(creator_talk); creatortalkpage = creatortalkpage + "\n\n=="+currentPageName+"==\n{"+"{subst:UploadedFPC|File:"+promoted_file+"}}"; closeFPC_editPage(creator_talk, creatortalkpage, token, FPClink); }

// Notifies editor of promotion if (editor !== '') { var editor_talk = "User talk:"+editor; var editortalkpage = closeFPC_getPageText(editor_talk); editortalkpage = editortalkpage + "\n\n=="+currentPageName+"==\n{"+"{subst:UploadedFPC|File:"+promoted_file+"}}"; closeFPC_editPage(editor_talk, editortalkpage, token, FPClink); }

// Notifies editor2 of promotion if (editor2 !== '') { var editor2_talk = "User talk:"+editor2; var editor2talkpage = closeFPC_getPageText(editor2_talk); editor2talkpage = editor2talkpage + "\n\n=="+currentPageName+"==\n{"+"{subst:UploadedFPC|File:"+promoted_file+"}}"; closeFPC_editPage(editor2_talk, editor2talkpage, token, FPClink); }

closeFPC_informManual;

}	if (closetype === "nopromote") { // Closes nomination as no promote FPCpage = FPCpage.replace(/\n/, "\n{"+"{FPCresult|Not Promoted| }} --"+""+reason+"\n"); closeFPC_editPage(currentPageName, FPCpage, token, "Closed, not promoted"); }

// Moves transclusion to 'recently closed' section of WP:FPC var closeFPC_FPC = "Wikipedia:Featured picture candidates"; var FPClist = closeFPC_getPageText(closeFPC_FPC); var PageName_underscore = PageName_actual.replace(/ /g,'_'); FPClist = FPClist.replace('{{' + currentPageName + '}}\n', ''); FPClist = FPClist.replace('{{' + mw.config.get( 'wgPageName' ) + '}}\n', ''); FPClist = FPClist.replace('{{Wikipedia:Featured picture candidates/' + PageName_underscore + '}}\n', ''); FPClist = FPClist.replace(//,"\n{{"+currentPageName+"}}"); if (closetype == "promote") { closeFPC_editPage(closeFPC_FPC, FPClist, token, "Closed "+currentPageName+" (Promoted)"); }	if (closetype == "nopromote") { closeFPC_editPage(closeFPC_FPC, FPClist, token, "Closed "+currentPageName+" (Not promoted)"); }

// Archives nomination var closeFPC_log = "Wikipedia:Featured picture candidates/"+month+"-"+year; var FPClog = closeFPC_getPageText(closeFPC_log); if ( FPClog === undefined ) { FPClog = "{"+"{FPCArchiveBar}}\n\n{"+"{"+currentPageName+"}}"; }	else { FPClog = FPClog+"\n{"+"{"+currentPageName+"}}"; }	closeFPC_editPage(closeFPC_log, FPClog, token, "Archiving "+FPClink);

document.getElementById('closeFPC_finished_main').style.display = ''; }

function closeFPC_getPageText(title) { document.getElementById('closeFPC_status').innerHTML += '<li id="closeFPC_get'+escape(title)+'">Getting <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a></li>'; var req = new XMLHttpRequest; req.open("GET", mw.config.get( 'wgScriptPath' ) + "/api.php?action=query&prop=revisions&rvprop=content&format=json&indexpageids=1&titles="+encodeURIComponent(title), false); req.send(null); var response = eval('(' + req.responseText + ')'); pageid = response['query']['pageids'][0]; if (pageid == "-1") { document.getElementById('closeFPC_get'+escape(title)).innerHTML = '<a class="new" href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a> does not exist'; delete req; return ''; }	pagetext = response['query']['pages'][pageid]['revisions'][0]['*']; delete req; document.getElementById('closeFPC_get'+escape(title)).innerHTML = 'Got <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a>'; return pagetext; }

var FPtitle = "Wikipedia:Featured pictures"; function closeFPC_informManual { document.getElementById('closeFPC_status').innerHTML += '<li id="closeFPC_manual">*** You must manually make edits to the proper sub-page of <a href="/wiki/'+encodeURI(FPtitle)+'" FPtitle="'+FPtitle+'">'+FPtitle+'</a> ***</li>'; }

function closeFPC_getToken(show) { return mw.user.tokens.get('csrfToken');; }

function closeFPC_editPage(title, newtext, token, summary) { document.getElementById('closeFPC_finished_wrapper').innerHTML = '<span id="closeFPC_AJAX_finished_'+AJAXnumber+'" style="display:none">' + document.getElementById('closeFPC_finished_wrapper').innerHTML + ' '; var func_id = AJAXnumber; AJAXnumber++; document.getElementById('closeFPC_status').innerHTML += '<li id="closeFPC_edit'+escape(title)+'">Editing <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a></li>'; var req = new XMLHttpRequest; var params = "action=edit&format=json&token="+encodeURIComponent(token)+"&title="+encodeURIComponent(title)+"&text="+encodeURIComponent(newtext)+"&notminor=1&summary="+encodeURIComponent(summary); url = mw.config.get( 'wgScriptPath' ) + "/api.php"; req.open("POST", url, true); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); req.onreadystatechange = function { if(req.readyState == 4 && req.status == 200) { response = eval('(' + req.responseText + ')'); try { if (response['edit']['result'] == "Success") { document.getElementById('closeFPC_edit'+escape(title)).innerHTML = 'Saved <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a>'; } else { document.getElementById('closeFPC_edit'+escape(title)).innerHTML = ' Edit failed on <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a>. Error info:' +response['error']['code'] + ' : ' + response['error']['info']; }			}			catch(err) { document.getElementById('closeFPC_edit'+escape(title)).innerHTML = ' Edit failed on <a href="/wiki/'+encodeURI(title)+'" title="'+title+'">'+title+'</a> '; }			document.getElementById('closeFPC_AJAX_finished_'+func_id).style.display = ''; delete req; }	}	req.send(params) }

/** * The old mw.util.jsMessage function before https://gerrit.wikimedia.org/r/#/c/17605/, which * introduced the silly auto-hide function. Also with the original styles. * Add a little box at the top of the screen to inform the user of * something, replacing any previous message. * Calling with no arguments, with an empty string or null will hide the message * * Imported from https://en.wikipedia.org/w/index.php?title=User:Timotheus_Canens/displaymessage.js&oldid=512157257 * * @param message {mixed} The DOM-element, jQuery object or HTML-string to be put inside the message box. * @param className {String} Used in adding a class; should be different for each call * to allow CSS/JS to hide different boxes. null = no class used. * @return {Boolean} True on success, false on failure. */ function displayMessage( message, className ){ if ( !arguments.length || message === '' || message === null ) { $( '#display-message' ).empty.hide; return true; // Emptying and hiding message is intended behaviour, return true } else { // We special-case skin structures provided by the software. Skins that // choose to abandon or significantly modify our formatting can just define // an mw-js-message div to start with. var $messageDiv = $( '#display-message' ); if ( !$messageDiv.length ) { $messageDiv = $( '<div id="display-message" style="margin:1em;padding:0.5em 2.5%;border:solid 1px #ddd;background-color:#fcfcfc;font-size: 0.8em"> ' ); if ( mw.util.$content.length ) { mw.util.$content.prepend( $messageDiv ); } else { return false; }		}		if ( className ) { $messageDiv.prop( 'class', 'display-message-' + className ); }		if ( typeof message === 'object' ) { $messageDiv.empty; $messageDiv.append( message ); } else { $messageDiv.html( message ); }		$messageDiv.slideDown; return true; } }

function errorMessage(errorDescription) { displayMessage(' ' + errorDescription + ' ' ); }

function closeFPC_addLink { var el = mw.util.addPortletLink( "p-cactions", "", "Close FPC", "ca-closeFPC", "Close FPC"); $( el ).click( closeFPC_init ); }

if ( mw.config.get( 'wgPageName' ).indexOf( 'Wikipedia:Featured_picture_candidates/' ) !== -1) { $( closeFPC_addLink ); }

// Expose some stuff to global for now window.closeFPC_close = closeFPC_close;

}( jQuery, mediaWiki ) ); //