universal Infocard

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:Universal infocard/ohje

--[[   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;  -- Styles. local styles = { infobox = '', error = '', title = 'background:#eaecf0; font-weight:bold;', original = 'font-style:oblique;', label = 'width:9em;', text = '', media = '', split = 'vertical-align:middle; text-align:center;', footer = 'background:#eaecf0;', }; local classes = { title = 'infobox-above', media = 'infobox-image', footer = 'infobox-below' }; if config and config.styles then for key, value in pairs( config.styles ) do styles[ key ] = value; end end  function getTemplate( propertyId ) if config and config.templates and config.templates[ propertyId ] then return config.templates[ propertyId ]; end  if propertyId == 'title' then return '{{PAGENAME}}'; 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  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 .. ' .. colspan .. ' style="' .. styles.split .. '">'; 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 .. ' .. colspan .. ' style="' .. styles.split .. '">'; result = result .. value2; result = result .. ''; end result = result .. '\n'; end  return result; end  function getLine( value, style, class ) local result = ''; if ( value and string.len( value ) ~= 0 ) then result = result .. '.. ( style or '' ) .. '" class="duhoc-olo '.. ( class or '' ) .. '">'; 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 .. '.. styles.label .. '">' .. label .. ''; result = result .. '.. styles.text .. '">\n'; else result = result .. '.. styles.text .. '">'; 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 } );  mw.logObject( coord, 'coord' ); mw.logObject( title, 'title' );   local mapContent = [[ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ ]] .. coord['longitude'] .. [[, ]] .. coord['latitude'] .. [[ ] }, "properties": { "title": "]] .. title .. [[", "marker-symbol": "star", "marker-color": "#3366cc" }     }, { "type": "ExternalData", "service": "geoline", "ids": "]] .. entityId .. [[", "properties": { "stroke": "#FF9999" }     }, { "type": "ExternalData", "service": "geoshape", "ids": "]] .. entityId .. [[", "properties": { "fill": "#FF0000", "fill-opacity": 0.1, "stroke": "#FF9999" } } ]];      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  -- Filter deprecated claims and returning only preferred ones if present. function propertyHasEntity( claims, itemId ) if not claims then return false; 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 return true; end end  return false; end  function propertyLabel( propertyId ) local label, labelLang = mw.wikibase.getLabelWithLang( propertyId ); label = lang:ucfirst( label ); if labelLang ~= lang:getCode() then label = '[[d:Property:' .. propertyId .. '|' .. 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 = ';ifconfigandconfig.stylesandconfig.styles.inferroroboxthenresult=result..' style="'..config.styles.error..'"';endresult=result..'>\n';result=result..'\n';result=result..'
' .. message .. '
'
; 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 = ';ifconfigandconfig.i18nandconfig.i18n.dataNamethenresult=result..' data-name="'..config.i18n.dataName..'"';endifconfigandconfig.stylesandconfig.styles.infoboxthenresult=result..' style="'..config.styles.infobox..'"';endresult=result..'>\n';locallocalImage=nil;if(frame~=nilandframe:getParent()~=nil)thenlocalp_frame=frame:getParent();ifp_frame.args~=nilthen-- image under FU only in locallocalImage=p_frame.args.image;ifp_frame.args.from~=nilandp_frame.args.from~=''thenentityId=p_frame.args.from;elseifp_frame.args[1]~=nilandstring.gmatch(p_frame.args[1],'^Q\d+$')thenentityId=p_frame.args[1];endendendlocalwdStatus,entity=pcall(mw.wikibase.getEntity,entityId);ifwdStatus~=trueorentity==nilthenreturngetErrorMessage(i18n_error_noWikidataEntity);elseifentity.claims==nilthenreturngetErrorMessage(i18n_error_emptyWikidataEntity);end-- TODO: Need to consider how to display class properties (P31, P279, P361, ...).localskipPropertyIds={};ifconfig.skipPropertyIdsthenskipPropertyIds=mw.clone(config.skipPropertyIds);endlocalclaims=entity.claims;localorder=mw.wikibase.getPropertyOrder()or{};-- Header.localentityLabel,entityLabelLang=entity:getLabelWithLang(lang:getCode());locallabel;---- Name.localtitleTemplate=getTemplate('title');-- TODO: Make it possible to specify a template for any value, not just Q5.ifpropertyHasEntity(claims.P31,'Q5')thenlocaltitleTemplateQ5=getTemplate('title_Q5');iftitleTemplateQ5thentitleTemplate=titleTemplateQ5;endendifentityLabelLang==lang:getCode()thenlabel=expandTemplate(frame,titleTemplate,{wdLabel,from=entityId});elselabel=expandTemplate(frame,titleTemplate,{from=entityId});endresult=result..getLine(label,styles.title,classes.title);---- Original name.ifclaims.P1559~=nilthenresult=result..getLine(expandTemplate(frame,getTemplate('P1559'),{from=entityId}),styles.original);elseifclaims.P1705~=nilthenresult=result..getLine(expandTemplate(frame,getTemplate('P1705'),{from=entityId}),styles.original);end---- Flag and COA.ifclaims.P41orclaims.P94thenlocalflag=nil;localflagLabel=nil;localcoa=nil;localcoaLabel=nil;ifclaims.P41thenflag=renderValue(frame,'P41');ifclaims.P163thenflagLabel=renderValue(frame,'P163',{text=simpleLabel('Q14660')});elseflagLabel=simpleLabel('Q14660');endendifclaims.P94thencoa=renderValue(frame,'P94');ifclaims.P163thencoaLabel=renderValue(frame,'P237',{text=simpleLabel('Q14659')});elsecoaLabel=simpleLabel('Q14659');endendresult=result..splitLine(flagLabel,coaLabel);result=result..splitLine(flag,coa);end-- Body.localpropertyIds={};forpropertyId,claiminpairs(entity.claims)dotable.insert(propertyIds,propertyId);endlocalorderedProperties=mw.wikibase.orderProperties(propertyIds)localshownProperties=0fori,propertyIdinipairs(orderedProperties)dolocalpropertyClaims=claims[propertyId];ifnotskipPropertyIds[propertyId]andpropertyClaimsandpropertyClaims[1]andpropertyClaims[1].mainsnakandpropertyClaims[1].mainsnak.datatypeandpropertyClaims[1].mainsnak.datatype~='external-id'andpropertyClaims[1].mainsnak.datatype~='tabular-data'andpropertyClaims[1].mainsnak.datatype~='wikibase-property'thenlocallabel=propertyLabel(propertyId);ifpropertyClaims[1].mainsnak.datatype=='commonsMedia'thenresult=result..getLine(renderValue(frame,propertyId,{alt=label}),styles.media,classes.media);elseresult=result..getValue(label,renderValue(frame,propertyId));endskipPropertyIds[propertyId]=trueshownProperties=shownProperties+1endend-- Footer.---- Map.ifclaims.P625~=nilthenresult=result..getLine(renderValue(frame,'map'),styles.text);end---- Commons.ifclaims.P373~=nilthenresult=result..getLine(expandTemplate(frame,getTemplate('P373'),{from=entityId}),styles.footer,classes.footer);endresult=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 .. '[[Category:' .. config.categories['few-properties-shown'] .. '|' .. shownProperties .. ']]' end end return result; end return p;

Tags:

🔥 Trending searches on Wiki Karjalan:

1. pakkaskuudu199811. kezäkuuduBolivii23. syvyskuuduUuksujärviNelson MandelaOhotanmeriFilosoufii20. tuhukuudu20062. pakkaskuuduAntti TimonenRodii11. kevätkuuduNikaragua3. ligakuudu1980-lugu vuvvetVjentjanLatinan kieli1958Kuveit23. kevätkuuduUralilazet kieletSent-Kits da Nevis16. ligakuuduEswatini2020KazviArtugu12. tuhukuudu11. elokuudu21. pakkaskuudu15. heinykuudu171916487. elokuuduŠahmuattukižaNigeriiDakkaNikolai Rerih17. ligakuudu199510. pakkaskuuduEkvadorVladimir Putin2016Jevroupan unioni4. sulakuuduIslandiiXX vuozisada17131790Abraham LincolnCarl Friedrich GaussMruamoru17. kylmykuudu25. pakkaskuuduGondurasCerii5. tuhukuuduSygyzy20. heinykuuduArkhimedes28. sulakuuduXIX vuozisada29. tuhukuuduIndiiPragaTunisuKolmaspäivy🡆 More