模块:Dictionary-Vocabulary.lua:修订间差异

来自「荏苒之境」
无编辑摘要
无编辑摘要
第31行: 第31行:
     for i = 1, #rows do
     for i = 1, #rows do
         local entry = {
         local entry = {
        Language = language,
            Language = language,
        Priority = priority
            Priority = priority
         }
         }
         local row = rows[i]
         local row = rows[i]
第43行: 第43行:
             local r = query_res[1]
             local r = query_res[1]
             if r.Priority > priority then
             if r.Priority > priority then
            ignore_store = true
                ignore_store = true
             else
             else
            for j = 1, #row do
                for j = 1, #row do
                if r[HEADERS[j]] ~= row[j] then
                    if r[HEADERS[j]] ~= row[j] then
                    table.insert(conflicting_rows, r)
                        table.insert(conflicting_rows, r)
                    ignore_store = true
                        ignore_store = true
                    break
                        break
                end
                    end
            end
                end
             end
             end
         end
         end
          
          
         if not ignore_store then
         if not ignore_store then
        for j = 1, #HEADERS do
            for j = 1, #HEADERS do
            entry[HEADERS[j]] = row[j]
                entry[HEADERS[j]] = row[j]
        end
            end
        cargo.store(DICT_TABLE, entry)
            cargo.store(DICT_TABLE, entry)
    end
        end
     end
     end


第66行: 第66行:
     if #conflicting_rows ~= 0 then
     if #conflicting_rows ~= 0 then
         view:tag("pre")
         view:tag("pre")
        :css("color", "red")
            :css("color", "red")
        :tag("p")
            :tag("p")
        :wikitext("检测到以下词汇之前已经录入,且与当前词汇表中的内容不一致,请统一词汇信息:")
                :wikitext("检测到以下词汇之前已经录入,且与当前词汇表中的内容不一致,请统一词汇信息:")
            :done()
                :done()
        :node(dict_views.vocabulary(conflicting_rows))
            :node(dict_views.vocabulary(conflicting_rows))
     end
     end
     if frame.args.show_view == "true" then
     if frame.args.show_view == "true" then

2025年8月2日 (六) 06:41的版本

此模块的文档可以在模块:Dictionary-Vocabulary.lua/doc创建

local csv = require("Module:Csv.lua")
local html = mw.html
local cargo = mw.ext.cargo

local dict_views = require("Module:Dictionary-Views.lua")
local DICT_TABLE = dict_views.TABLE
local HEADERS = dict_views.HEADERS
local HEADER_TEXTS = dict_views.HEADER_TEXTS
local ALL_FIELDS = table.concat(HEADERS, ",")..",Priority"

local section = {}

section.show = function(frame)
    local args = frame.args
    local language = args.language
    local format = args.format
    local priority = args.priority
    local content = args.content

    local rows

    if format == "csv" then
        rows = csv.parse(ALL_FIELDS.."\n"..content, true)
    else
        mw.addWarning("无效的词典格式:"..tostring(format))
        return
    end
    
    local conflicting_rows = {}

    for i = 1, #rows do
        local entry = {
            Language = language,
            Priority = priority
        }
        local row = rows[i]
        
        local ignore_store
        local query_res = cargo.query(DICT_TABLE, ALL_FIELDS, {
            where = string.format([[Language="%s" AND Spelling="%s" AND Priority>=%d]], language, row[1], tonumber(priority))
        })
        if #query_res ~= 0 then
            local r = query_res[1]
            if r.Priority > priority then
                ignore_store = true
            else
                for j = 1, #row do
                    if r[HEADERS[j]] ~= row[j] then
                        table.insert(conflicting_rows, r)
                        ignore_store = true
                        break
                    end
                end
            end
        end
        
        if not ignore_store then
            for j = 1, #HEADERS do
                entry[HEADERS[j]] = row[j]
            end
            cargo.store(DICT_TABLE, entry)
        end
    end

    local view = html.create("div")
    if #conflicting_rows ~= 0 then
        view:tag("pre")
            :css("color", "red")
            :tag("p")
                :wikitext("检测到以下词汇之前已经录入,且与当前词汇表中的内容不一致,请统一词汇信息:")
                :done()
            :node(dict_views.vocabulary(conflicting_rows))
    end
    if frame.args.show_view == "true" then
        view:node(dict_views.vocabulary(rows))
    end
    return view
end

return section