Module:SongContestData
Appearance
![]() | This module is rated as pre-alpha. It is unfinished, and may or may not be in active development. It should not be used from article namespace pages. Modules remain pre-alpha until the original editor (or someone who takes one over if it is abandoned for some time) is satisfied with the basic structure. |
![]() | This module depends on the following other modules: |
Usage
[edit]The module looks for data in Module:SongContestData/
{{{1}}}
/{{{2}}}
main
[edit]Returns an attribute value from an entry
{{#invoke:SongContestData|main}}
- Required parameters
Parameter | Description |
---|---|
|1=
|
Contest |
|2=
|
Year |
|3=
|
Entry |
|4=
|
Attribute |
- Examples
{{#invoke:SongContestData|main|esc|2019|no|artist}}
Keiino{{#invoke:SongContestData|main|esc|2019|no|title}}
Spirit in the Sky{{#invoke:SongContestData|main|esc|2019|no|gf_pt}}
331
entryAmount
[edit]Returns the amount of entries in a given contest year
{{#invoke:SongContestData|entryAmount}}
- Required parameters
Parameter | Description |
---|---|
|1=
|
Contest |
|2=
|
Year |
- Examples
{{#invoke:SongContestData|entryAmount|esc|2019}}
41
order
[edit]- Required parameters
Parameter | Description |
---|---|
|1=
|
Contest |
|2=
|
Year |
|3=
|
Attribute to sort |
- Optional parameters
Parameter | Description | Default |
---|---|---|
|desc=
|
Sorts descending if set, otherwise sorts ascending | |
|excludeAtt='String'
|
Excludes entries with set attribute | |
|excludeVal=value
|
Excludes entries where the value of the |excludeAtt= attribute is equal to this parameter
|
|
|excludeInvert=true/false
|
Inverts exclusion. Excludes entries where the value of the |excludeAtt= attribute is not equal to |excludeVal=
|
false |
local getArgs = require('Module:Arguments').getArgs
--local isoName = require('Module:ISO 3166').luaname
local p = {}
local function makeInvokeFunc(funcName)
return function (frame)
local args = getArgs(frame)
return p[funcName](args)
end
end
local function processOutput(o)
if type(o) == "table" then
local result = {}
for _, v in pairs(o) do
table.insert(result, tostring(v))
end
return table.concat(result, ";")
end
return o
end
local function getData(contest, year)
return mw.loadData('Module:SongContestData/'..contest..'/'..year)
end
local function sortEntries(data, att, desc, excludeAtt, excludeVal, excludeInvert)
-- filter entries that do not have att, filter entries that have exclude
local filtered_data = {}
for k, v in pairs(data) do
if att then
if (v[att] and not (
excludeAtt and v[excludeAtt] and excludeVal and
((not excludeInvert and v[excludeAtt] == excludeVal) or -- normal exclusion
(excludeInvert and v[excludeAtt] ~= excludeVal)) -- inverted exclusion
)) then
filtered_data[k] = v
end
else
-- local newKey = isoName({k}) or k
-- filtered_data[newKey] = v
filtered_data[k] = v
end
end
-- sort the keys based on the corresponding attribute values
local keys = {}
for k in pairs(filtered_data) do
table.insert(keys, k) -- store original keys for sorting
end
table.sort(keys, function(a, b)
local aVal, bVal = a, b
if att then
aVal, bVal = filtered_data[a][att], filtered_data[b][att]
else
-- a, b = isoName({a}), isoName({b})
end
if desc then
return aVal > bVal
else
return aVal < bVal
end
end)
return keys
end
p.main = makeInvokeFunc('_main')
function p._main(args)
local data = getData(args[1], args[2])
local entryData = data[args[3]]
if entryData and entryData[args[4]] then
return processOutput(entryData[args[4]])
end
return ""
end
p.entryAmount = makeInvokeFunc('_entryAmount')
function p._entryAmount(args)
local data = getData(args[1], args[2])
local amount = 0
for _ in pairs(data) do amount = amount + 1 end
return amount
end
p.order = makeInvokeFunc('_order')
function p._order(args)
local data = getData(args[1], args[2])
return sortEntries(data, args[3], args['desc'] or false, args['excludeAtt'], args['excludeVal'], args['excludeInvert'] or false)
end
return p