Jump to content
Toggle menu
  • 8 articles
  • 75 files
  • 7 users
  • 37.8K edits
Neodyland Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Information: Difference between revisions

From Neodyland Wiki
No edit summary
No edit summary
 
(42 intermediate revisions by 11 users not shown)
Line 8: Line 8:
This module is intended to be the engine behind "Template:Information".
This module is intended to be the engine behind "Template:Information".


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


Line 14: Line 14:
* User:Jarekt - original version  
* User:Jarekt - original version  
]]
]]
local ISOdate = require('Module:ISOdate')  
-- =======================================
-- === Dependencies ======================
-- =======================================
require('strict') -- used for debugging purposes as it detects cases of unintended global variables
local ISOdate = require('Module:ISOdate')._ISOdate -- date localization
local core    = require('Module:Core')
 
-- ==================================================
-- === Internal functions ===========================
-- ==================================================
 
 
-------------------------------------------------------------------------------
local function message(name, lang)
return mw.message.new( 'wm-license-information-'..name ):inLanguage(lang):plain()
end


-- ====================================================================
-- ====================================================================
Line 21: Line 36:
-- ====================================================================
-- ====================================================================
local function Build_html(args)
local function Build_html(args)
local lang = args.lang -- user's language
-- get text direction
local dir = mw.language.new( lang ):getDir()   -- get text direction
local dir = 'ltr'
local desTag = mw.ustring.format('<span class="summary fn" style="display:none">%s</span>', args.pagename)
if mw.language.new( args.lang ):isRTL() then
local prmTag = mw.ustring.format("<br /><small>([[%s|%s]])</small>", message('permission-reusing-link', lang),  
dir = 'rtl'
message('permission-reusing-text', lang))
end
-- Permissions tag
local tag1 = mw.message.new( "wm-license-information-permission" ):inLanguage(args.lang):plain()
local tag2 = mw.message.new( "wm-license-information-permission-reusing-link" ):inLanguage(args.lang):plain()
local tag3 = mw.message.new( "wm-license-information-permission-reusing-text" ):inLanguage(args.lang):plain()
local permission_tag = string.format("%s<br /><small>([[%s|%s]])</small>", tag1, tag2, tag3)


-- add other fields
-- field specific preferences
local params = {
local params = {
{field='description'    , id='fileinfotpl_desc'  , tag='wm-license-information-description',   alt='Description missing', wrapper='<div class="description">\n%s</div>'},
{field='other_fields_0'},
{field='description'    , id='fileinfotpl_desc', tag2=desTag, td='class="description"'},
{field='other_fields_1'},
{field='other_fields_1'},
{field='date'          , id='fileinfotpl_date' , tag='wm-license-information-date',                                      wrapper='%s'},
{field='date'          , id='fileinfotpl_date', td=mw.ustring.format('lang="%s"', lang)},
{field='source'        , id='fileinfotpl_src'  , tag='wm-license-information-source',        alt='Source missing',      wrapper='%s'}, -- source
{field='source'        , id='fileinfotpl_src'},  
{field='author'        , id='fileinfotpl_aut'  , tag='wm-license-information-author',        alt='Author missing',      wrapper='<div class="fn value">\n%s</div>'},
{field='author'        , id='fileinfotpl_aut'},
{field='permission'    , id='fileinfotpl_perm' , tag=permission_tag,                                                    wrapper='%s'},
{field='permission'    , id='fileinfotpl_perm', tag2=prmTag },
{field='other_versions' , id='fileinfotpl_ver'   , tag='wm-license-information-other-versions',                            wrapper='%s'},  
{field='other_versions' , id='fileinfotpl_ver', tag='other-versions'},  
{field='other_fields'}
{field='other_fields'},
}
}
local results = {}
local results = {}
Line 49: Line 58:
local field, tag, cell1, cell2, id
local field, tag, cell1, cell2, id
field = args[param.field]
field = args[param.field]
if #field<2 then field=nul; end
if param.id then -- skip "other fields" parameter
if param.id then -- skip "other fields" parameter
if field then  -- add "id" to first <td> cell only if the field is present
if type(field) == 'string' then  -- add "id" to first <td> cell only if the field is present
id = string.format('id="%s" ', param.id)
id = mw.ustring.format('id="%s" ', param.id)
elseif param.alt then -- Some field have "Missing" message if field is not provided
elseif type(field) == 'table' then
field = mw.getCurrentFrame():expandTemplate{ title = param.alt }
-- 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
end
if field or args.demo then  -- skip the row if still no field
if field or (args.demo and param.tag) then  -- skip the row if still no field
tag = param.tag
tag   = message(param.tag or param.field, lang) .. (param.tag2 or '')
if string.sub(tag,1,10) == 'wm-license' then
cell1 = mw.ustring.format('<td %sclass="fileinfo-paramfield" lang="%s">%s</td>\n', id or '', lang, tag)
tag = mw.message.new( tag ):inLanguage(args.lang):plain() -- label message in args.lang language
cell2 = mw.ustring.format('<td %s>\n%s</td>', param.td or '', field or '')
end
field = mw.ustring.format('<tr>\n%s%s\n</tr>\n\n', cell1, cell2)
cell1 = string.format('<td %sclass="fileinfo-paramfield" lang="%s">%s</td>\n', id or '', args.lang, tag)
cell2 = string.format('<td>\n'.. param.wrapper ..'</td>', field or '')
field = string.format('<tr valign="top">\n%s%s\n</tr>\n\n', cell1, cell2)
end
end
end
end
table.insert(results, field)
table.insert(results, field)
end
-- To save space in the templatelinks table, skip templatestyles in the File namespace on Commons;
-- files get a copy of these styles via [[MediaWiki:Filepage.css]] instead.
-- See [[Module talk:Information/styles.css]].
local templatestyles = ''
if args.namespace ~= 6 or not(mw.site.server:match('//commons.wikimedia.org$')) then
templatestyles = mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = 'Module:Information/styles.css' }
}
end
end


-- add table and outer layers
-- add table and outer layers
local style = string.format('class="fileinfotpl-type-information toccolours vevent mw-content-%s" style="width: 100%%; direction: %s" cellpadding="4"', dir, dir)
local style = mw.ustring.format('class="fileinfotpl-type-information vevent" dir="%s"', dir)
results = string.format('<table %s>\n%s\n</table>\n', style, table.concat(results))
results = mw.ustring.format('<table %s>\n\n%s\n</table>\n', style, table.concat(results))
results = string.format('<div class="hproduct commons-file-information-table">\n%s\n</div>\n', results)
results = mw.ustring.format('<div class="hproduct commons-file-information-table">\n%s\n%s\n</div>', templatestyles, results)
return results
return results
end
end
Line 85: Line 101:
-- === Version of the function to be called from other LUA codes
-- === 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', 'author', 'other_versions', 
-- 'source','other_fields', 'other_fields_0', 'other_fields_1', 'demo' and 'lang'
-------------------------------------------------------------------------------
-- Dependencies: Build_html, Module:ISOdate (_date)
-------------------------------------------------------------------------------
function p._information(args)
function p._information(args)
local cats = ''
-- ============================================================================================
-- === add [[Category:Pages using Information template with incorrect parameter]] 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', 'author', 'other_versions', 
'source','other_fields', 'other_fields_0', '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:var(--color-error,red)">Error in [[Template:Information|Information'..
' template]]: unknown parameter "%s".</span>',  table.concat(badField,'", "'))
cats = cats .. '\n[[Category:Pages using Information template with incorrect parameter]]'
end
end
if args.date then
-- apply ISODate to function to date string to convert date in ISO format to translated date string
args.date = ISOdate(args.date, lang, '', 'dtstart', '100-999')     
end
args.pagename = page.text
args.namespace = namespace
-- ====================================================
-- ====================================================
-- === add tag templates used for tracking          ===  
-- === add tracking templates and categories        ===  
-- ====================================================
-- ====================================================
if args.date then
-- add the template tag (all official infoboxes transclude {{Infobox template tag}} so files without that tag do not have an infobox
  -- add an empty template which can be used as a tag in PetScan
mw.getCurrentFrame():expandTemplate{ title = 'Infobox template tag' }
local d    = os.date('!*t')                   -- current date table
 
local current_year  = tonumber(d.year)        -- current year
-- files are required to have at least the 3 fields below
local creation_year = tonumber(ISOdate._ISOyear(args.date))
if args.strict~=false then
if creation_year and current_year and (current_year-creation_year)>200 then
local reqFields = {description='Media lacking a description', author='Media lacking author information', source='Images without source'}
mw.getCurrentFrame():expandTemplate{ title ='Works created more than 200 years ago' }
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; color: inherit;background: var(--background-color-warning-subtle,#ffe); '..
'margin: .75em 15%; padding: .5em; border: 1px solid var(--border-color-content-removed,#e3e3b0);'
local tag2 = message(field..'-missing', lang)
local tag3 = message(field..'-missing-request', lang)
local dir  = mw.language.new( lang ):getDir()    -- get text direction
args[field] = {missing = mw.ustring.format('<div %s direction: %s;" lang="%s">%s\n%s\n</div>', tag1, dir, lang, tag2, tag3)}
cats = cats .. '\n[[Category:'.. errCat ..']]'
end
end
end
end  
end
mw.getCurrentFrame():expandTemplate{ title = 'Infobox template tag' } -- add the template tag
if namespace~=6  then
return Build_html(args)
cats = '' -- categories are added only to files
end
 
return Build_html(args) .. cats
end
end


-- ===========================================================================
-- ===========================================================================
-- === Version of the function to be called from template namespace
-- === 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_0', '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)
function p.information(frame)
-- switch to lowercase parameters to make them case independent
local args = core.getArgs(frame)
local args = {}
args.strict = true
for name, value in pairs( frame:getParent().args ) do
return p._information(args)
if value ~= '' then -- nuke empty strings
local name1 = string.gsub( string.lower(name), ' ', '_')
args[name1] = value
end
end
for name, value in pairs( frame.args ) do
if value ~= '' then -- nuke empty strings
local name1 = string.gsub( string.lower(name), ' ', '_')
args[name1] = value
end
end
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = frame:callParserFunction( "int", "lang" ) -- get user's chosen language
end
-- call the inner "core" function
local results, cats = p._information(args)
return results .. cats
end
end


return p
return p
-------------------------------------------------------------------------------
-- List of exported functions
-------------------------------------------------------------------------------
-- information

Latest revision as of 08:24, 9 August 2026

Module documentation[ create · purge ]
This documentation is transcluded from Module:Information/doc.
--[[  
  __  __           _       _        ___        __                            _   _             
 |  \/  | ___   __| |_   _| | ___ _|_ _|_ __  / _| ___  _ __ _ __ ___   __ _| |_(_) ___  _ __  
 | |\/| |/ _ \ / _` | | | | |/ _ (_)| || '_ \| |_ / _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \ 
 | |  | | (_) | (_| | |_| | |  __/_ | || | | |  _| (_) | |  | | | | | | (_| | |_| | (_) | | | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)___|_| |_|_|  \___/|_|  |_| |_| |_|\__,_|\__|_|\___/|_| |_|
                                                                                               
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('strict') -- used for debugging purposes as it detects cases of unintended global variables
local ISOdate = require('Module:ISOdate')._ISOdate -- date localization
local core    = require('Module:Core')

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


-------------------------------------------------------------------------------
local function message(name, lang)
	return mw.message.new( 'wm-license-information-'..name ):inLanguage(lang):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', lang), 
								 message('permission-reusing-text', lang))

	-- field specific preferences
	local params = {
		{field='other_fields_0'},
		{field='description'    , id='fileinfotpl_desc', tag2=desTag, td='class="description"'},
		{field='other_fields_1'},
		{field='date'           , id='fileinfotpl_date', td=mw.ustring.format('lang="%s"', lang)},
		{field='source'         , id='fileinfotpl_src'}, 
		{field='author'         , id='fileinfotpl_aut'},
		{field='permission'     , id='fileinfotpl_perm', tag2=prmTag },
		{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, lang) .. (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
	
	-- To save space in the templatelinks table, skip templatestyles in the File namespace on Commons;
	-- files get a copy of these styles via [[MediaWiki:Filepage.css]] instead.
	-- See [[Module talk:Information/styles.css]].
	local templatestyles = ''
	if args.namespace ~= 6 or not(mw.site.server:match('//commons.wikimedia.org$')) then
		templatestyles = mw.getCurrentFrame():extensionTag{
			name = 'templatestyles', args = { src = 'Module:Information/styles.css' }
		}
	end

	-- add table and outer layers
	local style = mw.ustring.format('class="fileinfotpl-type-information vevent" dir="%s"', 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%s\n</div>', templatestyles, 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', 'author', 'other_versions',  
-- 'source','other_fields', 'other_fields_0', 'other_fields_1', 'demo' and 'lang'
-------------------------------------------------------------------------------
-- Dependencies: Build_html, Module:ISOdate (_date)
-------------------------------------------------------------------------------
function p._information(args)

	local cats = ''
	
	-- ============================================================================================
	-- === add [[Category:Pages using Information template with incorrect parameter]] 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', 'author', 'other_versions',  
				'source','other_fields', 'other_fields_0', '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:var(--color-error,red)">Error in [[Template:Information|Information'..
				' template]]: unknown parameter "%s".</span>',  table.concat(badField,'", "'))
			cats = cats .. '\n[[Category:Pages using Information template with incorrect parameter]]'
		end
	end
	if args.date then
		-- apply ISODate to function to date string to convert date in ISO format to translated date string
		args.date = ISOdate(args.date, lang, '', 'dtstart', '100-999')      
	end 
	args.pagename = page.text
	args.namespace = namespace

	
	-- ====================================================
	-- === 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='Media lacking a description', author='Media lacking author information', source='Images without 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; color: inherit;background: var(--background-color-warning-subtle,#ffe); '..
					'margin: .75em 15%; padding: .5em; border: 1px solid var(--border-color-content-removed,#e3e3b0);'
				local tag2 = message(field..'-missing', lang)
				local tag3 = message(field..'-missing-request', lang)
				local dir  = mw.language.new( lang ):getDir()    -- get text direction
				args[field] = {missing =  mw.ustring.format('<div %s direction: %s;" lang="%s">%s\n%s\n</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_0', '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

-------------------------------------------------------------------------------
-- List of exported functions
-------------------------------------------------------------------------------
-- information
Cookies help us deliver our services. By using our services, you agree to our use of cookies.