User:Indagate/IdMatch.js

// var pageCats = mw.config.values.wgCategories; var wdVals = ["Netflix title", "Metacritic", "Disney+"]; var hasWDCats = false; for(let i = 0; i < wdVals.length; i++) { let idCats = ["Netflix title", "Metacritic", "Disney+"]; if(pageCats.indexOf(wdVals[i] + " ID same as Wikidata") != -1) { hasWDCats = true; } }

if (hasWDCats) { var idTemplates = "Netflix title"],	["Metacritic film", "Metacritic Movie"],	["Metacritic television", "Metacritic TV"],	["Metacritic video game", "Metacritic game", "Metacritic video games", "Metacritic vg"],	["Metacritic album", "Metacritic music"],	["Disney+ movie", "Disney+ film"],	["Disney+ series"; var wdIds = ["P1874", "P1712", "P1712tv", "P1712game", "P1712music", "P7595", "P7596"]; //Add remove button mw.loader.using(["oojs-ui-core", "oojs-ui-windows"]).done(function {		OO.ui.confirm("Remove identical WikiData ids?").done(function(confirmed) { if (confirmed) { removeIDs; }		});	}); }

function removeIDs { let wikiDataID = mw.config.values.wgWikibaseItemId; let apiTarget = "https://www.wikidata.org/w/api.php?action=wbgetclaims&format=json&entity=" + wikiDataID; var api = new mw.Api; api.get( {		action: "parse",		page: mw.config.values.wgPageName,		prop: "wikitext"	}).done(function(data) {		let pageData = data.parse.wikitext["*"];		fetch(apiTarget + "&origin=*").then(function(response){return response.json;}).then(function(response) { let wdProps = Object.entries(response.claims); let props = []; let templates = []; let claims = []; let edits = []; let editSummary = "Removed any id values that are stored at WikiData from template: "; for (let i = 0; i < wdProps.length; i++) { let wdID = wdIds.indexOf(wdProps[i][0]); if (wdID != -1) { let prop = wdProps[i]; props[props.length] = prop; let claim = prop[1][0].mainsnak.datavalue.value.split("/"); let fullClaim = claim[claim.length - 1]; if (claim.length > 1) { if (wdIds.indexOf(wdProps[i][0] + claim[0]) != -1) { wdID = wdIds.indexOf(wdProps[i][0] + claim[0]); }						fullClaim = claim[1]; for (let j = 2; j < claim.length; j++) { fullClaim += "/" + claim[j]; }					}					claims[claims.length] = fullClaim; templates[templates.length] = idTemplates[wdID]; }			}			for (let i = 0; i < props.length; i++) { let claim = claims[i].replaceAll(/[.*+?^${}|[\]\\]/g, '\\$&');; let template = templates[i]; for (let j = 0; j < template.length; j++) { let tRegex = new RegExp("|\\|)", "gi");					if (pageData.search(tRegex) != -1) {						if (edits.indexOf(i) == -1) {							edits[edits.length] = [template[0], props[i]];						}					}					pageData = pageData.replaceAll(tRegex, "{{" + template[0]);					tRegex = new RegExp("{{" + template[0] + "\\|( *title *= *)?(?=[^\\|\\n=]+(\\| *access-?date=[^\\n]+)?}})", "gi");					pageData = pageData.replaceAll(tRegex, "{{" + template[0] + "|title=");				}			}			for (let i = 0; i < edits.length; i++) {				editSummary += "{{" + edits[i][0] + "}} ";				editSummary += ("(Identifier)");			}			if (edits.length > 1) {				editSummary = editSummary.replace("template", "templates");				editSummary = editSummary.replaceAll("){{", "), {{");			}			editSummary += ". (User JS)";			api.postWithEditToken({ action: 'edit', title: mw.config.values.wgPageName, text: pageData, summary: editSummary, minor: true }).done(function(result) { if (result.edit.result == "Success") { window.location = "/w/index.php?title=" + pageName.replaceAll("&", "%26") + "&type=revision&diff=cur&oldid=prev"; //location.reload; }				console.log(result); });		});	}); }

//