User:Theleekycauldron/DYK modification detector.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Theleekycauldron/DYK modification detector. |
mw.loader.load('User:Theleekycauldron/Query pages.js');
importScript('User:Theleekycauldron/User contributions.js');
mw.loader.load('http://localhost:8000/Desktop/Programming/Toolforge/DYK credit finder.js');
function log(input){
document.getElementById("log").innerHTML += "\n"+String(input);
}
function arrays_equal(a, b) {
if (a === b) return true;
if ((a == null) != (b == null)) return false;
if (a.length !== b.length) return false;
// If you don't care about the order of the elements inside
// the array, you should sort both arrays here.
// Please note that calling sort on an array will modify that array.
// you might want to clone your array first.
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
function capitalize_first_letter(string) { //capitalizes first letter of a string
return string.charAt(0).toUpperCase() + string.slice(1);
}
function detect_changes(obj){
obj.instances.sort((a, b) => (a.revid > b.revid) ? 1 : -1);
changes = []
for (var i=0; i < obj.instances.length-1; i++){
if (obj.instances[i].hook != obj.instances[i+1].hook){
changes.push([obj.instances[i],obj.instances[i+1]]);
}
}
return changes;
}
function bolded_articles(hook){
let articles = []; //if it's a multihook, we'll have to save the articlenames to this so we can push it to multis later
try {
let re = /('{3}[^']*?\[\[.*?]].*?'{3})|(\[\[.*'{3}.*?'{3}]])/g; //... idk what it does honestly, i just fell asleep on my keyboard and now this mash looks for bolded articles in hooks (it looks for the '''[[Article]]''' wrapper)
let matches = hook.match(re); //save all bolded articles into an array
for (var j=0; j<matches.length; j++) { //search each match for pageviews
//parsing the match to make sure we get the correct article title, to feed into the query
matches[j] = matches[j].match(/\[\[.*?(\||]])/mg)[0]; //if the page is piped, e.g. '''[[Article|something else]]''', we're gonna want to separate out the "Article"
if (matches[j].includes("|")){
matches[j] = matches[j].substring(2,matches[j].indexOf("|"));
} else {
matches[j] = matches[j].substring(2,matches[j].lastIndexOf("]")-1);
}
if (matches[j].includes("#")){
matches[j] = matches[j].substring(0,matches[j].indexOf("#")); // same thing if for some goddamn reason the bolded article is to a section
}
matches[j] = capitalize_first_letter(matches[j]); //capitalize the first letter of the matches, in case we got '''[[article]]'''
articles.push(matches[j]);
}
} catch(err){
return [];
}
return articles;
}
async function main(){
pages = await get_pages("Template:Did you know","content|user|ids|comment","1069569365","newer");
hooks = [];
console.log(await pages);
for (var page of pages){
let title = page.title;
console.log(page);
for (var revision of page.revisions){
let revid = revision.revid;
let user = revision.user;
let wikitext = revision.slots.main.content;
let hooklist = wikitext.substring(wikitext.indexOf("<!--Hooks-->"),wikitext.indexOf("<!--HooksEnd-->")).split("\n");
for (var hook of hooklist){
if (hook.substring(0,6) == "* ... ") { //make sure it's really a hook
let obj = {}
obj.articles = bolded_articles(hook);
if (obj.articles == []){
continue;
}
obj.instances = [{}]
obj.instances[0].hook = hook;
obj.instances[0].revid = revid;
obj.instances[0].page = title;
obj.instances[0].editor = user;
let s = hooks.find((o, i) => {
if (arrays_equal(o.articles,obj.articles)) {
hooks[i].instances.push(obj.instances[0]);
return true; // stop searching
}
});
if (typeof(s) === 'undefined'){
hooks.push(obj);
}
}
}
}
}
for (hook of hooks){
let changes = detect_changes(hook);
if (changes.length > 0){
hook.instances.sort((a, b) => (a.revid < b.revid) ? 1 : -1);
let use = -1;
for (instance of hook.instances){
if (instance.page == "Template:Did you know"){ //most reliable, albeit slowest
use = 0;
break;
} else if (instance.page.includes("Template:Did you know/Queue")) { //faster, but possibility of change
use = 1;
p = instance.page;
break;
} else if (instance.page.includes("Template:Did you know/Preparation area")) { //same speed as queue, less reliable
use = 2;
p = instance.page;
break;
}
}
let contrib_data = main_page_hunt(obj.articles);
console.log(await contrib_data);
}
}
}
main()