Module:SDC tracking

From Gratispaideia Commons

Documentation for this module may be created at Module:SDC tracking/doc

--[[  
  __  __           _       _        ____  ____   ____    _                  _    _             
 |  \/  | ___   __| |_   _| | ___ _/ ___||  _ \ / ___|  | |_ _ __ __ _  ___| | _(_)_ __   __ _ 
 | |\/| |/ _ \ / _` | | | | |/ _ (_)___ \| | | | |      | __| '__/ _` |/ __| |/ / | '_ \ / _` |
 | |  | | (_) | (_| | |_| | |  __/_ ___) | |_| | |___   | |_| | | (_| | (__|   <| | | | | (_| |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)____/|____/ \____|___\__|_|  \__,_|\___|_|\_\_|_| |_|\__, |
                                                    |_____|                              |___/ 
This module is intended to be the engine behind "Template:SDC_tracking".

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

Authors and maintainers:
* User:Jarekt - original version from Wikimedia Commons
* User:Ugochimobi
]]

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


local function Category(catName)
	if catName then
		return '[[Category:' .. catName .. ']]'
	else
		return ''
	end
end

function p.SDC_statement_exist(frame)
	-- skip if we are not in file namespace
	local page = mw.title.getCurrentTitle()
	if page.namespace~=6 then
		return ''
	end	
	
	-- switch to lowercase parameters to make them case independent
	local args = {}
	for name, value in pairs( frame.args ) do 
		if value ~= '' then -- nuke empty strings
			args[string.lower(name)] = value
		end
	end
	
	-- check if statement exist
	local output = ''
	local entity = mw.wikibase.getEntity()
	if entity and entity.statements and entity.statements[args.property] then
		output = Category(args.positive_category)
	else
		output = Category(args.negative_category)
	end
	return output
end

-----------------------------------------------------------------------
function p.SDC_statement_has_value (frame)
	-- skip if we are not in file namespace
	local page = mw.title.getCurrentTitle()
	if page.namespace~=6 then
		return 
	end	
	
	-- switch to lowercase parameters to make them case independent
	local args = {}
	for name, value in pairs( frame.args ) do 
		if value ~= '' then -- nuke empty strings
			args[string.lower(name)] = value
		end
	end
	if not args.value then
		return 
	end
	
	-- check if statement exist
	local entity = mw.wikibase.getEntity()
	if entity and entity.statements and entity.statements[args.property] then
		for _, statement in pairs( entity:getBestStatements( args.property )) do
			if (statement.mainsnak.snaktype == "value") then 
				local val = statement.mainsnak.datavalue.value
				if val.id then 
					val = val.id
				elseif val.text then
					val = val.text
				end
				if val==args.value then
					return Category(args.matching_category)
				end				
			end
		end
		return Category(args.mismatching_category)
	else
		return Category(args.missing_category)
	end
end

return p