Module:Depicted people/sandbox

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Depicted people/sandbox/doc

Code

local p = {}

--- To know if a string is a Wikidata Q-code.
--
-- @param s string E.g. 'Q123'
-- @param boolean
local function is_wikidata_q_code( s )
	return string.match( s, '^[qQ][0-9]+$' )
end

--- Retrieve only values which have numeric keys.
--
-- @param t table
-- @return table
function only_numerics( t )
	local numerics = {}
	for k, v in pairs( t ) do
		local is_numeric_arg = nil ~= tonumber( k )
		if is_numeric_arg then
			numerics[ #numerics + 1 ] = v
		end
	end
	return numerics
end

--- Handle parent and direct arguments.
--
-- @param frame table Page frame
-- @return table Arguments
function frame_arguments( frame )
	local args = {}
	for k, v in pairs( frame:getParent().args ) do 
		if v ~= '' then
			args[ k ] = v
		end
	end
	for k, v in pairs( frame.args ) do 
		if v ~= '' then
			args[ k ] = v
		end
	end
	return args
end

--- To get labels from Lua modules
--
-- @param args table Arguments
-- @return string
function p._label( args )

	-- [[Module:Linguistic]] for conjunction
	local conjunct       = require('Module:Linguistic').conj

	-- [[Module:Wikidata label] for Wikidata labels
	local wikidata_label = require('Module:Wikidata label')._getLabel

	if not args.lang then
		args.lang = mw.getCurrentFrame():callParserFunction( 'int', 'lang' )
	end

	local values = only_numerics( args )

	-- Do something more on Wikidata entity IDs
	local out_values = {}
	for k, v in pairs( values ) do
		if is_wikidata_q_code( v ) then
			v = wikidata_label( v, args.lang, 'wikipedia' )
		end
		out_values[ #out_values + 1 ] = v
	end

	return conjunct( out_values, args.lang )
end

--- To get comment from Lua modules
-- If the user specifies a comment, use that.
-- If the user specify only one Wikidata Q-code, use its Wikidata description.
--
-- @param args table Arguments
-- @return string
function p._comment( args )
	local comment
	if args.comment and args.comment ~= '' then
		comment = args.comment
	else
		local values = only_numerics( args )
		if #values == 1 and values[ 1 ] and is_wikidata_q_code( values[ 1 ] ) then
			comment = require('Module:Wikidata')._getDescription( values[1], args.lang )
		end
	end
	if comment then
		comment = ' – ' .. comment
	end
	return comment
end

--- To get both label and description from template namespace
--
-- @param args table Arguments
-- @return string
function p._main( args )
	return ( p._label(   args ) or '' ) ..
	       ( p._comment( args ) or '' )
end

--- To get the labels from template namespace
--
-- @param frame Page frame table
-- @return string
function p.label( frame )
	return p._label( frame_arguments( frame ) )
end

--- To get the description from template namespace
--
-- @param frame table Page frame
-- @return string
function p.comment( frame )
	return p._comment( frame_arguments( frame) )
end

--- To get both label and description from template namespace
--
-- @param frame table Page frame
-- @return string
function p.main( frame )
	return p._main( frame_arguments( frame ) )
end

return p