وحدة:ملعب/Mr. Ibrahem/QS

--[[  
 
This module is intended to be the engine behind "Template:Creator".

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

Authors and maintainers:
* User:Jarekt - original version 

--]]
local p = {}
local function add(args, data)
	local qsTable = {}  -- table to store QuickStatements 
	-- two forms of QuickStatements command with and without quotes
	local qsCommand = 
				{ '%s\t%s\t%s' 
				, '%s\t%s\t"%s"'
				}
	-- QS 
	if args.taxname and not data.P225 then
		table.insert( qsTable, string.format(qsCommand[2], args.wikidata, 'P225', args.taxname) )
	end
	if args.image and not data.P18 then
		table.insert( qsTable, string.format(qsCommand[2], args.wikidata, 'P18', args.image) )
	end
	-- convert QS table to a string
	local QS   = ''     -- quick_statements final string
	if #qsTable>0 then
		local qsHeader  = 'https://tools.wmflabs.org/quickstatements/#v1='
		local qsWrapper = ' [[File:Commons_to_Wikidata_QuickStatements.svg|15px|link=%s]]'
		QS = string.format(qsWrapper, qsHeader .. table.concat( qsTable, '%0A'))
		QS = mw.ustring.gsub(QS, '\t', "%%09")
		QS = mw.ustring.gsub(QS, '"' , "%%22")
		QS = mw.ustring.gsub(QS, ' ' , "%%20")
	end
	return QS
end

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

-- ===========================================================================
-- === Version of the function to be called from other LUA codes
-- ===========================================================================
function p._creator(args)
	local str
	local cats = '' -- categories 
	local QS = ''   -- quick_statements 
	local entity = nil
	if mw.wikibase and args.wikidata then
	  --mw.log("'" .. args.wikidata .. "'")
		entity = mw.wikibase.getEntity(args.wikidata)
	end
	-- ===========================================================================
	-- === Step 1: harverst wikidata properties matching creator template fields
	-- ===========================================================================
	local data = {} -- structure similar to "args" but filled with wikidata data
	if entity then
		-- harvest few properties
		local property = {'P225' , 'P18'}
		for _,P in ipairs( property ) do
			if entity.claims and entity.claims[P] then -- if we have wikidata item and item has the property
				-- capture all Wikidata values for the identifier
				for _, statement in pairs( entity:getBestStatements( P )) do
					if (statement.mainsnak.snaktype == "value") then -- or if statement.mainsnak.datavalue then 
						local v = statement.mainsnak.datavalue.value
						if v['numeric-id'] then
							v = 'Q' .. tostring( v['numeric-id'] )
						end
						data[P] = v
					end
				end
			end
		end
	end
	-- If creator namespace and "person" template than add maintenance categories
	str = add(args, data)
	-- ===========================================================================
	-- === Step 3: create maintenance categories and render html of the table
	-- ===========================================================================
	--local results = Build_html(args)
	return str
end

-- ===========================================================================
-- === Version of the function to be called from template namespace
-- ===========================================================================
function p.creator(frame)
	-- switch to lowercase parameters to make them case independent
	local args = {}
	for name, value in pairs( frame:getParent().args ) do 
		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

	-- call the inner "core" function
	local results = p._creator(args)	
	return results 
end

return p