Module:DefeatScenarioSources

From Outward Wiki
Jump to navigation Jump to search
Template-info.svg Documentation

Lists sources of a Defeat Scenario.


local p = {}

function p.main(frame)
    if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
	end

	local id = tonumber(args.id)

    -- cargo query
    local fields = '_pageName, id'
    local cargoArgs = { orderBy = '_pageName', where = 'id = "' .. id .. '"', limit = 5000 }
    local data = mw.ext.cargo.query('DefeatScenarioSources', fields, cargoArgs)

    if #data < 1 then
        return '<b>No results found!</b>'
    end
    
    -- build table output 
    local html = mw.html.create()
	local ul = html:tag('ul')
	
	for i,row in ipairs(data) do
		local li = ul:tag('li')
		li:wikitext(frame:preprocess('[[' .. row._pageName .. ']]'))
	end
	
    return html:done()
end

----------------------------------------------------------------
--------------------------- HELPERS ----------------------------

function find(tbl, val)
    for k, v in pairs(tbl) do
        if k == val then return v end
    end
    return nil
end

function notempty(string)
    return string ~= nil and string ~= ''
end

function hasmatch(table1, table2)
    for _,v in ipairs(table1) do
        if contains(table2, v) then
            return true
        end
    end
    return false
end

function contains(table, value)
    for _,v in ipairs(table) do
        if v == value then
            return true
        end
    end
    return false
end

function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={}
    if notempty(inputstr) then
        for str in string.gmatch(inputstr .. ',', "([^"..sep.."]+)") do
            table.insert(t, str)
        end
    end
    return t
end

return p