Module:Depicted people

From Multilingual Bookbinding Dictionary
Jump to navigation Jump to search
The Module:Depicted people lists provided subjects. They can be both strings or Wikidata entity IDs. When they are Wikidata entity IDs, a wikilink on the user's language Wikipedia to that subject is provided.

Actually it list whatever Wikidata entity is provided, so the name should be Module:Wikidata labels or something more clear.

From Wikitext

Usage example:

{{#invoke:Depicted people|main|Q62849|Foo Bar|Q7259|Q188954|Q7439}}

Output:

Lua error in Module:Wikidata_label at line 283: attempt to index field 'wikibase' (a nil value).

From Lua

Use something as follow from Lua to have the same behaviour as above:

<source lang="lua"> local s = require('Module:Depicted people')._main( { 'Q62849', 'Foo Bar', 'Q7259', 'Q188954', 'Q7439' } ) </source>

See also


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 = '&#32;– ' .. 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