User:BilkaBot/common.js: Difference between revisions
Jump to navigation
Jump to search
(removed no longer need script) |
(shorter code, update to new organization) |
||
Line 2: | Line 2: | ||
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"] | 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"] | ||
var version = "0.15. | var version = "0.15.30"; | ||
Line 14: | Line 14: | ||
var paraCut = item.slice(paraStart); //lets the string begin after the parameter name | var paraCut = item.slice(paraStart); //lets the string begin after the parameter name | ||
var paraEnd = paraCut.search(/\||}}/); //finds the end of the parameter definition | var paraEnd = paraCut.search(/\||}}/); //finds the end of the parameter definition | ||
para = paraEnd < 1 ? paraCut : paraCut.slice(0, paraEnd); //para ends at para-end if it exists | |||
} | } | ||
return para; | return para; | ||
Line 34: | Line 30: | ||
pageParaCut = pageParaCut.slice(pageParaStart); //removes anything before the parameter that does not belong there, like = and \s | pageParaCut = pageParaCut.slice(pageParaStart); //removes anything before the parameter that does not belong there, like = and \s | ||
var pageParaEnd = pageParaCut.search(/\||}}/); //finds the end of the parameter definition | var pageParaEnd = pageParaCut.search(/\||}}/); //finds the end of the parameter definition | ||
pagePara = pageParaEnd < 1 ? pageParaCut : pageParaCut.slice(0, pageParaEnd); //pagePara ends at para-end if it exists | |||
} | } | ||
return pagePara; | return pagePara; | ||
Line 84: | Line 76: | ||
type: 'POST', | type: 'POST', | ||
success: function( data ) { | success: function( data ) { | ||
console.log("Updated " + itemName); | |||
}, | }, | ||
error: function( xhr ) { | error: function( xhr ) { | ||
Line 99: | Line 91: | ||
function getRecipes() { | function getRecipes() { | ||
getUserGroup(); | getUserGroup(); | ||
if (userGroup.some(isBot) == | if (userGroup.some(isBot) == false) return; | ||
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); | |||
} | } | ||
}; | }; | ||
Line 113: | Line 104: | ||
function removeDuplicateRecipesAndUpdateInfobox(recipes) { | function removeDuplicateRecipesAndUpdateInfobox(recipes) { | ||
var itemNameEnd = recipes.search("\\|"); | var itemNameEnd = recipes.search("\\|"); | ||
var itemName = recipes.slice(0, itemNameEnd) | var itemName = recipes.slice(0, itemNameEnd).trim(); | ||
//Remove Itemnames if the item does not have a page on the wiki, so that the item is removed from the output | //Remove Itemnames if the item does not have a page on the wiki, so that the item is removed from the output | ||
Line 127: | Line 117: | ||
} | } | ||
var recipe = getInputPara(recipes, "\\|recipe = ", 10, "recipe", itemName); | var recipe = getInputPara(recipes, "\\|recipe = ", 10, "recipe", itemName).trim(); | ||
var totalRaw = getInputPara(recipes, "\\|total-raw = ", 13, "total-raw", itemName); | var totalRaw = getInputPara(recipes, "\\|total-raw = ", 13, "total-raw", itemName).trim(); | ||
var expRecipe = getInputPara(recipes, "\\|expensive-recipe = ", 20, "expensive-recipe", itemName); | var expRecipe = getInputPara(recipes, "\\|expensive-recipe = ", 20, "expensive-recipe", itemName).trim(); | ||
var expTotalRaw = getInputPara(recipes, "\\|expensive-total-raw = ", 23, "expensive-total-raw", itemName) | var expTotalRaw = getInputPara(recipes, "\\|expensive-total-raw = ", 23, "expensive-total-raw", itemName).trim(); | ||
//remove duplicate recipes, but only if the recipe actually exists | //remove duplicate recipes, but only if the recipe actually exists | ||
Line 164: | Line 147: | ||
format: 'json', | format: 'json', | ||
action: 'query', | action: 'query', | ||
titles: | titles: 'Infobox:' + itemName, | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content' | rvprop: 'content' | ||
Line 175: | Line 158: | ||
var revisions = pages[Object.keys(pages)[0]].revisions[0]; | var revisions = pages[Object.keys(pages)[0]].revisions[0]; | ||
oldContent = revisions[Object.keys(revisions)[2]]; | oldContent = revisions[Object.keys(revisions)[2]]; | ||
}, | }, | ||
error: function( xhr ) { | error: function( xhr ) { | ||
Line 192: | Line 174: | ||
var recipeLength = match == null ? 0 : match.toString().length; //var value = *condition* ? *true* : *false*; | var recipeLength = match == null ? 0 : match.toString().length; //var value = *condition* ? *true* : *false*; | ||
var pageRecipeStart = oldContent.search(/\|\s*recipe/) + recipeLength; //so that it doesn't find "|prototype-type = recipe" | var pageRecipeStart = oldContent.search(/\|\s*recipe/) + recipeLength; //so that it doesn't find "|prototype-type = recipe" | ||
var pageRecipe = getOldPara(oldContent, pageRecipeStart, recipeLength, "recipe", itemName); | var pageRecipe = getOldPara(oldContent, pageRecipeStart, recipeLength, "recipe", itemName).trim(); | ||
var pageTotalRawStart = oldContent.search(/(\s|\|)total-raw/) + 10; | var pageTotalRawStart = oldContent.search(/(\s|\|)total-raw/) + 10; | ||
var pageTotalRaw = getOldPara(oldContent, pageTotalRawStart, 10, "total-raw", itemName); | var pageTotalRaw = getOldPara(oldContent, pageTotalRawStart, 10, "total-raw", itemName).trim(); | ||
var pageExpRecipeStart = oldContent.search(/(\s|\|)expensive-recipe/) + 17; | var pageExpRecipeStart = oldContent.search(/(\s|\|)expensive-recipe/) + 17; | ||
var pageExpRecipe = getOldPara(oldContent, pageExpRecipeStart, 17, "expensive-recipe", itemName); | var pageExpRecipe = getOldPara(oldContent, pageExpRecipeStart, 17, "expensive-recipe", itemName).trim(); | ||
var pageExpTotalRawStart = oldContent.search(/(\s|\|)expensive-total-raw/) + 20; | var pageExpTotalRawStart = oldContent.search(/(\s|\|)expensive-total-raw/) + 20; | ||
var pageExpTotalRaw = getOldPara(oldContent, pageExpTotalRawStart, 20, "expensive-total-raw", itemName) | var pageExpTotalRaw = getOldPara(oldContent, pageExpTotalRawStart, 20, "expensive-total-raw", itemName).trim(); | ||
summary = ""; | summary = ""; | ||
Line 223: | Line 198: | ||
} | } | ||
if (pageTotalRaw != totalRaw) { | if (pageTotalRaw != totalRaw) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageTotalRawStart = newContent.search(/(\s|\|)total-raw/) + 10; | var newPageTotalRawStart = newContent.search(/(\s|\|)total-raw/) + 10; | ||
updatePara(newContent, totalRaw, pageTotalRaw, "total-raw", newPageTotalRawStart, 10, itemName); | updatePara(newContent, totalRaw, pageTotalRaw, "total-raw", newPageTotalRawStart, 10, itemName); | ||
} | } | ||
if (pageExpRecipe != expRecipe) { | if (pageExpRecipe != expRecipe) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageExpRecipeStart = newContent.search(/(\s|\|)expensive-recipe/) + 17; | var newPageExpRecipeStart = newContent.search(/(\s|\|)expensive-recipe/) + 17; | ||
updatePara(newContent, expRecipe, pageExpRecipe, "expensive-recipe", newPageExpRecipeStart, 17, itemName); | updatePara(newContent, expRecipe, pageExpRecipe, "expensive-recipe", newPageExpRecipeStart, 17, itemName); | ||
} | } | ||
if (pageExpTotalRaw != expTotalRaw) { | if (pageExpTotalRaw != expTotalRaw) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageExpTotalRawStart = newContent.search(/(\s|\|)expensive-total-raw/) + 20; | var newPageExpTotalRawStart = newContent.search(/(\s|\|)expensive-total-raw/) + 20; | ||
updatePara(newContent, expTotalRaw, pageExpTotalRaw, "expensive-total-raw", newPageExpTotalRawStart, 20, itemName); | updatePara(newContent, expTotalRaw, pageExpTotalRaw, "expensive-total-raw", newPageExpTotalRawStart, 20, itemName); | ||
Line 245: | Line 214: | ||
} | } | ||
//alright, newContent should be defined, change page: | //alright, newContent should be defined, change page: | ||
if (newContent.length > 0) | if (newContent.length > 0) editPage("Infobox:" + itemName, itemName); | ||
} | } | ||
Line 256: | Line 223: | ||
function getItems() { | function getItems() { | ||
getUserGroup(); | getUserGroup(); | ||
if (userGroup.some(isBot) == false) | if (userGroup.some(isBot) == false) return; | ||
var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies"); | var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies"); | ||
if (itemInput != null) { | if (itemInput != null) { | ||
Line 270: | Line 235: | ||
function updateItemInfoboxes(item) { | function updateItemInfoboxes(item) { | ||
var itemNameEnd = item.search("\\|"); | var itemNameEnd = item.search("\\|"); | ||
var itemName = item.slice(0, itemNameEnd) | var itemName = item.slice(0, itemNameEnd).trim(); | ||
//Remove items that don't have Infoboxes on the wiki | //Remove items that don't have Infoboxes on the wiki | ||
Line 280: | Line 244: | ||
} | } | ||
}) | }) | ||
if (itemName.length == 0) | if (itemName.length == 0) return; | ||
var consumers = getInputPara(item, "\\|consumers = ", 13, "consumers", itemName).trim(); | |||
var stackSize = getInputPara(item, "\\|stack-size = ", 14, "stack-size", itemName).trim(); | |||
var reqTech = getInputPara(item, "\\|required-technologies = ", 25, "required-technologies", itemName).trim(); | |||
//get page content of the item -> oldContent | //get page content of the item -> oldContent | ||
Line 300: | Line 257: | ||
format: 'json', | format: 'json', | ||
action: 'query', | action: 'query', | ||
titles: | titles: 'Infobox:' + itemName, | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content' | rvprop: 'content' | ||
Line 311: | Line 268: | ||
var revisions = pages[Object.keys(pages)[0]].revisions[0]; | var revisions = pages[Object.keys(pages)[0]].revisions[0]; | ||
oldContent = revisions[Object.keys(revisions)[2]]; | oldContent = revisions[Object.keys(revisions)[2]]; | ||
}, | }, | ||
error: function( xhr ) { | error: function( xhr ) { | ||
Line 325: | Line 281: | ||
//find recipes in page (oldContent) | //find recipes in page (oldContent) | ||
var pageConsumersStart = oldContent.search(/(\s|\|)consumers/) + 10; | var pageConsumersStart = oldContent.search(/(\s|\|)consumers/) + 10; | ||
var pageConsumers = getOldPara(oldContent, pageConsumersStart, 10, "consumers", itemName); | var pageConsumers = getOldPara(oldContent, pageConsumersStart, 10, "consumers", itemName).trim(); | ||
var pageStackSizeStart = oldContent.search(/(\s|\|)stack-size/) + 11; | var pageStackSizeStart = oldContent.search(/(\s|\|)stack-size/) + 11; | ||
var pageStackSize = getOldPara(oldContent, pageStackSizeStart, 11, "stack-size", itemName); | var pageStackSize = getOldPara(oldContent, pageStackSizeStart, 11, "stack-size", itemName).trim(); | ||
var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22; | var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22; | ||
var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", itemName) | var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", itemName).trim(); | ||
Line 350: | Line 301: | ||
} | } | ||
if (pageStackSize != stackSize) { | if (pageStackSize != stackSize) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageStackSizeStart = newContent.search(/(\s|\|)stack-size/) + 11; | var newPageStackSizeStart = newContent.search(/(\s|\|)stack-size/) + 11; | ||
updatePara(newContent, stackSize, pageStackSize, "stack-size", newPageStackSizeStart, 11, itemName); | updatePara(newContent, stackSize, pageStackSize, "stack-size", newPageStackSizeStart, 11, itemName); | ||
} | } | ||
if (pageReqTech != reqTech) { | if (pageReqTech != reqTech) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageReqTechStart = newContent.search(/(\s|\|)required-technologies/) + 22; | var newPageReqTechStart = newContent.search(/(\s|\|)required-technologies/) + 22; | ||
updatePara(newContent, reqTech, pageReqTech, "required-technologies", newPageReqTechStart, 22, itemName); | updatePara(newContent, reqTech, pageReqTech, "required-technologies", newPageReqTechStart, 22, itemName); | ||
Line 366: | Line 313: | ||
//alright, newContent should be defined, change page: | //alright, newContent should be defined, change page: | ||
if (newContent.length > 0) | if (newContent.length > 0) editPage("Infobox:" + itemName, itemName); | ||
} | } | ||
Line 377: | Line 322: | ||
function getTechnologies() { | function getTechnologies() { | ||
getUserGroup(); | getUserGroup(); | ||
if (userGroup.some(isBot) == false) return; | |||
var techInput = prompt("Please enter the technologies"); | var techInput = prompt("Please enter the technologies"); | ||
if (techInput != null) { | if (techInput != null) { | ||
Line 391: | Line 334: | ||
function updateTechnologyInfobox(tech) { | function updateTechnologyInfobox(tech) { | ||
var techNameEnd = tech.search("\\|"); | var techNameEnd = tech.search("\\|"); | ||
var techName = tech.slice(0, techNameEnd) | var techName = tech.slice(0, techNameEnd).trim(); | ||
var cost = getInputPara(tech, "\\|cost = ", 8, "cost", techName); | var cost = getInputPara(tech, "\\|cost = ", 8, "cost", techName).trim(); | ||
var costMulti = getInputPara(tech, "\\|cost-multiplier = ", 19, "cost-multiplier", techName); | var costMulti = getInputPara(tech, "\\|cost-multiplier = ", 19, "cost-multiplier", techName).trim(); | ||
var expCostMulti = getInputPara(tech, "\\|expensive-cost-multiplier = ", 29, "expensive-cost-multiplier", techName); | var expCostMulti = getInputPara(tech, "\\|expensive-cost-multiplier = ", 29, "expensive-cost-multiplier", techName).trim(); | ||
var reqTech = getInputPara(tech, "\\|required-technologies = ", 25, "required-technologies", techName); | var reqTech = getInputPara(tech, "\\|required-technologies = ", 25, "required-technologies", techName).trim(); | ||
var allows = getInputPara(tech, "\\|allows = ", 10, "allows", techName); | var allows = getInputPara(tech, "\\|allows = ", 10, "allows", techName).trim(); | ||
var effects = getInputPara(tech, "\\|effects = ", 11, "effects", techName) | var effects = getInputPara(tech, "\\|effects = ", 11, "effects", techName).trim(); | ||
Line 418: | Line 352: | ||
format: 'json', | format: 'json', | ||
action: 'query', | action: 'query', | ||
titles: techName + ' (research) | titles: 'Infobox:' + techName + ' (research)', | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content' | rvprop: 'content' | ||
Line 429: | Line 363: | ||
var revisions = pages[Object.keys(pages)[0]].revisions[0]; | var revisions = pages[Object.keys(pages)[0]].revisions[0]; | ||
oldContent = revisions[Object.keys(revisions)[2]]; | oldContent = revisions[Object.keys(revisions)[2]]; | ||
}, | }, | ||
error: function( xhr ) { | error: function( xhr ) { | ||
Line 444: | Line 377: | ||
//find costs etc in page (oldContent) | //find costs etc in page (oldContent) | ||
var pageCostStart = oldContent.search(/(\s|\|)cost(\s|=)/) + 6; | var pageCostStart = oldContent.search(/(\s|\|)cost(\s|=)/) + 6; | ||
var pageCost = getOldPara(oldContent, pageCostStart, 6, "cost", techName); | var pageCost = getOldPara(oldContent, pageCostStart, 6, "cost", techName).trim(); | ||
var pageCostMultiStart = oldContent.search(/(\s|\|)cost-multiplier/) + 16; | var pageCostMultiStart = oldContent.search(/(\s|\|)cost-multiplier/) + 16; | ||
var pageCostMulti = getOldPara(oldContent, pageCostMultiStart, 16, "cost-multiplier", techName); | var pageCostMulti = getOldPara(oldContent, pageCostMultiStart, 16, "cost-multiplier", techName).trim(); | ||
var pageExpCostMultiStart = oldContent.search(/(\s|\|)expensive-cost-multiplier/) + 26; | var pageExpCostMultiStart = oldContent.search(/(\s|\|)expensive-cost-multiplier/) + 26; | ||
var pageExpCostMulti = getOldPara(oldContent, pageExpCostMultiStart, 26, "expensive-cost-multiplier", techName); | var pageExpCostMulti = getOldPara(oldContent, pageExpCostMultiStart, 26, "expensive-cost-multiplier", techName).trim(); | ||
var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22; | var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22; | ||
var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", techName); | var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", techName).trim(); | ||
var pageAllowsStart = oldContent.search(/(\s|\|)allows/) + 7; | var pageAllowsStart = oldContent.search(/(\s|\|)allows/) + 7; | ||
var pageAllows = getOldPara(oldContent, pageAllowsStart, 7, "allows", techName); | var pageAllows = getOldPara(oldContent, pageAllowsStart, 7, "allows", techName).trim(); | ||
var pageEffectsStart = oldContent.search(/(\s|\|)effects/) + 8; | var pageEffectsStart = oldContent.search(/(\s|\|)effects/) + 8; | ||
var pageEffects = getOldPara(oldContent, pageEffectsStart, 8, "effects", techName) | var pageEffects = getOldPara(oldContent, pageEffectsStart, 8, "effects", techName).trim(); | ||
Line 483: | Line 408: | ||
} | } | ||
if (pageCostMulti != costMulti) { | if (pageCostMulti != costMulti) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageCostMultiStart = newContent.search(/(\s|\|)cost-multiplier/) + 16; | var newPageCostMultiStart = newContent.search(/(\s|\|)cost-multiplier/) + 16; | ||
updatePara(newContent, costMulti, pageCostMulti, "cost-multiplier", newPageCostMultiStart, 16, techName); | updatePara(newContent, costMulti, pageCostMulti, "cost-multiplier", newPageCostMultiStart, 16, techName); | ||
} | } | ||
if (pageExpCostMulti != expCostMulti) { | if (pageExpCostMulti != expCostMulti) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageExpCostMultiStart = newContent.search(/(\s|\|)expensive-cost-multiplier/) + 26; | var newPageExpCostMultiStart = newContent.search(/(\s|\|)expensive-cost-multiplier/) + 26; | ||
updatePara(newContent, expCostMulti, pageExpCostMulti, "expensive-cost-multiplier", newPageExpCostMultiStart, 26, techName); | updatePara(newContent, expCostMulti, pageExpCostMulti, "expensive-cost-multiplier", newPageExpCostMultiStart, 26, techName); | ||
} | } | ||
if (pageReqTech != reqTech) { | if (pageReqTech != reqTech) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageReqTechStart = newContent.search(/(\s|\|)required-technologies/) + 22; | var newPageReqTechStart = newContent.search(/(\s|\|)required-technologies/) + 22; | ||
updatePara(newContent, reqTech, pageReqTech, "required-technologies", newPageReqTechStart, 22, techName); | updatePara(newContent, reqTech, pageReqTech, "required-technologies", newPageReqTechStart, 22, techName); | ||
} | } | ||
if (pageAllows != allows) { | if (pageAllows != allows) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageAllowsStart = newContent.search(/(\s|\|)allows/) + 7; | var newPageAllowsStart = newContent.search(/(\s|\|)allows/) + 7; | ||
updatePara(newContent, allows, pageAllows, "allows", newPageAllowsStart, 7, techName); | updatePara(newContent, allows, pageAllows, "allows", newPageAllowsStart, 7, techName); | ||
} | } | ||
if (pageEffects != effects) { | if (pageEffects != effects) { | ||
if (newContent.length == 0) | if (newContent.length == 0) newContent = oldContent; | ||
var newPageEffectsStart = newContent.search(/(\s|\|)effects/) + 8; | var newPageEffectsStart = newContent.search(/(\s|\|)effects/) + 8; | ||
updatePara(newContent, effects, pageEffects, "effects", newPageEffectsStart, 8, techName); | updatePara(newContent, effects, pageEffects, "effects", newPageEffectsStart, 8, techName); | ||
Line 519: | Line 434: | ||
} | } | ||
//alright, newContent should be defined, change page: | //alright, newContent should be defined, change page: | ||
if (newContent.length > 0) | if (newContent.length > 0) editPage("Infobox:" + techName + " (research)", techName); | ||
} | } | ||
Line 530: | Line 443: | ||
function getAll() { | function getAll() { | ||
getUserGroup(); | getUserGroup(); | ||
if (userGroup.some(isBot) == false) return; | |||
getToken(); | getToken(); | ||
newVersion = prompt("Please enter the new version."); | newVersion = prompt("Please enter the new version."); | ||
if (newVersion != null) | if (newVersion != null) version = newVersion.trim(); | ||
var recipeInput = prompt("Please enter the recipes."); | var recipeInput = prompt("Please enter the recipes."); | ||
var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies."); | var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies."); | ||
Line 589: | Line 498: | ||
format: 'json', | format: 'json', | ||
action: 'query', | action: 'query', | ||
titles: techName + ' (research) | titles: 'Infobox:' + techName + ' (research)', | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content' | rvprop: 'content' | ||
Line 638: | Line 547: | ||
//alright, newContent should be defined, change page: | //alright, newContent should be defined, change page: | ||
if (newContent.length > 0) { | if (newContent.length > 0) { | ||
editPage(techName + " (research) | editPage("Infobox:" + techName + " (research)", techName); | ||
} | } | ||
} | } | ||
Line 681: | Line 590: | ||
format: 'json', | format: 'json', | ||
action: 'query', | action: 'query', | ||
titles: | titles: 'Infobox:' + itemName, | ||
prop: 'revisions', | prop: 'revisions', | ||
rvprop: 'content' | rvprop: 'content' | ||
Line 725: | Line 634: | ||
} | } | ||
} | } | ||
if (newContent.length > 0) editPage( | if (newContent.length > 0) editPage("Infobox:" + itemName, itemName); | ||
} | } | ||
/*** other.js ***/ | /*** other.js ***/ |
Revision as of 09:28, 27 July 2017
/* 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"]
var version = "0.15.30";
var para = "";
function getInputPara(item, search, length, name, itemName) {
var paraStart = item.search(search) + length; //finds the beginning of the para, is after para-name
if (paraStart < length) {
console.log(itemName + ": No " + name + " found.");
para = "";
} 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
para = paraEnd < 1 ? paraCut : paraCut.slice(0, paraEnd); //para ends at para-end if it exists
}
return para;
};
var pagePara = "";
function getOldPara(content, pageParaStart, length, name, itemName) {
if (pageParaStart < length) { //if the start is less than the length of the para name (name) then the search was unsuccessful
console.log(itemName + ": No " + name + " found on page.");
pagePara = "";
} else {
var pageParaCut = content.slice(pageParaStart);
pageParaStart = pageParaCut.search(/\w/);
pageParaCut = pageParaCut.slice(pageParaStart); //removes anything before the parameter that does not belong there, like = and \s
var pageParaEnd = pageParaCut.search(/\||}}/); //finds the end of the parameter definition
pagePara = pageParaEnd < 1 ? pageParaCut : pageParaCut.slice(0, pageParaEnd); //pagePara ends at para-end if it exists
}
return pagePara;
};
var summary = "";
var newContent = "";
function updatePara(content, para, pagePara, name, newPageParaStart, length, itemName) { //also needs version, summary, changes newContent and summary
if (pagePara.length > 0) {
var newPageParaCut = content.slice(newPageParaStart); //lets the string being after the parameter name
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 + ". ";
}
};
function editPage(title, itemName) { //also uses summary, globalToken, newContent
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'edit',
title: title,
text: newContent,
token: globalToken,
summary: summary,
bot: true,
nocreate: true
},
dataType: 'json',
type: 'POST',
success: function( data ) {
console.log("Updated " + itemName);
},
error: function( xhr ) {
console.log("Failed to update " + itemName);
}
});
};
$("#RecipeUpdate").click(function(){
getRecipes();
});
function getRecipes() {
getUserGroup();
if (userGroup.some(isBot) == false) return;
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 removeDuplicateRecipesAndUpdateInfobox(recipes) {
var itemNameEnd = recipes.search("\\|");
var itemName = recipes.slice(0, itemNameEnd).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).trim();
var totalRaw = getInputPara(recipes, "\\|total-raw = ", 13, "total-raw", itemName).trim();
var expRecipe = getInputPara(recipes, "\\|expensive-recipe = ", 20, "expensive-recipe", itemName).trim();
var expTotalRaw = getInputPara(recipes, "\\|expensive-total-raw = ", 23, "expensive-total-raw", itemName).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: 'Infobox:' + itemName,
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]];
},
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 match = oldContent.match(/\|\s*recipe/);
var recipeLength = match == null ? 0 : match.toString().length; //var value = *condition* ? *true* : *false*;
var pageRecipeStart = oldContent.search(/\|\s*recipe/) + recipeLength; //so that it doesn't find "|prototype-type = recipe"
var pageRecipe = getOldPara(oldContent, pageRecipeStart, recipeLength, "recipe", itemName).trim();
var pageTotalRawStart = oldContent.search(/(\s|\|)total-raw/) + 10;
var pageTotalRaw = getOldPara(oldContent, pageTotalRawStart, 10, "total-raw", itemName).trim();
var pageExpRecipeStart = oldContent.search(/(\s|\|)expensive-recipe/) + 17;
var pageExpRecipe = getOldPara(oldContent, pageExpRecipeStart, 17, "expensive-recipe", itemName).trim();
var pageExpTotalRawStart = oldContent.search(/(\s|\|)expensive-total-raw/) + 20;
var pageExpTotalRaw = getOldPara(oldContent, pageExpTotalRawStart, 20, "expensive-total-raw", itemName).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("Infobox:" + itemName, itemName);
}
$("#ItemUpdate").click(function(){
getItems();
});
function getItems() {
getUserGroup();
if (userGroup.some(isBot) == false) return;
var itemInput = prompt("Please enter the consumers, stack-sizes and required-technologies");
if (itemInput != null) {
getToken();
var items = itemInput.split(/\s\s/g);
console.log(items.length + " items detected");
items.forEach(updateItemInfoboxes);
}
};
function updateItemInfoboxes(item) {
var itemNameEnd = item.search("\\|");
var itemName = item.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 consumers = getInputPara(item, "\\|consumers = ", 13, "consumers", itemName).trim();
var stackSize = getInputPara(item, "\\|stack-size = ", 14, "stack-size", itemName).trim();
var reqTech = getInputPara(item, "\\|required-technologies = ", 25, "required-technologies", 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: 'Infobox:' + itemName,
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]];
},
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).trim();
var pageStackSizeStart = oldContent.search(/(\s|\|)stack-size/) + 11;
var pageStackSize = getOldPara(oldContent, pageStackSizeStart, 11, "stack-size", itemName).trim();
var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22;
var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", itemName).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("Infobox:" + itemName, 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).trim();
var cost = getInputPara(tech, "\\|cost = ", 8, "cost", techName).trim();
var costMulti = getInputPara(tech, "\\|cost-multiplier = ", 19, "cost-multiplier", techName).trim();
var expCostMulti = getInputPara(tech, "\\|expensive-cost-multiplier = ", 29, "expensive-cost-multiplier", techName).trim();
var reqTech = getInputPara(tech, "\\|required-technologies = ", 25, "required-technologies", techName).trim();
var allows = getInputPara(tech, "\\|allows = ", 10, "allows", techName).trim();
var effects = getInputPara(tech, "\\|effects = ", 11, "effects", techName).trim();
//get page content of the tech -> oldContent
var oldContent = "";
$.ajax({
url: 'https://wiki.factorio.com/api.php',
data: {
format: 'json',
action: 'query',
titles: 'Infobox:' + techName + ' (research)',
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]];
},
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).trim();
var pageCostMultiStart = oldContent.search(/(\s|\|)cost-multiplier/) + 16;
var pageCostMulti = getOldPara(oldContent, pageCostMultiStart, 16, "cost-multiplier", techName).trim();
var pageExpCostMultiStart = oldContent.search(/(\s|\|)expensive-cost-multiplier/) + 26;
var pageExpCostMulti = getOldPara(oldContent, pageExpCostMultiStart, 26, "expensive-cost-multiplier", techName).trim();
var pageReqTechStart = oldContent.search(/(\s|\|)required-technologies/) + 22;
var pageReqTech = getOldPara(oldContent, pageReqTechStart, 22, "required-technologies", techName).trim();
var pageAllowsStart = oldContent.search(/(\s|\|)allows/) + 7;
var pageAllows = getOldPara(oldContent, pageAllowsStart, 7, "allows", techName).trim();
var pageEffectsStart = oldContent.search(/(\s|\|)effects/) + 8;
var pageEffects = getOldPara(oldContent, pageEffectsStart, 8, "effects", techName).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("Infobox:" + techName + " (research)", techName);
}
$("#AllUpdate").click(function(){
getAll();
});
function getAll() {
getUserGroup();
if (userGroup.some(isBot) == false) return;
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() {
getUserGroup();
if (userGroup.some(isBot) == false) return;
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).trim();
var internalName = getInputPara(tech, "\\|internal-name = ", 17, "internal-name", techName).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: 'Infobox:' + techName + ' (research)',
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]];
},
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).trim();
var pagePrototypeTypeStart = oldContent.search(/(\s|\|)prototype-type/) + 15;
var pagePrototypeType = getOldPara(oldContent, pagePrototypeTypeStart, 15, "prototype-type", techName).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("Infobox:" + techName + " (research)", techName);
}
}
$("#ProtypeTypeAndItemInternalNameUpdate").click(function(){
getProtypeTypesAndItemInternalNames();
});
function getProtypeTypesAndItemInternalNames() {
getUserGroup();
if (userGroup.some(isBot) == false) return;
var typeAndNameInput = prompt("Please enter the prototype-type and internal-name of everything except technologies.");
if (typeAndNameInput != null) {
getToken();
var typesAndNames = typeAndNameInput.split(/\s\s/g);
console.log(typesAndNames.length + " types and names detected");
typesAndNames.forEach(updateProtypeTypeAndInternalNameInItemInfobox);
}
}
function updateProtypeTypeAndInternalNameInItemInfobox(typeAndName) {
var itemNameEnd = typeAndName.search("\\|");
var itemName = typeAndName.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 prototypeType = getInputPara(typeAndName, "\\|prototype-type = ", 18, "prototype-type", itemName).trim();
var internalName = getInputPara(typeAndName, "\\|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: 'Infobox:' + itemName,
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]];
},
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();
var pageInternalNameStart = oldContent.search(/(\s|\|)internal-name/) + 14;
var pageInternalName = getOldPara(oldContent, pageInternalNameStart, 14, "internal-name", itemName).trim();
summary = "";
newContent = "";
if ((pageInternalName == internalName) && (pagePrototypeType == prototypeType)) {
console.log(itemName + " 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, itemName);
}
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, itemName);
}
}
if (newContent.length > 0) editPage("Infobox:" + itemName, itemName);
}
/*** other.js ***/
/* get the number of users who ever made an edit, for conveniece */
$("#GetEditingUsers").click(function(){
getNumberOfUsersWhoMadeEdits();
});
function getNumberOfUsersWhoMadeEdits() {
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.' );
}
});
};