יחידה:PropertyLink
מראה
יחידה זו משמשת כדי לקשר בוויקיפדיה לפי ויקינתונים. היחידה מיועדת לשימוש בעיקר בתבניות וכוללת את הפונקציות הבאות:
- property או מאפיין - לקבלת קישור לפי "קביעה" מוויקינתונים
- label או תווית: דומה לפונקציה הקודמת, רק שלא מחזיר קישור אלא ללא קישור - מחזיר את התווית (label) וללא תלות בקישור כלשהו שקיים או לא קיים בוויקיפדיה בעברית
- פרמטרים: שם המאפיין (property) המתאים (בצורה של P123)
- imageLink או תמונה: פונקציה שמחזירה תמונה שמתאימה לשילוב בתבנית מידע בערך.
- פרמטרים:
- פרמטר ראשון - (אופציונלי) אם לא מקבל פרמטר בכלל משתמש ב־d:Property:p18 (מאפיין כללי של תמונה), אחרת משתמש בפרמטר המאפיין המכיל את התמונה
- width - גודל התמונה (אחרת 220px)
- פרמטרים:
- hasEntry או יש פריט: פונקציה שמחזירה "1" אם לערך יש פריט ויקינתונים; או "" אם אין.
- פרמטרים: אין
הערות נוספות:
- ייתכן שבעתיד תהיה תמיכה מובנית לפחות בחלק מהאפשרויות הנ"ל ראו בmeta:Wikidata/Notes/Inclusion_syntax#Items
--[[
Fetch the "as of date" property of a associated with a property
]]
function asOfDateQuanifier(property)
local AS_OF_PROPERTY = 'P585'
local as_of_value = (property.qualifiers and property.qualifiers[AS_OF_PROPERTY] and property.qualifiers[AS_OF_PROPERTY].value and property.qualifiers[AS_OF_PROPERTY].value.time)
if as_of_value then
-- hack to extract the year associated with "as of date". be careful - it doesn't take to account the precision...
local m = mw.ustring.match(as_of_value, '([+-][0-9]+)-')
if m then
return ' (נכון ל־'.. tostring(tonumber(m)) ..')'
end
end
return ''
end
--[[
Fetch property from wikidata:
* if the entity or the claim doesn't exist - nil
* if the property exist:
- for entity reference - returns link to entity (using sitelink) with label as text, otherwise wikidata label as text
- for string - returns the string
- for quantity - returns the amount
- for time - returns the time as string
- for image - returns image inclusion with 250px size
]]
function getProperty( propertyName )
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 isImage = (property.mainsnak.datatype=='commonsMedia')
if propValue['type'] == 'wikibase-entityid' then
local linkTarget = mw.wikibase.sitelink( "Q" .. propValue.value['numeric-id'] )
local linkTitle = mw.wikibase.label( "Q" .. propValue.value['numeric-id'] )
return linkTarget and linkTitle and mw.ustring.format( "[[%s|%s]]", linkTarget, linkTitle )
or linkTitle
elseif propValue['type'] == 'string' then
if isImage then
return mw.ustring.format( '[[File:%s|250px]]', propValue.value )
end
return propValue.value
elseif propValue['type'] == 'quantity' then
local lang = mw.getContentLanguage()
local asOfDate = asOfDateQuanifier(property)
return lang:formatNum( tonumber(propValue.value.amount) ) .. asOfDate
elseif propValue['type'] == 'time' then
return entity:formatPropertyValues( propertyName ).value
end
end
function property( frame )
return getProperty(string.upper(frame.args[1]))
end
function getLabel( propertyName )
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.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
-- Return the label for property, or the label of the linked entiy of that property
function label( frame )
return getLabel( string.upper(frame.args[1] ))
end
function getImageLink( propName, width)
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[propName or "P18"]
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
-- returns "1" if the page has an associated wikidata entry, "" otherwise
function hasEntry()
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 then return end --the entity doesnt exist or have no claims
return 1
end
return {
imageLink = imageLink,
['תמונה'] = imageLink,
label = label,
['תווית'] = label,
property = property,
['מאפיין'] = property,
getProperty = getProperty,
getImageLink = getImageLink,
getLabel = getLabel,
hasEntry = hasEntry,
['יש פריט'] = hasEntry
}