Module:CommonData

-- CommonData

cd_module = cd_module or {} local has = cd_module["bit"] cd_module["bit"] = cd_module["bit"] or require('Module:CommonData/Bit') local bit = cd_module["bit"]   -- regular loader+frame manager is leak terrible -- doesnt free/nil references of instances on lower frames -- 100s of megs in same vm just due to 'bit' -- after ~500+ single calls, one call per row -- if wiki code calling module directly or through template

--local function VarSet(frame, name, value) --   local var = require('Module:Variables') --   --local n = tonumber( var.get('mynumber',0) ) or 100 --   var.define(name, value and tostring(value) or "") --end

-- get one value from a row matching the querry local function GetSingleValueByName(frame) local args = frame.args local p1,p2,p3,p4 = args[1],args[2],args[3],args[4] local tableModule = require('Module:CommonData/' .. p1) local cols = tableModule.GetRawColumns(frame) local rows = tableModule.GetRawTable(frame) local colsR = {} for i,v in ipairs(cols) do       colsR[v] = i    end local key,keyColIndex,result = p3, colsR[p2], "" for _,v in pairs(rows) do       local value = tostring(v[keyColIndex]) if value == key then result = v[colsR[p4]]; break end end if not result and args.default and args.default ~= "" then result = frame:preprocess(args.default) end return result end

-- process wiki code per row matching the query local function GetTranscludeEachRowByName(frame, limit) local args = frame.args local data, code, vars, istable, issource = args[1], args.wiki, args.vars, args.istable, args.source

code = code and code ~= "" and code or nil istable = istable and istable ~= "" and istable or nil issource = issource and issource ~= "" and issource or nil

limit = limit or tonumber(args.limit) or 100 vars = vars and vars ~= "" and (","..vars..",") or nil

local tableModule = require('Module:CommonData/' .. data) local cols = tableModule.GetRawColumns(frame) local rows = tableModule.GetRawTable(frame)

if code then code = mw.text.unstripNoWiki(code) -- go ahead and remove the nowiki end if code and istable then               -- table or alternate other markup code = code :gsub("@([^!^@]+)@", "<%1>")   -- unescape any wraped tags :gsub("@!([^@]+)@!", "@%1@")   -- unescape next tag layer :gsub("%^%^", "@##@")          -- escape our table markup :gsub("%^([%s%{%}%-%+])", "@#%1@")  -- escape our table markup end local colsR, colsCode = {}, {}         -- reverse cols and cols in code for i,v in ipairs(cols) do       colsR[v] = i        if code and code:find(v) and code:find("{{#var:[!]?"..v.."[}|]") then colsCode[#colsCode + 1] = i       elseif vars and vars:find(v) and vars:find(","..v..",") then colsCode[#colsCode + 1] = i       end end

local colsVar = {}                     -- cache define parts for i,v in pairs(colsCode) do -- add cols colsVar[i] = '' end if code then rescodei = rescodei + 1 rescode[rescodei] = code end count = count + 1 if count >= limit then break; end end end local result = rescodei > 0 and table.concat(rescode) or "" if result ~= "" and (code or not issource) then result = frame:preprocess(result) or "" end if result == "" then result = mw.text.unstripNoWiki(args.default or "") or "" result = frame:preprocess(result) or "" end

if code and istable then result = result :gsub("@##@", "||") :gsub("@# @", "| ") :gsub("@#%+@", "\n|") :gsub("@#%-@", "\n|-") :gsub("@#%{@", "\n{|") :gsub("@#%}@", "\n|}") end if issource then result = " "..result.." " end

return (has and "yes" or "no") .. " " .. result end

local p = {}

-- CommonData function p.GetRowValue(frame) return GetSingleValueByName(frame) end

function p.GetRowsTransclude(frame) return GetTranscludeEachRowByName(frame) end

function p.GetRows(frame) return GetTranscludeEachRowByName(frame) end

function p.GetRowTransclude(frame) return GetTranscludeEachRowByName(frame, 1) end

function p.GetRow(frame) return GetTranscludeEachRowByName(frame, 1) end

-- Bit function p.IsFlag( frame ) return bit.IsFlag(frame) end

function p.AnyFlag( frame ) return bit.AnyFlag(frame) end

function p.bor( frame ) return bit.bor(frame) end

function p.bxor( frame ) return bit.bxor(frame) end

function p.band( frame ) return bit.band(frame) end

return p