User:Xover/change-status.js

// change-status.js // // Lightly modified version of User:DannyS712/Change status.js // $(function { var CS_config = {	name: 'Change status.js',	version: 2.0,	debug: true }; var CS_summary = 'Change status with ' + CS_config.name + ' (version ' + CS_config.version + ')'; var scriptUrl = mw.config.get( 'wgScriptPath' ) + '/api.php';

mw.loader.using('mediawiki.util', function {    $(document).ready(function  { if (mw.config.get('wgNamespaceNumber') === 104) { var statusPortlet = mw.util.addPortletLink(   			'p-cactions', '#', 'Change status',    			'ca-change-status', 'Change status'    		); $(statusPortlet).on('click', function(e) {   			e.preventDefault;        		change_status;    		}); var $validateLink = $('[validate] ') .css("float", "right"); $validateLink.on("click", function (e) {   			e.preventDefault;    			change_status;    		}); $(".prp-page-qualityheader.quality3").append($validateLink); var $proofreadLink = $('[proofread] ') .css("float", "right"); $proofreadLink.on("click", function (e) {   			e.preventDefault;    			change_status;    		}); $(".prp-page-qualityheader.quality1, .prp-page-qualityheader.quality2") .append($proofreadLink); }   } ); } ); function change_status{ var name = mw.config.get( 'wgPageName' ); var page = get_page ( name ); var this_user = mw.config.get( 'wgUserName' ); if (CS_config.debug) console.log ( page ); var status = page.replace(/(?:.|\n)*()(?:.|\n)*/, '$1'); var status_user = status.replace(/.*?user="(.*?)" \/>/, '$1'); var status_level = status.replace(/.*?level="(\d+)" user=".*\/>/, '$1');	console.log( status, status_user, status_level, this_user );	var new_status, new_page;	if (status_level === "1" || status_level === "2"){		console.log('Status is 1 or 2');		new_status = '';		new_page = page.replace(//, new_status);		CS_summary = 'Mark as proofread with ' + CS_config.name + ' (version ' + CS_config.version + ')';	} else if (status_level === "3" && status_user != this_user){		console.log('Status is 3, can be validated');		new_status = '';		new_page = page.replace(//, new_status);		CS_summary = 'Mark as validated with ' + CS_config.name + ' (version ' + CS_config.version + ')';	}	if (CS_config.debug) console.log ( new_status, new_page );	if (page === new_page) return; set_new ( name, new_page ); } function get_page( name ){ var page_to_get = { action: 'query', titles: name, prop: 'revisions', rvprop: 'content', format: 'json', formatversion: 2 };   var result = null; $.ajax({		url: scriptUrl,		type: 'get',		data: page_to_get,		dataType: 'json',		async: false,		success: function(page) {			if (CS_config.debug) console.log( page );	   	result = page.query.pages["0"].revisions["0"].content;	    	if (CS_config.debug) console.log( result );		} 	}); return result; } function set_new ( page, new_content ){ console.log( page, new_content ); var to_send = { action: 'edit', title: page, text: new_content, summary: CS_summary, token: mw.user.tokens.get( 'csrfToken' ) };   console.log( to_send ); $.when(       $.post( scriptUrl, to_send, function( response ){ } )    ).done( function {		$(".prp-page-qualityheader.quality3")			.html("This page has been validated.")			.addClass("quality4")			.removeClass("quality3");		$(".prp-page-qualityheader.quality1, .prp-page-qualityheader.quality2")			.html("This page has been proofread, but needs to be validated.")			.addClass("quality3")			.removeClass("quality1 quality2");    } ); } });