Tài liệu mô đun[xem] [sửa] [lịch sử] [làm mới]

Mô đun này cung cấp các phép toán luận lý cho {{đầu đề}}.

--[=[
Đây là mô đun thực hiện các phép toán luận lý cho bản mẫu {{đầu đề}}

It doesn't do everything yet, but over time it can accrete functions from
the template, which will become simpler and simpler until it's just an invoke.

Not implemented yet:
 * Categories (deprecated anyway)
 * Subpage checking
]=]

local p = {} --p stands for package

local yesno = require('Mô đun:Yesno')
local getArgs = require('Mô đun:Arguments').getArgs


-- get the param_override or param parameter in that order
-- nil if neither is there
function get_arg_or_override(args, param)
	if args["ghi đè " .. param] then
		return args["ghi đè " .. param]
	elseif args[param] then
		return args[param]
	end
	
	return nil
end

-- return true if any value in list is nil in args
-- (nil means not present; empty string is not nil)
function any_arg_nil(args, list)
	for k,v in pairs(list) do
		if args[v] == nil then
			return true
		end
	end
	return false
end

-- return true if any value in list is not nil or empty in args
-- (nil means not present; empty string is not nil)
function has_any_arg(args, list)
	for k,v in pairs(list) do
		if args[v] ~= nil and args[v] ~= "" then
			return true
		end
	end
	return false
end

--[=[
Construct a warning if required parameters are missing
]=]
function p.missing_params_error(frame)
	local args = getArgs(frame, {
		removeBlanks = false
	})
	
	local s = ""
	
	local required_args = {'tựa đề', 'phần', 'tác giả', 'trước', 'sau', 'ghi chú'}
	if any_arg_nil(args, required_args) then
		local error_div = mw.html.create("div")
			:css({['margin-right'] = 'auto',
				['margin-left'] = 'auto',
				['border-top'] = '1px solid #CCC',
				['border-right']  = '1px solid #CCC',
				['border-bottom'] = '1px hidden transparent',
				['border-left'] = '1px solid #CCC',
				['text-align'] = 'center'
			})
			
		error_div:tag("span")
			:addClass("error")
			:css({
				['font-size'] = "90%",
				['font-weight'] = "bold",
			})
			:wikitext("Lỗi bản mẫu: xin đừng bỏ các thông số trống đi (xem [[WS:BIENSOAN#Bản mẫu|cẩm nang biên soạn]] và [[Bản mẫu:Đầu đề#tài liệu sử dụng|tài liệu hướng dẫn sử dụng bản mẫu]]).")

		s = tostring(error_div)
		
		-- categorise only in mainspace
		if mw.title.getCurrentTitle().namespace == 0 then
			s = s .. "[[Thể loại:Đầu đề thiếu thông số]]"
		end
	end
	return s
end

-- Place each value from a table of IDs:values into the given parent element
function append_mf_values(parent, values)
	for k,v in pairs(values) do
		parent:tag("span")
			:attr("id", k)
			:wikitext(v)
	end
end

-- Create the Microformat wrapper div
function construct_mf_wrapper(args)
	local mf_div =  mw.html.create("div")
	mf_div:addClass("ws-noexport")
	    :attr("id", "ws-data")
		:css({
			speak = "none"
		})
	
	-- hide the microformat unless it's overriden
	if not (args["show_microformat"] and yesno(args['show_microformat'])) then
		mf_div:css("display", "none")
	end
	return mf_div
end

-- Collect all the values of microformat data from the arguments provided
-- Returns a table of microformat ID:contents.
function collect_mf_data(args)
	-- collect the MF values here
	local mf = {};

	mf["ws-article-id"] = mw.title.getCurrentTitle().id 
	
	-- add the title
	if args["tựa đề"] then
		mf["ws-title"] = args['tựa đề']

		-- append section if there is one
		if args["phần"] then
			mf["ws-title"] = mf["ws-title"] .. " — " .. args["phần"]
		end
	end
	
	local author = get_arg_or_override(args, "người đóng góp")
	if not author then
		author = get_arg_or_override(args, "tác giả của phần")	
	end
	if not author then
		author = get_arg_or_override(args, "tác giả")	
	end
	if author then
		mf['ws-author'] = author
	end
	
	local translator = get_arg_or_override(args, "dịch giả")
	if translator then
		mf['ws-translator'] = translator
	end
	
	local year = get_arg_or_override(args, "năm")
	if year then
		mf['ws-year'] = year
	end
	
	if args['bìa'] then
		mf['ws-cover'] = args['bìa']	
	end
	
	return mf
end

--[=[
Construct the [[Help:Microformat]] for the page.

This is in the form:
<div id="ws-data" ...>
  <span id="ws-title">Title here...</span>
  ...
<div>
]=]
function p.microformat(frame)
	local args = getArgs(frame)

	local mf_div =  construct_mf_wrapper(args)
	local mf = collect_mf_data(args)

	append_mf_values(mf_div, mf)
	return tostring(mf_div)
end

function make_category_list(categories)
	local s = ""
	for k,v in pairs(categories) do
		s = s .. "[[Thể loại:" .. v .. "]]\n"
	end
	return s
end

--[=[
Detect illegal formatting in fields like "phần" and "tựa đề"
]=]
function illegal_formatting(str)
	return string.match(str, "'''?")
		or string.match(str, "<%s*/?%s*[iIbB]%s*>")
		-- add more cases here
end

function check_non_existent_author_pages(args, param, categories)

	if args[param] then
		-- some pages expect an invalid author
		local special = false
		local lower_arg =  string.lower(args[param])
		if lower_arg == "không rõ" or lower_arg == "không đề cập" then
			special = true
		end
		
		if not special then
			local target = mw.title.makeTitle("Tác_gia", args[param])
			-- expensive function!
			if not target.exists then
				table.insert(categories, "Tác phẩm không tồn tại trang tác gia")
			end
		end
	end
end

--[=[
Construct the automatic categories for the header
]=]
function p.categories(frame)
	local args = getArgs(frame)
	
	local categories = {}
	local this_page = mw.title.getCurrentTitle();

	if args["ghi đè tác giả"] then
		table.insert(categories, "Trang có ghi đè tác giả")
	end
	
	if this_page:inNamespaces(0) then
		check_non_existent_author_pages(args, "tác giả", categories)
		check_non_existent_author_pages(args, "người hiệu đính", categories)
		check_non_existent_author_pages(args, "dịch giả", categories)
		check_non_existent_author_pages(args, "dịch giả của phần", categories)
		check_non_existent_author_pages(args, "tác giả của phần", categories)
		check_non_existent_author_pages(args, "người đóng góp", categories)
	end
	
	if args["người đóng góp"] or args["tác giả của phần"] then
		table.insert(categories, "Trang có người đóng góp")
	end
	
	if args["ghi đè người đóng góp"] or args["ghi đè tác giả của phần"] then
		table.insert(categories, "Trang có ghi đè người đóng góp")
	end
	
	local author = get_arg_or_override(args, "tác giả")
	if author and (string.lower(author) == "không rõ") then
		table.insert(categories, "Văn kiện vô danh")
	end
	
	local editor = get_arg_or_override(args, "người hiệu đính")
	if editor then
		editor = string.lower(editor)
		if editor == "không rõ" or editor == "?" then
			table.insert(categories, "Tác phẩm không rõ người hiệu đính")
		elseif editor == "không đề cập" then
			table.insert(categories, "Tác phẩm có người hiệu đính không được nhắc tới")
		end
	end
	
	local translator = get_arg_or_override(args, "dịch giả")
	if translator then
		translator = string.lower(translator)
		if translator == "không rõ" or translator == "không đề cập" or translator == "?" then
			table.insert(categories, "Bản dịch không có thông tin dịch giả")
		elseif translator == "wikisource" and (this_page.baseText == this_page.text) then
			-- if a basepage
			-- ?? why is this not done by {{translation}} ??
			table.insert(categories, "Bản dịch của Wikisource")
		end
	end

	if args["viết tắt"] then
		table.insert(categories, "Trang không gian chính có viết tắt")
	end
	
	if args["ghi đè năm"] then
		table.insert(categories, "Trang có ghi đè năm")
	end
	if args["không năm"] then
		table.insert(categories, "Trang dùng không năm")
	end
	if args["không thể loại năm"] then
		table.insert(categories, "Trang dùng không thể loại năm")
	end
	
	if args["bìa"] then
		table.insert(categories, "Trang có bìa riêng")
	end
	
	-- sanity/maintenance checks on various parameters
	
	-- allow_illegal_formatting parameter suppresses this check
	-- used by, for example, [[Template:Versions]]
	if args["allow_illegal_formatting"] == nil then

		if args["tựa đề"] and illegal_formatting(args['tựa đề']) then
			table.insert(categories, "Trang có định dạng sai trong trường đầu đề")
		end
		
		if args["phần"] and illegal_formatting(args['phần']) then
			table.insert(categories, "Trang có định dạng sai trong trường đầu đề")
		end
	end

	return make_category_list(categories)
end

function get_plain_sister(frame, args, sister_args)
	local ps_args = {}
	for k,v in pairs(sister_args) do
		if args[v] then
			ps_args[v] = args[v]
		end
	end
	
	return frame:expandTemplate{title = "đề mục liên quan", args = ps_args}
end

--[=[
Return the title span

> 
--]=]
function construct_title(args)
	local title = mw.html.create("span")
		:attr("id", "header_title_text")
		:css({
			["font-weight"] = "bold",
		})
		
	if args["tựa đề"] then
		title:wikitext(args["tựa đề"])
	else
		title:wikitext("Vô đề")
	end
	return title
end

--[=[
Construct the title field
]=]
function p.title(frame)
	local args = getArgs(frame)

	local title = construct_title(args)
	return tostring(title)
end

--[=[
Construct the year span
--]=]
function construct_year(frame, args)

	local year = mw.html.create("span")
		:attr("id", "header_year_text")
		
	if args["ghi đè năm"] then
		year:wikitext(args["ghi đè năm"])
	else
		local year_args = {
			[1] = args["năm"],
			noprint = "0",
			nocat = "0",
		}
		
		if args["không năm"] then
			year_args['noprint'] = "1"
		end
		
		if args["định hướng"] and yesno(args['định hướng']) then
			-- disambiguations never categorise
			year_args['nocat'] = "1"
		else
			if args["không thể loại năm"] and yesno(args['không thể loại năm']) then
				-- manually no-catted
				year_args['nocat'] = "1"
			elseif mw.title.getCurrentTitle().isSubpage then
				-- only categorise if this is a base page
				year_args['nocat'] = "1"
			end
		end
		mw.logObject(year_args)
		year:wikitext(mw.text.trim(frame:expandTemplate{
			title = "đầu đề/năm",
			args  = year_args
		}))
	end
	return year
end

--[=[
Construct the year field
]=]	
function p.year(frame)
	local args = getArgs(frame)

	ret = ""
	if get_arg_or_override(args, "năm") then
		ret = tostring(construct_year(frame, args))
	end

	return ret
end

function create_vcard(id, content, wrap_fn)
	local span = mw.html.create("span")
		:addClass("vcard")
		:css({
			['font-style'] = 'italic'
		})
		:attr("id", id)
	
	if wrap_fn then
		span:tag("span")
			:addClass("fn")
			:wikitext(content)
	else
		span:wikitext(content)
	end
	
	return span
end

function p.author(frame)
	local args = getArgs(frame)
	
	local param_name = "tác giả"
	local prefix = "của"
	local id = "header_author_text"

	local s = ""
	local atext;
	
	local wrap_fn = true

	if args["ghi đè " .. param_name] then
		s = s .. "<br/>"
		atext = args["ghi đè " .. param_name]
	elseif args[param_name] then
		if args['phần'] then
			s = s .. "&#32;"
		else
			s = s .. "<br/>"
		end
		
		if string.lower(args[param_name]) == "không rõ" then
			atext = "không rõ"
			wrap_fn = false
		else
			atext = "[[Tác gia:" .. args[param_name] .. "|" .. args[param_name] .. "]]"
		end
 		
		s = s .. "<i>" .. prefix .. "</i>" .. " "
	end
	
	local a_span = create_vcard(id, atext, wrap_fn)
	return s .. tostring(a_span)
end


function p.editor(frame)
	local args = getArgs(frame)

	local ed = get_arg_or_override(args, "người hiệu đính")

	-- no editors
	if ed == nil then
		return ""
	end
	
	local have_authors = get_arg_or_override(args, "tác giả") ~= nil

	local s = " "
	if have_authors then
		s = ", "
	end
	
	if not have_authors and not args["phần"] then
		s = s .. "<br/>"
	end
	
	-- need to tidy this up and check for ghi đè editor = unknown|not mentioned|?
	local special
	if ed == "?" or string.lower(ed) == "không rõ" then
		special = "không rõ người hiệu đính"
	elseif string.lower(ed) == "không đề cập" then
		special = "người hiệu đính không được đề cập"
	elseif ed ~= nil then
		s = s 
	end
	
	s = "<i>" .. s .. "</i> "

	local etext
	local wrap_fn = true
	if args["ghi đè người hiệu đính"] then
		etext = args["ghi đè người hiệu đính"]
	elseif args["người hiệu đính"] then

		if special then
			etext = special
			wrap_fn = false
		else
			etext = "[[Tác gia:" .. args["người hiệu đính"] .. "|" .. args["người hiệu đính"] .. "]] hiệu đính"
		end
	end
	
	local span = create_vcard("header_editor_text", etext, true)
	return s .. tostring(span)
end

function p.translator(frame)
	local args = getArgs(frame)

	local tr = get_arg_or_override(args, "dịch giả")

	-- no translator
	if tr == nil then
		return ""
	end

	local have_authors = get_arg_or_override(args, "tác giả") ~= nil
	local have_editors = get_arg_or_override(args, "người hiệu đính") ~= nil

	local s = " "
	if have_authors or have_editors then
		s = ", "
	end
	
	if not have_authors and not have_authors and args["phần"] == nil then
		s = s .. "<br/>"
	end

	local special
	if tr == "?" or string.lower(tr) == "không rõ" then
		special = "không rõ dịch giả"
	elseif string.lower(tr) == "không đề cập" then
		special = "dịch giả không được đề cập"
	elseif string.lower(tr) == "wikisource" then
		special = "do [[Wikisource:Biên dịch|<span id=\"header_translator_text\">Wikisource</span>]] dịch"
	elseif tr ~= nil then
		s = s .. "do"
	end
	
	s = "<i>" .. s .. "</i> "

	local etext
	local wrap_fn = true
	if args["ghi đè dịch giả"] then
		etext = args["ghi đè dịch giả"] .. " dịch"
	elseif args["dịch giả"] then

		if special then
			etext = special
			wrap_fn = false
		else
			etext = "[[Tác gia:" .. args["dịch giả"] .. "|" .. args["dịch giả"] .. "]] dịch"
		end
	end
	
	local span = create_vcard("header_translator_text", etext, true)
	return s .. tostring(span)
end

function p.section(frame)
	local args = getArgs(frame)
	
	if not args["phần"] then
		return
	end
	
	local s = "<br /><span id=\"header_section_text\">" .. args["phần"] .. "</span>"

	-- there are synonyms for this
	local trans = get_arg_or_override(args, "dịch giả của phần")
	if trans == nil then
		trans = get_arg_or_override(args, "dịch giả đóng góp")
	end

	-- there are synonyms for this
	local sec_author = get_arg_or_override(args, "tác giả của phần")
	if sec_author == nil then
		sec_author = get_arg_or_override(args, "người đóng góp")
	end
	
	-- first part of section_translator, adds a line return to split to two lines due to length if section translator exists	
	if trans and sec_author then
		s = s .. "<br/>"
	end
	
	if sec_author then
		s = s .. "<i> của <span id=\"header_contributor_text\" class=\"vcard\">"
		s = s .. "<span class=\"fn\">"
		
		if args["ghi đè section_tác giả"] then
			s = s .. args["ghi đè tác giả của phần"]
		elseif args["ghi đè người đóng góp"] then
			s = s .. args["ghi đè người đóng góp"]
		else
			s = s .. "[[Tác gia:" .. sec_author .. "|" .. sec_author .. "]]"
		end
		s = s .. "</span></span></i>"
	end
	
	if trans then
		if sec_author then
			s = s .. ","	
		end
		s = s .. " <i>do <span id=\"header_section_translator_text\" class=\"vcard\">"
		
		if args["ghi đè dịch giả của phần"] then
			s = s .. args['ghi đè dịch giả của phần']
		else
			s = s .. "[[Tác gia:" .. trans .. "|" .. trans .. "]]"
		end
		s = s .. " dịch</span></span></i>"		
	end
	
	return s
end

--[=[
Construct the notes field
]=]
function p.notes(frame)
	local args = getArgs(frame)
	
	local notes_args = {
		style = "border-bottom: 1px solid #A0A0A0; background-color: #FAFAFF;",
		id = navigationNotes
	}

	local wdid = mw.wikibase.getEntityIdForCurrentPage()
	local sister_args = {"định hướng", "ấn bản", "chủ đề", "tác gia liên quan",
		"wikipedia", "commons", "commonscat", "wikiquote", "wikinews", 
		"wiktionary", "wikibooks",  "wikidata", "wikivoyage", 
		"wikiversity", "wikispecies", "meta"}
	local sisters = ""
	if wdid ~= nil or has_any_arg(args, sister_args) then
		notes_args['dự án khác'] = (get_plain_sister(frame, args, sister_args))
	end

    -- TODO: Temporarily track pages sisterlinking to Wikilivres.
    if args["wikilivres"] ~= nil and args["wikilivres"] ~= "" then
        table.insert(categories, "Pages using header with sister links to Wikilivres")
    end

	if args["viết tắt"] then
		notes_args['viết tắt'] = args["viết tắt"]
	end
		
	if args["ghi chú"] then
		notes_args['nội dung'] = args['ghi chú'] 
	end

	return frame:expandTemplate{title = "đầu_đề/ghi_chú", args = notes_args}
end

return p