Module:Recipes

local p = {} function p.recipes(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end local noHeader = args.noheader or 'false' local searchBy = args.searchby or "ingredient" local searchFor = args.searchfor or mw.title.getCurrentTitle.text local fulfills = args.fulfills or '' local limit = args.limit or 500 local isListing = args.listing or 'false' local category = args.category or '' local class = args.class or '' local itype = args.type or '' local fullImage = args.fullimage or 'false'

local isBlacksmith = args.isBlacksmith or nil

mw.log(string.format('searchBy: %s', searchBy)) mw.log(string.format('searchFor: %s', searchFor)) mw.log(string.format('fulfills: %s', fulfills)) local result = p.doQuery(searchBy,searchFor,fulfills,limit,category,class,itype,isListing,isBlacksmith) if not result then return ' \'\'No items found.\'\' ' end formattedResult = p.formatResult(result, frame) mw.logObject(formattedResult) return p.makeTable(formattedResult,noHeader) end

function p.doQuery(searchBy, searchFor, fulfills,limit,category,class,itype,isListing,isBlacksmith) local result = mw.ext.cargo.query('Recipes,ItemData',		p.cargoFields,		{			join = 'ItemData.name = Recipes.name',			where = p.cargoWhere(searchBy,searchFor,fulfills,category,class,itype,isListing,isBlacksmith),			orderBy = 'Recipes.name',			groupBy = 'name,station,count',			limit = limit		}	) if not next(result) then return nil else return result end end

function p.cargoFields return 'Recipes._pageName=_pageName,Recipes.name=rname,ItemData.name=name,ItemData.image=image,Recipes.count=count,Recipes.ingredient1=ingredient1,Recipes.ingredient2=ingredient2,Recipes.ingredient3=ingredient3,Recipes.ingredient4=ingredient4,Recipes.station=station,Recipes.blacksmith=blacksmith,Recipes.blacksmithSilver=blacksmithSilver,Recipes.bIngredient1=bIngredient1,Recipes.bIngredient1qty=bIngredient1qty,Recipes.bIngredient2=bIngredient2,Recipes.bIngredient2qty=bIngredient2qty,Recipes.bIngredient3=bIngredient3,Recipes.bIngredient3qty=bIngredient3qty,ItemData.DLC=DLC' end

function p.cargoWhere(searchBy,searchFor,fulfills,category,class,itype,isListing,isBlacksmith) if isBlacksmith == nil then local whereOr = {} local whereAnd = {} local fields = {} local fulfills = p.explode(',', fulfills) local categories = p.explode(',', category) whereAnd[#whereAnd+1] = 'Recipes.ingredient1 IS NOT NULL' --'Recipes.ingredient1 IS NOT NULL OR Recipes.bIngredient1 IS NOT NULL' if searchBy == 'ingredient' then fields = {'Recipes.ingredient1','Recipes.ingredient2','Recipes.ingredient3','Recipes.ingredient4','Recipes.alternateIngredients','Recipes.bIngredient1','Recipes.bIngredient2','Recipes.bIngredient3'} whereAnd[#whereAnd+1] = string.format('Recipes._pageName != "%s"', searchFor) elseif searchBy == 'station' then fields = {'Recipes.station'} elseif searchBy == 'result' then fields = {'Recipes.name'} end for k,v in ipairs(fields) do			if v == 'Recipes.alternateIngredients' then operator = 'HOLDS' else operator = '=' end if searchFor == 'none' then whereOr[#whereOr+1] = string.format('%s IS NULL', v)			else whereOr[#whereOr+1] = string.format('%s %s "%s"', v, operator, searchFor) end for k2,v2 in ipairs(fulfills) do				whereOr[#whereOr+1] = string.format('%s %s "%s"', v, operator, v2) end end whereOrString = table.concat(whereOr, ' OR ') if isListing == 'true' then if categories ~= nil then local whereCategory = {} for kc,vc in ipairs(categories) do					if not p.isempty(vc) then whereCategory[#whereCategory+1] = string.format('ItemData.category = "%s"', vc) end end whereCategoryString = table.concat(whereCategory, ' OR ') if not p.isempty(whereCategoryString) then whereAnd[#whereAnd+1] = string.format('(%s)', whereCategoryString) end end if not p.isempty(class) then whereAnd[#whereAnd+1] = string.format('ItemData.class = "%s"', class) end if not p.isempty(itype) then whereAnd[#whereAnd+1] = string.format('ItemData.type = "%s"', itype) end end whereAndString = table.concat(whereAnd, ' AND ') if not p.isempty(whereOrString) and not p.isempty(whereAndString) then whereString = string.format('%s AND (%s)', whereAndString, whereOrString) elseif not p.isempty(whereAndString) then whereString = whereAndString else whereString = whereOrString end return whereString else local whereOr = {} local whereAnd = {} local fields = {} local fulfills = p.explode(',', fulfills) local categories = p.explode(',', category) whereAnd[#whereAnd+1] = 'Recipes.bIngredient1 IS NOT NULL' if searchBy == 'ingredient' then fields = {'Recipes.ingredient1','Recipes.ingredient2','Recipes.ingredient3','Recipes.ingredient4','Recipes.alternateIngredients','Recipes.bIngredient1','Recipes.bIngredient2','Recipes.bIngredient3'} whereAnd[#whereAnd+1] = string.format('Recipes._pageName != "%s"', searchFor) elseif searchBy == 'station' then fields = {'Recipes.station'} elseif searchBy == 'result' then fields = {'Recipes.name'} end for k,v in ipairs(fields) do			if v == 'Recipes.alternateIngredients' then operator = 'HOLDS' else operator = '=' end if searchFor == 'none' then whereOr[#whereOr+1] = string.format('%s IS NULL', v)			else whereOr[#whereOr+1] = string.format('%s %s "%s"', v, operator, searchFor) end for k2,v2 in ipairs(fulfills) do				whereOr[#whereOr+1] = string.format('%s %s "%s"', v, operator, v2) end end whereOrString = table.concat(whereOr, ' OR ') if isListing == 'true' then if categories ~= nil then local whereCategory = {} for kc,vc in ipairs(categories) do					if not p.isempty(vc) then whereCategory[#whereCategory+1] = string.format('ItemData.category = "%s"', vc) end end whereCategoryString = table.concat(whereCategory, ' OR ') if not p.isempty(whereCategoryString) then whereAnd[#whereAnd+1] = string.format('(%s)', whereCategoryString) end end if not p.isempty(class) then whereAnd[#whereAnd+1] = string.format('ItemData.class = "%s"', class) end if not p.isempty(itype) then whereAnd[#whereAnd+1] = string.format('ItemData.type = "%s"', itype) end end whereAndString = table.concat(whereAnd, ' AND ') if not p.isempty(whereOrString) and not p.isempty(whereAndString) then whereString = string.format('%s AND (%s)', whereAndString, whereOrString) elseif not p.isempty(whereAndString) then whereString = whereAndString else whereString = whereOrString end return whereString end end

function p.formatResult(result,frame) local formatted = {} for k,v in ipairs(result) do		formatted[k] = p.formatRow(v,frame) end return formatted end

function p.formatRow(row,frame) local formattedRow = {} local columns = {'Result','Ingredients','Station'} local count = '' local link = row.rname or row.name or row._pageName local image = '' local ingredient1 = "unknown" local ingredient2 = '' local ingredient3 = '' local ingredient4 = '' if p.isempty(row.image) then image = 'Placeholder.png' else image = row.image end if not p.isempty(row.count) then count = string.format('%dx ', row.count) end if p.isempty(row.station) then station = "None" else station = row.station if (station == 'Blacksmiths') then station = string.format("%s", row.blacksmith) else station = string.format("%s", row.station) end end if (row.station == 'Blacksmiths') then if not p.isempty(row.blacksmithSilver) then ingredient1 = string.format("%s silver", row.blacksmithSilver) else ingredient1 = '' end if not p.isempty(row.bIngredient1) then ingredient2 = string.format("%sx %s", row.bIngredient1qty, row.bIngredient1) end if not p.isempty(row.bIngredient2) then ingredient3 = string.format("%sx %s", row.bIngredient2qty, row.bIngredient2) end if not p.isempty(row.bIngredient3) then ingredient4 = string.format("%sx %s", row.bIngredient3qty, row.bIngredient3) else ingredient4 = "" end else if not p.isempty(row.ingredient1) then ingredient1 = string.format("%s", row.ingredient1) end if not p.isempty(row.ingredient2) then ingredient2 = string.format("%s</li>", row.ingredient2) end if not p.isempty(row.ingredient3) then ingredient3 = string.format("%s</li>", row.ingredient3) end if not p.isempty(row.ingredient4) then ingredient4 = string.format("%s</li>", row.ingredient4) else ingredient4 = "</ul>" end end local imgAndName = string.format('%s%s', image, link, count, link) if not p.isempty(row.DLC) then imgAndName = imgAndName .. ' '	end

formattedRow = { frame:preprocess(imgAndName), frame:preprocess(string.format(' %s%s%s%s ', ingredient1, ingredient2, ingredient3, ingredient4)), frame:preprocess(station) }	return formattedRow end

function p.makeTable(formatted, noHeader) local tbl = mw.html.create('table'):addClass('wikitable') if noHeader == 'false' then tbl:tag('tr') :tag('th'):wikitext('Result'):done :tag('th'):wikitext('Ingredients'):done :tag('th'):wikitext('Station'):done end for _, row in ipairs(formatted) do 		tr = tbl:tag('tr') for i, v in ipairs(row) do			if (i == 2) then tr:tag('td'):cssText('text-align: left'):wikitext(v) else tr:tag('td'):cssText('text-align: center'):wikitext(v) end end end return tbl end

-- Source: http://lua-users.org/wiki/MakingLuaLikePhp -- Credit: http://richard.warburton.it/ function p.explode(div,str) if (div=='') then return false end local pos,arr = 0,{} for st,sp in function return string.find(str,div,pos,true) end do       table.insert(arr,string.sub(str,pos,st-1)) pos = sp + 1 end table.insert(arr,string.sub(str,pos)) return arr end

function p.isempty(s) return s == nil or s == '' or s == 0 or s == false end

return p