MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
(most searches now use proper regular expressions which reduces eror and the amount of code I need, replaced the adding to the end of the infobox with adding to the start of it becuase I could not get it to work with more than one }} on the page) |
(added tech infobox updating, fixed page searches) |
||
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. */ | ||
/* Infobox updating */ | /* 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"] | 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"] | ||
var version = "0.15.11"; | var version = "0.15.11"; | ||
$("#RecipeUpdate").click(function(){ | $("#RecipeUpdate").click(function(){ | ||
getRecipes(); | getRecipes(); | ||
}); | }); | ||
function getRecipes() { | function getRecipes() { | ||
getUserGroup(); | getUserGroup(); | ||
if (UserGroup.some(isBot) == true) { | if (UserGroup.some(isBot) == true) { | ||
var | var RecipeInput = prompt("Please enter the recipes"); | ||
if ( | if (RecipeInput != null) { | ||
getToken(); | getToken(); | ||
var OldContent = ""; | var OldContent = ""; | ||
var Items = | var Items = RecipeInput.split(/\s\s/g); | ||
console.log(Items.length + " Items detected"); | console.log(Items.length + " Items detected"); | ||
Items.forEach(RemoveDuplicateRecipesAndUpdateInfobox); | Items.forEach(RemoveDuplicateRecipesAndUpdateInfobox); | ||
Line 259: | Line 234: | ||
if (PageRecipe != Recipe) { | if (PageRecipe != Recipe) { | ||
if (PageRecipe.length > 0) { | if (PageRecipe.length > 0) { | ||
var NewPageRecipeStart = OldContent.search( | var NewPageRecipeStart = OldContent.search(/(\s|\|)recipe/) + 7; | ||
var NewPageRecipeCut = OldContent.slice(NewPageRecipeStart); | var NewPageRecipeCut = OldContent.slice(NewPageRecipeStart); | ||
var NewPageRecipeEnd = NewPageRecipeCut.search(/\||}}/); | var NewPageRecipeEnd = NewPageRecipeCut.search(/\||}}/); | ||
Line 285: | Line 260: | ||
} | } | ||
if (PageTotalRaw.length > 0) { | if (PageTotalRaw.length > 0) { | ||
var NewPageTotalRawStart = NewContent.search( | var NewPageTotalRawStart = NewContent.search(/(\s|\|)total-raw/) + 10; | ||
var NewPageTotalRawCut = NewContent.slice(NewPageTotalRawStart); | var NewPageTotalRawCut = NewContent.slice(NewPageTotalRawStart); | ||
var NewPageTotalRawEnd = NewPageTotalRawCut.search(/\||}}/); | var NewPageTotalRawEnd = NewPageTotalRawCut.search(/\||}}/); | ||
Line 311: | Line 286: | ||
} | } | ||
if (PageExpRecipe.length > 0) { | if (PageExpRecipe.length > 0) { | ||
var NewPageExpRecipeStart = NewContent.search( | var NewPageExpRecipeStart = NewContent.search(/(\s|\|)expensive-recipe/) + 17; | ||
var NewPageExpRecipeCut = NewContent.slice(NewPageExpRecipeStart); | var NewPageExpRecipeCut = NewContent.slice(NewPageExpRecipeStart); | ||
var NewPageExpRecipeEnd = NewPageExpRecipeCut.search(/\||}}/); | var NewPageExpRecipeEnd = NewPageExpRecipeCut.search(/\||}}/); | ||
Line 337: | Line 312: | ||
} | } | ||
if (PageExpTotalRaw.length > 0) { | if (PageExpTotalRaw.length > 0) { | ||
var NewPageExpTotalRawStart = NewContent.search( | var NewPageExpTotalRawStart = NewContent.search(/(\s|\|)expensive-total-raw/) + 20; | ||
var NewPageExpTotalRawCut = NewContent.slice(NewPageExpTotalRawStart); | var NewPageExpTotalRawCut = NewContent.slice(NewPageExpTotalRawStart); | ||
var NewPageExpTotalRawEnd = NewPageExpTotalRawCut.search(/\||}}/); | var NewPageExpTotalRawEnd = NewPageExpTotalRawCut.search(/\||}}/); | ||
Line 394: | Line 369: | ||
getUserGroup(); | getUserGroup(); | ||
if (UserGroup.some(isBot) == true) { | if (UserGroup.some(isBot) == true) { | ||
var | var ItemInput = prompt("Please enter the consumers"); | ||
if ( | if (ItemInput != null) { | ||
getToken(); | getToken(); | ||
var Items = | var Items = ItemInput.split(/\s\s/g); | ||
console.log(Items.length + " Items detected"); | console.log(Items.length + " Items detected"); | ||
Items.forEach(UpdateInfoboxWithConsumers); | Items.forEach(UpdateInfoboxWithConsumers); | ||
Line 404: | Line 379: | ||
}; | }; | ||
function UpdateInfoboxWithConsumers( | function UpdateInfoboxWithConsumers(Item) { | ||
var ItemNameEnd = | var ItemNameEnd = Item.search("\\|"); | ||
var ItemName = | var ItemName = Item.slice(0, ItemNameEnd); | ||
ItemName = ItemName.trim(); | ItemName = ItemName.trim(); | ||
Line 418: | Line 393: | ||
if (ItemName.length > 0) { | if (ItemName.length > 0) { | ||
var ConsumersStart = | var ConsumersStart = Item.search("\\|consumers = ") + 13; | ||
var Consumers = | var Consumers = Item.slice(ConsumersStart); | ||
Consumers = Consumers.trim(); | Consumers = Consumers.trim(); | ||
} | } | ||
Line 457: | Line 432: | ||
if (OldContent.length > 0) { | if (OldContent.length > 0) { | ||
//find recipes in page (OldContent) | //find recipes in page (OldContent) | ||
var PageConsumersStart = OldContent.search( | var PageConsumersStart = OldContent.search(/(\s|\|)consumers/) + 10; | ||
if (PageConsumersStart < 10) { | if (PageConsumersStart < 10) { | ||
console.log(ItemName + ": No consumers found on page."); | console.log(ItemName + ": No consumers found on page."); | ||
Line 483: | Line 458: | ||
} else { | } else { | ||
if (PageConsumers.length > 0) { | if (PageConsumers.length > 0) { | ||
var NewPageConsumersStart = OldContent.search( | var NewPageConsumersStart = OldContent.search(/(\s|\|)consumers/) + 10; | ||
var NewPageConsumersCut = OldContent.slice(NewPageConsumersStart); | var NewPageConsumersCut = OldContent.slice(NewPageConsumersStart); | ||
var NewPageConsumersEnd = NewPageConsumersCut.search(/\||}}/); | var NewPageConsumersEnd = NewPageConsumersCut.search(/\||}}/); | ||
Line 525: | Line 500: | ||
} | } | ||
} | |||
$("#TechUpdate").click(function(){ | |||
getTechnologies(); | |||
}); | |||
function getTechnologies() { | |||
getUserGroup(); | |||
if (UserGroup.some(isBot) == false) { | |||
return; | |||
} | |||
var TechInput = prompt("Please enter the technologies"); | |||
if (TechInput != null) { | |||
getToken(); | |||
var Techs = TechInput.split(/\s\s/g); | |||
console.log(Techs.length + " Techs detected"); | |||
Techs.forEach(UpdateTechnologyInfobox); | |||
} | |||
} | |||
function UpdateTechnologyInfobox(Tech) { | |||
var TechNameEnd = Tech.search("\\|"); | |||
var TechName = Tech.slice(0, TechNameEnd); | |||
TechName = TechName.trim(); | |||
var CostStart = Tech.search("\\|cost = ") + 8; | |||
if (CostStart < 8) { | |||
console.log(TechName + ": No cost found."); | |||
var Cost = ""; | |||
} else { | |||
var CostCut = Tech.slice(CostStart); | |||
var CostEnd = CostCut.search(/\||}}/); | |||
if (CostEnd < 1) { | |||
var Cost = CostCut; | |||
} else { | |||
var Cost = CostCut.slice(0, CostEnd); | |||
} | |||
} | |||
var CostMultiStart = Tech.search("\\|cost-multiplier = ") + 19; | |||
if (CostMultiStart < 19) { | |||
console.log(TechName + ": No cost-multiplier found."); | |||
var CostMulti = ""; | |||
} else { | |||
var CostMultiCut = Tech.slice(CostMultiStart); | |||
var CostMultiEnd = CostMultiCut.search(/\||}}/); | |||
if (CostMultiEnd < 1) { | |||
var CostMulti = CostMultiCut; | |||
} else { | |||
var CostMulti = CostMultiCut.slice(0, CostMultiEnd); | |||
} | |||
} | |||
var ExpCostMultiStart = Tech.search("\\|expensive-cost-multiplier = ") + 29; | |||
if (ExpCostMultiStart < 29) { | |||
console.log(TechName + ": No expensive-cost-multiplier found."); | |||
var ExpCostMulti = ""; | |||
} else { | |||
var ExpCostMultiCut = Tech.slice(ExpCostMultiStart); | |||
var ExpCostMultiEnd = ExpCostMultiCut.search(/\||}}/); | |||
if (ExpCostMultiEnd < 1) { | |||
var ExpCostMulti = ExpCostMultiCut; | |||
} else { | |||
var ExpCostMulti = ExpCostMultiCut.slice(0, ExpCostMultiEnd); | |||
} | |||
} | |||
var ReqTechStart = Tech.search("\\|required-technologies = ") + 25; | |||
if (ReqTechStart < 25) { | |||
console.log(TechName + ": No required-technologies found."); | |||
var ReqTech = ""; | |||
} else { | |||
var ReqTechCut = Tech.slice(ReqTechStart); | |||
var ReqTechEnd = ReqTechCut.search(/\||}}/); | |||
if (ReqTechEnd < 1) { | |||
var ReqTech = ReqTechCut; | |||
} else { | |||
var ReqTech = ReqTechCut.slice(0, ReqTechEnd); | |||
} | |||
} | |||
var AllowsStart = Tech.search("\\|allows = ") + 6; | |||
if (AllowsStart < 6) { | |||
console.log(TechName + ": No allows found."); | |||
var Allows = ""; | |||
} else { | |||
var AllowsCut = Tech.slice(AllowsStart); | |||
var AllowsEnd = AllowsCut.search(/\||}}/); | |||
if (AllowsEnd < 1) { | |||
var Allows = AllowsCut; | |||
} else { | |||
var Allows = AllowsCut.slice(0, AllowsEnd); | |||
} | |||
} | |||
var EffectsStart = Tech.search("\\|effects = ") + 11; | |||
if (EffectsStart < 11) { | |||
console.log(TechName + ": No effects found."); | |||
var Effects = ""; | |||
} else { | |||
var EffectsCut = Tech.slice(EffectsStart); | |||
var EffectsEnd = EffectsCut.search(/\||}}/); | |||
if (EffectsEnd < 1) { | |||
var Effects = EffectsCut; | |||
} else { | |||
var Effects = EffectsCut.slice(0, EffectsEnd); | |||
} | |||
} | |||
Cost = Cost.trim(); | |||
CostMulti = CostMulti.trim(); | |||
ExpCostMulti = ExpCostMulti.trim(); | |||
ReqTech = ReqTech.trim(); | |||
Allows = Allows.trim(); | |||
Effects = Effects.trim(); | |||
var OldContent = ""; | |||
//get page content of the tech -> 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) { | |||
return; | |||
} | |||
//find costs etc in page (OldContent) | |||
var PageCostStart = OldContent.search(/(\s|\|)cost/) + 5; | |||
if (PageCostStart < 5) { | |||
console.log(TechName + ": No cost found."); | |||
var PageCost = ""; | |||
} else { | |||
var PageCostCut = OldContent.slice(PageCostStart); | |||
PageCostStart = PageCostCut.search(/\w/); | |||
PageCostCut = PageCostCut.slice(PageCostStart); | |||
var PageCostEnd = PageCostCut.search(/\||}}/); | |||
if (PageCostEnd < 1) { | |||
var PageCost = PageCostCut; | |||
} else { | |||
var PageCost = PageCostCut.slice(0, PageCostEnd); | |||
} | |||
} | |||
var PageCostMultiStart = OldContent.search(/(\s|\|)cost-multiplier/) + 16; | |||
if (PageCostMultiStart < 16) { | |||
console.log(TechName + ": No cost-multiplier found."); | |||
var PageCostMulti = ""; | |||
} else { | |||
var PageCostMultiCut = OldContent.slice(PageCostMultiStart); | |||
PageCostMultiStart = PageCostMultiCut.search(/\w/); | |||
PageCostMultiCut = PageCostMultiCut.slice(PageCostMultiStart); | |||
var PageCostMultiEnd = PageCostMultiCut.search(/\||}}/); | |||
if (PageCostMultiEnd < 1) { | |||
var PageCostMulti = PageCostMultiCut; | |||
} else { | |||
var PageCostMulti = PageCostMultiCut.slice(0, PageCostMultiEnd); | |||
} | |||
} | |||
var PageExpCostMultiStart = OldContent.search(/(\s|\|)expensive-cost-multiplier/) + 26; | |||
if (PageExpCostMultiStart < 26) { | |||
console.log(TechName + ": No expensive-cost-multiplier found."); | |||
var PageExpCostMulti = ""; | |||
} else { | |||
var PageExpCostMultiCut = OldContent.slice(PageExpCostMultiStart); | |||
PageExpCostMultiStart = PageExpCostMultiCut.search(/\w/); | |||
PageExpCostMultiCut = PageExpCostMultiCut.slice(PageExpCostMultiStart); | |||
var PageExpCostMultiEnd = PageExpCostMultiCut.search(/\||}}/); | |||
if (PageExpCostMultiEnd < 1) { | |||
var PageExpCostMulti = PageExpCostMultiCut; | |||
} else { | |||
var PageExpCostMulti = PageExpCostMultiCut.slice(0, PageExpCostMultiEnd); | |||
} | |||
} | |||
var PageReqTechStart = OldContent.search(/(\s|\|)required-technologies/) + 22; | |||
if (PageReqTechStart < 22) { | |||
console.log(TechName + ": No required-technologies found."); | |||
var PageReqTech = ""; | |||
} else { | |||
var PageReqTechCut = OldContent.slice(PageReqTechStart); | |||
PageReqTechStart = PageReqTechCut.search(/\w/); | |||
PageReqTechCut = PageReqTechCut.slice(PageReqTechStart); | |||
var PageReqTechEnd = PageReqTechCut.search(/\||}}/); | |||
if (PageReqTechEnd < 1) { | |||
var PageReqTech = PageReqTechCut; | |||
} else { | |||
var PageReqTech = PageReqTechCut.slice(0, PageReqTechEnd); | |||
} | |||
} | |||
var PageAllowsStart = OldContent.search(/(\s|\|)allows/) + 7; | |||
if (PageAllowsStart < 7) { | |||
console.log(TechName + ": No allows found."); | |||
var PageAllows = ""; | |||
} else { | |||
var PageAllowsCut = OldContent.slice(PageAllowsStart); | |||
PageAllowsStart = PageAllowsCut.search(/\w/); | |||
PageAllowsCut = PageAllowsCut.slice(PageAllowsStart); | |||
var PageAllowsEnd = PageAllowsCut.search(/\||}}/); | |||
if (PageAllowsEnd < 1) { | |||
var PageAllows = PageAllowsCut; | |||
} else { | |||
var PageAllows = PageAllowsCut.slice(0, PageAllowsEnd); | |||
} | |||
} | |||
var PageEffectsStart = OldContent.search(/(\s|\|)effects/) + 8; | |||
if (PageEffectsStart < 8) { | |||
console.log(TechName + ": No effects found."); | |||
var PageEffects = ""; | |||
} else { | |||
var PageEffectsCut = OldContent.slice(PageEffectsStart); | |||
PageEffectsStart = PageEffectsCut.search(/\w/); | |||
PageEffectsCut = PageEffectsCut.slice(PageEffectsStart); | |||
var PageEffectsEnd = PageEffectsCut.search(/\||}}/); | |||
if (PageEffectsEnd < 1) { | |||
var PageEffects = PageEffectsCut; | |||
} else { | |||
var PageEffects = PageEffectsCut.slice(0, PageEffectsEnd); | |||
} | |||
} | |||
PageCost = PageCost.trim(); | |||
PageCostMulti = PageCostMulti.trim(); | |||
PageExpCostMulti = PageExpCostMulti.trim(); | |||
PageReqTech = PageReqTech.trim(); | |||
PageAllows = PageAllows.trim(); | |||
PageEffects = PageEffects.trim(); | |||
var Summary = ""; | |||
var 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) { | |||
if (PageCost.length > 0) { | |||
var NewPageCostStart = OldContent.search(/(\s|\|)cost/) + 5; | |||
var NewPageCostCut = OldContent.slice(NewPageCostStart); | |||
var NewPageCostEnd = NewPageCostCut.search(/\||}}/) + NewPageCostStart; | |||
if (Cost.length > 0) { | |||
NewContent = OldContent.slice(0, NewPageCostStart) + " = " + Cost + "\n" + OldContent.slice(NewPageCostEnd); | |||
console.log("Replaced " + TechName + " cost."); | |||
Summary = Summary + "Updated cost to " + version + ". "; | |||
} else { | |||
NewPageCostStart = NewPageCostStart - 5; | |||
NewContent = OldContent.slice(0, NewPageCostStart) + OldContent.slice(NewPageCostEnd); | |||
console.log("Removed " + ItemName + " cost."); | |||
Summary = Summary + "Removed cost. "; | |||
} | |||
} else if (Cost.length > 0) { | |||
var InfoboxStart = OldContent.search(/{{Infobox/i) + 9; | |||
NewContent = OldContent.slice(0, InfoboxStart) + "\n|cost = " + Cost + OldContent.slice(InfoboxStart); | |||
console.log("Added " + ItemName + " cost to the start of the infobox."); | |||
Summary = Summary + "Added cost. "; | |||
} | |||
} | |||
if (PageCostMulti != CostMulti) { | |||
if (NewContent.length == 0) { | |||
NewContent = OldContent | |||
} | |||
if (PageCostMulti.length > 0) { | |||
var NewPageCostMultiStart = NewContent.search(/(\s|\|)cost-multiplier/) + 16; | |||
var NewPageCostMultiCut = NewContent.slice(NewPageCostMultiStart); | |||
var NewPageCostMultiEnd = NewPageCostMultiCut.search(/\||}}/) + NewPageCostMultiStart; | |||
if (CostMulti.length > 0) { | |||
NewContent = NewContent.slice(0, NewPageCostMultiStart) + " = " + CostMulti + "\n" + NewContent.slice(NewPageCostMultiEnd); | |||
console.log("Replaced " + TechName + " cost-multiplier."); | |||
Summary = Summary + "Updated cost-multiplier to " + version + ". "; | |||
} else { | |||
NewPageCostMultiStart = NewPageCostMultiStart - 16; | |||
NewContent = NewContent.slice(0, NewPageCostMultiStart) + NewContent.slice(NewPageCostMultiEnd); | |||
console.log("Removed " + TechName + " cost-multiplier. "); | |||
Summary = Summary + "Removed cost-multiplier. "; | |||
} | |||
} else if (CostMulti.length > 0) { | |||
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9; | |||
NewContent = NewContent.slice(0, InfoboxStart) + "\n|cost-multiplier = " + CostMulti + NewContent.slice(InfoboxStart); | |||
console.log("Added " + TechName + " cost-multiplier. "); | |||
Summary = Summary + "Added cost-multiplier. "; | |||
} | |||
} | |||
if (PageCostMulti != CostMulti) { | |||
if (NewContent.length == 0) { | |||
NewContent = OldContent | |||
} | |||
if (PageCostMulti.length > 0) { | |||
var NewPageCostMultiStart = NewContent.search(/(\s|\|)cost-multiplier/) + 16; | |||
var NewPageCostMultiCut = NewContent.slice(NewPageCostMultiStart); | |||
var NewPageCostMultiEnd = NewPageCostMultiCut.search(/\||}}/) + NewPageCostMultiStart; | |||
if (CostMulti.length > 0) { | |||
NewContent = NewContent.slice(0, NewPageCostMultiStart) + " = " + CostMulti + "\n" + NewContent.slice(NewPageCostMultiEnd); | |||
console.log("Replaced " + TechName + " cost-multiplier."); | |||
Summary = Summary + "Updated cost-multiplier to " + version + ". "; | |||
} else { | |||
NewPageCostMultiStart = NewPageCostMultiStart - 16; | |||
NewContent = NewContent.slice(0, NewPageCostMultiStart) + NewContent.slice(NewPageCostMultiEnd); | |||
console.log("Removed " + TechName + " cost-multiplier. "); | |||
Summary = Summary + "Removed cost-multiplier. "; | |||
} | |||
} else if (CostMulti.length > 0) { | |||
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9; | |||
NewContent = NewContent.slice(0, InfoboxStart) + "\n|cost-multiplier = " + CostMulti + NewContent.slice(InfoboxStart); | |||
console.log("Added " + TechName + " cost-multiplier. "); | |||
Summary = Summary + "Added cost-multiplier. "; | |||
} | |||
} | |||
if (PageExpCostMulti != ExpCostMulti) { | |||
if (NewContent.length == 0) { | |||
NewContent = OldContent | |||
} | |||
if (PageExpCostMulti.length > 0) { | |||
var NewPageExpCostMultiStart = NewContent.search(/(\s|\|)expensive-cost-multiplier/) + 26; | |||
var NewPageExpCostMultiCut = NewContent.slice(NewPageExpCostMultiStart); | |||
var NewPageExpCostMultiEnd = NewPageExpCostMultiCut.search(/\||}}/) + NewPageExpCostMultiStart; | |||
if (ExpCostMulti.length > 0) { | |||
NewContent = NewContent.slice(0, NewPageExpCostMultiStart) + " = " + ExpCostMulti + "\n" + NewContent.slice(NewPageExpCostMultiEnd); | |||
console.log("Replaced " + TechName + " expensive-cost-multiplier."); | |||
Summary = Summary + "Updated expensive-cost-multiplier to " + version + ". "; | |||
} else { | |||
NewPageExpCostMultiStart = NewPageExpCostMultiStart - 26; | |||
NewContent = NewContent.slice(0, NewPageExpCostMultiStart) + NewContent.slice(NewPageExpCostMultiEnd); | |||
console.log("Removed " + TechName + " expensive-cost-multiplier. "); | |||
Summary = Summary + "Removed expensive-cost-multiplier. "; | |||
} | |||
} else if (ExpCostMulti.length > 0) { | |||
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9; | |||
NewContent = NewContent.slice(0, InfoboxStart) + "\n|expensive-cost-multiplier = " + ExpCostMulti + NewContent.slice(InfoboxStart); | |||
console.log("Added " + TechName + " expensive-cost-multiplier. "); | |||
Summary = Summary + "Added expensive-cost-multiplier. "; | |||
} | |||
} | |||
if (PageReqTech != ReqTech) { | |||
if (NewContent.length == 0) { | |||
NewContent = OldContent | |||
} | |||
if (PageReqTech.length > 0) { | |||
var NewPageReqTechStart = NewContent.search(/(\s|\|)required-technologies/) + 22; | |||
var NewPageReqTechCut = NewContent.slice(NewPageReqTechStart); | |||
var NewPageReqTechEnd = NewPageReqTechCut.search(/\||}}/) + NewPageReqTechStart; | |||
if (ReqTech.length > 0) { | |||
NewContent = NewContent.slice(0, NewPageReqTechStart) + " = " + ReqTech + "\n" + NewContent.slice(NewPageReqTechEnd); | |||
console.log("Replaced " + TechName + " required-technologies."); | |||
Summary = Summary + "Updated required-technologies to " + version + ". "; | |||
} else { | |||
NewPageReqTechStart = NewPageReqTechStart - 22; | |||
NewContent = NewContent.slice(0, NewPageReqTechStart) + NewContent.slice(NewPageReqTechEnd); | |||
console.log("Removed " + TechName + " required-technologies. "); | |||
Summary = Summary + "Removed required-technologies. "; | |||
} | |||
} else if (ReqTech.length > 0) { | |||
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9; | |||
NewContent = NewContent.slice(0, InfoboxStart) + "\n|required-technologies = " + ReqTech + NewContent.slice(InfoboxStart); | |||
console.log("Added " + TechName + " required-technologies. "); | |||
Summary = Summary + "Added required-technologies. "; | |||
} | |||
} | |||
if (PageAllows != Allows) { | |||
if (NewContent.length == 0) { | |||
NewContent = OldContent | |||
} | |||
if (PageAllows.length > 0) { | |||
var NewPageAllowsStart = NewContent.search(/(\s|\|)allows/) + 7; | |||
var NewPageAllowsCut = NewContent.slice(NewPageAllowsStart); | |||
var NewPageAllowsEnd = NewPageAllowsCut.search(/\||}}/) + NewPageAllowsStart; | |||
if (Allows.length > 0) { | |||
NewContent = NewContent.slice(0, NewPageAllowsStart) + " = " + Allows + "\n" + NewContent.slice(NewPageAllowsEnd); | |||
console.log("Replaced " + TechName + " allows."); | |||
Summary = Summary + "Updated allows to " + version + ". "; | |||
} else { | |||
NewPageAllowsStart = NewPageAllowsStart - 7; | |||
NewContent = NewContent.slice(0, NewPageAllowsStart) + NewContent.slice(NewPageAllowsEnd); | |||
console.log("Removed " + TechName + " allows. "); | |||
Summary = Summary + "Removed allows. "; | |||
} | |||
} else if (Allows.length > 0) { | |||
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9; | |||
NewContent = NewContent.slice(0, InfoboxStart) + "\n|allows = " + Allows + NewContent.slice(InfoboxStart); | |||
console.log("Added " + TechName + " allows. "); | |||
Summary = Summary + "Added allows. "; | |||
} | |||
} | |||
if (PageEffects != Effects) { | |||
if (NewContent.length == 0) { | |||
NewContent = OldContent | |||
} | |||
if (PageEffects.length > 0) { | |||
var NewPageEffectsStart = NewContent.search(/(\s|\|)effects/) + 8; | |||
var NewPageEffectsCut = NewContent.slice(NewPageEffectsStart); | |||
var NewPageEffectsEnd = NewPageEffectsCut.search(/\||}}/) + NewPageEffectsStart; | |||
if (Effects.length > 0) { | |||
NewContent = NewContent.slice(0, NewPageEffectsStart) + " = " + Effects + "\n" + NewContent.slice(NewPageEffectsEnd); | |||
console.log("Replaced " + TechName + " effects."); | |||
Summary = Summary + "Updated effects to " + version + ". "; | |||
} else { | |||
NewPageEffectsStart = NewPageEffectsStart - 8; | |||
NewContent = NewContent.slice(0, NewPageEffectsStart) + NewContent.slice(NewPageEffectsEnd); | |||
console.log("Removed " + TechName + " effects. "); | |||
Summary = Summary + "Removed effects. "; | |||
} | |||
} else if (Effects.length > 0) { | |||
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9; | |||
NewContent = NewContent.slice(0, InfoboxStart) + "\n|effects = " + Effects + NewContent.slice(InfoboxStart); | |||
console.log("Added " + TechName + " effects. "); | |||
Summary = Summary + "Added effects. "; | |||
} | |||
} | |||
} | |||
//alright, NewContent should be defined, change page: | |||
if (NewContent.length > 0) { | |||
$.ajax({ | |||
url: 'https://wiki.factorio.com/api.php', | |||
data: { | |||
format: 'json', | |||
action: 'edit', | |||
title: TechName + ' (research)/infobox', | |||
text: NewContent, | |||
token: globalToken, | |||
summary: Summary, | |||
bot: true, | |||
nocreate: true | |||
}, | |||
async: false, | |||
dataType: 'json', | |||
type: 'POST', | |||
success: function( data ) { | |||
console.log("Updated " + TechName); | |||
}, | |||
error: function( xhr ) { | |||
console.log("Failed to update " + TechName); | |||
} | |||
}); | |||
} | |||
} | |||
/* user is bot if UserGroup.some(isBot) == true */ | |||
var UserGroup = ""; | |||
function getUserGroup() { | |||
$.ajax({ | |||
url: 'https://wiki.factorio.com/api.php', | |||
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"; | |||
} | } | ||
Revision as of 19:43, 17 May 2017
/* Any JavaScript here will be loaded for all users on every page load. */
/* 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"]
var version = "0.15.11";
$("#RecipeUpdate").click(function(){
getRecipes();
});
function getRecipes() {
getUserGroup();
if (UserGroup.some(isBot) == true) {
var RecipeInput = prompt("Please enter the recipes");
if (RecipeInput != null) {
getToken();
var OldContent = "";
var Items = RecipeInput.split(/\s\s/g);
console.log(Items.length + " Items detected");
Items.forEach(RemoveDuplicateRecipesAndUpdateInfobox);
}
}
};
function RemoveDuplicateRecipesAndUpdateInfobox(Recipes) {
var ItemNameEnd = Recipes.search("\\|");
var ItemName = Recipes.slice(0, ItemNameEnd);
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) {
var RecipeStart = Recipes.search("\\|recipe = ") + 10;
if (RecipeStart < 10) {
console.log(ItemName + ": No normal recipe found.");
var Recipe = "";
} else {
var RecipeCut = Recipes.slice(RecipeStart);
var RecipeEnd = RecipeCut.search(/\||}}/);
if (RecipeEnd < 1) {
var Recipe = RecipeCut;
} else {
var Recipe = RecipeCut.slice(0, RecipeEnd);
}
}
var TotalRawStart = Recipes.search("\\|total-raw = ") + 13;
if (TotalRawStart < 13) {
console.log(ItemName + ": No total raw found.");
var TotalRaw = "";
} else {
var TotalRawCut = Recipes.slice(TotalRawStart);
var TotalRawEnd = TotalRawCut.search(/\||}}/);
if (TotalRawEnd < 1) {
var TotalRaw = TotalRawCut;
} else {
var TotalRaw = TotalRawCut.slice(0, TotalRawEnd);
}
}
var ExpRecipeStart = Recipes.search("\\|expensive-recipe = ") + 20;
if (ExpRecipeStart < 20) {
console.log(ItemName + ": No expensive recipe found.");
var ExpRecipe = "";
} else {
var ExpRecipeCut = Recipes.slice(ExpRecipeStart);
var ExpRecipeEnd = ExpRecipeCut.search(/\||}}/);
if (ExpRecipeEnd < 1) {
var ExpRecipe = ExpRecipeCut;
} else {
var ExpRecipe = ExpRecipeCut.slice(0, ExpRecipeEnd);
}
}
var ExpTotalRawStart = Recipes.search("\\|expensive-total-raw = ") + 23;
if (ExpTotalRawStart < 23) {
console.log(ItemName + ": No expensive total raw found.");
var ExpTotalRaw = "";
} else {
var ExpTotalRawCut = Recipes.slice(ExpTotalRawStart);
var ExpTotalRawEnd = ExpTotalRawCut.search(/\||}}/);
if (ExpTotalRawEnd < 1) {
var ExpTotalRaw = ExpTotalRawCut;
} else {
var ExpTotalRaw = ExpTotalRawCut.slice(0, ExpTotalRawEnd);
}
}
//remove whitespace
//ItemName = ItemName.trim(); this is done further up for asthestics
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.");
}
}
var OldContent = "";
if (ItemName.length > 0) {
//get page content of the item -> 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 = "";
}
});
} else {
OldContent = "";
}
if (OldContent.length > 0) {
//find recipes in page (OldContent)
var PageRecipeStart = OldContent.search(/(\s|\|)recipe/) + 7;
if (PageRecipeStart < 7) {
console.log(ItemName + ": No normal recipe found on page.");
var PageRecipe = "";
} else {
var PageRecipeCut = OldContent.slice(PageRecipeStart);
PageRecipeStart = PageRecipeCut.search(/\w/);
PageRecipeCut = PageRecipeCut.slice(PageRecipeStart); //removes anything before the recipe that does not belong there, like = and \s
var PageRecipeEnd = PageRecipeCut.search(/\||}}/);
if (PageRecipeEnd < 1) {
var PageRecipe = PageRecipeCut;
} else {
var PageRecipe = PageRecipeCut.slice(0, PageRecipeEnd);
}
}
var PageTotalRawStart = OldContent.search(/(\s|\|)total-raw/) + 10;
if (PageTotalRawStart < 10) {
console.log(ItemName + ": No total raw found on page.");
var PageTotalRaw = "";
} else {
var PageTotalRawCut = OldContent.slice(PageTotalRawStart);
PageTotalRawStart = PageTotalRawCut.search(/\w/);
PageTotalRawCut = PageTotalRawCut.slice(PageTotalRawStart); //removes anything before the recipe that does not belong there, like = and \s
var PageTotalRawEnd = PageTotalRawCut.search(/\||}}/);
if (PageTotalRawEnd < 1) {
var PageTotalRaw = PageTotalRawCut;
} else {
var PageTotalRaw = PageTotalRawCut.slice(0, PageTotalRawEnd);
}
}
var PageExpRecipeStart = OldContent.search(/(\s|\|)expensive-recipe/) + 17;
if (PageExpRecipeStart < 17) {
console.log(ItemName + ": No expensive recipe found on page.");
var PageExpRecipe = "";
} else {
var PageExpRecipeCut = OldContent.slice(PageExpRecipeStart);
PageExpRecipeStart = PageExpRecipeCut.search(/\w/);
PageExpRecipeCut = PageExpRecipeCut.slice(PageExpRecipeStart); //removes anything before the recipe that does not belong there, like = and \s
var PageExpRecipeEnd = PageExpRecipeCut.search(/\||}}/);
if (PageExpRecipeEnd < 1) {
var PageExpRecipe = PageExpRecipeCut;
} else {
var PageExpRecipe = PageExpRecipeCut.slice(0, PageExpRecipeEnd);
}
}
var PageExpTotalRawStart = OldContent.search(/(\s|\|)expensive-total-raw/) + 20;
if (PageExpTotalRawStart < 20) {
console.log(ItemName + ": No expensive total raw found on page.");
var PageExpTotalRaw = "";
} else {
var PageExpTotalRawCut = OldContent.slice(PageExpTotalRawStart);
PageExpTotalRawStart = PageExpTotalRawCut.search(/\w/);
PageExpTotalRawCut = PageExpTotalRawCut.slice(PageExpTotalRawStart); //removes anything before the recipe that does not belong there, like = and \s
var PageExpTotalRawEnd = PageExpTotalRawCut.search(/\||}}/);
if (PageExpTotalRawEnd < 1) {
var PageExpTotalRaw = PageExpTotalRawCut;
} else {
var PageExpTotalRaw = PageExpTotalRawCut.slice(0, PageExpTotalRawEnd);
}
}
//remove whitespace
PageRecipe = PageRecipe.trim();
PageTotalRaw = PageTotalRaw.trim();
PageExpRecipe = PageExpRecipe.trim();
PageExpTotalRaw = PageExpTotalRaw.trim();
var Summary = "";
//change page if anything is different (this INCLUDES different formatting)
var NewContent = "";
if ((PageRecipe == Recipe) && (PageTotalRaw == TotalRaw) && (PageExpRecipe == ExpRecipe) && (PageExpTotalRaw == ExpTotalRaw)) {
console.log(ItemName + " page was not changed.")
} else {
if (PageRecipe != Recipe) {
if (PageRecipe.length > 0) {
var NewPageRecipeStart = OldContent.search(/(\s|\|)recipe/) + 7;
var NewPageRecipeCut = OldContent.slice(NewPageRecipeStart);
var NewPageRecipeEnd = NewPageRecipeCut.search(/\||}}/);
var ActualNewPageRecipeEnd = NewPageRecipeStart + NewPageRecipeEnd;
if (Recipe.length > 0) {
NewContent = OldContent.slice(0, NewPageRecipeStart) + " = " + Recipe + "\n" + OldContent.slice(ActualNewPageRecipeEnd);
console.log("Replaced " + ItemName + " recipe.");
Summary = Summary + "Updated recipe to " + version + ". ";
} else {
NewPageRecipeStart = NewPageRecipeStart - 7;
NewContent = OldContent.slice(0, NewPageRecipeStart) + OldContent.slice(ActualNewPageRecipeEnd);
console.log("Removed " + ItemName + " recipe.");
Summary = Summary + "Removed recipe. ";
}
} else if (Recipe.length > 0) {
var InfoboxStart = OldContent.search(/{{Infobox/i) + 9;
NewContent = OldContent.slice(0, InfoboxStart) + "\n|recipe = " + Recipe + OldContent.slice(InfoboxStart);
console.log("Added " + ItemName + " recipe to the start of the infobox.");
Summary = Summary + "Added recipe. ";
}
}
if (PageTotalRaw != TotalRaw) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageTotalRaw.length > 0) {
var NewPageTotalRawStart = NewContent.search(/(\s|\|)total-raw/) + 10;
var NewPageTotalRawCut = NewContent.slice(NewPageTotalRawStart);
var NewPageTotalRawEnd = NewPageTotalRawCut.search(/\||}}/);
var ActualNewPageTotalRawEnd = NewPageTotalRawStart + NewPageTotalRawEnd;
if (TotalRaw.length > 0) {
NewContent = NewContent.slice(0, NewPageTotalRawStart) + " = " + TotalRaw + "\n" + NewContent.slice(ActualNewPageTotalRawEnd);
console.log("Replaced " + ItemName + " total-raw.");
Summary = Summary + "Updated total-raw to " + version + ". ";
} else {
NewPageTotalRawStart = NewPageTotalRawStart - 10;
NewContent = NewContent.slice(0, NewPageTotalRawStart) + NewContent.slice(ActualNewPageTotalRawEnd);
console.log("Removed " + ItemName + " total-raw. ");
Summary = Summary + "Removed total-raw.";
}
} else if (TotalRaw.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|total-raw = " + TotalRaw + NewContent.slice(InfoboxStart);
console.log("Added " + ItemName + " total-raw to the start of the infobox.");
Summary = Summary + "Added total-raw. ";
}
}
if (PageExpRecipe != ExpRecipe) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageExpRecipe.length > 0) {
var NewPageExpRecipeStart = NewContent.search(/(\s|\|)expensive-recipe/) + 17;
var NewPageExpRecipeCut = NewContent.slice(NewPageExpRecipeStart);
var NewPageExpRecipeEnd = NewPageExpRecipeCut.search(/\||}}/);
var ActualNewPageExpRecipeEnd = NewPageExpRecipeStart + NewPageExpRecipeEnd;
if (ExpRecipe.length > 0) {
NewContent = NewContent.slice(0, NewPageExpRecipeStart) + " = " + ExpRecipe + "\n" + NewContent.slice(ActualNewPageExpRecipeEnd);
console.log("Replaced " + ItemName + " expensive-recipe.");
Summary = Summary + "Updated expensive-recipe to " + version + ". ";
} else {
NewPageExpRecipeStart = NewPageExpRecipeStart - 17;
NewContent = NewContent.slice(0, NewPageExpRecipeStart) + NewContent.slice(ActualNewPageExpRecipeEnd);
console.log("Removed " + ItemName + " expensive-recipe.");
Summary = Summary + "Removed expensive-recipe. ";
}
} else if (ExpRecipe.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|expensive-recipe = " + ExpRecipe + NewContent.slice(InfoboxStart);
console.log("Added " + ItemName + " expensive-recipe to the start of the infobox.");
Summary = Summary + "Added expensive-recipe. ";
}
}
if (PageExpTotalRaw != ExpTotalRaw) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageExpTotalRaw.length > 0) {
var NewPageExpTotalRawStart = NewContent.search(/(\s|\|)expensive-total-raw/) + 20;
var NewPageExpTotalRawCut = NewContent.slice(NewPageExpTotalRawStart);
var NewPageExpTotalRawEnd = NewPageExpTotalRawCut.search(/\||}}/);
var ActualNewPageExpTotalRawEnd = NewPageExpTotalRawStart + NewPageExpTotalRawEnd;
if (ExpTotalRaw.length > 0) {
NewContent = NewContent.slice(0, NewPageExpTotalRawStart) + " = " + ExpTotalRaw + "\n" + NewContent.slice(ActualNewPageExpTotalRawEnd);
console.log("Replaced " + ItemName + " expensive-total-raw.");
Summary = Summary + "Updated expensive-total-raw to " + version + ". ";
} else {
NewPageExpTotalRawStart = NewPageExpTotalRawStart - 20;
NewContent = NewContent.slice(0, NewPageExpTotalRawStart) + NewContent.slice(ActualNewPageExpTotalRawEnd);
console.log("Removed " + ItemName + " expensive-total-raw.");
Summary = Summary + "Removed expensive-total-raw. ";
}
} else if (ExpTotalRaw.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|expensive-total-raw = " + ExpTotalRaw + NewContent.slice(InfoboxStart);
console.log("Added " + ItemName + " expensive-total-raw to the start of the infobox.");
Summary = Summary + "Added expensive-total-raw. ";
}
}
}
//alright, NewContent should be defined, change page:
if (NewContent.length > 0) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'edit',
title: ItemName + '/infobox',
text: NewContent,
token: globalToken,
summary: Summary,
bot: true,
nocreate: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Updated " + ItemName);
},
error: function( xhr ) {
console.log("Failed to update " + ItemName);
}
});
}
}
}
$("#ConsumerUpdate").click(function(){
getConsumers();
});
function getConsumers() {
getUserGroup();
if (UserGroup.some(isBot) == true) {
var ItemInput = prompt("Please enter the consumers");
if (ItemInput != null) {
getToken();
var Items = ItemInput.split(/\s\s/g);
console.log(Items.length + " Items detected");
Items.forEach(UpdateInfoboxWithConsumers);
}
}
};
function UpdateInfoboxWithConsumers(Item) {
var ItemNameEnd = Item.search("\\|");
var ItemName = Item.slice(0, ItemNameEnd);
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) {
var ConsumersStart = Item.search("\\|consumers = ") + 13;
var Consumers = Item.slice(ConsumersStart);
Consumers = Consumers.trim();
}
var OldContent = "";
if (ItemName.length > 0) {
//get page content of the item -> 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 = "";
}
});
} else {
OldContent = "";
}
if (OldContent.length > 0) {
//find recipes in page (OldContent)
var PageConsumersStart = OldContent.search(/(\s|\|)consumers/) + 10;
if (PageConsumersStart < 10) {
console.log(ItemName + ": No consumers found on page.");
var PageConsumers = "";
} else {
var PageConsumersCut = OldContent.slice(PageConsumersStart);
PageConsumersStart = PageConsumersCut.search(/\w/);
PageConsumersCut = PageConsumersCut.slice(PageConsumersStart); //removes anything before the recipe that does not belong there, like = and \s
var PageConsumersEnd = PageConsumersCut.search(/\||}}/);
if (PageConsumersEnd < 1) {
var PageConsumers = PageConsumersCut;
} else {
var PageConsumers = PageConsumersCut.slice(0, PageConsumersEnd);
}
}
PageConsumers = PageConsumers.trim();
var Summary = "";
//change page if anything is different (this INCLUDES different formatting)
var NewContent = "";
if (PageConsumers == Consumers) {
console.log(ItemName + " page was not changed.")
} else {
if (PageConsumers.length > 0) {
var NewPageConsumersStart = OldContent.search(/(\s|\|)consumers/) + 10;
var NewPageConsumersCut = OldContent.slice(NewPageConsumersStart);
var NewPageConsumersEnd = NewPageConsumersCut.search(/\||}}/);
var ActualNewPageConsumersEnd = NewPageConsumersStart + NewPageConsumersEnd;
NewContent = OldContent.slice(0, NewPageConsumersStart) + " = " + Consumers + "\n" + OldContent.slice(ActualNewPageConsumersEnd) //I'm just assuming that PageRecipeEnd must exist because the infobox HAS to end with }}
console.log("Replaced " + ItemName + " consumers.");
Summary = Summary + "Updated consumers to " + version + ". ";
} else if (Consumers.length > 0) {
var InfoboxStart = OldContent.search(/{{Infobox/i) + 9;
NewContent = OldContent.slice(0, InfoboxStart) + "|consumers = " + Consumers + "\n" + OldContent.slice(InfoboxStart)
console.log("Added " + ItemName + " consumers to the start of the infobox.");
Summary = Summary + "Added consumers. ";
}
}
//alright, NewContent should be defined, change page:
if (NewContent.length > 0) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'edit',
title: ItemName + '/infobox',
text: NewContent,
token: globalToken,
summary: Summary,
bot: true,
nocreate: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Updated " + ItemName);
},
error: function( xhr ) {
console.log("Failed to update " + ItemName);
}
});
}
}
}
$("#TechUpdate").click(function(){
getTechnologies();
});
function getTechnologies() {
getUserGroup();
if (UserGroup.some(isBot) == false) {
return;
}
var TechInput = prompt("Please enter the technologies");
if (TechInput != null) {
getToken();
var Techs = TechInput.split(/\s\s/g);
console.log(Techs.length + " Techs detected");
Techs.forEach(UpdateTechnologyInfobox);
}
}
function UpdateTechnologyInfobox(Tech) {
var TechNameEnd = Tech.search("\\|");
var TechName = Tech.slice(0, TechNameEnd);
TechName = TechName.trim();
var CostStart = Tech.search("\\|cost = ") + 8;
if (CostStart < 8) {
console.log(TechName + ": No cost found.");
var Cost = "";
} else {
var CostCut = Tech.slice(CostStart);
var CostEnd = CostCut.search(/\||}}/);
if (CostEnd < 1) {
var Cost = CostCut;
} else {
var Cost = CostCut.slice(0, CostEnd);
}
}
var CostMultiStart = Tech.search("\\|cost-multiplier = ") + 19;
if (CostMultiStart < 19) {
console.log(TechName + ": No cost-multiplier found.");
var CostMulti = "";
} else {
var CostMultiCut = Tech.slice(CostMultiStart);
var CostMultiEnd = CostMultiCut.search(/\||}}/);
if (CostMultiEnd < 1) {
var CostMulti = CostMultiCut;
} else {
var CostMulti = CostMultiCut.slice(0, CostMultiEnd);
}
}
var ExpCostMultiStart = Tech.search("\\|expensive-cost-multiplier = ") + 29;
if (ExpCostMultiStart < 29) {
console.log(TechName + ": No expensive-cost-multiplier found.");
var ExpCostMulti = "";
} else {
var ExpCostMultiCut = Tech.slice(ExpCostMultiStart);
var ExpCostMultiEnd = ExpCostMultiCut.search(/\||}}/);
if (ExpCostMultiEnd < 1) {
var ExpCostMulti = ExpCostMultiCut;
} else {
var ExpCostMulti = ExpCostMultiCut.slice(0, ExpCostMultiEnd);
}
}
var ReqTechStart = Tech.search("\\|required-technologies = ") + 25;
if (ReqTechStart < 25) {
console.log(TechName + ": No required-technologies found.");
var ReqTech = "";
} else {
var ReqTechCut = Tech.slice(ReqTechStart);
var ReqTechEnd = ReqTechCut.search(/\||}}/);
if (ReqTechEnd < 1) {
var ReqTech = ReqTechCut;
} else {
var ReqTech = ReqTechCut.slice(0, ReqTechEnd);
}
}
var AllowsStart = Tech.search("\\|allows = ") + 6;
if (AllowsStart < 6) {
console.log(TechName + ": No allows found.");
var Allows = "";
} else {
var AllowsCut = Tech.slice(AllowsStart);
var AllowsEnd = AllowsCut.search(/\||}}/);
if (AllowsEnd < 1) {
var Allows = AllowsCut;
} else {
var Allows = AllowsCut.slice(0, AllowsEnd);
}
}
var EffectsStart = Tech.search("\\|effects = ") + 11;
if (EffectsStart < 11) {
console.log(TechName + ": No effects found.");
var Effects = "";
} else {
var EffectsCut = Tech.slice(EffectsStart);
var EffectsEnd = EffectsCut.search(/\||}}/);
if (EffectsEnd < 1) {
var Effects = EffectsCut;
} else {
var Effects = EffectsCut.slice(0, EffectsEnd);
}
}
Cost = Cost.trim();
CostMulti = CostMulti.trim();
ExpCostMulti = ExpCostMulti.trim();
ReqTech = ReqTech.trim();
Allows = Allows.trim();
Effects = Effects.trim();
var OldContent = "";
//get page content of the tech -> 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) {
return;
}
//find costs etc in page (OldContent)
var PageCostStart = OldContent.search(/(\s|\|)cost/) + 5;
if (PageCostStart < 5) {
console.log(TechName + ": No cost found.");
var PageCost = "";
} else {
var PageCostCut = OldContent.slice(PageCostStart);
PageCostStart = PageCostCut.search(/\w/);
PageCostCut = PageCostCut.slice(PageCostStart);
var PageCostEnd = PageCostCut.search(/\||}}/);
if (PageCostEnd < 1) {
var PageCost = PageCostCut;
} else {
var PageCost = PageCostCut.slice(0, PageCostEnd);
}
}
var PageCostMultiStart = OldContent.search(/(\s|\|)cost-multiplier/) + 16;
if (PageCostMultiStart < 16) {
console.log(TechName + ": No cost-multiplier found.");
var PageCostMulti = "";
} else {
var PageCostMultiCut = OldContent.slice(PageCostMultiStart);
PageCostMultiStart = PageCostMultiCut.search(/\w/);
PageCostMultiCut = PageCostMultiCut.slice(PageCostMultiStart);
var PageCostMultiEnd = PageCostMultiCut.search(/\||}}/);
if (PageCostMultiEnd < 1) {
var PageCostMulti = PageCostMultiCut;
} else {
var PageCostMulti = PageCostMultiCut.slice(0, PageCostMultiEnd);
}
}
var PageExpCostMultiStart = OldContent.search(/(\s|\|)expensive-cost-multiplier/) + 26;
if (PageExpCostMultiStart < 26) {
console.log(TechName + ": No expensive-cost-multiplier found.");
var PageExpCostMulti = "";
} else {
var PageExpCostMultiCut = OldContent.slice(PageExpCostMultiStart);
PageExpCostMultiStart = PageExpCostMultiCut.search(/\w/);
PageExpCostMultiCut = PageExpCostMultiCut.slice(PageExpCostMultiStart);
var PageExpCostMultiEnd = PageExpCostMultiCut.search(/\||}}/);
if (PageExpCostMultiEnd < 1) {
var PageExpCostMulti = PageExpCostMultiCut;
} else {
var PageExpCostMulti = PageExpCostMultiCut.slice(0, PageExpCostMultiEnd);
}
}
var PageReqTechStart = OldContent.search(/(\s|\|)required-technologies/) + 22;
if (PageReqTechStart < 22) {
console.log(TechName + ": No required-technologies found.");
var PageReqTech = "";
} else {
var PageReqTechCut = OldContent.slice(PageReqTechStart);
PageReqTechStart = PageReqTechCut.search(/\w/);
PageReqTechCut = PageReqTechCut.slice(PageReqTechStart);
var PageReqTechEnd = PageReqTechCut.search(/\||}}/);
if (PageReqTechEnd < 1) {
var PageReqTech = PageReqTechCut;
} else {
var PageReqTech = PageReqTechCut.slice(0, PageReqTechEnd);
}
}
var PageAllowsStart = OldContent.search(/(\s|\|)allows/) + 7;
if (PageAllowsStart < 7) {
console.log(TechName + ": No allows found.");
var PageAllows = "";
} else {
var PageAllowsCut = OldContent.slice(PageAllowsStart);
PageAllowsStart = PageAllowsCut.search(/\w/);
PageAllowsCut = PageAllowsCut.slice(PageAllowsStart);
var PageAllowsEnd = PageAllowsCut.search(/\||}}/);
if (PageAllowsEnd < 1) {
var PageAllows = PageAllowsCut;
} else {
var PageAllows = PageAllowsCut.slice(0, PageAllowsEnd);
}
}
var PageEffectsStart = OldContent.search(/(\s|\|)effects/) + 8;
if (PageEffectsStart < 8) {
console.log(TechName + ": No effects found.");
var PageEffects = "";
} else {
var PageEffectsCut = OldContent.slice(PageEffectsStart);
PageEffectsStart = PageEffectsCut.search(/\w/);
PageEffectsCut = PageEffectsCut.slice(PageEffectsStart);
var PageEffectsEnd = PageEffectsCut.search(/\||}}/);
if (PageEffectsEnd < 1) {
var PageEffects = PageEffectsCut;
} else {
var PageEffects = PageEffectsCut.slice(0, PageEffectsEnd);
}
}
PageCost = PageCost.trim();
PageCostMulti = PageCostMulti.trim();
PageExpCostMulti = PageExpCostMulti.trim();
PageReqTech = PageReqTech.trim();
PageAllows = PageAllows.trim();
PageEffects = PageEffects.trim();
var Summary = "";
var 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) {
if (PageCost.length > 0) {
var NewPageCostStart = OldContent.search(/(\s|\|)cost/) + 5;
var NewPageCostCut = OldContent.slice(NewPageCostStart);
var NewPageCostEnd = NewPageCostCut.search(/\||}}/) + NewPageCostStart;
if (Cost.length > 0) {
NewContent = OldContent.slice(0, NewPageCostStart) + " = " + Cost + "\n" + OldContent.slice(NewPageCostEnd);
console.log("Replaced " + TechName + " cost.");
Summary = Summary + "Updated cost to " + version + ". ";
} else {
NewPageCostStart = NewPageCostStart - 5;
NewContent = OldContent.slice(0, NewPageCostStart) + OldContent.slice(NewPageCostEnd);
console.log("Removed " + ItemName + " cost.");
Summary = Summary + "Removed cost. ";
}
} else if (Cost.length > 0) {
var InfoboxStart = OldContent.search(/{{Infobox/i) + 9;
NewContent = OldContent.slice(0, InfoboxStart) + "\n|cost = " + Cost + OldContent.slice(InfoboxStart);
console.log("Added " + ItemName + " cost to the start of the infobox.");
Summary = Summary + "Added cost. ";
}
}
if (PageCostMulti != CostMulti) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageCostMulti.length > 0) {
var NewPageCostMultiStart = NewContent.search(/(\s|\|)cost-multiplier/) + 16;
var NewPageCostMultiCut = NewContent.slice(NewPageCostMultiStart);
var NewPageCostMultiEnd = NewPageCostMultiCut.search(/\||}}/) + NewPageCostMultiStart;
if (CostMulti.length > 0) {
NewContent = NewContent.slice(0, NewPageCostMultiStart) + " = " + CostMulti + "\n" + NewContent.slice(NewPageCostMultiEnd);
console.log("Replaced " + TechName + " cost-multiplier.");
Summary = Summary + "Updated cost-multiplier to " + version + ". ";
} else {
NewPageCostMultiStart = NewPageCostMultiStart - 16;
NewContent = NewContent.slice(0, NewPageCostMultiStart) + NewContent.slice(NewPageCostMultiEnd);
console.log("Removed " + TechName + " cost-multiplier. ");
Summary = Summary + "Removed cost-multiplier. ";
}
} else if (CostMulti.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|cost-multiplier = " + CostMulti + NewContent.slice(InfoboxStart);
console.log("Added " + TechName + " cost-multiplier. ");
Summary = Summary + "Added cost-multiplier. ";
}
}
if (PageCostMulti != CostMulti) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageCostMulti.length > 0) {
var NewPageCostMultiStart = NewContent.search(/(\s|\|)cost-multiplier/) + 16;
var NewPageCostMultiCut = NewContent.slice(NewPageCostMultiStart);
var NewPageCostMultiEnd = NewPageCostMultiCut.search(/\||}}/) + NewPageCostMultiStart;
if (CostMulti.length > 0) {
NewContent = NewContent.slice(0, NewPageCostMultiStart) + " = " + CostMulti + "\n" + NewContent.slice(NewPageCostMultiEnd);
console.log("Replaced " + TechName + " cost-multiplier.");
Summary = Summary + "Updated cost-multiplier to " + version + ". ";
} else {
NewPageCostMultiStart = NewPageCostMultiStart - 16;
NewContent = NewContent.slice(0, NewPageCostMultiStart) + NewContent.slice(NewPageCostMultiEnd);
console.log("Removed " + TechName + " cost-multiplier. ");
Summary = Summary + "Removed cost-multiplier. ";
}
} else if (CostMulti.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|cost-multiplier = " + CostMulti + NewContent.slice(InfoboxStart);
console.log("Added " + TechName + " cost-multiplier. ");
Summary = Summary + "Added cost-multiplier. ";
}
}
if (PageExpCostMulti != ExpCostMulti) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageExpCostMulti.length > 0) {
var NewPageExpCostMultiStart = NewContent.search(/(\s|\|)expensive-cost-multiplier/) + 26;
var NewPageExpCostMultiCut = NewContent.slice(NewPageExpCostMultiStart);
var NewPageExpCostMultiEnd = NewPageExpCostMultiCut.search(/\||}}/) + NewPageExpCostMultiStart;
if (ExpCostMulti.length > 0) {
NewContent = NewContent.slice(0, NewPageExpCostMultiStart) + " = " + ExpCostMulti + "\n" + NewContent.slice(NewPageExpCostMultiEnd);
console.log("Replaced " + TechName + " expensive-cost-multiplier.");
Summary = Summary + "Updated expensive-cost-multiplier to " + version + ". ";
} else {
NewPageExpCostMultiStart = NewPageExpCostMultiStart - 26;
NewContent = NewContent.slice(0, NewPageExpCostMultiStart) + NewContent.slice(NewPageExpCostMultiEnd);
console.log("Removed " + TechName + " expensive-cost-multiplier. ");
Summary = Summary + "Removed expensive-cost-multiplier. ";
}
} else if (ExpCostMulti.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|expensive-cost-multiplier = " + ExpCostMulti + NewContent.slice(InfoboxStart);
console.log("Added " + TechName + " expensive-cost-multiplier. ");
Summary = Summary + "Added expensive-cost-multiplier. ";
}
}
if (PageReqTech != ReqTech) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageReqTech.length > 0) {
var NewPageReqTechStart = NewContent.search(/(\s|\|)required-technologies/) + 22;
var NewPageReqTechCut = NewContent.slice(NewPageReqTechStart);
var NewPageReqTechEnd = NewPageReqTechCut.search(/\||}}/) + NewPageReqTechStart;
if (ReqTech.length > 0) {
NewContent = NewContent.slice(0, NewPageReqTechStart) + " = " + ReqTech + "\n" + NewContent.slice(NewPageReqTechEnd);
console.log("Replaced " + TechName + " required-technologies.");
Summary = Summary + "Updated required-technologies to " + version + ". ";
} else {
NewPageReqTechStart = NewPageReqTechStart - 22;
NewContent = NewContent.slice(0, NewPageReqTechStart) + NewContent.slice(NewPageReqTechEnd);
console.log("Removed " + TechName + " required-technologies. ");
Summary = Summary + "Removed required-technologies. ";
}
} else if (ReqTech.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|required-technologies = " + ReqTech + NewContent.slice(InfoboxStart);
console.log("Added " + TechName + " required-technologies. ");
Summary = Summary + "Added required-technologies. ";
}
}
if (PageAllows != Allows) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageAllows.length > 0) {
var NewPageAllowsStart = NewContent.search(/(\s|\|)allows/) + 7;
var NewPageAllowsCut = NewContent.slice(NewPageAllowsStart);
var NewPageAllowsEnd = NewPageAllowsCut.search(/\||}}/) + NewPageAllowsStart;
if (Allows.length > 0) {
NewContent = NewContent.slice(0, NewPageAllowsStart) + " = " + Allows + "\n" + NewContent.slice(NewPageAllowsEnd);
console.log("Replaced " + TechName + " allows.");
Summary = Summary + "Updated allows to " + version + ". ";
} else {
NewPageAllowsStart = NewPageAllowsStart - 7;
NewContent = NewContent.slice(0, NewPageAllowsStart) + NewContent.slice(NewPageAllowsEnd);
console.log("Removed " + TechName + " allows. ");
Summary = Summary + "Removed allows. ";
}
} else if (Allows.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|allows = " + Allows + NewContent.slice(InfoboxStart);
console.log("Added " + TechName + " allows. ");
Summary = Summary + "Added allows. ";
}
}
if (PageEffects != Effects) {
if (NewContent.length == 0) {
NewContent = OldContent
}
if (PageEffects.length > 0) {
var NewPageEffectsStart = NewContent.search(/(\s|\|)effects/) + 8;
var NewPageEffectsCut = NewContent.slice(NewPageEffectsStart);
var NewPageEffectsEnd = NewPageEffectsCut.search(/\||}}/) + NewPageEffectsStart;
if (Effects.length > 0) {
NewContent = NewContent.slice(0, NewPageEffectsStart) + " = " + Effects + "\n" + NewContent.slice(NewPageEffectsEnd);
console.log("Replaced " + TechName + " effects.");
Summary = Summary + "Updated effects to " + version + ". ";
} else {
NewPageEffectsStart = NewPageEffectsStart - 8;
NewContent = NewContent.slice(0, NewPageEffectsStart) + NewContent.slice(NewPageEffectsEnd);
console.log("Removed " + TechName + " effects. ");
Summary = Summary + "Removed effects. ";
}
} else if (Effects.length > 0) {
var InfoboxStart = NewContent.search(/{{Infobox/i) + 9;
NewContent = NewContent.slice(0, InfoboxStart) + "\n|effects = " + Effects + NewContent.slice(InfoboxStart);
console.log("Added " + TechName + " effects. ");
Summary = Summary + "Added effects. ";
}
}
}
//alright, NewContent should be defined, change page:
if (NewContent.length > 0) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'edit',
title: TechName + ' (research)/infobox',
text: NewContent,
token: globalToken,
summary: Summary,
bot: true,
nocreate: true
},
async: false,
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Updated " + TechName);
},
error: function( xhr ) {
console.log("Failed to update " + TechName);
}
});
}
}
/* user is bot if UserGroup.some(isBot) == true */
var UserGroup = "";
function getUserGroup() {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
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";
}
var globalToken;
function getToken() {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
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 perform null edit");
}
});
}
/*** 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
$(".spoiler-container .button").click(function() {
$(this).siblings(".text").toggle("slow");
});
var wantedPagesListsLocation = "User:TheWombatGuru/WantedPages"
$("#create-wanted-pages-list").click(function(){
getToken();
createWantedPagesLists();
});
function createWantedPagesLists() {
getUserGroup();
if (UserGroup.some(isBot) == false) {
return;
}
var wantedPages = getWantedPages();
wantedPages = wantedPages.sort(compare);
splitWantedPagesIntoDifferentLanguages(wantedPages);
};
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) {
$.ajax({
url: 'https://wiki.factorio.com/api.php',
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: 'https://wiki.factorio.com/api.php',
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: 'https://wiki.factorio.com/api.php',
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");
}
});
}
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 */