模块:Phrases/Query:修订间差异

来自「荏苒之境」
无编辑摘要
无编辑摘要
第13行: 第13行:
     local phrase_limit_per_theme = config.phrase_limit_per_theme
     local phrase_limit_per_theme = config.phrase_limit_per_theme


     local themes = cargo.query("PhraseThemes", "Theme,Translations", {
     local themes = cargo.query("PhraseThemes", "Theme,Translation", {
         where = "PhraseTable='"..table_name.."'",
         where = "PhraseTable='"..table_name.."' AND Language='"..language.."'",
         orderBy = theme_order_by,
         orderBy = theme_order_by,
        groupBy = "Theme",
         limit = theme_limit
         limit = theme_limit
     })
     })
第23行: 第24行:
     local theme = theme_entry.Theme
     local theme = theme_entry.Theme
         local phrase_query = cargo.query(table_name, "Phrase", {
         local phrase_query = cargo.query(table_name, "Phrase", {
             where = "Theme='"..escape_sql(theme).."' AND Language='"..escape_sql(language).."'",
             where = "Theme='"..escape_sql(theme).."' AND Language='"..language.."'",
             orderBy = phrase_order_by,
             orderBy = phrase_order_by,
             limit = phrase_limit_per_theme,
             limit = phrase_limit_per_theme,
第31行: 第32行:
             phrases[#phrases+1] = entry.Phrase
             phrases[#phrases+1] = entry.Phrase
         end
         end
         local translations = theme_entry.Translations
         local translation = theme_entry.Translation
         if translations == "" then translations = nil end
         if translation == "" then translations = nil end
         r[#r+1] = {
         r[#r+1] = {
         theme = theme,
         theme = theme,
         phrases = phrases,
         phrases = phrases,
         translations = translations
         translation = translation
         }
         }
     end
     end
第82行: 第83行:
local entry = result[i]
local entry = result[i]
local phrases = entry.phrases
local phrases = entry.phrases
local translations = mw.text.jsonDecode(entry.translations or "null")
local translation = entry.translation
t[#t+1] = "<b>"
t[#t+1] = "<b>"
t[#t+1] = translations and translations[language] or entry.theme
t[#t+1] = translation and translation or entry.theme
t[#t+1] = "</b><ul>"
t[#t+1] = "</b><ul>"
for j = 1, #phrases do
for j = 1, #phrases do

2025年8月24日 (日) 00:17的版本

此模块的文档可以在模块: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,Translation", {
        where = "PhraseTable='"..table_name.."' AND Language='"..language.."'",
        orderBy = theme_order_by,
        groupBy = "Theme",
        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='"..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.Translation
        if translation == "" then translations = nil end
        r[#r+1] = {
        	theme = theme,
        	phrases = phrases,
        	translation = translation
        }
    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 translation = entry.translation
		
		t[#t+1] = "<b>"
		t[#t+1] = translation and translation 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