Module:PropertyChain

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

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

no documentation yet

Code

--[[  
  __  __           _       _        ____                            _          ____ _           _       
 |  \/  | ___   __| |_   _| | ___ _|  _ \ _ __ ___  _ __   ___ _ __| |_ _   _ / ___| |__   __ _(_)_ __  
 | |\/| |/ _ \ / _` | | | | |/ _ (_) |_) | '__/ _ \| '_ \ / _ \ '__| __| | | | |   | '_ \ / _` | | '_ \ 
 | |  | | (_) | (_| | |_| | |  __/_|  __/| | | (_) | |_) |  __/ |  | |_| |_| | |___| | | | (_| | | | | |
 |_|  |_|\___/ \__,_|\__,_|_|\___(_)_|   |_|  \___/| .__/ \___|_|   \__|\__, |\____|_| |_|\__,_|_|_| |_|
                                                   |_|                  |___/                           
Maintainers:
 * Jatekt

]]

local getLabel = require('Module:Wikidata label')._getLabel		-- get localized translations of date formats
local core = require('Module:Core')		-- get localized translations of date formats

-------------------------------------------------------------------------------
local function getProperty(entity, prop)
	return (core.parseStatements(entity:getBestStatements( prop ), nil) or {nil})[1]
end

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

--[[ ========================================================================================
Follow a chain of properties:
 * item - starting item id
 * prop - property to follow
 * endItem - (optional) item id used as a stopping criteria when encountered
 * link - (optional) link type: wikidata, wikipedia (default) , commons, commonscat etc. same as in "Module:Wikidata label"
 * separator - (optional with '→' default) symbol or string used to separate items in the list
 * lang - (optional defaulting to user's language) language of the labels
]]
function p._PropertyChain(item, prop, endItem, link, separator, lang)	
	if not (lang and mw.language.isSupportedLanguage(lang)) then 
		lang =  mw.getCurrentFrame():callParserFunction( "int", "lang" ) -- get user's chosen language 
	end
	local chain, items = {}, {}
	for iter = 1,50 do -- at maximum only 50 iterations are allowed
		local entity = mw.wikibase.getEntity(item)
		table.insert(chain, getLabel(entity, lang, link) )
		items[entity.id] = true -- early detection of loops
		item = getProperty(entity, prop)
		if not item or items[item] or item==endItem then
			-- stopping criteria: item is missing property "prop", or a loop is detected or end-item ID was found
			break
		end
	end
	if item and item==endItem then
		table.insert(chain, getLabel(item, lang, link) )
	end
	return table.concat(chain, separator or '→')
end

function p.PropertyChain(frame)
	local args = core.getArgs(frame)
	
	-- find stopping item ID
	local endItem = args.endqid
	if not endItem and args.endpid then
		endItem = getProperty(mw.wikibase.getEntity(args.qid), args.endpid, 'one')
	end
	return p._PropertyChain( args.qid, args.pid, endItem, args.link, args.separator, args.lang )	
end

return p