Module:OSMWikiBase

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

getLabel

Usage
{{#invoke: OSMWikiBase|getLabel}}
Parameters
1 - Entity ID, e.g. Q2
2 - Language code, e.g. en, or if not given, will use the language of the current page.
Returns
if any of these properties are available, returns property value together with localized label (non-English): permanent key ID (P16), permanent tag ID (P19), relation role ID (P21). Otherwise, the label in the requested language or English.

getDescription

Usage
{{#invoke: OSMWikiBase|getDescription}}
Parameters
1 - Entity ID, e.g. Q2
2 - Language code, e.g. en, or if not given, will use the language of the current page.
Returns
the description of the given entity in the specified language if available, otherwise the description in English if available, otherwise an empty string.

formatEntity

Usage
{{#invoke:OSMWikiBase|formatEntity}}
Parameters
1 - Entity ID, e.g. Q2
2 - Language code, e.g. en, or if not given, will use the language of the current page.
3 - Anchor in the data item page, e.g. P5 (optional)
Returns
a wiki markup link to the Item or Property page, with the label with Q-code as text, and the description as the tooltip.

  1 local getArgs = require('Module:Arguments').getArgs
  2 local titleParser = require('Module:OsmPageTitleParser')
  3 local p = {}
  4 
  5 
  6 local wb = function()
  7 	if mw and mw.wikibase and mw.wikibase.getEntity then
  8 		return nil
  9 	else
 10 		return "<span class='error'>DataItems are having issues</span>"
 11 	end
 12 end
 13 
 14 -- Given an entity, get key/tag/relation string with the optional localization,
 15 -- or just get the label in the requested language
 16 local getLabel = function(entity, lang)
 17 	if entity then
 18 		if entity.claims then
 19 			for _, prop in ipairs({'P16', 'P19', 'P21'}) do
 20 				local statement = entity:getBestStatements(prop)[1]
 21 				if statement then
 22 					local result = statement.mainsnak.datavalue.value
 23 					if lang and lang ~= 'en' then
 24 						-- get nativekey/nativevalue if available
 25 						local native = entity:getLabel(lang)
 26 						if native then
 27 							result = result .. ' (' .. native .. ')'
 28 						end
 29 					end
 30 					return result
 31 				end
 32 			end
 33 		end
 34 		return (
 35 			entity:getLabel(lang) or
 36 			entity:getLabel('en') or
 37 			'(' .. entity:getId() .. ')'
 38 		)
 39 	else
 40 		return "ERROR: Invalid ID"
 41 	end
 42 end
 43 
 44 -- Given an entity, get the description in the requested language or fallback to EN
 45 local getDescription = function(entity, lang)
 46 	local res = ''
 47 	if entity.descriptions then
 48 		res = entity:getDescription(lang) or entity:getDescription('en') or ''
 49 	end
 50 	return res
 51 end
 52 
 53 -- Formats entity's label with the optional description
 54 local formatEntity = function(entity, lang, anchor)
 55 	local label = getLabel(entity, lang)
 56 	if not entity then
 57 		return label
 58 	end
 59 	local desc = getDescription(entity, lang)
 60 	local ns = entity.id:sub(1, 1) == 'Q' and 'Item:' or 'Property:'
 61 	local link = ns .. entity.id
 62 	if anchor then
 63 		link = link .. '#' .. anchor
 64 	end
 65 	local text = mw.html.create( 'span' )
 66 	text:attr('title', desc)
 67 		:wikitext(mw.text.nowiki(label) .. ' ')
 68 		:tag('small')
 69     		:wikitext('(' .. entity.id .. ')')
 70 
 71 	return '[[' .. link .. '|' .. tostring(text) .. ']]'
 72 end
 73 
 74 -- if lang is not given, tries to figure it out based on the current page title
 75 local getLang = function(lang)
 76 	if not lang then
 77 		local parsed = titleParser.parseTitle(mw.title.getCurrentTitle())
 78 		if parsed then
 79 		 	return parsed.language:getCode()
 80 	 	end
 81 	end
 82 	return lang
 83 end
 84 
 85 --------------------------------------------------------------------------------
 86 -- Public functions
 87 --------------------------------------------------------------------------------
 88 
 89 -- Debug console usage:    =p.formatEntity({'Q888'})
 90 -- with lang               =p.formatEntity({'Q888', 'ru'})
 91 function p.formatEntity(frame)
 92 	local args = getArgs(frame)
 93 	return wb() or formatEntity(mw.wikibase.getEntity(args[1]), getLang(args[2]), args[3])
 94 end
 95 
 96 -- Debug console usage:    =p.getLabel({'Q888'})
 97 -- with lang               =p.getLabel({'Q888', 'ru'})
 98 function p.getLabel(frame)
 99 	local args = getArgs(frame)
100 	return wb() or getLabel(mw.wikibase.getEntity(args[1]), getLang(args[2]))
101 end
102 
103 -- Debug console usage:    =p.getDescription({'Q888'})
104 -- with lang               =p.getDescription({'Q888', 'ru'})
105 function p.getDescription(frame)
106 	local args = getArgs(frame)
107 	return wb() or getDescription(mw.wikibase.getEntity(args[1]), getLang(args[2]))
108 end
109 
110 return p