Jump to content

Module:Taxonbar/sandbox2

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 20:14, 3 January 2018 (create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
require('Module:No globals')

local conf = require( "Module:Taxonbar/conf" )

local function getIdsFromWikidata( item, property )
	local ids = {}
	if not item.claims[property] then
		return ids
	end
	for _, statement in pairs( item.claims[property] ) do
		if statement.mainsnak.datavalue then
			table.insert( ids, statement.mainsnak.datavalue.value )
		end
	end
	return ids
end

local function getLink( property, val )
	local link = ""
	if mw.ustring.find( val, '//' ) then
		link = val
	else
		if type(property) == 'number' then
			local entityObject = mw.wikibase.getEntityObject('P'..property)
			local dataType = entityObject.datatype
			if dataType == "external-id" then
				local formatterURL = entityObject:getBestStatements('P1630')[1]
				if formatterURL then link = formatterURL.mainsnak.datavalue.value end
			elseif dataType == "url" then
				local subjectItem = entityObject:getBestStatements('P1629')[1]
				if subjectItem then
					local officialWebsite = mw.wikibase.getEntityObject(subjectItem.mainsnak.datavalue.value.id):getBestStatements('P856')[1]
					if officialWebsite then	link = officialWebsite.mainsnak.datavalue.value end
				end
			elseif dataType == "string" then
				local formatterURL = entityObject:getBestStatements('P1630')[1]
				if formatterURL then
					link = formatterURL.mainsnak.datavalue.value
				else
					local subjectItem = entityObject:getBestStatements('P1629')[1]
					if subjectItem then
						local officialWebsite = mw.wikibase.getEntityObject(subjectItem.mainsnak.datavalue.value.id):getBestStatements('P856')[1]
						if officialWebsite then	link = officialWebsite.mainsnak.datavalue.value end
					end
				end
			end
		elseif type(property) == 'string' then
			link = property
		end
		link = mw.ustring.gsub(link, '$1', val)
	end
	link = mw.ustring.gsub(link, '^[Hh][Tt][Tt][Pp]([Ss]?)://', 'http%1://') -- fix wikidata URL
	val = mw.ustring.match(val, '([^=/]*)/?$') -- get display name from end of URL
	if mw.ustring.find( link, '//' ) then
		return '['..link..' '..val..']'
	elseif link == "" then
		return val
	else
		return '<span class="external">[['..link..'|'..val..']]</span>'
	end
end

local function createRow( id, label, rawValue, link, withUid )
	if link then
		local outStr = '*<span style="white-space:nowrap;">' .. label .. ' <span'
		if withUid then outStr = outStr..' class="uid"' end
		return outStr..'>' .. link .. '</span></span>\n'
	else
		return '* <span class="error">The identifier ' .. id .. ' ' .. rawValue .. ' is not valid.</span>\n'
	end
end

local function copyTable(inTable)
	if type(inTable) ~= 'table' then return inTable end
	local outTable = setmetatable({}, getmetatable(inTable))
	for key, value in pairs (inTable) do outTable[copyTable(key)] = copyTable(value) end
	return outTable
end

local p = {}

function p.authorityControlTaxon( frame )
	local parentArgs = copyTable(frame:getParent().args)
	local stringArgs = false
	--Make args case insensitive
	for k, v in pairs( frame:getParent().args ) do
		if type(k) == 'string' then
			v = mw.ustring.lower(v)
			local lowerk = mw.ustring.lower(k)
			if not parentArgs[lowerk] or parentArgs[lowerk] == '' then
				parentArgs[lowerk] = v
				parentArgs[k] = nil
			end
			if v and v ~= '' and v ~= 'no' and lowerk ~= 'from' then stringArgs = true end
		end
	end
	--remap aliases
	for _, a in pairs( conf.aliases ) do
		local alias, name = mw.ustring.lower(a[1]), mw.ustring.lower(a[2])
		if parentArgs[alias] and not parentArgs[name] then
			parentArgs[name] = parentArgs[alias]
			parentArgs[alias] = nil
		end
	end
	--Create rows
	local elements, title = {}, {}
	
	--Look for for2/from2, etc.
	if parentArgs['from'] == '' then parentArgs['from'] = nil end
	local fromCount, forCount = 0, 0
	for k, v in pairs( parentArgs ) do
		if v and v ~= '' then
			if mw.ustring.sub(k,1,4) == "from"  then
				local fromNumber = tonumber(mw.ustring.sub(k,5,-1))
				if fromNumber and fromNumber > fromCount then fromCount = fromNumber end
			elseif mw.ustring.sub(k,1,3) == "for" then
				local forNumber = tonumber(mw.ustring.sub(k,4,-1))
				if forNumber and forNumber > forCount then forCount = forNumber end
			end
		end
	end	
	local maxCount = math.max(forCount, fromCount)
	if maxCount > 0 then
		parentArgs['from'] = parentArgs['from' .. maxCount]
		parentArgs['for'] = parentArgs['for' .. maxCount]
	end
	
	--Fetch Wikidata item
	local item = mw.wikibase.getEntity(parentArgs['from'])
	local label = item and item:getLabel()
	if label and label ~= '' then
		title = mw.title.new(label)
		if not title then title = mw.title.getCurrentTitle() end
	else
		title = mw.title.getCurrentTitle()
	end
	if (not parentArgs['wikidata'] or parentArgs['wikidata'] == '') and (title.namespace == 0) then
		if parentArgs['from'] then parentArgs['wikidata'] = parentArgs['from'] elseif item then parentArgs['wikidata'] = item.id end
	end
	if title.namespace == 0 or stringArgs then --Only in the main namespace or if there are manual overrides
		local sourcesFound = false
		for _, params in pairs( conf.databases ) do
			params[1] = mw.ustring.lower(params[1])
			--Wikidata fallback if requested
			if (item ~= nil and item.claims ~= nil) and params[3] ~= 0 and (not parentArgs[params[1]] or parentArgs[params[1]] == '') then
				local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
				if wikidataIds[1] then
					parentArgs[params[1]] = wikidataIds[1]
				end
			end
			local val = parentArgs[params[1]]
			if val and val ~= '' and mw.ustring.lower(val) ~= 'no' and params[3] ~= 0 then
				table.insert( elements, createRow( params[1], params[2] .. ':', val, getLink( params[3], val ), true ) )
				if params[1] ~= 'wikidata' then sourcesFound = true end
			end
		end
		
		--Generate navbox title
		local groupName = ""
		if maxCount < 1 then groupName = '[[Help:Taxon identifiers|Taxon identifiers]]<br />' end
		if parentArgs['for'] and parentArgs['for'] ~= '' then groupName = groupName .. "''" .. parentArgs['for'] .. "''" end

		local Navbox = require('Module:Navbox')

		if sourcesFound then
			return Navbox._navbox( {
				name  = 'Taxonbar',
				bodyclass = 'hlist',
				listclass = '',
				groupstyle = 'text-align: left;',
				group1 = groupName, --.. elementscats,
				list1 = table.concat( elements )
				} )
		else
			return ""
		end
	end
end

return p