模块:Phrases/Query

来自「荏苒之境」
Sicusa留言 | 贡献2025年8月24日 (日) 01:32的版本

此模块的文档可以在模块: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_flatten(table_name, language, config)
	config = config or {}
	local phrase_order_by = config.phrase_order_by and escape_sql(config.phrase_order_by)
    local phrase_limit = config.phrase_limit

    local phrase_query = cargo.query(table_name, "Phrase,Theme", {
        where = "Language='"..language.."'"..(config.where or ""),
        orderBy = phrase_order_by,
        limit = phrase_limit
    })
    
    local r = {}
    for _, entry in pairs(phrase_query) do
        r[#r+1] = {
        	phrase = entry.Phrase,
        	theme = entry.Theme
        }
    end
    return r
end

local function get(table_name, language, config)
	config = config or {}
    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.."'"..(config.where or ""),
        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.get_uncategorized = get_uncategorized

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_flatten = function(frame)
    local args = frame.args
    local table_name = args.table
    local language = get_language(frame)

	local phrases = get_flatten(table_name, language, args)
    local t = {"<ul>"}
    
	for j = 1, #phrases do
		t[#t+1] = "<li>"
		t[#t+1] = phrases[j].phrase
		t[#t+1] = "</li>"
	end
	
	t[#t+1] = "</ul>"
    return table.concat(t)
end


query.show_ul_compond = function(frame)
    local args = frame.args
    local table_name = args.table
    local language = get_language(frame)
	
	args.phrase_order_by = args.flatten_phrase_order_by
	local phrases_flatten = get_flatten(table_name, language, args)
	args.phrase_order_by = args.themed_phrase_order_by
	local phrases_themed = get(table_name, language, args)
	local phrase_map = {}
	
    local t = {"<ul>"}
    
	for j = 1, #phrases_flatten do
		local phrase = phrases_flatten[j].phrase
		phrase_map[phrase] = true
		t[#t+1] = "<li>"
		t[#t+1] = phrase == "" and "?" or phrase
		t[#t+1] = "</li>"
	end
	t[#t+1] = "<li>sdsd</li>"
	
	for j = 1, #phrases_themed do
		if phrase_map[phrase] == nil then
			t[#t+1] = "<li>"
			t[#t+1] = phrases_themed[j].phrase
			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
			local entry = phrases[i]
			t[#t+1] = "<li>"
			t[#t+1] = entry.theme
			t[#t+1] = entry.phrase
			t[#t+1] = "</li>"
		end
		t[#t+1] = "</ul>"
	end
	
    return table.concat(t)
end

return query