Modul:PropertyLink: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
link to URL if qualifier has one |
||
(6 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
function | function getLinkLabel(propValue, frame) | ||
if propValue['type'] == 'wikibase-entityid' then | |||
local isWikipedia = false | |||
local linkTarget = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] ) | local linkTarget = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] ) | ||
if not linkTarget then | if not linkTarget then | ||
Zeile 13: | Zeile 7: | ||
if targetEntity and targetEntity['sitelinks'] then | if targetEntity and targetEntity['sitelinks'] then | ||
if targetEntity['sitelinks']['dewiki'] then | if targetEntity['sitelinks']['dewiki'] then | ||
linkTarget = ' | linkTarget = string.gsub('de:' .. targetEntity['sitelinks']['dewiki']['title'], ' ', '_') | ||
isWikipedia = true | |||
elseif targetEntity['sitelinks']['enwiki'] then | elseif targetEntity['sitelinks']['enwiki'] then | ||
linkTarget = | linkTarget = string.gsub(targetEntity['sitelinks']['enwiki']['title'], ' ', '_') | ||
isWikipedia = true | |||
end | end | ||
end | end | ||
end | |||
local linkTitle = mw.wikibase.label( "Q" ..propValue.value['numeric-id'] ) | local linkTitle = mw.wikibase.label( "Q" ..propValue.value['numeric-id'] ) | ||
return linkTarget | |||
if isWikipedia then | |||
elseif propValue and propValue['type'] == 'string' then return propValue.value end | template = "{{WikipediaLink|%s|%s}}" | ||
end | |||
if linkTarget and linkTitle then | |||
if isWikipedia then | |||
return frame:expandTemplate{ title = 'WikipediaLink', args = { linkTarget, linkTitle } } | |||
end | |||
return mw.ustring.format( "[[%s|%s]]", linkTarget, linkTitle ) | |||
else | |||
return linkTitle | |||
end | |||
elseif propValue and propValue['type'] == 'string' then | |||
return propValue.value | |||
end | |||
end | |||
function getProperty(propertyName, frame) | |||
local entity = mw.wikibase.getEntityObject() | |||
if not entity or not entity.claims then return end--the entity doesnt exist or have no claims | |||
local property = entity.claims[propertyName] | |||
if not property then return end--no such property for this item | |||
property = property[1] | |||
local propValue = property.mainsnak and property.mainsnak.datavalue | |||
if not propValue then return end --property doesnt exist | |||
local label = getLinkLabel(propValue, frame) | |||
if not label == propValue then | |||
return label | |||
end | |||
if property.qualifiers and property.qualifiers["P27"] then | |||
return mw.ustring.format( "[%s %s]", property.qualifiers["P27"][1].datavalue.value, label ) | |||
end | |||
return label | |||
end | end | ||
function property( frame ) | function property( frame ) | ||
return getProperty(string.upper(frame.args[1])) | return getProperty(string.upper(frame.args[1], frame), frame) | ||
end | end | ||
Zeile 46: | Zeile 75: | ||
end | end | ||
function | function getLinkLabels( frame ) | ||
local entity = mw.wikibase. | propertyName = frame.args[1] | ||
local entity = mw.wikibase.getEntityObject() | |||
if not entity or not entity.claims then return end--the entity doesnt exist or have no claims | if not entity or not entity.claims then return end--the entity doesnt exist or have no claims | ||
local properties = entity.claims[propertyName] | local properties = entity.claims[propertyName] | ||
Zeile 53: | Zeile 83: | ||
local retVal = '' | local retVal = '' | ||
for | for propIndex in pairs(properties) do | ||
local property = properties[propIndex] | |||
local propValue = property.mainsnak.datavalue | local propValue = property.mainsnak.datavalue | ||
if not propValue then return '' end --property doesnt exist | if not propValue then return '' end --property doesnt exist | ||
local | local value = getLinkLabel(propValue, frame) | ||
if retVal == '' then | if retVal == '' then | ||
retVal = value | |||
else | else | ||
retVal = retVal .. ', ' .. value | |||
end | end | ||
end | end | ||
Zeile 76: | Zeile 101: | ||
-- Return the label for property, or the label of the linked entiy of that property | -- Return the label for property, or the label of the linked entiy of that property | ||
function label( frame ) | function label( frame ) | ||
return getLabel( string.lower(frame.args[1] )) | return getLabel(string.lower(frame.args[1])) | ||
end | end | ||
Zeile 105: | Zeile 130: | ||
getImageLink = getImageLink, | getImageLink = getImageLink, | ||
getLabel = getLabel, | getLabel = getLabel, | ||
getLinkLabels = getLinkLabels, | |||
} | } |
Aktuelle Version vom 25. November 2019, 15:35 Uhr
This module can be used for advance linking, according to Wikidata. It includes the following functions:
- property - get a link according to wikidata claim
- label: gives the label in wikidata (no [[link]])
- Parameters: property (e.g p123)
- imageLink: Get a related file to be used in the article
- Parameters:
- 1 param (optional) - Property to get commons link. If not specified it uses d:Property:p18 (a generic property for image in wikidata)
- width - default 220px
- Parameters:
Skriptfehler: Interner Lua-Fehler: Der Interpreter beendet sich mit dem Status 127.
function getLinkLabel(propValue, frame)
if propValue['type'] == 'wikibase-entityid' then
local isWikipedia = false
local linkTarget = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] )
if not linkTarget then
local targetEntity = mw.wikibase.getEntity( "Q" .. propValue.value['numeric-id'] )
if targetEntity and targetEntity['sitelinks'] then
if targetEntity['sitelinks']['dewiki'] then
linkTarget = string.gsub('de:' .. targetEntity['sitelinks']['dewiki']['title'], ' ', '_')
isWikipedia = true
elseif targetEntity['sitelinks']['enwiki'] then
linkTarget = string.gsub(targetEntity['sitelinks']['enwiki']['title'], ' ', '_')
isWikipedia = true
end
end
end
local linkTitle = mw.wikibase.label( "Q" ..propValue.value['numeric-id'] )
if isWikipedia then
template = "{{WikipediaLink|%s|%s}}"
end
if linkTarget and linkTitle then
if isWikipedia then
return frame:expandTemplate{ title = 'WikipediaLink', args = { linkTarget, linkTitle } }
end
return mw.ustring.format( "[[%s|%s]]", linkTarget, linkTitle )
else
return linkTitle
end
elseif propValue and propValue['type'] == 'string' then
return propValue.value
end
end
function getProperty(propertyName, frame)
local entity = mw.wikibase.getEntityObject()
if not entity or not entity.claims then return end--the entity doesnt exist or have no claims
local property = entity.claims[propertyName]
if not property then return end--no such property for this item
property = property[1]
local propValue = property.mainsnak and property.mainsnak.datavalue
if not propValue then return end --property doesnt exist
local label = getLinkLabel(propValue, frame)
if not label == propValue then
return label
end
if property.qualifiers and property.qualifiers["P27"] then
return mw.ustring.format( "[%s %s]", property.qualifiers["P27"][1].datavalue.value, label )
end
return label
end
function property( frame )
return getProperty(string.upper(frame.args[1], frame), frame)
end
function getLabel( propertyName )
local entity = mw.wikibase.getEntity()
if not entity or not entity.claims then return end--the entity doesnt exist or have no claims
local property = entity.claims[propertyName]
if not property then return end--no such property for this item
property = property[0]
local propValue = property.mainsnak.datavalue
if not propValue then return '' end --property doesnt exist
if propValue['type']=='wikibase-entityid' then
return mw.wikibase.label( "Q" ..propValue.value['numeric-id'] )
elseif propValue['type'] == 'string' then
return propValue.value
end
end
function getLinkLabels( frame )
propertyName = frame.args[1]
local entity = mw.wikibase.getEntityObject()
if not entity or not entity.claims then return end--the entity doesnt exist or have no claims
local properties = entity.claims[propertyName]
if not properties then return end--no such property for this item
local retVal = ''
for propIndex in pairs(properties) do
local property = properties[propIndex]
local propValue = property.mainsnak.datavalue
if not propValue then return '' end --property doesnt exist
local value = getLinkLabel(propValue, frame)
if retVal == '' then
retVal = value
else
retVal = retVal .. ', ' .. value
end
end
return retVal
end
-- Return the label for property, or the label of the linked entiy of that property
function label( frame )
return getLabel(string.lower(frame.args[1]))
end
function getImageLink( propName, width)
local entity = mw.wikibase.getEntity()
if not entity or not entity.claims then return end --the entity doesnt exist or have no claims
local property = entity.claims[propName or "P4"]
if property then
local width = width or "220"
return mw.ustring.format( '[[File:%s|%spx]]', property[1].mainsnak.datavalue.value, width )
end
end
--use this function to get associated image to be used in the article
function imageLink( frame )
return getImageLink( string.upper(frame.args[1]), frame.args["width"])
end
return {
imageLink = imageLink,
Image = imageLink,
File = imageLink,
label = label,
Label = label,
property = property,
Property = property,
getProperty = getProperty,
getImageLink = getImageLink,
getLabel = getLabel,
getLinkLabels = getLinkLabels,
}