אונטערשייד צווישן ווערסיעס פון "יחידה:PropertyLink"

ק (1 רעוויזיע אימפארטירט פון wikipedia:he:יחידה:PropertyLink: אימפארטירט פון די העברעאישע וויקיפעדיע. זע דארט ביישטייערער ליסטע)
(אפדעיט)
 
(4 צווישנדיגע ווערסיעס פונעם זעלבן באַניצער נישט געוויזן)
שורה 1: שורה 1:
local FEMALE_FORM_PROPERTY = 'P2521'
local FEMALE_FORM_PROPERTY = 'P2521'
local Date = require('Module:תאריך')
local Date = require('Module:דאטום')
local wikidataModule = require('Module:Wikidata')
local WDS = require('Module:WikidataSelectors')


--[[  
--[[  
שורה 7: שורה 9:
function asOfDateQualifier(claim)
function asOfDateQualifier(claim)
local AS_OF_PROPERTY = 'P585'
local AS_OF_PROPERTY = 'P585'
local wikidataModule = require('Module:Wikidata')
local value
local value
local error
local error
שורה 35: שורה 36:
local langElementId = 'Q9288' -- hebrew
local langElementId = 'Q9288' -- hebrew
local labelFilter = 'P5061[language:'..langCode..'], P5061[P282:' .. writingSystemElementId .. ', P407:' .. langElementId .. ']'
local labelFilter = 'P5061[language:'..langCode..'], P5061[P282:' .. writingSystemElementId .. ', P407:' .. langElementId .. ']'
local WDS = require( 'Module:WikidataSelectors' )
local labelClaims = WDS.filter( {['P5061']=unitSymbol }, labelFilter )
local labelClaims = WDS.filter( {['P5061']=unitSymbol }, labelFilter )
for r, claim in pairs( labelClaims ) do
for r, claim in pairs( labelClaims ) do
if claim.mainsnak and claim.mainsnak.datavalue.type == 'monolingualtext' and claim.mainsnak.datavalue.value.text then
if claim.mainsnak and claim.mainsnak.datavalue.type == 'monolingualtext' and claim.mainsnak.datavalue.value.text then
unit = claim.mainsnak.datavalue.value.text
unit = claim.mainsnak.datavalue.value.text
break;
break
elseif claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.type == 'string' and claim.mainsnak.datavalue.value ~= '' then
elseif claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.type == 'string' and claim.mainsnak.datavalue.value ~= '' then
unit = claim.mainsnak.datavalue.value
unit = claim.mainsnak.datavalue.value
break;
break
end
end
end
end
שורה 55: שורה 55:


function getPropertyQualifier(property, qualifier)
function getPropertyQualifier(property, qualifier)
local wikidataModule = require('Module:Wikidata')
local entity = mw.wikibase.getEntityIdForCurrentPage()
local entity = mw.wikibase.getEntityIdForCurrentPage()
     if not entity 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
שורה 66: שורה 65:


local function missingLabelCategory(propertyName)
local function missingLabelCategory(propertyName)
return '[[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( propertyName) or propertyName) .. ']][[קטגוריה:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
return '[[קאַטעגאָריע:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( propertyName) or propertyName) .. ']][[קאַטעגאָריע:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
end
end


local function getFemaleLabelForEntityId( entityId )
local function getFemaleLabelForEntityId( entityId )
local WDS = require('Module:WikidataSelectors')
local femaleForm = mw.wikibase.getBestStatements(entityId, FEMALE_FORM_PROPERTY)
local femaleForm = mw.wikibase.getBestStatements(entityId, FEMALE_FORM_PROPERTY)
שורה 97: שורה 95:
@param {string} entityId - id of entity to link to (e.g Q42)
@param {string} entityId - id of entity to link to (e.g Q42)
@param {string} genderAwareEntity - entity from which to fetch gender for gender form
@param {string} genderAwareEntity - entity from which to fetch gender for gender form
@param {bool} noLinktoWikidata - if true, returns without the link to Wikidata
@returns {string, bool} formattedValue, missingTranslation - the formatted value along with indicator of missing translation
@returns {string, bool} formattedValue, missingTranslation - the formatted value along with indicator of missing translation
]]
]]
function formatEntity(entityId, genderAwareEntity)
function formatEntity(entityId, genderAwareEntity, noLinktoWikidata)
local linkTarget = mw.wikibase.sitelink( entityId )
local linkTarget = mw.wikibase.sitelink( entityId )
     local localLabel, langLabel, missingFemaleForm
     local localLabel, langLabel, missingFemaleForm
שורה 112: שורה 111:
                     gender[1].mainsnak.datavalue and
                     gender[1].mainsnak.datavalue and
                     gender[1].mainsnak.datavalue.value and
                     gender[1].mainsnak.datavalue.value and
                     (gender[1].mainsnak.datavalue.value["numeric-id"] == 6581072 or gender[1].mainsnak.datavalue.value["numeric-id"] == 1052281)
                     (gender[1].mainsnak.datavalue.value["numeric-id"] == 6581072
                  or gender[1].mainsnak.datavalue.value["numeric-id"] == 1052281
                  or gender[1].mainsnak.datavalue.value["numeric-id"] == 15145779)


     if isFemale then
     if isFemale then
שורה 150: שורה 151:
     formattedValue = mw.ustring.format('<span lang="en" dir="ltr">%s</span>', localLabel)
     formattedValue = mw.ustring.format('<span lang="en" dir="ltr">%s</span>', localLabel)
     else
     else
formattedValue = mw.ustring.format( "[[:d:%s|%s]]", entityId, entityId )
    if noLinktoWikidata then
    formattedValue = 'בפסקה זו רשומה אחת נוספת שטרם תורגמה'
else
formattedValue = mw.ustring.format( "[[:d:%s|%s]]", entityId, entityId )
end
end
end
     end
     end
שורה 156: שורה 161:
end
end


function formatOptionalQualifiers(property, qualifiers, qualifiersSep)
function formatOptionalQualifiers(property, qualifiers, qualifiersSep, defaultValue, addParenthesis)
if (qualifiers==nil) or (#qualifiers == 0) or (not property.qualifiers) then return '' end
if (qualifiers==nil) or (#qualifiers == 0) or (not property.qualifiers) then return '' end
qualifiersSep = qualifiersSep or ', '
qualifiersSep = qualifiersSep or ', '
שורה 162: שורה 167:
local formmatedQualifiers = {}
local formmatedQualifiers = {}
local warnings = ''
local warnings = ''
local defaultIsOn = true
for _, optionalQualifier in pairs( qualifiers ) do
for _, optionalQualifier in pairs( qualifiers ) do
-- handling case of "first or default" qualifier (e.g.: for 'P1/P2' take P1 if exists, otherwise - P2)
-- handling case of "first or default" qualifier (e.g.: for 'P1/P2' take P1 if exists, otherwise - P2)
local was_found = false
for selectableQualifier in string.gmatch(optionalQualifier, '[^/]+') do
for selectableQualifier in string.gmatch(optionalQualifier, '[^/]+') do
if selectableQualifier and property.qualifiers[selectableQualifier] then
if selectableQualifier and property.qualifiers[selectableQualifier] then
שורה 177: שורה 185:
local formattedValue = '<span title="'.. mw.wikibase.label(selectableQualifier)..'" class="propertylink-wikidata-qualifier">'..mw.wikibase.formatValues(property.qualifiers[selectableQualifier]) ..'</span>'
local formattedValue = '<span title="'.. mw.wikibase.label(selectableQualifier)..'" class="propertylink-wikidata-qualifier">'..mw.wikibase.formatValues(property.qualifiers[selectableQualifier]) ..'</span>'
table.insert(formmatedQualifiers, formattedValue)
table.insert(formmatedQualifiers, formattedValue)
was_found = true
defaultIsOn = false
break -- found first available value
break -- found first available value
end
end
elseif string.sub(selectableQualifier, 1, 1) ~= 'P' then
elseif string.sub(selectableQualifier, 1, 1) ~= 'P' then
table.insert(formmatedQualifiers, selectableQualifier)
table.insert(formmatedQualifiers, selectableQualifier)
was_found = true
defaultIsOn = false
break -- found first available value
break -- found first available value
end
end
end
if not was_found then
table.insert(formmatedQualifiers, defaultValue)
end
end
local res = ""
if not defaultIsOn then
res = table.concat( formmatedQualifiers, qualifiersSep)
if #formmatedQualifiers > 0 and addParenthesis then
res = mw.ustring.format('<span style="font-size:0.9em"> (%s)</span>', res)
end
end
end
end
local res =  table.concat( formmatedQualifiers, qualifiersSep)
if #formmatedQualifiers > 0 then
res = mw.ustring.format('<span style="font-size:0.9em;"> (%s)</span>', res)
end
return res .. warnings
return res .. warnings
end
end
שורה 231: שורה 249:


function property( frame )
function property( frame )
    if frame.args['entity']==nil and frame.args['title'] and #frame.args['title']>0  then
    return ''
        frame.args['entity'] = mw.wikibase.getEntityIdForTitle( frame.args['title'] )
        if frame.args['entity']==nil then return end
    end
    return getProperty(string.upper(frame.args[1]), (frame.args[2] and string.len(frame.args[2])>0) or false, true, frame.args['entity'], frame.args['sep'], frame.args['q'])
end
end


שורה 291: שורה 305:
options['source'] = options['source'] or false
options['source'] = options['source'] or false
options['filter'] = options['filter'] or nil
options['filter'] = options['filter'] or nil
options['sort'] = options['sort'] or { 'P1545' }
    return --no such property for this item
options['default-value'] = options['default-value'] or nil
 
    return --no such property for this item
end
end


שורה 337: שורה 353:


function getImageLink( propName, width, align, description, border, entityId)
function getImageLink( propName, width, align, description, border, entityId)
    return --the entity doesnt exist or have no claims
    return --the entity doesnt exist or have no claims
end
end
      
      
שורה 359: שורה 375:


function getSitelinksFromQid(frame)
function getSitelinksFromQid(frame)
resTable =mw.wikibase.getEntity(string.upper(frame.args[1] ))
resTable = mw.wikibase.getEntity(string.upper(frame.args[1] ))
resTable = resTable["sitelinks"]
resTable = resTable["sitelinks"]
s= "<table>\n"
s= "<table>\n"
שורה 366: שורה 382:
s=s.."<tr>".. "<td>" .. k.."</td>".. "<td>" .. v.title.."</td>".."</tr>" .. "\n"
s=s.."<tr>".. "<td>" .. k.."</td>".. "<td>" .. v.title.."</td>".."</tr>" .. "\n"
end  
end  
end
return s .. "</table>"
end
local function createInterwikiForQid(frame)
local raw = mw.wikibase.getEntity( string.upper( frame.args[1] ) )["sitelinks"]
local interwikis = ''
for site, val in pairs(raw) do
truncated, found = string.gsub(site, 'wiki$', '')
truncated = string.gsub(truncated, '_', '-')
if found == 1 and truncated ~= 'he' and truncated ~= 'commons' then interwikis = interwikis .. '[[' .. truncated .. ':' .. val.title .. ']]' end
end
return interwikis
end
end
return s .. "</table>"


end
 
return {
return {
     imageLink = imageLink,
     imageLink = imageLink,
שורה 385: שורה 412:
     getPropertyByOptions = getPropertyByOptions,
     getPropertyByOptions = getPropertyByOptions,
     getPropertyQualifier = getPropertyQualifier,
     getPropertyQualifier = getPropertyQualifier,
    getItem = getItem,
     getImageLink = getImageLink,
     getImageLink = getImageLink,
     getLabel = getLabel,
     getLabel = getLabel,
     hasEntry = hasEntry,
     hasEntry = hasEntry,
     getPageEntry = getPageEntry,
     getPageEntry = getPageEntry,
     getSitelinksFromQid=getSitelinksFromQid,
     getSitelinksFromQid = getSitelinksFromQid,
    createInterwikiForQid = createInterwikiForQid,
     ['יש פריט'] = hasEntry
     ['יש פריט'] = hasEntry
}
}

יעצטיגע רעוויזיע זינט 02:42, 24 סעפטעמבער 2023

דעם דאזיקן מאדול קען מען ניצן פאר פאראויסלינקען, לויט וויקידאטן. ער שליסט איין די פאלגנדע פונקציעס:

  • אייגנשאפט אדער property - באקומען א לינק לויט א וויקידאטן טענה
    • פאראמעטערס: נאמען פון אייגנשאפט (property) המתאים (בצורה של P123). למשל בערך דמויי כלב:
      • {{#invoke:PropertyLink|property|p171}} - יחזיר "[[טורפים (סדרת יונקים)|טורפים]]" (טורפים), להבדיל מ-[[{{#property:p70}}]], שיחזיר "[[טורפים]]" (טורפים), שהוא דף פירושונים.
    • Parameters: property (e.g p123)
    • Example: in Lion {{#invoke:PropertyLink|property|p171}} - will give Panthera. It is better to use this function and not [[{{#property:p171}}]] as the property gives the label in Wikidata, while this function will use the correct sitelink.
  • 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

local FEMALE_FORM_PROPERTY = 'P2521'
local Date = require('Module:דאטום')
local wikidataModule = require('Module:Wikidata')
local WDS = require('Module:WikidataSelectors')

--[[ 
Fetch the "as of date" property of a associated with a property
]]
function asOfDateQualifier(claim)
	local AS_OF_PROPERTY = 'P585'
	local value
	local error
	value, error = wikidataModule.getValueOfClaim(claim, AS_OF_PROPERTY, nil)
	if value then
		return ' (נכון ל־'..value ..')'
	end
	return ''
end

local function formatQuantity(property) 
	local propValue = property.mainsnak and property.mainsnak.datavalue
	local lang = mw.getContentLanguage()
	local langCode = lang:getCode()
	local asOfDate = asOfDateQualifier(property)
	local localAmount = lang:formatNum( tonumber(propValue.value.amount) )
	local unit = ''
	if propValue.value.unit and string.match( propValue.value.unit, 'http://www.wikidata.org/entity/' ) then
		local unitEntityId = string.gsub( propValue.value.unit, 'http://www.wikidata.org/entity/', '' )
		if unitEntityId and #unitEntityId>0 then
			-- name from label
			unit =  mw.wikibase.label( unitEntityId ) or ''
			local unitSymbol = mw.wikibase.getBestStatements(unitEntityId, 'P5061')
			--  name from properties
			if unitSymbol then
				local writingSystemElementId = 'Q33513' -- hebrew writing system
				local langElementId = 'Q9288' -- hebrew
				local labelFilter = 'P5061[language:'..langCode..'], P5061[P282:' .. writingSystemElementId .. ', P407:' .. langElementId .. ']'
				local labelClaims = WDS.filter( {['P5061']=unitSymbol }, labelFilter )
				for r, claim in pairs( labelClaims ) do
					if claim.mainsnak and claim.mainsnak.datavalue.type == 'monolingualtext' and claim.mainsnak.datavalue.value.text then
						unit = claim.mainsnak.datavalue.value.text
						break
					elseif claim.mainsnak and claim.mainsnak.datavalue and claim.mainsnak.datavalue.type == 'string' and claim.mainsnak.datavalue.value ~= '' then
						unit = claim.mainsnak.datavalue.value
						break
					end
				end
			end
		
			unit = ' ' .. unit
		end
    end	
	return localAmount .. unit .. asOfDate
end

function getPropertyQualifier(property, qualifier)
	local entity = mw.wikibase.getEntityIdForCurrentPage()
    if not entity then return end --the entity doesnt exist or have no claims
	
    local propertyVals = mw.wikibase.getAllStatements( entity, property )
    if not propertyVals or not propertyVals[1] then return end --no such property for this item
 	value, error = wikidataModule.getValueOfClaim(propertyVals[1], qualifier, nil)
	return value
end

local function missingLabelCategory(propertyName)
	return '[[קאַטעגאָריע:ויקינתונים:ערכים_חסרי_תווית_בעברית: ' .. (mw.wikibase.label( propertyName) or propertyName) .. ']][[קאַטעגאָריע:ויקינתונים:ערכים_חסרי_תווית_בעברית]]'
end

local function getFemaleLabelForEntityId( entityId )
	local femaleForm = mw.wikibase.getBestStatements(entityId, FEMALE_FORM_PROPERTY)
	
	if not femaleForm then return nil, false end
	
	local femLabels = WDS.filterByLanguage( femaleForm, 'he' )
	if femLabels ~= nil and
	   femLabels[1] and
	   femLabels[1].mainsnak and
	   femLabels[1].mainsnak.datavalue and
	   femLabels[1].mainsnak.datavalue.value and
	   femLabels[1].mainsnak.datavalue.value.text then
		return femLabels[1].mainsnak.datavalue.value.text
	end

	return nil, #femaleForm > 0
end

--[[
Get a link to specific entity. E.g sitelink|label.
* Missing label => use sitelink as fallback
* Missing sitelink => just show label
* Mssing gender form: adds category of missing gender label


@param {string} entityId - id of entity to link to (e.g Q42)
@param {string} genderAwareEntity - entity from which to fetch gender for gender form
@param {bool} noLinktoWikidata - if true, returns without the link to Wikidata
@returns {string, bool} formattedValue, missingTranslation - the formatted value along with indicator of missing translation
]]
function formatEntity(entityId, genderAwareEntity, noLinktoWikidata)
	local linkTarget = mw.wikibase.sitelink( entityId )
    local localLabel, langLabel, missingFemaleForm
    local formattedValue
    local missingTranslation = false
    local fallbackLanguage = false
    if genderAwareEntity then
    	local gender = mw.wikibase.getBestStatements(genderAwareEntity, 'P21') -- P21 - gender
    	local isFemale = gender and
    	                 gender[1] and
    	                 gender[1].mainsnak and
    	                 gender[1].mainsnak.datavalue and
    	                 gender[1].mainsnak.datavalue.value and
    	                 (gender[1].mainsnak.datavalue.value["numeric-id"] == 6581072
    	               or gender[1].mainsnak.datavalue.value["numeric-id"] == 1052281
    	               or gender[1].mainsnak.datavalue.value["numeric-id"] == 15145779)

    	if isFemale then
    		localLabel, missingFemaleForm = getFemaleLabelForEntityId( entityId )
    		langLabel = 'he'
    	end
    	-- if we dont have/need female form label,  use regular label
    	if localLabel == nil then
    		localLabel, langLabel = mw.wikibase.getLabelWithLang( entityId )
    	end
    else
    	-- use Hebrew label
    	localLabel, langLabel = mw.wikibase.getLabelWithLang( entityId )
    end
    
    local isLocalLabel = langLabel=='he'
    -- fallback if there is target but no label, use target as label
    if linkTarget and not isLocalLabel then
    	localLabel = linkTarget
    	isLocalLabel = true
    end
    if localLabel and isLocalLabel then
			if linkTarget and localLabel and linkTarget == localLabel then
				formattedValue = mw.ustring.format( "[[%s]]", linkTarget )
			else
    			formattedValue = linkTarget and localLabel and mw.ustring.format( "[[%s|%s]]", linkTarget, localLabel ) or localLabel
    		end
    		if missingFemaleForm then
    			mw.log(mw.ustring.format('חסרה צורת הנקבה ל"%s" (%s)', formattedValue, entityId))
    			formattedValue = formattedValue .. missingLabelCategory( FEMALE_FORM_PROPERTY )
    		end
    else
		missingTranslation = true
		-- in that case we would unfourtanly can show only the entity id which is meaningless for users
		 if langLabel=='en' then
		 	fallbackLanguage = true
    		formattedValue = mw.ustring.format('<span lang="en" dir="ltr">%s</span>', localLabel)
    	else
    		if noLinktoWikidata then
    			formattedValue = 'בפסקה זו רשומה אחת נוספת שטרם תורגמה'
			else
				formattedValue = mw.ustring.format( "[[:d:%s|%s]]", entityId, entityId )
			end
		end
    end
    return formattedValue, missingTranslation, fallbackLanguage
end

function formatOptionalQualifiers(property, qualifiers, qualifiersSep, defaultValue, addParenthesis)
	if (qualifiers==nil) or (#qualifiers == 0) or (not property.qualifiers) then return '' end
	qualifiersSep = qualifiersSep or ', '
	
	local formmatedQualifiers = {}
	local warnings = ''
	local defaultIsOn = true
	
	for _, optionalQualifier in pairs( qualifiers ) do
		-- handling case of "first or default" qualifier (e.g.: for 'P1/P2' take P1 if exists, otherwise - P2)
		local was_found = false
		for selectableQualifier in string.gmatch(optionalQualifier, '[^/]+') do
			if selectableQualifier and property.qualifiers[selectableQualifier] then
				local optionalQualifierVal = mw.wikibase.formatValues(property.qualifiers[selectableQualifier])
				local isQualLocalLabel = true
				if optionalQualifierVal and property.qualifiers[selectableQualifier][1] and property.qualifiers[selectableQualifier][1]['datatype']=='wikibase-item' then 
					for i, optionalQValues in ipairs(property.qualifiers[selectableQualifier]) do
						local qualLabel, qualLang = mw.wikibase.getLabelWithLang( optionalQValues['datavalue']['value']['id'] )
						isQualLocalLabel = isQualLocalLabel and (qualLang=='he')
					end
				end
				if optionalQualifierVal then
					local formattedValue = '<span title="'.. mw.wikibase.label(selectableQualifier)..'" class="propertylink-wikidata-qualifier">'..mw.wikibase.formatValues(property.qualifiers[selectableQualifier]) ..'</span>'
					table.insert(formmatedQualifiers, formattedValue)
					was_found = true
					defaultIsOn = false
					break -- found first available value
				end
			elseif string.sub(selectableQualifier, 1, 1) ~= 'P' then
				table.insert(formmatedQualifiers, selectableQualifier)
				was_found = true
				defaultIsOn = false
				break -- found first available value
			end
		end
		if not was_found then
			table.insert(formmatedQualifiers, defaultValue)
		end
	end
	local res = ""
	if not defaultIsOn then
		res = table.concat( formmatedQualifiers, qualifiersSep)
		if #formmatedQualifiers > 0 and addParenthesis then
			res = mw.ustring.format('<span style="font-size:0.9em"> (%s)</span>', res)
		end
	end
	return res .. warnings
end

--[[ 
Fetch property from wikidata and format it:
* if the entity or the claim doesn't exist - nil
* Formating rules:
	- 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
* Multivalues:
	Multivalue is supported with allowMulti. seperator between values defined by multiSeperator
	
@param {string} propertyName - name of property (e.g P123)
@param {bool} allowMulti - whether only first statement should be fetched are all statements.
@param {bool} allowNA - whether if is valid to fetch somevalue snaks: Default: nil/false
@param {string} entityId - Qid of the entity for which we fetch the data. Default: nil (page entity)
@param {string} multiSeperator - seperator between multiple values. Default: ,
@param {string} optionalQualifier - Property id of optional extra qualifier (e.g P123). will be formatted as propertyName (optionalQualifier)
@param {bool} genderAware - Fetch gender form instead of regular label
]]
function getProperty( propertyName, allowMulti, allowNA, entityId, multiSeperator, optionalQualifier, genderAware )
	if propertyName==nil or #propertyName==0 then return end -- no property specified
	entityId = entityId
	if entityId == nil then return end -- entity doesnt exist
	options = {
		['allowMulti'] = allowMulti,
		['allowNA'] = allowNA,
		['seperator'] = multiSeperator or ', ',
		['qualifier'] = optionalQualifier,
		['entity-gender-aware'] = genderAware
	}
	if optionalQualifier then
		options['qualifiers'] = {optionalQualifier}
	end
	
	return getPropertyByOptions(propertyName, entityId, options)
end

function property( frame )
     return ''
end

function propertyWithGender(frame)
	return getProperty(string.upper(frame.args[1]), (frame.args[2] and string.len(frame.args[2])>0) or false, true, frame.args['entity'], ', ', '', true)
end

-- formatted reference for statement. Only for non imported statements
function refStatement(statement)
	if not statement.references or #statement.references == 0 then return '' end -- no reference
	local formattedReferences = {}
	local frame = mw.getCurrentFrame()
	for j, curRef in ipairs(statement.references) do
		local isImportedRef = curRef.snaks and (curRef.snaks['P143'] or curRef.snaks['P4656'])
		if not isImportedRef then
			if curRef.snaks['P854'] then
				table.insert(formattedReferences, frame:extensionTag{  name = 'ref', content = mw.wikibase.renderSnak(curRef.snaks['P854'][1])})
			elseif curRef.snaks['P2699'] then
				table.insert(formattedReferences, frame:extensionTag{  name = 'ref', content = mw.wikibase.renderSnak(curRef.snaks['P2699'][1])})
			else
				table.insert(formattedReferences, frame:extensionTag{  name = 'ref', content = mw.wikibase.formatValues(curRef.snaks)})
			end
		end
	end
	return table.concat(formattedReferences, '')
end

--[[ 
Fetch property from specified entity.

@param {string} propertyName - name of property (e.g P123)
@param {string} entityId - Qid of the entity for which we fetch the data. Default: nil (page entity)
@param {table} options - table with following supported parameters:
	- seperator - seperator between multiple values. Default: ,
	- allowMulti - whether only first statement should be fetched are all statements.
	- allowNA - whether if is valid to fetch somevalue snaks: Default: nil/false
	- entity-gender-aware - Fetch gender form instead of regular label 
	- qualifiers - list of optional qualifers to append to property value (Example {'P1', 'P2'))
	- qualifiers-sep - seperator between optional qualifiers
	- img-width - width of image
]]
function getPropertyByOptions( propertyName, entityId, options )
	-- verify entity exists
	if entityId==nil then return end
	
	-- defaults
	options = options or {}
	options['seperator'] = options['seperator'] or ', '
	options['allowMulti'] = options['allowMulti'] or false
	options['allowNA'] = options['allowNA'] or false
	options['entity-gender-aware'] = options['entity-gender-aware'] or false -- Fetch gender form instead of regular label
	options['qualifiers'] = options['qualifiers'] or {} -- fetch optional qualifiers
	options['qualifiers-sep'] = options['qualifiers-sep'] or ', '
	options['img-width'] = options['img-width'] or '250px'
	options['source'] = options['source'] or false
	options['filter'] = options['filter'] or nil
	options['sort'] = options['sort'] or { 'P1545' }
	options['default-value'] = options['default-value'] or nil

     return --no such property for this item
end

function getLabel( propertyName, entityId )
    local entity = entityId
    if not entity then return end--the entity doesnt exist or have no claims
    local property =  mw.wikibase.getBestStatements(entity, propertyName)
    if not property or not property[1] 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
        local label, lang = mw.wikibase.label( propValue.value['id'] )
        return label
    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 getItem( propertyName, entityId )
    local entity = entityId
    if not entity then return end--the entity doesnt exist or have no claims
    local property =  mw.wikibase.getBestStatements(entity, propertyName)
    if not property or not property[1] 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

    return propValue.value['id']
end

-- Return the Item for property, or the item of the linked entiy of that property
function item( frame )
    return getItem( string.upper(frame.args[1] ))
end

function getImageLink( propName, width, align, description, border, entityId)
     return --the entity doesnt exist or have no claims
end
    
--use this function to get associated image to be used in the article
function imageLink( frame )
    return getImageLink(string.upper(frame.args[1] or 'P18'), frame.args["width"], frame.args["align"], frame.args["description"], frame.args["גבול"])
end

-- returns "1" if the page has an associated wikidata entry, "" otherwise
function hasEntry()
    local entity = mw.wikibase.getEntityIdForCurrentPage()
    --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

-- returns the wikidata Qid, if exists, "" otherwise
function getPageEntry(frame)
	return mw.wikibase.getEntityIdForTitle(string.upper(frame.args[1] ))
end

function getSitelinksFromQid(frame)
	resTable = mw.wikibase.getEntity(string.upper(frame.args[1] ))
	resTable = resTable["sitelinks"]
	s= "<table>\n"
		for k,v in pairs(resTable) do
			if  v.title then
				s=s.."<tr>".. "<td>" .. k.."</td>".. "<td>" .. v.title.."</td>".."</tr>" .. "\n"
		end 
	end
	return s .. "</table>"
end 

local function createInterwikiForQid(frame)
	local raw = mw.wikibase.getEntity( string.upper( frame.args[1] ) )["sitelinks"]
	local interwikis = ''
	for site, val in pairs(raw) do
		truncated, found = string.gsub(site, 'wiki$', '')
		truncated = string.gsub(truncated, '_', '-')
		if found == 1 and truncated ~= 'he' and truncated ~= 'commons' then interwikis = interwikis .. '[[' .. truncated .. ':' .. val.title .. ']]' end
	end
	return interwikis
end


return {
    imageLink = imageLink,
    ['תמונה'] = imageLink,
    label = label,
    ['תווית'] = label,
    item = item,
    ['פריט'] = item,
    formatEntity = formatEntity,
    property = property,
    propertyWithGender = propertyWithGender,
    ['מאפיין'] = property,
    getProperty = getProperty,
    getPropertyByOptions = getPropertyByOptions,
    getPropertyQualifier = getPropertyQualifier,
    getItem = getItem,
    getImageLink = getImageLink,
    getLabel = getLabel,
    hasEntry = hasEntry,
    getPageEntry = getPageEntry,
    getSitelinksFromQid = getSitelinksFromQid,
    createInterwikiForQid = createInterwikiForQid,
    ['יש פריט'] = hasEntry
}