MediaWiki:Common.js: Difference between revisions

From Official Factorio Wiki
Jump to navigation Jump to search
m (formatting, fixed function)
(Collapsible prototype tree with indentation guides)
 
(92 intermediate revisions by 3 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
var apiUrl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php"


/* Script in here will be executed when the page is "ready" */
/* Script in here will be executed when the page is "ready" */
Line 7: Line 9:


function getNumberOfActiveUsers() {
function getNumberOfActiveUsers() {
$.ajax({
if (document.getElementById("active-users")) {
url: 'https://wiki.factorio.com/api.php',
$.ajax({
data: {
url: apiUrl,
format: 'json',
data: {
action: 'query',
format: 'json',
list: 'allusers',
action: 'query',
aulimit: 500,
list: 'allusers',
auactiveusers: true
aulimit: 500,
},
auactiveusers: true
dataType: 'json',
},
type: 'GET',
dataType: 'json',
success: function(data) {
type: 'GET',
var numberOfActiveUsers = data.query.allusers.length;
success: function(data) {
document.getElementById("active-users").innerHTML = numberOfActiveUsers.toString();
document.getElementById("active-users").innerHTML = data.query.allusers.length.toString();
},
},
error: function(xhr) {
error: function(xhr) {
console.log( 'Error: Request failed.' );
console.log( 'Error: Request failed.' );
}
}
});
});
}
};
};
/* Infobox updating */


var noInfobox = ["Basic oil processing", "Advanced oil processing", "Coal liquefaction", "Empty barrel", "Heavy oil cracking", "Light oil cracking", "Solid fuel from heavy oil", "Solid fuel from light oil", "Solid fuel from petroleum gas", "Water barrel", "Crude oil barrel", "Heavy oil barrel", "Sulfuric acid barrel", "Light oil barrel", "Petroleum gas barrel", "Lubricant barrel", "Empty crude oil barrel", "Empty heavy oil barrel", "Empty light oil barrel", "Empty lubricant barrel", "Empty petroleum gas barrel", "Empty sulfuric acid barrel", "Empty water barrel", "Fill crude oil barrel", "Fill heavy oil barrel", "Fill light oil barrel", "Fill lubricant barrel", "Fill petroleum gas barrel", "Fill sulfuric acid barrel", "Fill water barrel"]
/* Collapsible list on [[Prototype definitions]] */
var version = "0.15.12";


$(".prototype-tree li").each(function (index, element) {
  if ( $( element ).children("ul").length > 0 ) {
    $( element ).addClass("prototype-tree-parent");
  };
});


var para = "";
$(".prototype-tree li.prototype-tree-parent").on("click", function(event) {
function getInputPara(item, search, length, name, itemName) {
// only collapse if I clicked a li or the direct child of a li that is not another list or a link
var paraStart = item.search(search) + length; //finds the beginning of the para, is after para-name
  if ( $( event.currentTarget ).children("ul").length > 0 && $( event.target ).get(0).tagName != "UL" && $( event.target ).get(0).tagName != "A" && ($( event.target ).hasClass("prototype-tree-parent") || $( event.target ).parent().hasClass("prototype-tree-parent")) ) {
if (paraStart < length) {
    $( event.currentTarget ).children("ul")[0].classList.toggle("hidden");
console.log(itemName + ": No " + name + " found.");
    $( event.currentTarget ).toggleClass("prototype-tree-parent-collapsed");
para = "";
    return false; // prevents further event bubbling
} else {
  }
var paraCut = item.slice(paraStart);  //lets the string begin after the parameter name
});
var paraEnd = paraCut.search(/\||}}/); //finds the end of the parameter definition
if (paraEnd < 1) { //para ends at para-end if it exists
para = paraCut;
} else {
para = paraCut.slice(0, paraEnd);
}
}
return para;
};


/* Evolution calc */


var pagePara = "";
//values are in the form [evolution, weight]
function getOldPara(content, pageParaStart, length, name, itemName) {
const biterSpawner = [
if (pageParaStart < length) { //if the start is less than the length of the para name (name) then the search was unsuccessful
["Small Biter",   [[0.0, 0.3], [0.6, 0.0]]],
console.log(itemName + ": No " + name + " found on page.");
["Medium Biter",  [[0.2, 0.0], [0.6, 0.3], [0.7, 0.1]]],
pagePara = "";
["Big Biter",      [[0.5, 0.0], [1.0, 0.4]]],
} else {
["Behemoth Biter", [[0.9, 0.0], [1.0, 0.3]]]
var pageParaCut = content.slice(pageParaStart);
];
pageParaStart = pageParaCut.search(/\w/);
const spitterSpawner = [
pageParaCut = pageParaCut.slice(pageParaStart); //removes anything before the parameter that does not belong there, like = and \s
["Small Biter",      [[0.0, 0.3], [0.35, 0.0]]],
var pageParaEnd = pageParaCut.search(/\||}}/); //finds the end of the parameter definition
["Small Spitter",    [[0.25, 0.0], [0.5, 0.3], [0.7, 0.0]]],
if (pageParaEnd < 1) { //para ends at para-end if it exists
["Medium Spitter",  [[0.4, 0.0], [0.7, 0.3], [0.9, 0.1]]],
pagePara = pageParaCut;
["Big Spitter",      [[0.5, 0.0], [1.0, 0.4]]],
} else {
["Behemoth Spitter", [[0.9, 0.0], [1.0, 0.3]]]
pagePara = pageParaCut.slice(0, pageParaEnd);
];
}
}
return pagePara;
};


var summary = "";
//calculates the interpolated value
var newContent = "";
function lerp(low, high, pos) {
function updatePara(content, para, pagePara, name, newPageParaStart, length, itemName) { //also needs version, summary, changes newContent and summary
const s = high[0] - low[0];
if (pagePara.length > 0) {
const l = (pos - low[0]) / s;
var newPageParaCut = content.slice(newPageParaStart); //lets the string being after the parameter name
return (low[1] * (1-l)) + (high[1] * l);
var newPageParaEnd = newPageParaCut.search(/\||}}/) + newPageParaStart; //finds the end of the parameter definition, with added parastart so that it's relative to the start of the entire string, not the parameter string
if (para.length > 0) {
newContent = content.slice(0, newPageParaStart) + " = " + para + "\n" + content.slice(newPageParaEnd);
console.log("Replaced " + itemName + " " + name + ".");
summary = summary + "Updated " + name + " to " + version + ". ";
} else {
newPageParaStart = newPageParaStart - length; //makes it so that the start of the para string if before the name, so that the name also gets deleted
newContent = content.slice(0, newPageParaStart) + content.slice(newPageParaEnd); //removes what is between parastart and paraend from newcontent
console.log("Removed " + itemName + " " + name + ". ");
summary = summary + "Removed " + name + ". ";
}
} else if (para.length > 0) {
var InfoboxStart = content.search(/{{Infobox/i) + 9; //finds the start of the infobox, excludes {{infobox from the resulting string
newContent = content.slice(0, InfoboxStart) + "\n|" + name + " = " + para + content.slice(InfoboxStart);
console.log("Added " + itemName + " " + name + ". ");
summary = summary + "Added " + name + ". ";
}
};
};


 
//gets the weight list
function editPage(title, itemName) { //also uses summary, globalToken, newContent
function getValues(map, evo) {
$.ajax({
var result = {};
url: 'https://wiki.factorio.com/api.php',
var sum = 0;
data: {
map.forEach(function(data) {
format: 'json',
const list = data[1];
action: 'edit',
var low = list[0];
title: title,
var high = list[list.length-1];
text: newContent,
list.forEach(function(val) {
token: globalToken,
if(val[0] <= evo && val[0] >  low[0])  low = val;
summary: summary,
if(val[0] >= evo && val[0] < high[0]) high = val;
bot: true,
});
nocreate: true
var val = null;
},
if(evo <= low[0]) val = low[1];
dataType: 'json',
else if(evo >= high[0]) val = high[1];
type: 'POST',
else val = lerp(low, high, evo);
success: function( data ) {
sum += val;
  console.log("Updated " + itemName);
result[data[0]] = val;
},
});
error: function( xhr ) {
Object.keys(result).forEach(function(data, index) {
console.log("Failed to update " + itemName);
result[data] = result[data] / sum;
}
});
});
return result;
};
};


function calcEvo() {
const evo = document.getElementById("evoInput").value;
genTable(getValues(biterSpawner, evo), document.getElementById("evoOutputBiter"), "Biter's Nest");
genTable(getValues(spitterSpawner, evo), document.getElementById("evoOutputSpitter"), "Spitter's Nest");
};


$("#RecipeUpdate").click(function(){
function percentile(value) {
    getRecipes();
return (value*100).toFixed(2) + "%";
});
 
function getRecipes() {
getUserGroup();
if (userGroup.some(isBot) == true) {
var recipeInput = prompt("Please enter the recipes");
if (recipeInput != null) {
getToken();
var items = recipeInput.split(/\s\s/g);
console.log(items.length + " items detected");
items.forEach(removeDuplicateRecipesAndUpdateInfobox);
}
}
};
};


 
function genTable(data, tableElem, title) {
function removeDuplicateRecipesAndUpdateInfobox(recipes) {
var html = '<tr><th>'+title+'</th><th>Chance</th>';
var itemNameEnd = recipes.search("\\|");
Object.keys(data).forEach(function(value) {
var itemName = recipes.slice(0, itemNameEnd);
html += '<tr><td>'+value+'</td><td>'+percentile(data[value])+'</td>';
itemName = itemName.trim();
//Remove Itemnames if the item does not have a page on the wiki, so that the item is removed from the output
noInfobox.forEach(function(infoboxName) {
if (itemName == infoboxName) {
console.log("Removed " + itemName + " from output.");
itemName = "";
}
})
if (itemName.length == 0) {
return;
}
var recipe = getInputPara(recipes, "\\|recipe = ", 10, "recipe", itemName);
var totalRaw = getInputPara(recipes, "\\|total-raw = ", 13, "total-raw", itemName);
var expRecipe = getInputPara(recipes, "\\|expensive-recipe = ", 20, "expensive-recipe", itemName);
var expTotalRaw = getInputPara(recipes, "\\|expensive-total-raw = ", 23, "expensive-total-raw", itemName);
//remove whitespace
recipe = recipe.trim();
totalRaw = totalRaw.trim();
expRecipe = expRecipe.trim();
expTotalRaw = expTotalRaw.trim();
//remove duplicate recipes, but only if the recipe actually exists
if ((expTotalRaw == expRecipe) && (expTotalRaw.length > 0)) {
expTotalRaw = "";
console.log(itemName + ": Removed expensive-total-raw because it was a duplicate of expensive-recipe.");
} else if ((expTotalRaw == totalRaw) && (expTotalRaw.length > 0)) {
expTotalRaw = "";
console.log(itemName + ": Removed expensive-total-raw because it was a duplicate of total-raw.");
}
if ((expRecipe == recipe) && (expRecipe.length > 0)) {
expRecipe = "";
console.log(itemName + ": Removed expensive-recipe because it was a duplicate of recipe.");
}
if ((totalRaw == recipe) && (totalRaw.length > 0)) {
totalRaw = "";
console.log(itemName + ": Removed total-raw because it was a duplicate of recipe.");
}
//get page content of the item -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: itemName + '/infobox',
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
oldContent = revisions[Object.keys(revisions)[2]];
var title = pages[Object.keys(pages)[0]].title;
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
oldContent = "";
}
});
});
if (oldContent.length = 0) {
tableElem.innerHTML = html;
console.log("No " + itemName + " page found.");
};
return;
}
//find recipes in page (oldContent)
var pageRecipeStart = oldContent.search(/(\s|\|)recipe/) + 7;
var pageRecipe = getOldPara(oldContent, pageRecipeStart, 7, "recipe", itemName);
 
var pageTotalRawStart = oldContent.search(/(\s|\|)total-raw/) + 10;
var pageTotalRaw = getOldPara(oldContent, pageTotalRawStart, 10, "total-raw", itemName);
var pageExpRecipeStart = oldContent.search(/(\s|\|)expensive-recipe/) + 17;
var pageExpRecipe = getOldPara(oldContent, pageExpRecipeStart, 17, "expensive-recipe", itemName);
 
var pageExpTotalRawStart = oldContent.search(/(\s|\|)expensive-total-raw/) + 20;
var pageExpTotalRaw = getOldPara(oldContent, pageExpTotalRawStart, 20, "expensive-total-raw", itemName);
 
//remove whitespace
pageRecipe = pageRecipe.trim();
pageTotalRaw = pageTotalRaw.trim();
pageExpRecipe = pageExpRecipe.trim();
pageExpTotalRaw = pageExpTotalRaw.trim();
summary = "";
//change page if anything is different (this INCLUDES different formatting)
newContent = "";
if ((pageRecipe == recipe) && (pageTotalRaw == totalRaw) && (pageExpRecipe == expRecipe) && (pageExpTotalRaw == expTotalRaw)) {
console.log(itemName + " page was not changed.")
} else {
if (pageRecipe != recipe) {
newContent = oldContent;
var newPageRecipeStart = newContent.search(/(\s|\|)recipe/) + 7;
updatePara(newContent, recipe, pageRecipe, "recipe", newPageRecipeStart, 7, itemName);
}
if (pageTotalRaw != totalRaw) {
if (newContent.length == 0) {
newContent = oldContent
}
var newPageTotalRawStart = newContent.search(/(\s|\|)total-raw/) + 10;
updatePara(newContent, totalRaw, pageTotalRaw, "total-raw", newPageTotalRawStart, 10, itemName);
}
if (pageExpRecipe != expRecipe) {
if (newContent.length == 0) {
newContent = oldContent
}
var newPageExpRecipeStart = newContent.search(/(\s|\|)expensive-recipe/) + 17;
updatePara(newContent, expRecipe, pageExpRecipe, "expensive-recipe", newPageExpRecipeStart, 17, itemName);
}
if (pageExpTotalRaw != expTotalRaw) {
if (newContent.length == 0) {
newContent = oldContent
}
var newPageExpTotalRawStart = newContent.search(/(\s|\|)expensive-total-raw/) + 20;
updatePara(newContent, expTotalRaw, pageExpTotalRaw, "expensive-total-raw", newPageExpTotalRawStart, 20, itemName);
}
}
//alright, newContent should be defined, change page:
if (newContent.length > 0) {
editPage(itemName + "/infobox", itemName);
}
}
 
$("#ItemUpdate").click(function(){
    getItems();
});


function getItems() {
window.onload = function() {
getUserGroup();
var elem = document.getElementById("evoChecker");
if (userGroup.some(isBot) == false) {
if(elem != null) {
return;
elem.innerHTML =
}
'Alternatively, enter an evolution factor below to see the chances.<br>' +
var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies");
'<input style="margin-bottom:10px" type="number" id="evoInput" min=0 max=1 step=0.01 placeholder="Evolution" onchange="calcEvo()"></input><br>' +
if (itemInput != null) {
'<table style="float:left;margin-right:10px" class="wikitable" id="evoOutputBiter"></table>' +
getToken();
'<table class="wikitable" id="evoOutputSpitter"></table>';
var items = itemInput.split(/\s\s/g);
calcEvo();
console.log(items.length + " items detected");
items.forEach(updateItemInfoboxes);
}
}
};
};


function updateItemInfoboxes(item) {
/* Template:Inventory tooltips */
var itemNameEnd = item.search("\\|");
var itemName = item.slice(0, itemNameEnd);
itemName = itemName.trim();
//Remove items that don't have Infoboxes on the wiki
noInfobox.forEach(function(infoboxName) {
if (itemName == infoboxName) {
console.log("Removed " + itemName + " from output.");
itemName = "";
}
})
if (itemName.length == 0) {
return;
}
var consumers = getInputPara(item, "\\|consumers = ", 13, "consumers", itemName);
var stackSize = getInputPara(item, "\\|stack-size = ", 14, "stack-size", itemName);
var reqTech = getInputPara(item, "\\|required-technologies = ", 25, "required-technologies", itemName);


consumers = consumers.trim();
var lastTouchTime = 0;
stackSize = stackSize.trim();
document.addEventListener('touchstart', updateLastTouchTime, true);
reqTech = reqTech.trim();
function updateLastTouchTime() {
  lastTouchTime = new Date();
//get page content of the item -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: itemName + '/infobox',
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
oldContent = revisions[Object.keys(revisions)[2]];
var title = pages[Object.keys(pages)[0]].title;
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
oldContent = "";
}
});
if (oldContent.length = 0) {
console.log("No " + itemName + " page found.");
return;
}
//find recipes in page (oldContent)
var pageConsumersStart = oldContent.search(/(\s|\|)consumers/) + 10;
var pageConsumers = getOldPara(oldContent, pageConsumersStart, 10, "consumers", itemName);
var pageStackSizeStart = oldContent.search(/(\s|\|)stack-size/) + 11;
var pageStackSize = getOldPara(oldContent, pageStackSizeStart, 11, "stack-size", itemName);
var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22;
var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", itemName);
pageConsumers = pageConsumers.trim();
pageStackSize = pageStackSize.trim();
pageReqTech = pageReqTech.trim();
summary = "";
newContent = "";
if ((pageConsumers == consumers) && (pageStackSize == stackSize) && (pageReqTech == reqTech)) {
console.log(itemName + " page was not changed.")
} else {
if (pageConsumers != consumers) {
newContent = oldContent;
var newPageConsumersStart = newContent.search(/(\s|\|)consumers/) + 10;
updatePara(newContent, consumers, pageConsumers, "consumers", newPageConsumersStart, 10, itemName);
}
if (pageStackSize != stackSize) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageStackSizeStart = newContent.search(/(\s|\|)stack-size/) + 11;
updatePara(newContent, stackSize, pageStackSize, "stack-size", newPageStackSizeStart, 11, itemName);
}
if (pageReqTech != reqTech) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageReqTechStart = newContent.search(/(\s|\|)required-technologies/) + 22;
updatePara(newContent, reqTech, pageReqTech, "required-technologies", newPageReqTechStart, 22, itemName);
}
}
//alright, newContent should be defined, change page:
if (newContent.length > 0) {
editPage(itemName + "/infobox", itemName);
}
}
}


$("#TechUpdate").click(function(){
$(".tab-head").mousemove(function(e) {
    getTechnologies();
  if (e.buttons > 0) return;
  if (new Date() - lastTouchTime < 500) return;
  var countCssRules = document.styleSheets[0].cssRules.length;
  var newRule = '.tab-head:hover:after{display: block; left: ' + (e.offsetX + 20) + 'px; top: ' + (e.offsetY + 20) + 'px;}';
  document.styleSheets[0].insertRule(newRule, countCssRules);
});
});


function getTechnologies() {
$(".tab .factorio-icon").mousemove(function(e) {
getUserGroup();
  if (e.buttons > 0) return;
if (userGroup.some(isBot) == false) {
  if (new Date() - lastTouchTime < 500) return;
return;
  var countCssRules = document.styleSheets[0].cssRules.length;
}
  $(e.currentTarget).children("a").attr("title", "");
var techInput = prompt("Please enter the technologies");
  var text = $(e.currentTarget).children("a").children("img").attr("alt");
if (techInput != null) {
  var newRule = '.tab .factorio-icon:hover:after{display: block; ' + "content: '" + text + "'}";
getToken();
  document.styleSheets[0].insertRule(newRule, countCssRules);
var techs = techInput.split(/\s\s/g);
});
console.log(techs.length + " techs detected");
techs.forEach(updateTechnologyInfobox);
}
}


function updateTechnologyInfobox(tech) {
/* Template:BlueprintString */
var techNameEnd = tech.search("\\|");
var techName = tech.slice(0, techNameEnd);
techName = techName.trim();
var cost = getInputPara(tech, "\\|cost = ", 8, "cost", techName);
var costMulti = getInputPara(tech, "\\|cost-multiplier = ", 19, "cost-multiplier", techName);
var expCostMulti = getInputPara(tech, "\\|expensive-cost-multiplier = ", 29, "expensive-cost-multiplier", techName);
var reqTech = getInputPara(tech, "\\|required-technologies = ", 25, "required-technologies", techName);
var allows = getInputPara(tech, "\\|allows = ", 10, "allows", techName);
var effects = getInputPara(tech, "\\|effects = ", 11, "effects", techName);


$(".bps-box").click(function(event) {
cost = cost.trim();
var copyTarget = document.createElement("input");
costMulti = costMulti.trim();
copyTarget.setAttribute("value", $( event.target ).children("p").html());
expCostMulti = expCostMulti.trim();
document.body.appendChild(copyTarget);
reqTech = reqTech.trim();
copyTarget.select();
allows = allows.trim();
document.execCommand("copy");
effects = effects.trim();
document.body.removeChild(copyTarget);
});
//get page content of the tech -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: techName + ' (research)/infobox',
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
oldContent = revisions[Object.keys(revisions)[2]];
var title = pages[Object.keys(pages)[0]].title;
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
oldContent = "";
}
});
if (oldContent.length = 0) {
console.log("No " + techName + " page found.");
return;
}
//find costs etc in page (oldContent)
var pageCostStart = oldContent.search(/(\s|\|)cost(\s|=)/) + 6;
var pageCost = getOldPara(oldContent, pageCostStart, 6, "cost", techName);
var pageCostMultiStart = oldContent.search(/(\s|\|)cost-multiplier/) + 16;
var pageCostMulti = getOldPara(oldContent, pageCostMultiStart, 16, "cost-multiplier", techName);
var pageExpCostMultiStart = oldContent.search(/(\s|\|)expensive-cost-multiplier/) + 26;
var pageExpCostMulti = getOldPara(oldContent, pageExpCostMultiStart, 26, "expensive-cost-multiplier", techName);
var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22;
var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", techName);
var pageAllowsStart = oldContent.search(/(\s|\|)allows/) + 7;
var pageAllows = getOldPara(oldContent, pageAllowsStart, 7, "allows", techName);
var pageEffectsStart = oldContent.search(/(\s|\|)effects/) + 8;
var pageEffects = getOldPara(oldContent, pageEffectsStart, 8, "effects", techName);


/* Template:Inventory */
pageCost = pageCost.trim();
pageCostMulti = pageCostMulti.trim();
pageExpCostMulti = pageExpCostMulti.trim();
pageReqTech = pageReqTech.trim();
pageAllows = pageAllows.trim();
pageEffects = pageEffects.trim();
summary = "";
newContent = "";
//change page if anything is different (this INCLUDES different formatting)
if ((pageCost == cost) && (pageCostMulti == costMulti) && (pageExpCostMulti == expCostMulti) && (pageReqTech == reqTech) && (pageAllows == allows) && (pageEffects == effects)) {
console.log(techName + " page was not changed.")
} else {
if (pageCost != cost) {
newContent = oldContent;
var newPageCostStart = newContent.search(/(\s|\|)cost(\s|=)/) + 6;
updatePara(newContent, cost, pageCost, "cost", newPageCostStart, 6, techName);
}
if (pageCostMulti != costMulti) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageCostMultiStart = newContent.search(/(\s|\|)cost-multiplier/) + 16;
updatePara(newContent, costMulti, pageCostMulti, "cost-multiplier", newPageCostMultiStart, 16, techName);
}
if (pageExpCostMulti != expCostMulti) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageExpCostMultiStart = newContent.search(/(\s|\|)expensive-cost-multiplier/) + 26;
updatePara(newContent, expCostMulti, pageExpCostMulti, "expensive-cost-multiplier", newPageExpCostMultiStart, 26, techName);
}
if (pageReqTech != reqTech) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageReqTechStart = newContent.search(/(\s|\|)required-technologies/) + 22;
updatePara(newContent, reqTech, pageReqTech, "required-technologies", newPageReqTechStart, 22, techName);
}
if (pageAllows != allows) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageAllowsStart = newContent.search(/(\s|\|)allows/) + 7;
updatePara(newContent, allows, pageAllows, "allows", newPageAllowsStart, 7, techName);
}
if (pageEffects != effects) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPageEffectsStart = newContent.search(/(\s|\|)effects/) + 8;
updatePara(newContent, effects, pageEffects, "effects", newPageEffectsStart, 8, techName);
}
}
//alright, newContent should be defined, change page:
if (newContent.length > 0) {
editPage(techName + " (research)/infobox", techName);
}
}


$("#AllUpdate").click(function(){
$(".tab-head-1").click(function() {
    getAll();
$(".tab-head").removeClass("tab-head-active");
$(this).addClass("tab-head-active");
$(".tab").hide();
$(".tab-1").show();
});
});


function getAll() {
$(".tab-head-2").click(function() {
getUserGroup();
$(".tab-head").removeClass("tab-head-active");
if (userGroup.some(isBot) == false) {
$(this).addClass("tab-head-active");
return;
$(".tab").hide();
}
$(".tab-2").show();
getToken();
newVersion = prompt("Please enter the new version.");
if (newVersion != null) {
version = newVersion.trim();
}
var recipeInput = prompt("Please enter the recipes.");
var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies.");
var techInput = prompt("Please enter the technologies.");
if (recipeInput != null) {
var recipeArray = recipeInput.split(/\s\s/g);
console.log(recipeArray.length + " recipes detected");
recipeArray.forEach(removeDuplicateRecipesAndUpdateInfobox);
}
if (itemInput != null) {
var items = itemInput.split(/\s\s/g);
console.log(items.length + " items detected");
items.forEach(updateItemInfoboxes);
}
if (techInput != null) {
var techs = techInput.split(/\s\s/g);
console.log(techs.length + " techs detected");
techs.forEach(updateTechnologyInfobox);
}
}
 
$("#TechDataUpdate").click(function(){
    getTechData();
});
});


function getTechData() {
$(".tab-head-3").click(function() {
getUserGroup();
$(".tab-head").removeClass("tab-head-active");
if (userGroup.some(isBot) == false) {
$(this).addClass("tab-head-active");
return;
$(".tab").hide();
}
$(".tab-3").show();
var techInput = prompt("Please enter the technology internal-names.");
if (techInput != null) {
getToken();
var techs = techInput.split(/\s\s/g);
console.log(techs.length + " techs detected");
techs.forEach(updateTechnologyDataInfobox);
}
}
 
function updateTechnologyDataInfobox(tech) {
var techNameEnd = tech.search("\\|");
var techName = tech.slice(0, techNameEnd);
techName = techName.trim();
var internalName = getInputPara(tech, "\\|internal-name = ", 17, "internal-name", techName);
internalName = internalName.trim();
var prototypeType = "technology";
//get page content of the tech -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: techName + ' (research)/infobox',
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
oldContent = revisions[Object.keys(revisions)[2]];
var title = pages[Object.keys(pages)[0]].title;
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
oldContent = "";
}
});
if (oldContent.length = 0) {
console.log("No " + techName + " page found.");
return;
}
var pageInternalNameStart = oldContent.search(/(\s|\|)internal-name/) + 14;
var pageInternalName = getOldPara(oldContent, pageInternalNameStart, 14, "internal-name", techName);
var pagePrototypeTypeStart = oldContent.search(/(\s|\|)prototype-type/) + 15;
var pagePrototypeType = getOldPara(oldContent, pagePrototypeTypeStart, 15, "prototype-type", techName);
pageInternalName = pageInternalName.trim();
pagePrototypeType = pagePrototypeType.trim();
summary = "";
newContent = "";
if ((pageInternalName == internalName) && (pagePrototypeType == prototypeType)) {
console.log(techName + " page was not changed.")
} else {
if (pageInternalName != internalName) {
newContent = oldContent;
var newInternalNameStart = newContent.search(/(\s|\|)internal-name/) + 14;
updatePara(newContent, internalName, pageInternalName, "internal-name", newInternalNameStart, 14, techName);
}
if (pagePrototypeType != prototypeType) {
if (newContent.length == 0) {
newContent = oldContent;
}
var newPrototypeTypeStart = newContent.search(/(\s|\|)prototype-type/) + 15;
updatePara(newContent, prototypeType, pagePrototypeType, "prototype-type", newPrototypeTypeStart, 15, techName);
}
}
//alright, newContent should be defined, change page:
if (newContent.length > 0) {
editPage(techName + " (research)/infobox", techName);
}
}
 
$("#InternalItemNameUpdate").click(function(){
    getInternalItemNames();
});
});


function getInternalItemNames() {
$(".tab-head-4").click(function() {
getUserGroup();
$(".tab-head").removeClass("tab-head-active");
if (userGroup.some(isBot) == false) {
$(this).addClass("tab-head-active");
return;
$(".tab").hide();
}
$(".tab-4").show();
var nameInput = prompt("Please enter the item and recipe internal-names.");
if (nameInput != null) {
getToken();
var names = nameInput.split(/\s\s/g);
console.log(names.length + " names detected");
names.forEach(updateInternalItemNameInInfobox);
}
}
 
function updateInternalItemNameInInfobox(name) {
var itemNameEnd = name.search("\\|");
var itemName = name.slice(0, itemNameEnd).trim();
//Remove items that don't have Infoboxes on the wiki
noInfobox.forEach(function(infoboxName) {
if (itemName == infoboxName) {
console.log("Removed " + itemName + " from output.");
itemName = "";
}
})
if (itemName.length == 0) return;
var internalName = getInputPara(name, "\\|internal-name = ", 17, "internal-name", itemName).trim();
//get page content of the item -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: itemName + '/infobox',
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
oldContent = revisions[Object.keys(revisions)[2]];
var title = pages[Object.keys(pages)[0]].title;
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
if (oldContent.length = 0) {
console.log("No " + itemName + " page found.");
return;
}
var pageInternalNameStart = oldContent.search(/(\s|\|)internal-name/) + 14;
var pageInternalName = getOldPara(oldContent, pageInternalNameStart, 14, "internal-name", itemName).trim();
 
summary = "";
newContent = "";
if (pageInternalName == internalName) {
console.log(itemName + " page was not changed.")
} else {
var newInternalNameStart = oldContent.search(/(\s|\|)internal-name/) + 14;
updatePara(oldContent, internalName, pageInternalName, "internal-name", newInternalNameStart, 14, itemName);
editPage(itemName + "/infobox", itemName);
}
}
$("#ProtypeTypeUpdate").click(function(){
    getProtypeTypes();
});
});


function getProtypeTypes() {
//*** Language template ***//
getUserGroup();
if($(".languages-flags .flag").length == 0) {
if (userGroup.some(isBot) == false) {
console.log("Not showing languages bar because there's no other language's version of this page.");
return;
$(".languages-container").hide();
}
var typeInput = prompt("Please enter the prototype-types of everything except technologies.");
if (typeInput != null) {
getToken();
var types = typeInput.split(/\s\s/g);
console.log(types.length + " types detected");
types.forEach(updateProtypeTypeInInfobox);
}
}
}


function updateProtypeTypeInInfobox(type) {
//Spoiler template
var itemNameEnd = type.search("\\|");
$(".spoiler-container .button").click(function() {
var itemName = type.slice(0, itemNameEnd).trim();
$(this).siblings(".text").toggle("slow");
//Remove items that don't have Infoboxes on the wiki
if (itemName == "Raw fish") {
console.log("Removed Raw fish from output.");
return;
}
noInfobox.forEach(function(infoboxName) {
if (itemName == infoboxName) {
console.log("Removed " + itemName + " from output.");
itemName = "";
}
})
if (itemName.length == 0) return;
var prototypeType = getInputPara(type, "\\|prototype-type = ", 18, "prototype-type", itemName).trim();
//get page content of the item -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: itemName + '/infobox',
prop: 'revisions',
rvprop: 'content'
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var pages = data.query.pages;
var revisions = pages[Object.keys(pages)[0]].revisions[0];
oldContent = revisions[Object.keys(revisions)[2]];
var title = pages[Object.keys(pages)[0]].title;
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
if (oldContent.length = 0) {
console.log("No " + itemName + " page found.");
return;
}
var pagePrototypeTypeStart = oldContent.search(/(\s|\|)prototype-type/) + 15;
var pagePrototypeType = getOldPara(oldContent, pagePrototypeTypeStart, 15, "prototype-type", itemName).trim();
 
summary = "";
newContent = "";
if (pagePrototypeType == prototypeType) {
console.log(itemName + " page was not changed.")
} else {
var newPrototypeTypeStart = oldContent.search(/(\s|\|)prototype-type/) + 15;
updatePara(oldContent, prototypeType, pagePrototypeType, "prototype-type", newPrototypeTypeStart, 15, itemName);
editPage(itemName + "/infobox", itemName);
}
}
 
/* get the number of users who ever made an edit, for conveniece */
 
$("#GetEditingUsers").click(function(){
    getNumberOfUsersWhoMadeEdits();
});
});


function getNumberOfUsersWhoMadeEdits() {
//* General/generic functions *//
getUserGroup();
    if (userGroup.some(isBot) == false) {
      return;
    }
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
list: 'allusers',
aulimit: 5000,
                        auwitheditsonly: true
},
async: false,
dataType: 'json',
type: 'GET',
success: function( data ) {
var allusers = data.query.allusers;
var numberOfUsersWhoMadeEdits = data.query.allusers.length;
console.log(numberOfUsersWhoMadeEdits)
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
};
 


/* User is bot if userGroup.some(isBot) == true */
/* User is bot if userGroup.some(isBot) == true */
Line 873: Line 213:
function getUserGroup() {
function getUserGroup() {
$.ajax({
$.ajax({
url: 'https://wiki.factorio.com/api.php',
url: apiUrl,
data: {
data: {
format: 'json',
format: 'json',
Line 893: Line 233:
}
}


/* Get token of this session */


var globalToken;
var globalToken;
Line 898: Line 239:
function getToken() {
function getToken() {
     $.ajax({
     $.ajax({
         url: 'https://wiki.factorio.com/api.php',
         url: apiUrl,
         data: {
         data: {
             format: 'json',
             format: 'json',
Line 917: Line 258:
}
}


/* Infobox more info in header */


$(".more-button").click(function() {
function genericEditPage(title, content, summary) {
  $(".more-content").toggle("fast");
$.ajax({
});
url: apiUrl,
data: {
format: 'json',
action: 'edit',
title: title,
text: content,
token: globalToken,
summary: summary,
bot: true,
nocreate: true
},
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Edited " + title);
},
error: function( xhr ) {
alert("Failed to edit " + title);
}
});
};


/* Template:Inventory */


$(".tab-head-1").click(function() {
function createPage(pageTitle, content, summary) {
$(".tab-head").removeClass("tab-head-active");
    $.ajax({
$(this).addClass("tab-head-active");
        url: apiUrl,
         $(".tab").hide();
         data: {
$(".tab-1").show();
format: 'json',
});
action: 'edit',
 
title: pageTitle,
$(".tab-head-2").click(function() {
text: content,
$(".tab-head").removeClass("tab-head-active");
token: globalToken,
$(this).addClass("tab-head-active");
summary: summary,
$(".tab").hide();
bot: true
$(".tab-2").show();
        },
});
        async: false,
 
        dataType: 'json',
$(".tab-head-3").click(function() {
        type: 'POST',
$(".tab-head").removeClass("tab-head-active");
        success: function( data ) {
$(this).addClass("tab-head-active");
console.log("Created page: " + pageTitle);
$(".tab").hide();
        },
$(".tab-3").show();
        error: function( xhr ) {
});
console.log("Failed to create page");
 
        }
$(".tab-head-4").click(function() {
    });
$(".tab-head").removeClass("tab-head-active");
$(this).addClass("tab-head-active");
$(".tab").hide();
$(".tab-4").show();
});
 
/*** Language template ***/
if($(".languages-flags .flag").length == 0) {
console.log("Not showing languages bar because there's no other language's version of this page.");
$(".languages-container").hide();
}
}


//Spoiler template JavaScript
function getBacklinks(page) {
$(".spoiler-container .button").click(function() {
var backlinks = [];
  $(this).siblings(".text").toggle("slow");
$.ajax({
});
url: apiUrl,
data: {
format: 'json',
action: 'query',
list: 'backlinks',
bltitle: page,
bllimit: 1000,
},
async: false,
type: 'GET',
success: function( data ) {
backlinks = data.query.backlinks;
},
error: function( xhr ) {
alert( 'Error: Backlinks request failed.' );
}
});
return backlinks;
};


var wantedPagesListsLocation = "User:TheWombatGuru/WantedPages"
function getFileUsage(file) {
$("#create-wanted-pages-list").click(function(){
var imageusage = [];
    getToken();
$.ajax({
    createWantedPagesLists();
url: apiUrl,
});
data: {
 
format: 'json',
function createWantedPagesLists() {
action: 'query',
    getUserGroup();
list: 'imageusage',
    if (userGroup.some(isBot) == false) {
iutitle: file,
      return;
iulimit: 1000
    }
},
    var wantedPages = getWantedPages();
async: false,
    wantedPages = wantedPages.sort(compare);
type: 'GET',
    splitWantedPagesIntoDifferentLanguages(wantedPages);
success: function( data ) {
imageusage = data.query.imageusage;
},
error: function( xhr ) {
alert( 'Error: Imageusage request failed.' );
}
});
return imageusage;
};
};
function splitWantedPagesIntoDifferentLanguages(wantedPages) {
  var czechWantedPages = [];
  var germanWantedPages = [];
  var spanishWantedPages = [];
  var frenchWantedPages = [];
  var italianWantedPages = [];
  var japaneseWantedPages = [];
  var dutchWantedPages = [];
  var polishWantedPages = [];
  var portugueseWantedPages = [];
  var russianWantedPages = [];
  var swedishWantedPages = [];
  var ukrainianWantedPages = [];
  var chineseWantedPages = [];
  var turkishWantedPages = [];
  var wantedFiles = [];
  var wantedTemplates = [];
  var otherWantedPages = [];
  for (var i = 0; i < wantedPages.length; i++) {
    switch (wantedPages[i].title.slice(-3)) {//"/cs", "/de", "/es", "/fr", "/it", "/ja", "/nl", "/pl", "/-br", "/ru", "/sv", "/uk", "/zh", "/tr"
      case "/cs": czechWantedPages.push(wantedPages[i]); break;
      case "/de": germanWantedPages.push(wantedPages[i]); break;
      case "/es": spanishWantedPages.push(wantedPages[i]); break;
      case "/fr": frenchWantedPages.push(wantedPages[i]); break;
      case "/it": italianWantedPages.push(wantedPages[i]); break;
      case "/ja": japaneseWantedPages.push(wantedPages[i]); break;
      case "/nl": dutchWantedPages.push(wantedPages[i]); break;
      case "/pl": polishWantedPages.push(wantedPages[i]); break;
      case "-br": portugueseWantedPages.push(wantedPages[i]); break;
      case "/ru": russianWantedPages.push(wantedPages[i]); break;
      case "/sv": swedishWantedPages.push(wantedPages[i]); break;
      case "/uk": ukrainianWantedPages.push(wantedPages[i]); break;
      case "/zh": chineseWantedPages.push(wantedPages[i]); break;
      case "/tr": turkishWantedPages.push(wantedPages[i]); break;
      default: if (wantedPages[i].title.slice(0, 5) == "File:") {wantedFiles.push(wantedPages[i])} else if (wantedPages[i].title.slice(0, 9) == "Template:") {wantedTemplates.push(wantedPages[i])} else {otherWantedPages.push(wantedPages[i])}; break;
    }
  }
  createWantedPagesPage("cs", czechWantedPages, "Czech");
  createWantedPagesPage("de", germanWantedPages, "German");
  createWantedPagesPage("es", spanishWantedPages, "Spanish");
  createWantedPagesPage("fr", frenchWantedPages, "French");
  createWantedPagesPage("it", italianWantedPages, "Italian");
  createWantedPagesPage("ja", japaneseWantedPages, "Japanese");
  createWantedPagesPage("nl", dutchWantedPages, "Dutch");
  createWantedPagesPage("pl", polishWantedPages, "Polish");
  createWantedPagesPage("pt-br", portugueseWantedPages, "Portuguese");
  createWantedPagesPage("ru", russianWantedPages, "Russian");
  createWantedPagesPage("sv", swedishWantedPages, "Swedish");
  createWantedPagesPage("uk", ukrainianWantedPages, "Ukrainian");
  createWantedPagesPage("zh", chineseWantedPages, "Chinese");
  createWantedPagesPage("tr", turkishWantedPages, "Turkish");
  createWantedPagesPage("file", wantedFiles, "Files");
  createWantedPagesPage("template", wantedTemplates, "Templates");
  createWantedPagesPage("other", otherWantedPages, "Other");
}
function createWantedPagesPage(location, wantedPages, language) {
  var formattedWantedPages = "Number of wanted pages in " + language + ": " + wantedPages.length + "\n{|class=wikitable\n!#\n!Page\n!Links to this page";
  for (var i = 0; i < wantedPages.length; i++) {
    formattedWantedPages = formattedWantedPages.concat("\n|-\n|" + (i + 1) + "\n|[https://wiki.factorio.com/index.php?title=" + encodeURI(wantedPages[i].title) + " " + wantedPages[i].title + "]\n|[https://wiki.factorio.com/index.php?title=Special:WhatLinksHere/" + encodeURI(wantedPages[i].title) + " " + wantedPages[i].value + "]");
  }
  formattedWantedPages = formattedWantedPages.concat("\n|}");
  createPage(wantedPagesListsLocation + "/" + location, formattedWantedPages, "(BOT) - Update the list of wanted pages for " + language + ".");
}


function performNullEdit(pageTitle, summary) {
function performNullEdit(pageTitle, summary) {
     $.ajax({
     $.ajax({
         url: 'https://wiki.factorio.com/api.php',
         url: apiUrl,
         data: {
         data: {
             format: 'json',
             format: 'json',
Line 1,078: Line 381:
function purgeWhatLinksHere(pageTitle) {
function purgeWhatLinksHere(pageTitle) {
     $.ajax({
     $.ajax({
         url: 'https://wiki.factorio.com/api.php',
         url: apiUrl,
         data: {
         data: {
             format: "json",
             format: "json",
Line 1,103: Line 406:
function purgePage(pageTitle) {
function purgePage(pageTitle) {
     $.ajax({
     $.ajax({
         url: 'https://wiki.factorio.com/api.php',
         url: apiUrl,
         data: {
         data: {
             action: 'purge',
             action: 'purge',
Line 1,121: Line 424:
     });
     });
}
}
function compare(a,b) {
  if (parseInt(a.value) > parseInt(b.value))
    return -1;
  if (parseInt(a.value) < parseInt(b.value))
    return 1;
  if (a.title < b.title)
    return -1;
  if (a.title > b.title)
    return 1;
  return 0;
}
function createPage(pageTitle, content, summary) {
    $.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'edit',
            title: pageTitle,
            text: content,
            token: globalToken,
            summary: summary,
            bot: true
        },
        async: false,
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
          console.log("created page");
        },
        error: function( xhr ) {
            console.log("failed to create page");
        }
    });
}
function getWantedPages() {
  var wantedPages = [];
  $.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'query',
            list: 'querypage',
            qppage: 'Wantedpages',
            qplimit: '5000',
        },
        async: false,
        dataType: 'json',
        type: 'GET',
        success: function( data ) {
            var results = data.query.querypage.results;
            for (var i = 0; i < results.length; i++) {
              var pageObject = new WantedPage(results[i].title, results[i].value);
              var alreadyInArray = false;
              for (var j = 0; j < wantedPages.length; j++) {
                if (wantedPages[j].title == pageObject.title) {
                    alreadyInArray = true;
                }
              }
              if (!alreadyInArray) {
                wantedPages.push(pageObject);
              }
              if (pageObject.title == "Rocket defense/it") {
              }
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed. Category' );
        }
  });
$.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'query',
            list: 'querypage',
            qppage: 'Wantedpages',
            qplimit: '5000',
            qpoffset: '3000',
        },
        async: false,
        dataType: 'json',
        type: 'GET',
        success: function( data ) {
            var results = data.query.querypage.results;
            for (var i = 0; i < results.length; i++) {
              var pageObject = new WantedPage(results[i].title, results[i].value);
              var alreadyInArray = false;
              for (var j = 0; j < wantedPages.length; j++) {
                if (wantedPages[j].title == pageObject.title) {
                    alreadyInArray = true;
                }
              }
              if (!alreadyInArray) {
                wantedPages.push(pageObject);
              }
              if (pageObject.title == "Rocket defense/it") {
              }
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed. Category' );
        }
  });
  return wantedPages;
};
function WantedPage(pageTitle, pageValue) {
  this.title = pageTitle;
  this.value = pageValue;
}
/* OLD INFOBOX CONVERSION TOOLS */
/*function targetAllPagesInCategory(category) {
    var languageSuffixes = ["/fr", "/ru", "/de"]; //, "/cs", "/de", "/es", "/fr", "/it", "/nl", "/pl", "/pt-br", "/ru", "/sv", "/uk", "/zh", ""];
    for (var j = 0; j < languageSuffixes.length; j++) {
      $.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'query',
            list: 'categorymembers',
            cmtitle: (category + languageSuffixes[j]),
            cmlimit: 500
        },
        dataType: 'json',
        type: 'GET',
        success: function( data ) {
            var pages = data.query.categorymembers;
            for (var i = 0; i < pages.length; i++) {
                    //purgePage(pages[i].title);
                    extractPageInfo(pages[i].title, "9c28a1344a20bf189fda7d58339e518257f2dd9b+\\");
               
            }
            if ( data && data.query && data.query.result == 'Success' ) {
                window.location.reload(); // reload page if edit was successful
            } else if ( data && data.error ) {
                //alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info + 'Category' );
            } else {
                //alert( 'Error: Unknown result from API. Category' );
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed. Category' );
        }
    });
  }
}
function extractPageInfo(pageTitle, token) {
    $.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'query',
            titles: pageTitle,
            prop: 'revisions',
            rvprop: 'content'
        },
        async: false,
        dataType: 'json',
        type: 'GET',
        success: function( data ) {
            var pages = data.query.pages;
            var revisions = pages[Object.keys(pages)[0]].revisions[0];
            var content = revisions[Object.keys(revisions)[2]]
            var title = pages[Object.keys(pages)[0]].title;
            createNewInfoboxPage(title, content, token);
            if ( data && data.query && data.query.result == 'Success' ) {
                window.location.reload(); // reload page if edit was successful
            } else if ( data && data.error ) {
                //alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
            } else {
                //alert( 'Error: Unknown result from API.' );
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed.' );
        }
    });
}
function createNewInfoboxPage(page, contentOfMainPage, token) {
    var infoboxText = getInfoboxFromFullPageContent(contentOfMainPage);
    var infoboxPageTitle = page.replace(/\/(de|fr|nl|it|es|ru|pt\-br|cs|pl|sv|uk|zh)/g, function(piece) {return "";}).concat("/infobox");
    var oldPageRevisedText = getOldPageRevisedText(page, contentOfMainPage, infoboxPageTitle);
    removeInfoboxFromMain(page, oldPageRevisedText, token);
    if (infoboxText != null) {
      if (/\/(de|fr|nl|it|es|ru|pt\-br|cs|pl|sv|uk|zh)/g.test(page)) {
        return;
      }
      var newPageTitle = page.concat("/infobox");
      var convertedInfoboxText = convertInfobox(infoboxText, token);
      createPage(newPageTitle, convertedInfoboxText, token, page, contentOfMainPage);
    }
}
function getOldPageRevisedText(pageTitle, content, infoboxPageTitle) {
    content = content.replace(/{{\bCombat\b(\s+(\||{).+)+\s}}/gi, function (piece) {
        return "{{:" + infoboxPageTitle + "}}";
    });
    return content;
}
function removeInfoboxFromMain(pageTitle, content, token) {
    $.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'edit',
            title: pageTitle,
            text: content,
            bot: true,
            token: token,
            summary: "(BOT) - Replaced old infobox with a link to the /infobox subpage"
        },
        async: false,
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
            if ( data && data.edit && data.edit.result == 'Success' ) {
                window.location.reload(); // reload page if edit was successful
            } else if ( data && data.error ) {
                //alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
            } else {
                //alert( 'Error: Unknown result from API.' );
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed.' );
        }
    })
};
function getInfoboxFromFullPageContent(contentOfPage) {
    var matches = contentOfPage.match(/{{\bCombat\b(\s+\|.+)+\s}}/gi);
if (matches != null && matches.length > 0) {
infoboxText = matches[0];
} else {
infoboxText = null;
}
    return infoboxText;
}
function convertInfobox(text) {
    text = text.replace(/{{(\w+)/g, function (piece, $1) {
        var returnText = "{{Infobox\n| category = ";
        $1 = $1.toLowerCase();
        switch ($1) {
            case "item":
                returnText = returnText.concat("Items");
                break;
            case "machinery":
                returnText = returnText.concat("Machinery");
                break;
            case "combat":
                returnText = returnText.concat("Combat");
                break;
            case "technology":
                returnText = returnText.concat("Technology");
        }
        switch ($1) {
            case "machinery":
                returnText = returnText.concat("\n| category-name = Machine");
                break;
            case "item":
                returnText = returnText.concat("\n| category-name = Item");
                break;
        }
        return returnText;
    });
    text = text.replace("stack_size", "stack-size");
    text = text.replace("poweroutput", "power-output");
    text = text.replace("input", "recipe");
    text = text.replace("raw", "total-raw");
    text = text.replace("technologies", "required-technologies");
    text = text.replace("costmultiplier", "cost-multiplier");
    text = text.replace("requirements", "required-technologies");
    text = text.replace("walkingspeed", "walking-speed");
    text = text.replace("storagesize", "storage-size");
    text = text.replace("gridsize", "grid-size");
    text = text.replace("shootingspeed", "shooting-speed");
    text = text.replace("damagebonus", "damage-bonus");
    text = text.replace("clustersize", "cluster-size");
    text = text.replace("aoesize", "area-of-effect-size");
    text = text.replace("magazinesize", "magazine-size");
    text = text.replace("recharge", "robot-recharge-rate");
    text = text.replace("rechargebuffer", "internal-buffer-recharge-rate");
    text = text.replace("wirereach", "wire-reach");
    text = text.replace("craftingspeed", "crafting-speed");
    text = text.replace("smeltingspeed", "smelting-speed");
    text = text.replace("miningpower", "mining-power");
    text = text.replace("miningspeed", "mining-speed");
    text = text.replace("miningarea", "mining-area");
    text = text.replace("supplyarea", "supply-area");
    text = text.replace("constructionarea", "construction-area");
    text = text.replace("lifetime", "lifespan");
    text = text.replace("inventorysizebonus", "inventory-size-bonus");
    text = text.replace("gridsize", "grid-size");
    text = text.replace("boosttechs", "boosting-technologies");
    text = text.replace("allowstech", "allows");
    text = text.replace("storage", "storage-size");
    text = text.replace(/\|\s*\brecipe\b\s*=\s*(.+)\n\|\s*\boutput\b\s*=\s*(.+)/g, function (piece, $1, $2) {
        return "| recipe = " + $1 + " = " + $2;
    });
    text = text.concat("<noinclude>[[Category:Infobox page]]</noinclude>");
    return text;
}
function createPage(pageTitle, content, token, page, contentOfMainPage) {
    $.ajax({
        url: 'https://wiki.factorio.com/api.php',
        data: {
            format: 'json',
            action: 'edit',
            title: pageTitle,
            text: content,
            bot: true,
            createonly: true,
            token: token,
            summary: "(BOT) - Created infobox sub page for " + page
        },
        async: false,
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
         
            if ( data && data.edit && data.edit.result == 'Success' ) {
                window.location.reload(); // reload page if edit was successful
            } else if ( data && data.error ) {
                //alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
            } else {
                //alert( 'Error: Unknown result from API.' );
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed.' );
        }
    });
}*/
/* END OF OLD INFOBOX CONVERSION TOOL */

Latest revision as of 12:24, 20 February 2020

/* Any JavaScript here will be loaded for all users on every page load. */

var apiUrl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/api.php"

/* Script in here will be executed when the page is "ready" */
$(document).ready(getNumberOfActiveUsers);

/* show correct number of active users on the main page */

function getNumberOfActiveUsers() {
	if (document.getElementById("active-users")) {
		$.ajax({
			url: apiUrl,
			data: {
				format: 'json',
				action: 'query',
				list: 'allusers',
				aulimit: 500,
				auactiveusers: true
			},
			dataType: 'json',
			type: 'GET',
			success: function(data) {
				document.getElementById("active-users").innerHTML = data.query.allusers.length.toString();
			},
			error: function(xhr) {
				console.log( 'Error: Request failed.' );
			}
		});
	}
};

/* Collapsible list on [[Prototype definitions]] */

$(".prototype-tree li").each(function (index, element) {
  if ( $( element ).children("ul").length > 0 ) {
    $( element ).addClass("prototype-tree-parent");
  };
});

$(".prototype-tree li.prototype-tree-parent").on("click", function(event) {
 // only collapse if I clicked a li or the direct child of a li that is not another list or a link
  if ( $( event.currentTarget ).children("ul").length > 0 && $( event.target ).get(0).tagName != "UL" && $( event.target ).get(0).tagName != "A" && ($( event.target ).hasClass("prototype-tree-parent") || $( event.target ).parent().hasClass("prototype-tree-parent")) ) {
    $( event.currentTarget ).children("ul")[0].classList.toggle("hidden");
    $( event.currentTarget ).toggleClass("prototype-tree-parent-collapsed");
    return false; // prevents further event bubbling
  }
});

/* Evolution calc */

//values are in the form [evolution, weight]
const biterSpawner = [
	["Small Biter",    [[0.0, 0.3], [0.6, 0.0]]],
	["Medium Biter",   [[0.2, 0.0], [0.6, 0.3], [0.7, 0.1]]],
	["Big Biter",      [[0.5, 0.0], [1.0, 0.4]]],
	["Behemoth Biter", [[0.9, 0.0], [1.0, 0.3]]]
];
const spitterSpawner = [
	["Small Biter",      [[0.0, 0.3], [0.35, 0.0]]],
	["Small Spitter",    [[0.25, 0.0], [0.5, 0.3], [0.7, 0.0]]],
	["Medium Spitter",   [[0.4, 0.0], [0.7, 0.3], [0.9, 0.1]]],
	["Big Spitter",      [[0.5, 0.0], [1.0, 0.4]]],
	["Behemoth Spitter", [[0.9, 0.0], [1.0, 0.3]]]
];

//calculates the interpolated value
function lerp(low, high, pos) {
	const s = high[0] - low[0];
	const l = (pos - low[0]) / s;
	return (low[1] * (1-l)) + (high[1] * l);
};

//gets the weight list
function getValues(map, evo) {
	var result = {};
	var sum = 0;
	map.forEach(function(data) {
		const list = data[1];
		var low = list[0];
		var high = list[list.length-1];
		list.forEach(function(val) {
			if(val[0] <= evo && val[0] >  low[0])  low = val;
			if(val[0] >= evo && val[0] < high[0]) high = val;
		});
		var val = null;
		if(evo <= low[0]) val = low[1];
		else if(evo >= high[0]) val = high[1];
		else val = lerp(low, high, evo);
		sum += val;
		result[data[0]] = val;
	});
	Object.keys(result).forEach(function(data, index) {
		result[data] = result[data] / sum;
	});
	return result;
};

function calcEvo() {
	const evo = document.getElementById("evoInput").value;
	genTable(getValues(biterSpawner, evo), document.getElementById("evoOutputBiter"), "Biter's Nest");
	genTable(getValues(spitterSpawner, evo), document.getElementById("evoOutputSpitter"), "Spitter's Nest");
};

function percentile(value) {
	return (value*100).toFixed(2) + "%";
};

function genTable(data, tableElem, title) {
	var html = '<tr><th>'+title+'</th><th>Chance</th>';
	Object.keys(data).forEach(function(value) {
		html += '<tr><td>'+value+'</td><td>'+percentile(data[value])+'</td>';
	});
	tableElem.innerHTML = html;
};

window.onload = function() {
	var elem = document.getElementById("evoChecker");
	if(elem != null) {
		elem.innerHTML =
		'Alternatively, enter an evolution factor below to see the chances.<br>' +
		'<input style="margin-bottom:10px" type="number" id="evoInput" min=0 max=1 step=0.01 placeholder="Evolution" onchange="calcEvo()"></input><br>' +
		'<table style="float:left;margin-right:10px" class="wikitable" id="evoOutputBiter"></table>' +
		'<table class="wikitable" id="evoOutputSpitter"></table>';
		calcEvo();
	}
};

/* Template:Inventory tooltips */

var lastTouchTime = 0;
document.addEventListener('touchstart', updateLastTouchTime, true);
function updateLastTouchTime() {
  lastTouchTime = new Date();
}

$(".tab-head").mousemove(function(e) {
  if (e.buttons > 0) return;
  if (new Date() - lastTouchTime < 500) return;
  var countCssRules = document.styleSheets[0].cssRules.length;
  var newRule = '.tab-head:hover:after{display: block; left: ' + (e.offsetX + 20) + 'px; top: ' + (e.offsetY + 20) + 'px;}';
  document.styleSheets[0].insertRule(newRule, countCssRules);
});

$(".tab .factorio-icon").mousemove(function(e) {
  if (e.buttons > 0) return;
  if (new Date() - lastTouchTime < 500) return;
  var countCssRules = document.styleSheets[0].cssRules.length;
  $(e.currentTarget).children("a").attr("title", "");
  var text = $(e.currentTarget).children("a").children("img").attr("alt");
  var newRule = '.tab .factorio-icon:hover:after{display: block; ' + "content: '" + text + "'}";
  document.styleSheets[0].insertRule(newRule, countCssRules);
});

/* Template:BlueprintString */

$(".bps-box").click(function(event) {
	var copyTarget = document.createElement("input");
	copyTarget.setAttribute("value", $( event.target ).children("p").html());
	document.body.appendChild(copyTarget);
	copyTarget.select();
	document.execCommand("copy");
	document.body.removeChild(copyTarget);
});

/* Template:Inventory */

$(".tab-head-1").click(function() {
	$(".tab-head").removeClass("tab-head-active");
	$(this).addClass("tab-head-active");
	$(".tab").hide();
	$(".tab-1").show();
});

$(".tab-head-2").click(function() {
	$(".tab-head").removeClass("tab-head-active");
	$(this).addClass("tab-head-active");
	$(".tab").hide();
	$(".tab-2").show();
});

$(".tab-head-3").click(function() {
	$(".tab-head").removeClass("tab-head-active");
	$(this).addClass("tab-head-active");
	$(".tab").hide();
	$(".tab-3").show();
});

$(".tab-head-4").click(function() {
	$(".tab-head").removeClass("tab-head-active");
	$(this).addClass("tab-head-active");
	$(".tab").hide();
	$(".tab-4").show();
});

//*** Language template ***//
if($(".languages-flags .flag").length == 0) {
	console.log("Not showing languages bar because there's no other language's version of this page.");
	$(".languages-container").hide();
}

//Spoiler template
$(".spoiler-container .button").click(function() {
	$(this).siblings(".text").toggle("slow");
});

//* General/generic functions *//

/* User is bot if userGroup.some(isBot) == true */

var userGroup = "";

function getUserGroup() {
	$.ajax({
		url: apiUrl,
		data: {
			format: 'json',
			action: 'query',
			meta: 'userinfo',
			uiprop: 'groups',
		},
		async: false,
		dataType: 'json',
		type: 'GET',
		success: function(data) {
			userGroup = data.query.userinfo.groups
		},
	});
};

function isBot(group) {
	return group == "bot";
}

/* Get token of this session */

var globalToken;

function getToken() {
    $.ajax({
        url: apiUrl,
        data: {
            format: 'json',
            action: 'query',
            meta: 'tokens',
            bot: true
        },
        async: false,
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
           globalToken = data.query.tokens.csrftoken;
        },
        error: function( xhr ) {
            console.log("Failed to get token.");
        }
    });
}


function genericEditPage(title, content, summary) {
	$.ajax({
		url: apiUrl,
		data: {
			format: 'json',
			action: 'edit',
			title: title,
			text: content,
			token: globalToken,
			summary: summary,
			bot: true,
			nocreate: true
		},
		dataType: 'json',
		type: 'POST',
		success: function( data ) {
			console.log("Edited " + title);
		},
		error: function( xhr ) {
			alert("Failed to edit " + title);
		}
	});
};


function createPage(pageTitle, content, summary) {
    $.ajax({
        url: apiUrl,
        data: {
			format: 'json',
			action: 'edit',
			title: pageTitle,
			text: content,
			token: globalToken,
			summary: summary,
			bot: true
        },
        async: false,
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
			console.log("Created page: " + pageTitle);
        },
        error: function( xhr ) {
			console.log("Failed to create page");
        }
    });
}

function getBacklinks(page) {
	var backlinks = [];
	$.ajax({
		url: apiUrl,
		data: {
			format: 'json',
			action: 'query',
			list: 'backlinks',
			bltitle: page,
			bllimit: 1000,
		},
		async: false,
		type: 'GET',
		success: function( data ) {
			backlinks = data.query.backlinks;
		},
		error: function( xhr ) {
			alert( 'Error: Backlinks request failed.' );
		}
	});
	return backlinks;
};

function getFileUsage(file) {
	var imageusage = [];
	$.ajax({
		url: apiUrl,
		data: {
			format: 'json',
			action: 'query',
			list: 'imageusage',
			iutitle: file,
			iulimit: 1000
		},
		async: false,
		type: 'GET',
		success: function( data ) {
			imageusage = data.query.imageusage;
		},
		error: function( xhr ) {
			alert( 'Error: Imageusage request failed.' );
		}
	});
	return imageusage;
};

function performNullEdit(pageTitle, summary) {
    $.ajax({
        url: apiUrl,
        data: {
            format: 'json',
            action: 'edit',
            title: pageTitle,
            section: 0,
            text: "",
            token: globalToken,
            summary: summary,
            bot: true
        },
        async: false,
        dataType: 'json',
        type: 'POST',
        success: function( data ) {
           console.log("Performed null edit");
        },
        error: function( xhr ) {
            console.log("Failed to perform null edit");
        }
    });
}

function purgeWhatLinksHere(pageTitle) {
    $.ajax({
        url: apiUrl,
        data: {
            format: "json",
            action: 'query',
            list: "backlinks",
            bltitle: pageTitle,
            bllimit: 500
        },
        async: true,
        type: 'GET',
        success: function( data ) {
            console.log(data);
            for (var i = 0; i < data.query.backlinks.length; i++) {
                purgePage(data.query.backlinks[i].title);
            }
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed.' );
            console.log("Failed purging");
        }
    });
}

function purgePage(pageTitle) {
    $.ajax({
        url: apiUrl,
        data: {
            action: 'purge',
            forcelinkupdate: true,
            titles: pageTitle,
            prop: "info"
        },
        async: true,
        type: 'GET',
        success: function( data ) {
            console.log("purging " + pageTitle);
        },
        error: function( xhr ) {
            //alert( 'Error: Request failed.' );
            console.log("Failed purging");
        }
    });
}