模块:Db
本模块为自动表核心功能程序,根据输入的信息自动筛选数据。
数据库储存在Module:db_song,通过Template:db可以调用数据库中的信息,也可以通过Template:songlist生成自动表格。
local p = {}
local valueTypes = mw.loadData('Module:Autovalue/types')
local groupsConfig = mw.loadData('Module:Autovalue/groups')
local i18n = {
['true'] = '是',
['false'] = '否',
['none'] = '\'\'无\'\'',
['error_raw_only'] = '<span class="error">Autovalue:数据类型"%s"只能通过tryGetRawValue方法获取值。</span>',
['category_unknown_value_type'] = '未知自动值类型',
['category_missing_value'] = '缺失%s',
['content_or'] = '或',
['grouping_others'] = '其他',
}
local function tableHasValue (targetTable, targetValue)
for index, value in ipairs(targetTable) do
if value == targetValue then
return true
end
end
return false
end
local function IsSameArray(arr1, arr2)
if #arr1 ~= #arr2 then
return false
end
for i, entry in ipairs(arr1) do
if not tableHasValue (arr2, entry) then
return false
end
end
return true
end
local function ValueTypeExist(valueType)
return valueTypes[valueType] ~= nil
end
local function MappingKey(key, mappingName)
return p.getRawValue(key, mappingName) or key
end
local function GetValuesTable(valueType, onlyBE)
local values
if valueTypes[valueType].beTable then
values = mw.loadData('Module:' .. valueType .. ' values')
end
return values
end
local function MappingValueFull(mappingFullName, value)
local mapping = mw.loadData(mappingFullName)
if mapping[value] then
return mapping[value]
else
return value
end
end
local function MappingValue(mappingName, value)
return MappingValueFull('Module:db/' .. mappingName .. ' mapping', value)
end
local function ValueFromValuesByKey(values, key)
local value = values[key]
--if value == nil then
-- value = values[key:gsub('s$', '')]
--end
if value == nil then
value = values['__fallback']
end
return value
end
-- 键映射方法。
local keyMappingMethod = {
}
local valueGettingMethod = {
}
-- 将true和false分别转换为'是'和'否'。
local function TrueFalueMapping (value)
if value == true then
return i18n['true'];
elseif value == false then
return i18n['false'];
else
return value
end
end
-- 为数字添加逗号分隔符。
local function FormatNumber (value)
if type(value) == 'number' then
local i, j, minus, int, fraction = tostring(value):find('([-]?)(%d+)([.]?%d*)')
int = int:reverse():gsub("(%d%d%d)", "%1,")
return minus .. int:reverse():gsub("^,", "") .. fraction
else
return value
end
end
local function FormatRomanNumber(value)
local roman = require( [[Module:Roman number]] ).toroman(value)
if roman then return roman end
return FormatNumber (value)
end
local function EmptyStringToNone(value)
if value == '' then return i18n['none'] end
return value
end
-- 值映射方法,不设置则输出原值。
--数值类
local valueMappingMethod = {
['4b_nm_difficulty'] = FormatNumber,
--布尔类
['key_sound'] = TrueFalueMapping,
['key_sound2'] = (function (value)
if value == true then
return 'true';
elseif value == false then
return 'false';
else
return value
end
end),
--字符串类
['composer'] = (function (value)
return MappingValue('composer', value)
end),
}
-- 提供类型名称、键值名称来获取值。返回原始数据类型。
local function tryGetRawValue(valueType, targetName)
if type(keyMappingMethod[valueType]) == 'function' then
targetName = keyMappingMethod[valueType](targetName)
elseif type(valueTypes[valueType].keyMap) == 'string' then
targetName = MappingKey(targetName, valueTypes[valueType].keyMap)
end
if type(valueGettingMethod[valueType]) == 'function' then
return valueGettingMethod[valueType](targetName)
else
return ValueFromValuesByKey(GetValuesTable(valueType), targetName)
end
end
-- 提供类型名称、键值名称来获取值。返回值应当是字符串。
local function tryGetValue(valueType, targetName)
if valueTypes[valueType].rawOnly then
return string.format(
i18n['error_raw_only'],
valueType
)
end
local value = tryGetRawValue(valueType, targetName)
if type(valueMappingMethod[valueType]) == 'function' then
return valueMappingMethod[valueType](value)
else
return value
end
end
-- 获取值。
local function GetValue(targetName, argType, argMode, nocat)
if not ValueTypeExist(argType) then
return '?[[Category:'..i18n['category_unknown_value_type']..']]'
end
local value
local category = ''
local result
value = tryGetValue(argType, targetName)
if value == nil then
if valueTypes[argType].noNullError then
value = i18n['none']
else value = '?'
end
end
--707 741
end