User:Theleekycauldron/DYKPC.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.
var users = {
	"Cwmhiraeth": 0, 
	"Yoninah": 0,
	"Allen3": 0,
	"SL93": 0,
	"theleekycauldron": 0,
	"Hawkeye7": 0,
	"Crisco 1492": 0,
	"BlueMoonset": 0,
	"PFHLai": 0,
	"97198": 0,
	"PumpkinSky": 0,
	"Miyagawa": 0,
	"Panyd": 0,
	"The Squirrel Conspiracy": 0,
	"Kavyansh.Singh": 0,
	"Orlady": 0,
	"Desertarun": 0,
	"Narutolovehinata5": 0,
	"Montanabw": 0,
	"Gatoclass": 0,
	"Casliber": 0,
	"MeegsC": 0,
	"Vaticidalprophet": 0,
	"Victuallers": 0,
	"Z1720": 0,
	"SSTflyer": 0,
	"Carabinieri": 0,
	"Kingsif": 0,
	"HalfGig": 0,
	"MPJ-DK": 0,
	"Amkgp": 0,
	"Mentoz86": 0,
	"Valereee": 0,
	"Redtigerxyz": 0,
    "RoySmith": 0,
    "Bruxton": 0,
    "Cielquiparle": 0
}

var active = ["Cwmhiraeth","SL93","theleekycauldron", "RoySmith","Bruxton"]
var aftername = {
	'Yoninah': "<small>([[Z''L]])</small>"
}
async function search_user(username){
	var total = 0;
	var params = {
		action: 'query',
		list: 'search',
		srnamespace: '10',
		srsearch: '-insource:\"the result was: \'\'\'promoted\'\'\' by \[\[User:\"'+username+' \"the result was: promoted by '+username+'\" intitle:\"Did you know nominations\"',
		srinfo: 'totalhits',
		format: 'json'
	}
	res1 = api.get( params ).done( function ( data ) {
		total += data.query.searchinfo.totalhits; 
	} );
	
	params = {
		action: 'query',
		list: 'search',
		srnamespace: '10',
		srsearch: ' insource:\"the result was: \'\'\'promoted\'\'\' by \[\[User:'+username+' "intitle:\"Did you know nominations\"',
		srinfo: 'totalhits',
		format: 'json'
	}
	res2 = api.get( params ).done( function ( data ) {
		total += data.query.searchinfo.totalhits;
	} );
	await res1;
	await res2;
	return total;
}

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}


async function update_dykpc(){
	for (const key of Object.keys(users)){
		users[key] = await search_user(key);
		console.log(key,users[key]);
	}
	users = Object.entries(users).sort((a, b) => b[1] - a[1]);
	let lines = [];
	let activeadd, afteradd;
	users.forEach((user) => {
		activeadd = (active.includes(user[0])?"|a=y":"")
		afteradd = ((aftername[user[0]]!=undefined)?("|aftername="+aftername[user[0]]):(""))
		lines.push("{{/Template|"+user[0]+"|"+numberWithCommas(user[1])+afteradd+activeadd+"}}");
	});
	let pre  = "{{Shortcut|WP:DYKPC}}\n{{DYKbox}}\nThis page lists Wikipedia users by the number of DYK nominations closed and moved to [[WP:DYKQ|the prep sets]]. The qualification requirement for this list is 100 promotions; the list functions on the modern system of nomination closing, so substantially impactful users such as BorgQueen were too early for this to accurately display their contributions. Thanks to each and every person who has kept DYK functioning with their time and effort through prep-set building! :)\n\nRows in {{Color box|#b0dab9}}{{nbsp}}green denote currently active promoters.\n\n{| class=\"wikitable sortable\"\n|+\n!User\n!count\n!Search links (use these to update manually!)\n";
	let post = "\n|}\nIf you want, you can add this cool [[Wikipedia:Userboxes|userbox]] to [[Special:MyPage|your userpage]]! (The template can be found at [[Template:User DYK promotions]])\n{{User DYK promotions|0}}\n[[Category:Wikipedia Did you know administration]]"
	lines = pre+lines.join("\n")+post
	console.log(lines);
	var editparams = {
			action: 'edit',
			title: "Wikipedia:List of Wikipedians by number of DYK promotions",
			summary: "Updating list ([[user:theleekycauldron/DYKPC.js|script]])",
			text: lines,
			format: 'json'
		}

	api.postWithToken( 'csrf', editparams ).done( function ( data ) {
		console.log( data );
	} );
	await editparams;
	location.reload(true);
	return lines
}

if (mw.config.get('wgPageName').includes("Wikipedia:List_of_Wikipedians_by_number_of_DYK_promotions")){
    $('#bodyContent').prepend('<button onclick="update_dykpc()">update page :)</button>');
}