模块:Information

求闻百科,共笔求闻
文档图示 模块文档[创建] [跳转到代码]

本模块还没有文档页面。

您可以创建文档以让用户更好地理解本模块的用途。
编者可以在本模块的沙盒创建 | 镜像和测试样例创建页面进行实验。
请将模块自身所属的分类添加在文档中。本模块的子页面
--[[  
  __  __           _       _        ___        __                            _   _             
 |  \/  | ___   __| |_   _| | ___ _|_ _|_ __  / _| ___  _ __ _ __ ___   __ _| |_(_) ___  _ __  
 | |\/| |/ _ \ / _` | | | | |/ _ (_)| || '_ \| |_ / _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \ 
 | |  | | (_) | (_| | |_| | |  __/_ | || | | |  _| (_) | |  | | | | | | (_| | |_| | (_) | | | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)___|_| |_|_|  \___/|_|  |_| |_| |_|\__,_|\__|_|\___/|_| |_|
                                                                                               
This module is intended to be the engine behind "Template:Information".

Please do not modify this code without applying the changes first at
"Module:Information/sandbox" and testing at "Module:Information/testcases".

Authors and maintainers:
* User:Jarekt - original version 
]]
-- =======================================
-- === Dependencies ======================
-- =======================================
require('Module:No globals') -- used for debugging purposes as it detects cases of unintended global variables
local core    = require('Module:Core')

-- ==================================================
-- === Internal functions ===========================
-- ==================================================

local function langWrapper(text, textLang) 
-- code equivalent to https://commons.wikimedia.org/wiki/Template:Description
	local language = mw.language.new( textLang )
	local dir      = language:getDir()  
	local LangName = language:ucfirst(mw.language.fetchLanguageName( textLang, textLang))
	local str = mw.ustring.format('<span class="language %s"><b>%s:</b></span>', textLang, LangName)
    return mw.ustring.format('<div class="fileinfo-description mw-content-%s" dir="%s" lang="%s">%s %s</div>', dir, dir, textLang, str, text)
end

-------------------------------------------------------------------------------
local function getBareLabel(id, userLang) 
-- code equivalent to require("Module:Wikidata label")._getLabel with Wikidata=- option
	local label, link
	-- build language fallback list
	local langList = mw.language.getFallbacksFor(userLang)
	table.insert(langList, 1, userLang)
	for _, lang in ipairs(langList) do  -- loop over language fallback list looking for label in the specific language
		label = ''
		if label then break end                    -- label found and we are done
	end	
	return label or id
end

-------------------------------------------------------------------------------
local function message(name)
	return mw.message.new( 'wm-license-information-'..name ):plain()
end

-- ====================================================================
-- === This function is just responsible for producing HTML of the  ===
-- === template. At this stage all the fields are already filed     ===
-- ====================================================================
local function Build_html(args)
	local lang = args.lang -- user's language
	local dir  = mw.language.new( lang ):getDir()    -- get text direction
	local desTag = mw.ustring.format('<span class="summary fn" style="display: none;">%s</span>', args.pagename)
	local prmTag = mw.ustring.format("<br /><small>([[%s|%s]])</small>", message('permission-reusing-link'), 
								 message('permission-reusing-text'))

	-- field specific preferences
	local params = {
		{field='description'    , id='fileinfotpl_desc', tag2=desTag, td='class="description"'},
		{field='other_fields_1'},
		{field='date'           , id='fileinfotpl_date'},
		{field='source'         , id='fileinfotpl_src'}, 
		{field='author'         , id='fileinfotpl_aut'},
		{field='artist'         , id='fileinfotpl_aut'},
		{field='permission'     , id='fileinfotpl_perm', tag2=prmTag },
		{field='attribution'    , id='fileinfotpl_attribution'},
		{field='other_versions' , id='fileinfotpl_ver',  tag='other-versions'}, 
		{field='other_fields'},
	}
	local results = {}
	for _, param in ipairs(params) do
		local field, tag, cell1, cell2, id
		field = args[param.field]
		if param.id then -- skip "other fields" parameter
			if type(field) == 'string' then  -- add "id" to first <td> cell only if the field is present
				id = mw.ustring.format('id="%s" ', param.id)
			elseif type(field) == 'table' then
				-- the field was initially not present, it contains only our
				-- warning text; flatten it so that mw.ustring.format() gets a string
				field = field.missing
			end
			if field or (args.demo and param.tag) then  -- skip the row if still no field
				tag   = message(param.tag or param.field) .. (param.tag2 or '')
				cell1 = mw.ustring.format('<td %sclass="fileinfo-paramfield" lang="%s">%s</td>\n', id or '', lang, tag)
				cell2 = mw.ustring.format('<td %s>\n%s</td>', param.td or '', field or '')
				field = mw.ustring.format('<tr>\n%s%s\n</tr>\n\n', cell1, cell2)
			end
		end
		table.insert(results, field)
	end

	-- add table and outer layers
	local style = mw.ustring.format('class="fileinfotpl fileinfotpl-type-information vevent '..
		'mw-content-%s" style="direction: %s;" cellpadding="4"', dir, dir)
	results = mw.ustring.format('<table %s>\n\n%s\n</table>\n', style, table.concat(results))
	results = mw.ustring.format('<div class="hproduct commons-file-information-table">\n%s\n</div>', results)
	return results
end

-- ==================================================
-- === External functions ===========================
-- ==================================================
local p = {}

-- ===========================================================================
-- === Version of the function to be called from other LUA codes
-- ===========================================================================

-------------------------------------------------------------------------------
-- _information function creates a wikicode for {{Information}} template based on
-- passed arguments (through "args"). Allowed fields of 
-- "args" are : 'description', 'date', 'permission', 'attribution', 'author', 'other_versions',  
-- 'source','other_fields', 'other_fields_1', 'demo' and 'lang'
-------------------------------------------------------------------------------
-- Dependencies: Build_html
-------------------------------------------------------------------------------
function p._information(args)

	local cats = ''
	
	-- ============================================================================================
	-- === add [[Category:模板参数错误的页面]] if needed ===
	-- ============================================================================================
	local page = mw.title.getCurrentTitle()
	local lang = args.lang
	local namespace = page.namespace   -- get page namespace
	if namespace==6 or namespace==10 then
		local allowedFields = {'description', 'date', 'permission', 'attribution', 'author', 'artist', 'other_versions',  
				'source','other_fields', 'other_fields_1', 'demo', 'lang', 'strict'}
		local set, badField = {}, {}
		for _, field in ipairs(allowedFields) do set[field] = true end
		for field, _ in pairs( args ) do 
			if not set[field] then
				table.insert(badField, field)
			end
		end
		if #badField>0 then
			cats = mw.ustring.format('\n;<span style="color: red">Error in [[Template:Information|{{Information}}'..
				' template]]: unknown parameter "%s".</span>',  table.concat(badField,'", "'))
			cats = cats .. '\n[[Category:模板参数错误的页面]]'
		end
	end
	args.pagename = page.text

	-- ====================================================
	-- === add tracking templates and categories        === 
	-- ====================================================
	-- add the template tag (all official infoboxes transclude {{Infobox template tag}} so files without that tag do not have an infobox
	mw.getCurrentFrame():expandTemplate{ title = 'Infobox template tag' } 

	-- files are required to have at least the 3 fields below
	if args.strict~=false then
		local reqFields = {description='缺少描述的文件', author='缺少作者信息的文件', source='缺少来源的文件'}
		for field, errCat in pairs(reqFields) do 
			if args[field] and mw.ustring.match(args[field],"^[%s%p]+$") then 
				args[field]=nil; 
			end -- ignore punctuation only fields
			if not args[field] then
				-- code equivalent to Template:Source missing, Template:Author missing, Template:Description missing
				local tag1 = 'class="boilerplate metadata" id="cleanup" style="text-align: center; background: #ffe; '..
					'margin: .75em 15%; padding: .5em; border: 1px solid #e3e3b0;'
				local tag2 = message(field..'-missing')
				local tag3 = message(field..'-missing-request')
				local dir  = mw.language.new( lang ):getDir()    -- get text direction
				args[field] = {missing =  mw.ustring.format('<div %s direction: %s;" lang="%s">%s<br>%s</div>', tag1, dir, lang, tag2, tag3)}
				cats = cats .. '\n[[Category:'.. errCat ..']]'
			end
		end
	end
	if namespace~=6  then 
		cats = '' -- categories are added only to files
	end

	return Build_html(args) .. cats
end

-- ===========================================================================
-- === Version of the functions to be called from template namespace
-- ===========================================================================

-------------------------------------------------------------------------------
-- information function creates a wikicode for {{Information}} template based on
-- passed arguments (through "frame"). Allowed template
-- arguments are : 'description', 'date', 'permission', 'author', 'other_versions',  
-- 'source','other_fields', 'other_fields_1', 'demo' and 'lang'. All inputs do not 
-- depend on capitalization and all "_" can be replaced with spaces.
-------------------------------------------------------------------------------
-- Dependencies: p._information
-------------------------------------------------------------------------------
function p.information(frame)
	local args = core.getArgs(frame)
	args.strict = true
	return p._information(args)	
end

return p