Module:Exclusive

-- can set more than one flag cates for each platform, easier for l10n. local flag_cates ={ ['d'] = {'Desktop content', }, ['c'] = {'Console content', }, ['o'] = {'Old-gen console content', }, ['m'] = {'Mobile content', }, ['3'] = {'3DS content', }, ['j'] = {'Japanese console content', }, }

local trim = mw.text.trim local cargo = mw.ext.cargo

local currentFrame -- cache -- helper function local getArg = function(key) local value = currentFrame.args[key] if not value then return nil end value = trim(value) if value == '' then return nil else return value end end

local escape = function(str) return str:gsub("'", "\\'"):gsub("&#39;", "\\'") end local enclose = function(str) return "'" .. escape(str) .. "'" end

local getInfoFromCargo = function(page) -- try to get from cargo cache local result = mw.ext.cargo.query('Exclusive', 'desktop, console, oldgen, mobile, 3ds, japanese, cached', {		where = 'pagename='..enclose(page),		orderBy = "cached DESC",		limit = 1,	}) for _, row in ipairs(result) do		-- all are strings return row['desktop'] ~= , row['console'] ~= , row['oldgen'] ~= , row['mobile'] ~= , row['3ds'] ~= , row['japanese'] ~= , row['cached'] ~= '' end end

local storeInfoToCargo = function(page) -- dpl query: Get all categories in a single DPL query. Append ", " at the end. -- The result will look like: "Enemy NPCs, Desktop content, Console content, ", with ", " after each category name. -- By this way, we can avoid unintended category name match and simplify process. local cate_string = currentFrame:callParserFunction('#dpl:', {		['title'] = page,		['redirects'] = 'include',		['addcategories'] = 'true',		['format'] = ',%CATNAMES%,,, ',	})

local ex = {}

for platform, flags in pairs(flag_cates) do		for _, cate in ipairs(flags) do			if string.find(cate_string, cate..', ', 1, true) then ex[platform] = true break end end end

--store it to cargo table: currentFrame:callParserFunction('#cargo_store:_table=Exclusive',{		['pagename'] = page,		['desktop'] = ex['d'],		['console'] = ex['c'],		['oldgen'] = ex['o'],		['mobile'] = ex['m'],		['3ds'] = ex['3'],		['japanese'] = ex['j'],		['cached'] = os.time,	})

return ex['d'], ex['c'], ex['o'], ex['m'], ex['3'], ex['j'] end

local getInfo = function(page, invert) page = string.gsub(page, '#.*', '') -- remove #hash part.

local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, cached = getInfoFromCargo(page) -- cache missed, init cache if not cached then ex_d, ex_c, ex_o, ex_m, ex_3, ex_j = storeInfoToCargo(page) end -- invert? if invert then ex_d = not ex_d ex_c = not ex_c ex_o = not ex_o ex_m = not ex_m ex_3 = not ex_3 if ex_j then ex_j = false end end -- all versions check: if ex_d and ex_c and ex_o and ex_m and ex_3 then ex_d = false ex_c = false ex_o = false ex_m = false ex_3 = false end -- standardize if ex_d then ex_d = 'y' else ex_d = nil end if ex_c then ex_c = 'y' else ex_c = nil end if ex_o then ex_o = 'y' else ex_o = nil end if ex_m then ex_m = 'y' else ex_m = nil end if ex_3 then ex_3 = 'y' else ex_3 = nil end if ex_j then ex_j = 'y' else ex_j = nil end

return ex_d, ex_c, ex_o, ex_m, ex_3, ex_j end

---

local p = { -- for template, get all exclusive info, setting in dplvars. getInfo = function(frame) currentFrame = frame -- cache

local page = getArg(1)

if not page then frame:callParserFunction{ name = '#dplvar:set', args = { 'ex_d', , 'ex_c', , 'ex_o', , 'ex_m', , 'ex_3', , 'ex_j',  } }			return end

local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j = getInfo(page, getArg('invert'))

frame:callParserFunction{ name = '#dplvar:set', args = { 'ex_d', ex_d or , 'ex_c', ex_c or , 'ex_o', ex_o or , 'ex_m', ex_m or , 'ex_3', ex_3 or , 'ex_j', ex_j or  } }	end,

-- for other module(still need frame for dpl query) getExclusiveInfo = function(page ,invert, frame) currentFrame = frame -- cache return getInfo(page, invert) end, }

return p