Module:Sandbox/Premeditated/test

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/Premeditated/test/doc

local sdc = require 'Module:Structured Data'
local yesno = require('Module:Yesno')
--------------------------------------------------------------------------------
-- Builder
--------------------------------------------------------------------------------
local file = {}

local function get(val)
	local text = {}
	local text2 = ''
	for i, v in ipairs(val) do
    	text[#text+1] = mw.wikibase.getLabel(v.mainsnak.datavalue.value.id)
	end
	
	if not text[1] then
		return ''
	elseif not text[2] then 
		text2 = text[1]:gsub("^%l", string.upper)
	else
		text[1] = text[1]:gsub("^%l", string.upper)
		text2 = table.concat(text, ', ')
	end
	return text2
end

function file.new(data, args)
	local pid = args[2] or 'P275'
	local val = mw.wikibase.getBestStatements(data, pid)
	local file = nil

	if args then
		file = '[[File:' .. args[1] .. '|thumb|'
		if mw.wikibase.getLabel(data) then
			file = file .. mw.wikibase.getLabel(data) .. '. '
		end
		if val[1] then
			file = file .. mw.wikibase.getLabel(pid):gsub("^%l", string.upper) .. ': ' .. get(val) .. ']]'
		else
			file = file .. ']]'
		end
		return file
	end
	return nil

end
	
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
local p = {}

local function getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then
		return titleObj
	else
		return nil
	end
end

function p._main(args)
	local title = nil
	if args[1] then
		title = 'M' .. getTitle('File:' .. args[1]).id
	else
		title = sdc.getMID()
	end
	return file.new(title, args)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'User:Premeditated/sandbox'
	})
	return p._main(args)
end

return p