User:Theleekycauldron/Scripts/PSHAW/protocols/queuer.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() {
	console.log("queuer ready!");
	if (mw.config.get('wgPageName').includes("Template:Did_you_know/Preparation_area")) {
		$(mw.util.addPortletLink(window.DYKH_portlet || 'p-cactions', '#', 'Move to queue (PSHAW)', 'dyk-portlet')).click(moveToQueue);
	}
});

moveToQueue = async function (e){
    let prep = new Prep(mw.config.get('wgPageName'))
    await prep.initialize();
    e.preventDefault();
	var Window = new Morebits.simpleWindow(480, 270);
	Window.setTitle("The prep set's half-assed workbench (PSHAW)");
	Window.addFooterLink("Prep builder instructions", "Wikipedia:Did you know/Prep builder instructions");
	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();
	}
	
	async function promote(){
		if (!mw.config.get('wgUserGroups').includes("sysop")){
			alert("Might I suggest [[Wikipedia:Requests for adminship]] first?");
			return;
		}
		let pqnum = prep.title.slice(-1);
        let queue = new Prep("Template:Did you know/Queue/"+pqnum);
		let nextpreppagetitle = "Template:Did you know/Queue/NextPrep";
		
		console.log(prep);
		console.log(queue);
		
		form.root.childs = [];
		form.append({
			type: 'div',
			name: 'queuemove',
			label: `moving to [[${queue.title}]]... `
		});
		form.append({
			type: 'div',
			name: 'prepclear',
			label: `clearing [[${prep.title}]]... `
		});
		form.append({
			type: 'div',
			name: 'updatecounter',
			label: `updating [[Template:Did you know/Queue/NextPrep|prep counter]]... `
		});
		show();
		
		let clearpage = await get_pages("Template:Did you know/Clear","user|content",null,"older");
		let nextpreppage = await get_pages(nextpreppagetitle,"user|content",null,"older");

		
		await clearpage;
		await nextpreppage;
		
		let cleartext = clearpage[0].revisions[0].slots.main.content;
		let nextpreptext = nextpreppage[0].revisions[0].slots.main.content;
		
		queue.content = `{{DYKbotdo|~~~}}\n${prep.content}`
		prep.content  = cleartext
 
        if (nextpreptext[0] != pqnum){
            alert("Promote your queues in order, I can't clean up all your messes!");
            return;
        }
		
		console.log(nextpreptext[0],nextpreptext[0]%7,nextpreptext[0]%7+1,(parseInt(nextpreptext[0])%7+1).toString()+nextpreptext.slice(1));
		nextpreptext = (parseInt(nextpreptext[0])%7+1).toString()+nextpreptext.slice(1);
    
		await queue.save(`promoting from [[${prep.title}|prep ${pqnum}]]`).done(function(data){
			form.root.childs[0].data.label += 'done! :)';
			console.log(data);
			show();
		});
		
		await prep.save(`rake to [[${queue.title}|queue ${pqnum}]]`).done(function (data){
			console.log(data);
			form.root.childs[1].data.label += 'done! :)';
			console.log(form.root.childs[1]);
			show();
		});
		
		var params = {
			action: 'edit',
			title: nextpreppagetitle,
			summary: `updating counter`+tag,
			text: nextpreptext,
			format: 'json'
		}
		api.postWithToken('csrf', params).done( function ( data ) {
			console.log(data);
			form.root.childs[2].data.label += 'done! :)';
			console.log(form.root.childs[2]);
			show();
			location.reload(true);
		} );
		
	}

    form.append({
		type: 'button',
		name: 'promoter',
		label: 'Move to queue',
		style: 'float: right',
		event: function p(){promote()}
	})
	
	function pull_hook(slotnum){
		
		var dykpage = new Morebits.wiki.page("Template talk:Did you know", 'Adding nomination to Template talk:Did you know'); //shamelessly stolen from SD0001
		var nompage = ""
		dykpage.load(function addNomToTTDYK(dykpage) {
			var pageText = dykpage.getPageText();
			var re = new RegExp('==Pulled nominations==\n^.*?$');
			var newPageText = pageText.replace(re, '$&\n{{Template:Did you know nominations/' + nompage + '}}');
			dykpage.setPageText(newPageText);
			dykpage.setEditSummary(`/* Pulled nominations */ adding [[Template:Did you know nominations/${nompage}']] ([[User:Theleekycauldron/DYK promoter|PSHAW]])`);
			dykpage.setMaxConflictRetries(3);
			dykpage.save();
		});
	}
	
	show();
}
// </nowiki>