Module:Discontinued Feature Categories

local p = {} --p stands for package local data = { feature = mw.loadData( 'Module:Discontinued Feature Categories/Feature' ), version = mw.loadData( 'Module:Discontinued Feature Categories/Version' ), persistance = mw.loadData( 'Module:Discontinued Feature Categories/Persistance' ), observability = mw.loadData( 'Module:Discontinued Feature Categories/Observability' ), toggle = mw.loadData( 'Module:Discontinued Feature Categories/Toggle' ) }

local yesno = require("Module:Yesno")

local has_missing_categories = false local missing_categories = {} local missing_category_fields = {} local invalid_categories = {} local templates = {} local categories = {}

function p.parse(frame) local parent = frame:getParent local features = get_categories(parent.args, "feature", 5, true) local versions = get_categories(parent.args, "version", 3, true) categories = add_array(features,versions) --categories = add_array(categories,get_categories(parent.args, "persistance", 1)) --categories = add_array(categories,get_categories(parent.args, "observability", 1)) for field, v in pairs(data.toggle.FIELDS) do       local value = yesno(parent.args[field],parent.args[field]) value = type(value) == "string" and get_data_name(value:lower) or type(value) == "boolean" and tostring(value):upper or nil if not parent.args[field] or not v[value] then has_missing_categories = true missing_category_fields[#missing_category_fields+1] = field else categories = add_array(categories,get_category(v[value], "toggle")) end end local template_text = "" for i=1,#templates do       template_text = template_text .. frame:expandTemplate{title = templates[i]} end if #invalid_categories > 0 or #missing_category_fields > 0 then categories[#categories+1] = "Missing Categories" local warning_args = {} if #invalid_categories > 0 then warning_args.invalidCategories = "\"" .. table.concat(invalid_categories,"\" \"") .. "\"" end if #missing_category_fields > 0 then warning_args.missingCategoryFields = table.concat(missing_category_fields," ") end template_text = template_text .. frame:expandTemplate{title = 'Missing Categories', args = warning_args} end table.sort(categories) return parse_categories(categories) .. template_text end

function get_category(str, _type) local category = get_category_data(str, _type) if not category then invalid_categories[#invalid_categories+1] = str return {} end local name = category_name(str,_type) list = {name} if category.template then templates[#templates+1] = category.template end if category.parents then if type(category.parents) =="string" then list = add_array(list, get_category(category.parents, _type)) elseif type(category.parents) =="table" then for i = 1, #category.parents do               list = add_array(list, get_category(category.parents[i], _type)) end end end return list end

function get_categories(args, _type, lim, field_cat) local field = _type if field_cat then field = field .. "-category" end if not args[field] then missing_category_fields[#missing_category_fields+1] = field return {} end local categories = {} for cat in string.gmatch(args[field]..",","(.-)".."%s*,%s*") do           categories = add_array(categories, get_category(cat,_type)) end return categories end

function get_category_data(str, _type) return data[_type][get_data_name(str)] end

function get_data_name(str) return string.gsub(string.gsub(str,"[%s%-]", "_"),"[^%w_]","") end

function parse_categories(list) if #list == 0 then return '' end return "" end

function category_name(str, _type) local cat = get_category_data(str, _type) if cat.name then return cat.name end return str .. " Discontinued Feature" end

function add_array(arr1,arr2) for i=1,#arr2 do      arr1[#arr1+1] = arr2[i] end return arr1 end

return p