Module:Universal infocard

-- Lua code for universal infocard.

local isConfig, config = pcall( require, 'Module:Universal infocard/config' ); if isConfig == false then config = { skipPropertyIds = { P31 = true, P279 = true, }	}; end

local p = {}; local lang = mw.getContentLanguage; local entityId = nil;

-- CSS classes. local classes = {}; if config and config.classes then for key, value in pairs( config.classes ) do		classes[ key ] = value; end end function getClassString( type ) local class; if classes[type] then class = classes[type]; elseif type ~= '' then class = 'infobox-' .. type; else class = 'infobox'; end

if class == '' then return ''; end

return ' class="'.. class .. '"'; end

function getTemplate( propertyId ) if config and config.templates then if config.templates[ propertyId ] then return config.templates[ propertyId ]; elseif string.match( propertyId, '^title_Q%d+$' ) then return nil; -- hack for getTitleTemplate end end

if propertyId == 'title' then return ''; end

if propertyId == 'map' then return getMap; end

if config and config.templates and config.templates.default then return config.templates.default; end

if string.match( propertyId, '^P%d+$' ) then return '#statements:' .. propertyId; end return nil; end

--	Checks entity classes and tries to return a custom template, if one exists.	Otherwise, returns the base template for the title. function getTitleTemplate( claims ) if not claims then return getTemplate( 'title' ); end for _, claim in pairs( claims ) do		if claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.value and claim.mainsnak.datavalue.value.id			and claim.mainsnak.datavalue.value.id == itemId then local template = getTemplate( 'title:' .. itemId ); if template then return template; end; end end return getTemplate( 'title' ); end

function expandTemplate( frame, title, args ) if not title then return ''; elseif type( title ) == 'function' then args.frame = frame; return title( args ) elseif type( title ) == 'string' then if string.match( title, '^#' ) then return frame:callParserFunction{ name = title, args = args }; elseif string.match( title, '^{' ) then return frame:preprocess( title ); else return frame:expandTemplate{ title = title, args = args }; end elseif type( title ) == 'table' then local realTitle = title[ 1 ]; table.remove( title, 1 ) return expandTemplate( frame, realTitle, title ) end end

function splitLine( value1, value2 ) local result = ''; if ( value1 and string.len( value1 ) ~= 0 ) or ( value2 and string.len( value2 ) ~= 0 ) then result = ' '; if ( value1 and string.len( value1 ) ~= 0 ) then local colspan = ''; if ( not value2 or string.len( value2 ) == 0 ) then colspan = 'colspan="2"'; end result = result .. '';			result = result .. value1; result = result .. ' ';		end if ( value2 and string.len( value2 ) ~= 0 ) then local colspan = ''; if ( not value1 or string.len( value1 ) == 0 ) then colspan = 'colspan="2"'; end result = result .. '';			result = result .. value2; result = result .. ' ';		end result = result .. ' \n'; end

return result; end

function getLine( value, class ) local result = ''; if ( value and string.len( value ) ~= 0 ) then result = result .. ' ';		result = result .. value; result = result .. ' \n'; return result; end return result; end

function getValue( label, value ) local result = ''; if ( value ~= nil and string.len( value ) ~= 0 ) then if label then result = result .. ' ' .. label .. ' ';			result = result .. '\n'; else result = result .. ' ';		end result = result .. value; result = result .. ' \n'; return result; end return result; end

function getMap( args ) local entityId = args.entityId or mw.wikibase.getEntityIdForCurrentPage; local statements = mw.wikibase.getBestStatements( entityId, 'P625' ); if not statements or		not statements[ 1 ] or		not statements[ 1 ].mainsnak or		statements[ 1 ].mainsnak.snaktype ~= 'value' or		statements[ 1 ].mainsnak.datavalue.value.globe ~= 'http://www.wikidata.org/entity/Q2' then return ''; end

local coord = statements[ 1 ].mainsnak.datavalue.value; local title = expandTemplate( args.frame, getTemplate( 'title' ), { from = entityId } );

local mapContent = {		"type": "Feature",		"geometry": {			"type": "Point",			"coordinates": [				 .. coord['longitude'] .. ,				 .. coord['latitude'] .. ]		},		"properties": {			"title": " .. title .. ",			"marker-symbol": "star",			"marker-color": " .. entityId .. ",		"properties": {			"stroke": " .. entityId .. ",		"properties": {			"fill": ";

return args.frame:extensionTag{ name = 'mapframe', content = '[' .. mapContent .. ']',   	args = { 'frameless', align = 'center', latitude = coord['latitude'], longitude = coord['longitude'], zoom = 11, width = 300, height = 250, }	}; end

function renderValue( frame, propertyId, args ) local tplArgs = { propertyId, from = entityId, nocat = frame.args['nocat'] };

if args then local k = nil; repeat k = next( args, k ); if k then tplArgs[ k ] = args[ k ]; end until not k   end

return expandTemplate( frame, getTemplate( propertyId ), tplArgs ); end

-- Filter deprecated claims and returning only preferred ones if present. function filterClaims( entity, propertyId ) if ( entity.claims == nil or entity.claims[ propertyId ] == nil ) then return {}; end local all = entity.claims[ propertyId ]; local normal = {}; local preferred = {}; for _, claim in pairs( all ) do		local rank = claim.rank or 'normal'; if ( rank == 'normal' ) then table.insert( normal, claim ); end if ( rank == 'preferred' ) then table.insert( preferred, claim ); end end if ( #preferred > 0 ) then return preferred; end return normal; end

function propertyLabel( propertyId ) local label, labelLang = mw.wikibase.getLabelWithLang( propertyId ); label = lang:ucfirst( label ); if labelLang ~= lang:getCode then label =  .. label .. ; end return label; end

function simpleLabel( entityId ) local label = mw.wikibase.label( entityId ); label = lang:ucfirst( label ); return label; end

function getErrorMessage( message ) local result = ' '; return result; end

function p.render( frame ) local i18n_error_emptyWikidataEntity = ''; local i18n_error_noWikidataEntity = ''; if config and config.i18n and config.i18n.error then if config.i18n.error.emptyWikidataEntity then i18n_error_emptyWikidataEntity = config.i18n.error.emptyWikidataEntity; end if config.i18n.error.noWikidataEntity then i18n_error_noWikidataEntity = config.i18n.error.noWikidataEntity; end end

local result = ' ';

-- Coords. if claims.P625 ~= nil then result = result .. renderValue( frame, 'P625', { display = 'title' } ); end -- Tracking category. if config and config.categories and config.categories['few-properties-shown'] then if shownProperties < 4 then result = result .. ''		end end

return result; end

return p;