Module:CommonData

-- CommonData

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 return result end

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

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

code = mw.text.unstripNoWiki(code)     -- go ahead and remove the nowiki if istable then                        -- table or other alternate 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:find(v) and code:find("{{#var:"..v.."[}|]") then colsCode[#colsCode + 1] = i       end end

local keyIds,keyVals,count = {}, {}, 0   -- reverse cols and cols in code for i = 2,10,2 do       local key = args[i] if not key or key == "" then break end

local keyId = colsR[key] if not keyId then return "Bad field \"'"..key.."\"" end count = count + 1 keyIds[count], keyVals[count] = keyId, args[i + 1] or "" end local logs = "" local result,resulti = {}, 0 count = 0 for _,v in ipairs(rows) do       local found = true for i3,v3 in ipairs(keyIds) do           local value = tostring(v[v3]) or "" if value ~= keyVals[i3] then found = false; break end end

if found then for _,v2 in pairs(colsCode) do -- add cols local value = v[v2] value = value and tostring(value) or "" resulti = resulti + 1 result[resulti] = '' end resulti = resulti + 1 result[resulti] = code count = count + 1

if count >= limit then logs = logs .. "\n limit reached at count " .. count .. " resulti " .. resulti break end end end result = table.concat(result) if result == "" then result = mw.text.unstripNoWiki(args.default or "") end result = frame:preprocess(result)

if istable then result = result :gsub("@##@", "||") :gsub("@# @", "| ") :gsub("@#%+@", "\n|") :gsub("@#%-@", "\n|-") :gsub("@#%{@", "\n{|") :gsub("@#%}@", "\n|}") end if args.source then result = result:gsub(               "\n","\n ") end return result .. logs end

local p = {}

function p.vardefine(frame) VarSet(frame, 'mynumber', frame.args[1]) end

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

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

return p