模块:Dictionary/Vocabulary.lua
来自「荏苒之境」
此模块的文档可以在模块: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 r = cargo.query(DICT_TABLE, ALL_FIELDS, {
where = string.format(
[[Language="%s" AND Spelling="%s" AND Priority>=%d]],
language, row[1], tonumber(priority))
})[1]
if r then
if r.Priority > priority then
ignore_store = true
else
for j = 1, #HEADERS do
local h = HEADERS[j]
if r[h] ~= row[j] then
r[h] = (r[h] or "").."<br/><i style=\"color: grey\">"..(row[j] or "").."</i>"
ignore_store = true
end
end
if ignore_store then
table.insert(conflicting_rows, r)
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