Module:Exclusive

-- l10n text, need to be translated. local l10n = { ['en'] = { ['icon_link_d'] = 'Desktop Version history', ['icon_text_d'] = 'Desktop Version', ['icon_link_c'] = 'Console version', ['icon_text_c'] = 'Console Version', ['icon_link_o'] = 'Old-gen console version', ['icon_text_o'] = 'Old-gen console version', ['icon_link_m'] = 'Mobile Version', ['icon_text_m'] = 'Mobile Version', ['icon_link_3'] = '3DS version', ['icon_text_3'] = '3DS Version', ['icon_link_j'] = 'Old-gen console version#Japanese Release', ['icon_text_j'] = 'Japanese Console version', },	['zh'] = { ['icon_link_d'] = '电脑版版本历史', ['icon_text_d'] = '电脑版', ['icon_link_c'] = '主机版', ['icon_text_c'] = '主机版', ['icon_link_o'] = '前代主机版', ['icon_text_o'] = '前代主机版', ['icon_link_m'] = '移动版', ['icon_text_m'] = '移动版', ['icon_link_3'] = '3DS版', ['icon_text_3'] = '3DS版', ['icon_link_j'] = '前代主机版#日本发布版', ['icon_text_j'] = '日本主机版', }, } -- Auto translated pagename when query excusive info? (for eicons/eil/exclusive..) -- Usually should be true for languages with seperated wiki (e.g. de/zh/fr ...), false for languages within main wiki(it/es ...) local l10n_autoTranslatePagename = { ['en'] = false, ['it'] = false, ['es'] = false, ['zh'] = true, ['de'] = true, ['fr'] = true, }

-- l10n function to get plural form, it may be different for each language. If function for target language does not exist, the EN version is used. -- about the function itself: -- return plural form, nil if no pluralism processing occurs. -- _arg1 is the original text(after auto translation), _arg2 is custom text or plural postfix, may be s/es/ies/ves (for english). local plural = { -- for English (en) ['en'] = function(_arg1, _arg2) -- intuitive pluralism -- The comparisons are performed in order, so putting the more common ones in front gives the best performance. if _arg2 == 's' then local suffix1 = _arg1:sub(-1) -- cache for better performance. local suffix2 = _arg1:sub(-2) -- cache for better performance. if suffix1 == 'y' then if suffix2 == 'ay' or suffix2 == 'ey' or suffix2 == 'iy' or suffix2 == 'oy' or suffix2 == 'uy' then return _arg1 .. 's'                -- eg. ray->rays else return _arg1:sub(1, -2) .. 'ies'   -- eg. firefly->fireflies end end if suffix2 == 'fe' and string.lower(_arg1) ~= 'safe' then return _arg1:sub(1, -3) .. 'ves'       -- eg. knife->knives end if suffix1 == 'f' and suffix2 ~= 'ff' then return _arg1:sub(1, -2) .. 'ves'       -- eg. wolf->wolves, leaf->leaves, but buff->buffs else return _arg1 .. 's'			end end

if _arg2 == 'es' then return _arg1 .. 'es' end

if _arg2 == 'ies' then return _arg1:sub(1, -2) .. 'ies' end

if _arg2 == 'ves' then if _arg1:sub(-2) == 'fe' then return _arg1:sub(1, -3) .. 'ves'   --replace last 2 letters, e.g. knife->knives end if _arg1:sub(-1) == 'f' or _arg1:sub(-2) ~= 'ff' then return _arg1:sub(1, -2) .. 'ves'   --replace last letter, e.g. wolf-wolves, leaf->leaves, but buff->buffs end end end,

-- for Chinese (zh) ['zh'] = function(_arg1, _arg2) -- there is no plural form in chinese. if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then return _arg1 end return nil end,

-- for German (de) !! may need to be improved !! ['de'] = function(_arg1, _arg2) if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then return _arg1 end return nil end,

-- for Italian (it) !! may need to be improved !! ['it'] = function(_arg1, _arg2) if _arg2 == 's' or _arg2 == 'es' or _arg2 == 'ies' or _arg2 == 'ves' then return _arg1 end return nil end,

}

--- The following is not related to l10n. --

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 or trim(value) == '' then return nil end return value end

local dplvar = function(name) local var = currentFrame:callParserFunction('#dplvar', name) if var == '' then return nil else return var end end

local function parameter(parameter, base) if parameter == 'y' or parameter == 'yes' then return 'y'	end if parameter == 'n' or parameter == 'no' then return nil end return base end

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

local function getText(_arg1, _arg2, lang, frame) _arg1 = frame:expandTemplate{ title = 'tr', args = {_arg1, lang=lang} } -- auto translate if not _arg2 then return _arg1 end local plural_function = plural[lang] or plural['en'] or function end return plural_function(_arg1, _arg2) or _arg2 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 getInfo = function(page, invert) page = string.gsub(page, '#.*', '') -- remove #hash part. page = string.gsub(page, '_', ' ') -- replace _ with space.

local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, cached = getInfoFromCargo(page)

-- 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 ex_d = ex_d and 'y' or nil ex_c = ex_c and 'y' or nil ex_o = ex_o and 'y' or nil ex_m = ex_m and 'y' or nil ex_3 = ex_3 and 'y' or nil ex_j = ex_j and 'y' or nil

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

local icons = function (vd, vc, vo, vm, v3, vj, _small, _link, lang) local link = '' local space = false local size local result = '' local l10n = l10n[lang] or l10n['en'] if vd then result = ''		space = true end if vc then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if vo then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if vm then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if v3 then result = result .. (space and '&thinsp;' or '') .. ''		space = true end if vj then result = result .. (space and '&thinsp;' or '') .. ''	end return result end

local eicons = function(ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, small, link, lang) if not(ex_d or ex_c or ex_o or ex_m or ex_3 or ex_j) then return '' end return mw.text.tag('span',{class='eico'}, icons(ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, small, link, lang)) end

local getExclusiveInfo = function(page) local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j if page then ex_d, ex_c, ex_o, ex_m, ex_3, ex_j = getInfo(page, getArg('invert')) end -- override ex_d = parameter(getArg('desktop') or getArg('dsk'), ex_d) ex_c = parameter(getArg('console') or getArg('cns'), ex_c) ex_o = parameter(getArg('old-gen') or getArg('oldgen') or getArg('old'), ex_o) ex_m = parameter(getArg('mobile') or getArg('mbl'), ex_m) ex_3 = parameter(getArg('3DS') or getArg('3ds'), ex_3) ex_j = parameter(getArg('japan') or getArg('jas'), ex_j)

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. -- parameters: $1 = pagename, $lang = language, $autotr = apply auto translation on pagename? y/n getInfo = function(frame) currentFrame = frame -- cache

local page = getArg(1) local lang = getArg('lang') or 'en'

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, cached = 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 , 'ex_cached', cached or '' } }

end,

-- for other module: local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, cached = require('Module:Exclusive').getExclusiveInfo(page, invert) getExclusiveInfo = getInfo,

-- for eicons = function(frame) currentFrame = frame:getParent -- cache

local page = getArg(1) local lang = getArg('lang') or frame:expandTemplate{ title = 'lang' } or 'en' local autoTranslate = parameter(getArg('tr'), l10n_autoTranslatePagename[lang])

if autoTranslate then page = frame:expandTemplate{ title = 'tr', args = {page, link = 'y', lang=lang} } end

local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j = getExclusiveInfo(page) return eicons(			ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, 			getArg('small') or dplvar('_eicons_flag_small'), 			getArg('link') or dplvar('_eicons_flag_link'),			lang		) end,

-- for other module, simple eicons: simpleEicons = function(page, lang, small, link) local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j = getInfo(page) return eicons(			ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, 			small, link, lang		) end, -- for eil = function(frame) currentFrame = frame:getParent -- cache local page = getArg(1) or '' local _arg2 = getArg(2) local lang = getArg('lang') or frame:expandTemplate{ title = 'lang' } or 'en' local autoTranslate = parameter(getArg('tr'), l10n_autoTranslatePagename[lang])

local link = frame:expandTemplate{ title = 'tr', args = {page, link = 'y', lang=lang} } local text = frame:expandTemplate{ title = 'tr', args = { getText(page, _arg2, lang, frame), lang=lang} }

-- link part: if not(getArg('nolink') or getArg('nl') or dplvar('_eil_flag_nolink')) then if link == text then text = ..text.. else text = ..text.. end end

-- icons

local ex_d, ex_c, ex_o, ex_m, ex_3, ex_j = getExclusiveInfo(currentFrame.args['epage'] or (autoTranslate and link or page)) -- reserve '' input local icon = eicons(			ex_d, ex_c, ex_o, ex_m, ex_3, ex_j, 			parameter(getArg('small') or dplvar('_eil_flag_small'), true), 			getArg('iconlink') or dplvar('_eil_flag_iconlink'),			lang		)

return mw.text.tag('span',{class='eil'}, text .. icon) end,

getText = getText, }

return p