User:Theleekycauldron/Scripts/PSHAW/dependencies/Prep.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.
*/
class Prep {
	constructor(title){
		this.title = title.replaceAll("_"," "); //MediaWiki stuff
		
	}
	
	async initialize(){
		await get_pages(this.title,"user|content",null,"older").then((res) => {
			this.page    = res
			this.content = this.page[0].revisions[0].slots.main.content;
			this.loadHooks();
			this.loadFiles();
		})
	
	}
	
	loadHooks(){
		let splittext = this.content.split("\n");
		let ip = [splittext.indexOf("<!--Hooks-->")+2,splittext.indexOf("<!--HooksEnd-->"),splittext.indexOf("-->")+1,splittext.indexOf("</div>")]; //important split points
		let hooktext = splittext.slice(ip[0],ip[1]);
		let creditstext = splittext.slice(ip[2],ip[3]);
		this.hooks = []
		for (var h=0; h<hooktext.length; h++){
			let hook = new Hook(h,hooktext[h].slice(2),[])
			this.hooks.push(hook)
			if (hook.articles.length > 0){
				for (let c in creditstext){
					let credit = new Credit(creditstext[c]);
					
					if (hook.articles.includes(credit.article)){
						hook.credits.push(credit)
						if (!credit.make){
							credit.subpage = hook.credits[0].subpage
						}
					}
				}
			} else {
				hook.credits.push(new Credit("* {{DYKmake|Example|Editor|subpage=}}"))
				if (h==hooktext.length-1){
					hook.credits.push(new Credit("* {{DYKnom|Example|Nominator}}"))
				}
			}
		}
	}
	
	loadFiles(){
		let filetext = this.content.match(/\{\{main page image\/DYK.*?}}/g);
		this.file = new File(filetext[0])
	}
	
	morebits(){
		let res = []
		for (let i=0; i<this.hooks.length; i++){
			res.push({ type: 'option', label: this.hooks[i].morebits(), value: i})
		}
		if (this.isEmpty()){
			res.push()
		}
		return res
	}
	
	isEmpty(){
		return this.hooks.every(hook => hook.articles.length == 0)
	}
	
	addHook(hook,slot,file){
		if (slot == 0){
			this.file = file
		}
		this.hooks[slot] = hook
	}
	
	assemble(){
		let res = []
		res.push(`${this.title.includes("Queue")?"{{DYKbotdo|~~~}}":""}
<noinclude>{{Did you know/Clear/header}}
==Hooks==
</noinclude>{{DYK bottom prep notice}}
<!--Hooks-->`)
		res.push(this.file.mpi()+"<!--See [[Template:Main page image/DYK]] for other parameters-->")
		for (let hook of this.hooks){
			res.push("* "+hook.content)
		}
		res.push(`<!--HooksEnd-->
{{flatlist|class=dyk-footer noprint|style=margin-top: 0.5em; text-align: right;}}
* '''[[Wikipedia:Recent additions|Archive]]'''
* '''[[Help:Your first article|Start a new article]]'''
* '''[[Template talk:Did you know|Nominate an article]]'''
{{endflatlist}}
<noinclude>
{{Did you know/Clear/torso}}

==Credits==
<div id="credits">This space is to credit the creators/nominators of the items in this template that in fact appear on the Main Page. If you replace or remove an item before it appears on the Main Page, please revert the promotion of the hook so its template appears again at [[Template talk:Did you know]] and add a note to the nomination's template explaining why you removed it.
<!--
******************** Example format ********************
** (Follow this or you WILL break something)
**
** Normal credit:      * {{DYKmake|ArticleName|Editor|subpage=}}
** Nomination credit:  * {{DYKnom|ArticleName|Nominator}}
**
** Each credit MUST go on a new line
*********************************************************
-->`)
		for (let hook of this.hooks){
			for (let credit of hook.credits){
				res.push(credit.content)
			}
		}
		res.push(`</div>
{{Did you know/Clear/footer}}</noinclude>`)
		this.content = res.join("\n")
		return this.content
	}
	
	save(summary){
		var params = {
				action: 'edit',
				title: this.title,
				summary: summary+tag,
				text: this.content,
				format: 'json'
			}
		return api.postWithToken( 'csrf', params );
	}
	
}
// </nowiki>