模块:Phrases/Query:修订间差异
来自「荏苒之境」
无编辑摘要 |
无编辑摘要 |
||
第31行: | 第31行: | ||
phrases[#phrases+1] = entry.Phrase | phrases[#phrases+1] = entry.Phrase | ||
end | end | ||
local translation = theme_entry.translations | |||
if translation == "" then translation = nil end | |||
r[#r+1] = { | r[#r+1] = { | ||
theme = theme, | theme = theme, | ||
phrases = phrases, | phrases = phrases, | ||
translations = | translations = translations | ||
} | } | ||
end | end | ||
第82行: | 第84行: | ||
local translations = mw.text.jsonDecode(entry.translations or "null") | local translations = mw.text.jsonDecode(entry.translations or "null") | ||
t[#t+1] = " | t[#t+1] = "<b>" | ||
t[#t+1] = translations and translations[language] or entry.theme | t[#t+1] = translations and translations[language] or entry.theme | ||
t[#t+1] = " | t[#t+1] = "</b><ul>" | ||
for j = 1, #phrases do | for j = 1, #phrases do | ||
t[#t+1] = "<li>" | t[#t+1] = "<li>" |
2025年8月23日 (六) 23:46的版本
此模块的文档可以在模块:Phrases/Query/doc创建
local translation = require("Module:Template translation")
local utils_str = require("Module:Utils/String")
local cargo = mw.ext.cargo
local escape_sql = utils_str.escape_sql
local query = {}
local function get(table_name, language, config)
local theme_order_by = config.theme_order_by and escape_sql(config.theme_order_by)
local phrase_order_by = config.phrase_order_by and escape_sql(config.phrase_order_by)
local theme_limit = config.theme_limit
local phrase_limit_per_theme = config.phrase_limit_per_theme
local themes = cargo.query("PhraseThemes", "Theme,Translations", {
where = "PhraseTable='"..table_name.."'",
orderBy = theme_order_by,
limit = theme_limit
})
local r = {}
for _, theme_entry in ipairs(themes) do
local theme = theme_entry.Theme
local phrase_query = cargo.query(table_name, "Phrase", {
where = "Theme='"..escape_sql(theme).."' AND Language='"..escape_sql(language).."'",
orderBy = phrase_order_by,
limit = phrase_limit_per_theme,
})
local phrases = {}
for _, entry in pairs(phrase_query) do
phrases[#phrases+1] = entry.Phrase
end
local translation = theme_entry.translations
if translation == "" then translation = nil end
r[#r+1] = {
theme = theme,
phrases = phrases,
translations = translations
}
end
return r
end
local function get_language(frame)
local parent_frame = frame:getParent()
local language = translation.getCurrentLanguageSubpage()
return (language == nil or language == "") and "zh-cn" or language
end
query.get = get
query.show_ul = function(frame)
local args = frame.args
local table_name = args.table
local language = get_language(frame)
local result = get(table_name, language, args)
local t = {"<ul>"}
for i = 1, #result do
local phrases = result[i].phrases
for j = 1, #phrases do
t[#t+1] = "<li>"
t[#t+1] = phrases[j]
t[#t+1] = "</li>"
end
end
t[#t+1] = "</ul>"
return table.concat(t)
end
query.show_ul_sections = function(frame)
local args = frame.args
local table_name = args.table
local language = get_language(frame)
local result = get(table_name, language, args)
local t = {}
for i = 1, #result do
local entry = result[i]
local phrases = entry.phrases
local translations = mw.text.jsonDecode(entry.translations or "null")
t[#t+1] = "<b>"
t[#t+1] = translations and translations[language] or entry.theme
t[#t+1] = "</b><ul>"
for j = 1, #phrases do
t[#t+1] = "<li>"
t[#t+1] = phrases[j]
t[#t+1] = "</li>"
end
t[#t+1] = "</ul>"
end
return table.concat(t)
end
return query