User:RheingoldRiver/item recipes backup

local lang = mw.getLanguage('en') local p = {} function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end local item = args[1] or mw.title.getCurrentTitle.text local result = p.doQuery(item) if not result then return ' \'\'No crafting recipes found.\'\' ' end local formatted = p.formatResult(result) return p.makeTable(formatted) end

-- cargo function p.doQuery(item) local result = mw.ext.cargo.query('Recipes',		p.cargoFields,		{			where = p.cargoWhere(item),			orderBy = 'tool'		}	) if not next(result) then return nil else return result end end

function p.cargoFields local fields = { 'result','resultAmount','tool' }	for i = 1,12 do fields[#fields+1] = 'item' .. i fields[#fields+1] = 'recipe' .. i	end return table.concat(fields,',') end

function p.cargoWhere(item) local where = {} for i = 1,12 do		where[#where+1] = string.format('item%s="%s"', i, item) end return table.concat(where,' OR ') end

-- process data for output function p.formatResult(result) local formatted = {} for k, row in ipairs(result) do		formatted[k] = { p.formatResult(row.result, row.resultAmount,			p.makeIngredientsTable(row),			p.formatTool(row.tool)		}	end	return formatted end

function p.formatResult(result, amount) return string.format(" %s (%s)",		result or ,		result or ,		result or ,		amount or 	) end

function p.makeIngredientsTable(row) local tbl = mw.html.create('table') for i = 1, 12 do if row['item' .. i] then tbl:tag('tr'):tag('td'):wikitext(p.formatResult(row['item' .. i], row['amount' .. i])) end end return tostring(tbl) end

function p.formatTool(tool) if lang:lc(tool) == 'by hand' then return 'By Hand' else return string.format(' %s',			tool or ,			tool or ,			tool or ''		) end end

-- make table for output function p.makeTable(formatted) local tbl = mw.html.create('table'):addClass('wikitable') tbl:tag('tr') :tag('th'):wikitext('Result') :tag('th'):wikitext('Ingredients') :tag('th'):wikitext('Crafting Station') for _, row in ipairs(formatted) do		tr = tbl:tag('tr') for _, v in ipairs(row) do			tr:tag('td'):wikitext(v) end end retun tostring(tbl) end

return p