Module:FilterWidget

-- (prevents literal interpretation of links or categories within the module code) -- Lua Templating for Filter Widget related logic local p = {} local cargo = mw.ext.cargo local md5 = require('Module:Md5')

local imageUrl = '/media/%s/%s/%s'

-- Filter header for wyrmrprint lists -- Usage: -- Test in console with: `=p.Wyrmprints(mw.getCurrentFrame)` function p.Wyrmprints(frame) local abFilterValues = 'abilities=%s|abilityIcons=%s' local affinityFilterValues = 'affinityIds=%s|affinityIcons=%s|affinityNames=%s'

local result = {} local abilities = cargo.query('Wyrmprints,Abilities', 'AbilityIconName', {   join    = 'Wyrmprints.Abilities13=Abilities.Id',    where   = 'Abilities13>0',    groupBy = 'AbilityIconName',    orderBy = 'AbilityIconName',  }) for i, ability in ipairs(abilities) do   -- Ability Icon local abIcon = ability['AbilityIconName'] local abIconPath = getImageUrl(abIcon..'.png') result[#result+1] = string.format(abFilterValues, abIcon, abIconPath) end

local affinities = cargo.query('Wyrmprints,AffinityBonus',   'UnionAbilityGroupId,AffinityBonus.Name=AffinityName', {    join    = 'Wyrmprints.UnionAbilityGroupId=AffinityBonus.Id',    where   = 'UnionAbilityGroupId>0',    groupBy = 'UnionAbilityGroupId',    orderBy = 'AffinityBonus.Id',  }) for i, aff in ipairs(affinities) do   local affinityId = aff['UnionAbilityGroupId'] local affinityIcon = 'Icon Union '..string.format("%02d",affinityId)..'.png' local affinityName = aff['AffinityName'] local affinityIconPath = getImageUrl(affinityIcon) result[#result+1] = string.format(affinityFilterValues,       affinityId, affinityIconPath, affinityName) end

return frame:preprocess(string.format('', table.concat(result, '|'))) end

-- Filter header for Dragon lists -- Usage: -- Test in console with: `=p.Dragons(mw.getCurrentFrame)` function p.Dragons(frame) local abFilterValues = 'abilities=%s|abilityIcons=%s'

local result = {} local abilities = cargo.query('Abilities,Dragons=D1,Dragons=D2', 'AbilityIconName', {   join    = 'Abilities.Id=D1.Abilities15,Abilities.Id=D2.Abilities25',    where   = '(D1.Id IS NOT NULL OR D2.Id IS NOT NULL)',    groupBy = 'AbilityIconName',    orderBy = 'COUNT(D1.Abilities15) DESC, MIN(Abilities.Id)',  }) for i, ability in ipairs(abilities) do   -- Ability Icon local abIcon = ability['AbilityIconName'] local abIconPath = getImageUrl(abIcon..'.png') result[#result+1] = string.format(abFilterValues, abIcon, abIconPath) end return frame:preprocess(string.format('', table.concat(result, '|'))) end

function getImageUrl(imageName) local hash = md5.sumhexa(imageName):sub(1, 2) return string.format(imageUrl, hash:sub(1,1), hash, imageName) end

return p --