User:Theleekycauldron/Scripts/PSHAW/protocols/main page updater.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.
/*<nowiki>
Copyright (c) 2023 theleekycauldron

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
$.when(
	mw.loader.using('ext.gadget.morebits'),
	$.ready
).then(async function() {
	if (mw.config.get('wgPageName').includes("Template:Did_you_know/Queue/")) {
		$(mw.util.addPortletLink(window.DYKH_portlet || 'p-cactions', '#', 'Update Main Page (PSHAW)', 'dyk-portlet', 'Update Main Page')).click(update);
	}
});

update = async function(e){
	if (!mw.config.get('wgUserGroups').includes("sysop")){
		alert("Might I suggest [[Wikipedia:Requests for adminship]] first?");
		return;
	}
	
	e.preventDefault();
	var Window = new Morebits.simpleWindow(480, 270);
	Window.setTitle("The prep set's half-assed workbench (PSHAW)");
	Window.addFooterLink("Admin instructions", "Wikipedia:Did you know/Admin instructions");
	var form = new Morebits.quickForm();

	function show(){
		result = form.render();
		Window.setContent(result);
		Window.display();
	}
	
	form.append({type: 'div', name: 'header1', label: "<b><span style=\"color:red\">Please be very careful, this machine edits the Main Page!</span></b>"})
	form.append({type: 'div', name: 'header2', label: "<b>Actions to take:</b>"})
	
	form.append({type: 'button', name: 'selectall', label: "select all", event: () => selectAll(4,true), style: "float: right"})
	form.append({type: 'button', name: 'deselectall', label: "deselect all", event: () => selectAll(4,false), style: "float: right"})
	
	function selectAll(c,s){
		for (let box of form.root.childs[c].data.list){
			box.checked = s;
		}
		show();
	}
	
	form.append({type: 'checkbox', name: 'actions', list: [
		{type: 'checkbox', name: 'updatemainpage', value: "updatemainpage", label: "Update [[Template:Did you know|Main Page]]", checked: true},
		{type: 'checkbox', name: 'resettimer', value: 'resettimer', label: "Reset [[Template:Did you know/Next update/Time|timer]]", checked: true},
		{type: 'checkbox', name: 'handleimage', value: 'handleimage', label: "Handle image protection", checked: true},
		{type: 'checkbox', name: 'incrementpointer', value: 'incrementpointer', label: "Increment [[Template:Did you know/Queue/Next|queue pointer]]", checked: true},
		{type: 'checkbox', name: 'clearqueue', value: 'clearqueue', label: `Clear this queue`, checked: true},
		{type: 'checkbox', name: 'archivehooks', value: 'archivehooks', label: "[[Wikipedia:Recent additions|Archive]] previous set of hooks", checked: true}
	]});

	
	form.append({type: 'button', name: 'submit', label: "submit!", event: () => evaluate(e,result), style: "clear: both"})
	show();
	
	evaluate = async function(e,result){
		form.root.childs = [];
		
		//form.append({type: 'div', name: 'messagingusers', label: 'Messaging users... '})
		//form.append({type: 'div', name: 'taggingtalks', label: 'Tagging talk pages... '})
		if (result.updatemainpage.checked){ //T:DYK
			form.append({type: 'div', name: 'updatingmainpage', label: 'Updating Main Page... '})
			
			form.root.childs[form.root.childs.length-1].data.label += "done! :)"
		}
		
		if (result.resettimer.checked){ //timer
			form.append({type: 'div', name: 'resettingtimer', label: 'Resetting timer... '})
			let interval = await get_section("User:DYKUpdateBot/Time Between Updates",0)
			interval = interval.wikitext
			let time = await get_section("Template:Did you know/Next update/Time",0)
			time = new Date(time.wikitext)
			console.log(time,interval)
			while (time<=new Date()){
				time.setSeconds(time.getSeconds()+interval)
			}
			time.setSeconds(time.getSeconds()-interval)
			
			time = time.toISOString()
			console.log(time)
			time = time.slice(0,time.length-5)+"Z"
			var params = {
				action: 'edit',
				title: `User:Theleekycauldron/sandbox/Time`,
				summary: `Resetting timer`+tag,
				text: time,
				format: 'json'
			}
			api.postWithToken( 'csrf', params );
			form.root.childs[form.root.childs.length-1].data.label += "done! :)"
		}
		
		if (result.handleimage.checked){ //image
			
			form.root.childs[form.root.childs.length-1].data.label += "done! :)"
		}
		
		
		if (result.incrementpointer.checked){ //pointer
			form.append({type: 'div', name: 'incrementing pointer', label: 'Incrementing pointer... '})
			let pointer = await get_section("Template:Did you know/Queue/Next",0)
			var params = {
				action: 'edit',
				title: `User:Theleekycauldron/sandbox/pointer`,
				summary: `Incrementing pointer`+tag,
				text: pointer.wikitext%7+1,
				format: 'json'
			}
			api.postWithToken( 'csrf', params );
			pointer.wikitext
			form.root.childs[form.root.childs.length-1].data.label += "done! :)"
		}
		
		if (result.clearqueue.checked){
			var params = {
				action: 'edit',
				title: mw.config.get('wgPageName'),
				summary: `Update is done, removing the hooks`+tag,
				text: "{{User:DYKUpdateBot/REMOVE THIS LINE}}",
				format: 'json'
			}
			api.postWithToken( 'csrf', params );
		}
		//location.reload(true)
	}
}
// </nowiki>