Modul:Referenshantering
Utseende
Dokumentation [visa] [redigera] [historik] [rensa sidcachen]
Detta är en modul för att ange referenser till uppgifter från Wikipedia. Modulen är tänkt att användas för referenshantering i andra moduler, vilket görs genom:
local wd = require( 'Modul:Referenshantering' )
Den innehåller funktionerna processAllRefsForItem(frame,tblReferences)
och processAllRefsForItemWD(frame,tblReferences,idEntity,idProperty)
. Funktionerna är identiska förutom vilken mall de visar om uppgiften saknar en referens. Den första funktionen använder mallen {{källa behövs}}, som visar [källa behövs], medan den andra använder {{källa behövs WD}}, som visar t.ex. .
localisation = require( 'Modul:Sportdata localisation' )
settings = require( 'Modul:Sportdata settings' )
local d={}
local t={}
local rm={}
edit_message = mw.message.new('vector-view-edit'):plain()
rm.iSourcesMissing=0;
--Adds a pen with a link to the relevant wikidataitem
function txtEditPen(iEntity,iProperty,txtOtherVersion)
if (txtOtherVersion) then
txtFile=txtOtherVersion
else
txtFile='Arbcom_ru_editing.svg'
end
return '<sup class="noprint Inline-Template">[[File:'..txtFile..'|'
.. edit_message .. '|8px|baseline|class=noviewer|link=https://www.wikidata.org/wiki/'
.. iEntity .. '#' .. iProperty .. ']]</sup>'
end
function txtaddWikiData(txtPropertyName,txtName,txtSupportText)
return localisation.txtBeginning .. txtPropertyName .. localisation.txtSelect .. txtName .. txtSupportText .. localisation.txtSave
end
function txtUnpackReference_model(frame,refs,qid,pid)
-- If there is no source then tbl is a string
local txt=''
local bNoRef=true
for key, templateFillArray in pairs( refs ) do
bNoRef=false
--txt=txt..tprint(templateFillArray)
-- Create a reference tag based on the content in the table
--Increase reference counter
p.iRefcounter=p.iRefcounter+1
if (templateFillArray['error'] == false) then
if ((templateFillArray[localisation.txtTitle] ~= nil)) then
if (templateFillArray[localisation.txtUrl] ~= nil) then
txtOneReference = frame:expandTemplate{ title = localisation.txtCiteweb , args =templateFillArray}
else
txtOneReference = frame:expandTemplate{ title = localisation.txtBookweb , args =templateFillArray}
end
else
if (templateFillArray[localisation.txtUrl] ~= nil) then
txtOneReference=templateFillArray[localisation.txtUrl]
else
txtOneReference=txtNoTitleorUrl
end
end
else
txtOneReference='<span style="color:#ff0000">' .. templateFillArray['error_text'] .. '</span>'
end
if (frame['id']) then
txtScopeIdentifier=frame['id'] .. frame:getTitle() .. '__' .. p.objectMainId
else
txtScopeIdentifier=frame:getTitle() .. '__' .. p.objectMainId
end
if d[txtOneReference]~=nil then
-- If reference already has been added
result={name='wikidatabox'..txtScopeIdentifier .. '_' .. d[txtOneReference], content=txtOneReference}
else
-- If reference not already added
result={name='wikidatabox'..txtScopeIdentifier .. '_' .. p.iRefcounter, content=txtOneReference}
d[txtOneReference]=p.iRefcounter
end
txt=txt..frame:extensionTag("ref", result.content,{ name = result.name})
end
if (bNoRef) then
rm.iSourcesMissing=rm.iSourcesMissing+1
end
if (qid and pid) then
txt= txt .. txtEditPen(qid,pid)
end
return txt
end
function tblProcessIndividualReference_model(thisref,idMainEntity)
local refparts=thisref.snaks
table.sort(refparts)
local isok=1 --is regarded as a source (imported from other wikipedia is not consieder a source)
local bKeyProcessed=false
local txtUrlLocal=''
local txt
local templateFillArray={}
templateFillArray['error'] = false -- No error to start with :-)
for key, value in orderedPairs( refparts) do
bKeyProcessed=false;
if (key=='P1065') then
templateFillArray[localisation.txtArchiveUrl] = value[1].datavalue.value
bKeyProcessed=true;
end
if (key=='P123') then
iPublisher=readQualifier(value[1],'id')
templateFillArray[localisation.txtPublisher]='[[' .. getLabelByEntity( iPublisher ) .. ']]'
bKeyProcessed=true;
end
if (key=='P143') then
txt=localisation.txtFromADifferentLanguageVersion
isok=0
bKeyProcessed=true;
end
if (key=='P1476') then
txtTitleLocal=readQualifier(value[1],'text')
if not (isP248) then
templateFillArray[localisation.txtLanguage]= readQualifier(value[1],'language')
templateFillArray[localisation.txtTitle] = string.gsub(txtTitleLocal,"|"," - ")
end
bKeyProcessed=true;
end
if (key=='P1810') then
txtTitleLocal=value[1].datavalue.value
templateFillArray[localisation.txtTitle] = string.gsub(txtTitleLocal,"|"," - ")
bKeyProcessed=true;
end
if (key=='P2093') then
templateFillArray[localisation.txtAuthor] = value[1].datavalue.value
bKeyProcessed=true;
end
if (key=='P248') then
txtSource=readQualifier(value[1],'id')
templateFillArray=processDatabaseSource_model(templateFillArray,idMainEntity,txtSource)
bKeyProcessed=true
end
if (key=='P2960') then
templateFillArray[localisation.txtArchiveDate] = string.sub(readQualifier(value[1],'time'),2,11)
bKeyProcessed=true;
end
if (key=='P304') then
templateFillArray[ txtPage]=value[1].datavalue.value
bKeyProcessed=true;
end
if (key=='P3452') then
-- txt="Härlett från"
isok=0
bKeyProcessed=true
end
if (key=='P407') then
-- Do not process (take language from P1476 instead)
bKeyProcessed=true
end
if (key=='P4656') then
-- txt="Från språkversion"
isok=0
bKeyProcessed=true
end
if (key=='P50') then
iAuthor=readQualifier(value[1],'id')
tmp=rm.tblWikilink(iAuthor)
templateFillArray[localisation.txtAuthorLabel]=tmp.txtLabel
bKeyProcessed=true;
end
if (key=='P577') then
templateFillArray[localisation.txtDateTemplate] = string.sub(readQualifier(value[1],'time'),2,11)
bKeyProcessed=true
end
if (key=='P813') then
templateFillArray[localisation.txtAccessdate] = string.sub(readQualifier(value[1],'time'),2,11)
bKeyProcessed=true
end
if (key=='P854') then
txtUrlLocal=string.gsub(value[1].datavalue.value,"|","{{!}}")
templateFillArray[localisation.txtUrl] = txtUrlLocal
bKeyProcessed=true
end
if (key=='P98') then
iAuthor=readQualifier(value[1],'id')
templateFillArray[localisation.txtAuthor]='[[' .. getLabelByEntity( iAuthor ) .. ']]'
bKeyProcessed=true
end
if (not (bKeyProcessed)) then
-- If not found by the keys above, assume it is from a database
-- Test for common database qids to show data
local subjectobject=mw.wikibase.getBestStatements(key,'P1629')
if (not(next(subjectobject))) then
local subjectobject=mw.wikibase.getBestStatements(key,'P2378')
end
if (not(next(subjectobject))) then
local subjectobject=mw.wikibase.getBestStatements(key,'P10476')
end
if (not(next(subjectobject))) then
local subjectobject=mw.wikibase.getBestStatements(key,'P10726')
end
-- If from a database try to add url-link if possible
if (next(subjectobject)) then
iSubjectobject=read(subjectobject[1],'id')
templateFillArray[localisation.txtTitle]=getLabelByEntity( iSubjectobject )
keyentity=mw.wikibase.getEntity( key )
urlsnak=mw.wikibase.getBestStatements(key,'P1630' )
if (next(urlsnak)) then
url=urlsnak[1].mainsnak.datavalue.value
thisid=value[1].datavalue.value
templateFillArray[localisation.txtUrl] = string.gsub(url,"$1",thisid)
else
templateFillArray[localisation.txtUrl] = 'No Url!'
end
else
templateFillArray[txtTitle] = mw.wikibase.getLabel(key)
answers=mw.wikibase.getBestStatements(key, 'P1630' )
if (answers[1]) then -- Only add url if the key has a url-format property
url=answers[1].mainsnak.datavalue.value
templateFillArray[localisation.txtUrl] = url_encode(string.gsub(url,"$1",url_decode(value[1].datavalue.value)))
end
end
end
end
return isok,templateFillArray
end
function processDatabaseSource_model(templateFillArray,idMainEntity,txtSource)
StatedInentity=mw.wikibase.getEntity( txtSource )
txtLabelStatedIn=StatedInentity:getLabel()
databaseproperty=StatedInentity:getBestStatements( 'P1687')
local bHasDatabase=false
local bFoundInDatabase=false
local idDatabase=nil
for keyDatabase, valueDatabase in pairs( databaseproperty ) do
bHasDatabase=true
idDatabase=read(valueDatabase,'id')
if not (idDatabase==nil) then
databaseentity=mw.wikibase.getEntity( idDatabase )
mainentity=mw.wikibase.getEntity( idMainEntity )
mainsnak=mainentity:getBestStatements(idDatabase )
if (next(mainsnak)) then
bFoundInDatabase=true
thisid=mainsnak[1].mainsnak.datavalue.value
thisid=url_decode(thisid)
urlsnak=databaseentity:getBestStatements( 'P1630' )
url=urlsnak[1].mainsnak.datavalue.value
templateFillArray[txtUrl] = url_encode(string.gsub(url,"$1",thisid))
if (templateFillArray[txtTitle]~=nil) then
templateFillArray[txtTitle] = txtLabelStatedIn .. ': ' .. templateFillArray[txtTitle]
else
templateFillArray[txtTitle] = txtLabelStatedIn
end
-- else
break
end
end
end
if ((bHasDatabase==true) and (bFoundInDatabase==false)) then
templateFillArray['error'] = true
templateFillArray['error_text']=txtNoDataBaseId .. '[[' .. txtCategory .. ':' .. txtDBreferenceWithoutId .. ']]'
end
if (bHasDatabase==false) then
templateFillArray[txtTitle] = txtLabelStatedIn
end
return templateFillArray
end
function processAllRefsForWDItem_model(tblReferences,idEntity)
local tmpResult={}
local bHasSource=0;
--Only add references if there are any
if (tblReferences) then
for keyRef, valueRef in pairs(tblReferences) do
-- Process individual reference
local isok,refparts=tblProcessIndividualReference_model(valueRef,idEntity)
-- Act if reference is ok
if (isok==1) then
bHasSource=1
-- Add reference to table
table.insert(tmpResult,refparts)
end
end
-- If no valid references (valid = not using wikipedia as a source) return a string composed of the template that says source needed (in wikidata)
end
return bHasSource,tmpResult
end
rm.now=function()
local currentDateStr = os.date("%Y-%m-%d %H:%M:%S")
return currentDateStr
end
--Returns a tbl with label, sitelink etc.
rm.tblWikilink=function (qid,asofdate,bDoNotCheckPartOf)
local tbl={}
local bHasLabel
if (qid) then
tbl.qid=qid
-- Read label (for specific year if relevant)
local txtLabelThis=mw.wikibase.getLabelByLang( qid, txtLanguageCode)
if (asofdate==nil) then
tbl.txtLabel=txtLabelThis
else
tbl.txtLabel=rm.getLabelFromEntityId(qid,asofdate)
end
if not tbl.txtLabel then
tbl.txtLabel=localisation.txtMissing .. '<!--'..qid..'-->' .. '[[' .. txtCategory .. ':' .. txtLinkWithoutLabel .. ']]' -- The commented id can be used by functions to, for example provide informatation on the entity that misses a label
bHasLabel=false
else
bHasLabel=true
end
tbl.txtLabel_current=rm.getLabelFromEntityId(qid,rm.now())
if not tbl.txtLabel_current then
tbl.txtLabel_current=localisation.txtMissing .. '<!--'..qid..'-->' .. '[[' .. txtCategory .. ':' .. txtLinkWithoutLabel .. ']]' -- The commented id can be used by functions to, for example provide informatation on the entity that misses a label
end
-- Get site link
tbl.txtSitelink=mw.wikibase.getSitelink(qid)
-- If site link exists - use it
if tbl.txtSitelink then
return tbl,true
else
if not(bDoNotCheckPartOf) then
-- if there is no site link, check if the item (organisation) is part of another one (either as 'part of' or 'mother organisation')
-- if so use it
local claimsPartOf = mw.wikibase.getAllStatements(qid, 'P361' ) -- part of
if (claimsPartOf[1]) then
local iPartof=read(claimsPartOf[1],'id')
local mytbl, isokpart
mytbl,isokpart=rm.tblWikilink(iPartof,asofdate)
if (isokpart) then
tbl.txtSitelink=mytbl.txtSitelink -- use sitelink from part of (but label from original organisation)
return tbl,true
end
end
local claimsPartOf2 = mw.wikibase.getAllStatements(qid,'P749' ) -- mother organisation
if (claimsPartOf2[1]) then
local iPartof2=read(claimsPartOf2[1],'id')
local mytbl, isokpart
mytbl,isokpart=rm.tblWikilink(iPartof2,asofdate)
if (isokpart) then
tbl.txtSitelink=mytbl.txtSitelink -- use sitelink from part of (but label from original organisation)
return tbl,true
end
else
-- If no site link at the item or any parent, consider using the label as sitelink
if bHasLabel and not tbl.txtSitelink then
idUsedLabel=mw.wikibase.getEntityIdForTitle(tbl.txtLabel)
if (idUsedLabel) then
tbl.txtSitelink=tbl.txtLabel
return tbl,true
else
if (txtLabelThis==nil) then
tbl.txtSitelink=tbl.txtLabel
return tbl, true
else
tbl.txtSitelink=txtLabelThis
return tbl, true
end
end
else
tbl.txtLabel=localisation.txtMissing .. '<!--'..qid..'-->' .. '[[' .. txtCategory .. ':' .. txtLinkWithoutLabel .. ']]' -- The commented id can be used by functions to, for example provide informatation on the entity that misses a label
tbl.txtSitelink=false
return tbl,false -- The commented id can be used by functions to, for example provide informatation on the entity that misses a label
end
end
end
end
end
return tbl,false
end
rm.getLabelFromEntityId=function( qid, asofdate )
if (asofdate==nil) then
return mw.wikibase.getLabelByLang(qid, txtLanguageCode )
else
--If available use P2561 (name), prioriticed as it can be edited with greater flexibility
local bFoundName,txtName=getLabelFromNamePropertyId2(qid,'P2561',asofdate )
if (not(bFoundName)) then
--If P2561 not available try P1448 (official name)
local bFoundName,txtName=getLabelFromNamePropertyId2(qid,'P1448',asofdate )
end
if (bFoundName) then
return txtName
else
return mw.wikibase.getLabelByLang(qid, txtLanguageCode )
end
end
end
--Returns a wikilink to the connect page if there is one. Otherwise create to the a page with the name of the label
function txtWikilink(qid,year)
-- Read label (for specific year if relevant)
local txtLabel
local txtLabelThis=mw.wikibase.getLabelByLang( qid,txtLanguageCode )
if (year==nil) then
txtLabel=txtLabelThis
else
txtLabel=getLabelFromEntityId(qid,year)
end
-- Get site link
local txtSitelink=mw.wikibase.getSitelink(qid)
-- If site link exists - use it
if (txtSitelink) then
return '[[' .. txtSitelink .. '|' .. txtLabel .. ']]'
else
-- if there is no site link, check if the item (organisation) is part of another one (either as 'part of' or 'mother organisation')
-- if so use it
local claimsPartOf = mw.wikibase.getAllStatements(qid, 'P361' ) -- part of
if (claimsPartOf[1]) then
local iPartof=read(claimsPartOf[1],'id')
local mytxt, isokpart
mytxt,isokpart=txtWikilink(iPartof,year)
if (isokpart) then
return mytxt,true
end
end
local claimsPartOf2 = mw.wikibase.getAllStatements(qid,'P749' ) -- mother organisation
if (claimsPartOf2[1]) then
local iPartof2=read(claimsPartOf2[1],'id')
return txtWikilink(iPartof2,year)
else
-- If no site link at the item or any parent, consider using the label
if (txtLabel) then
idThatLabel=mw.wikibase.getEntityIdForTitle(txtLabel)
if (idThatLabel) then
return txtLabel,true
else
if (txtLabelThis==nil) then
return '[[' .. txtLabel .. ']]',true
else
return '[[' .. txtLabelThis .. '|' .. txtLabel .. ']]',true
end
end
else
return '('..localisation.txtMissing..')<!--'..qid..'-->' .. '[[' .. txtCategory .. ':' .. txtLinkWithoutLabel .. ']]',false -- The commented id can be used by functions to, for example provide informatation on the entity that misses a label
end
end
end
end
--Returns a wikilink to the connect page if there is one. Otherwise create to the a page with the name of the label
--Does not try to use the label as a sitelink if there is no sitelink
--Further, it does not try to use any part of or mother organisation relation
function txtWikilinkWOsitelink(id,year)
-- Get entity
entityThis=mw.wikibase.getEntity(id)
-- Read label (for specific year if relevant)
local txtLabel
local txtLabelThis=getLabelByEntity(id)
if (year==nil) then
txtLabel=txtLabelThis
else
txtLabel=getLabelFromEntity(entityThis,year)
end
-- Get site link
txtSitelink=mw.wikibase.getSitelink(id)
-- If site link exists - use it
if (txtSitelink) then
return '[[' .. txtSitelink .. '|' .. txtLabel .. ']]'
else
if (txtLabel) then
return txtLabel
else
return '('..localisation.txtMissing..')<!--'..id..'-->' -- The commented id can be used by functions to, for example provide informatation on the entity that misses a label
end
end
end
function emptyifNil(data)
if (data) then
return data
else
return ''
end
end
function getLabelByEntity( id)
return mw.wikibase.getLabel(id, txtLanguageCode )
end
-- Ersätt med getLabelFromEntityId
function getLabelFromEntity( entity, year )
if (year==nil) then
return entity:getLabel( txtLanguageCode )
else
--If available use P2561 (name), prioriticed as it can be edited with greater flexibility
local bFoundName,txtName=getLabelFromNameProperty(entity,'P2561',year )
if (not(bFoundName)) then
--If P2561 not available try P1448 (official name)
bFoundName,txtName=getLabelFromNameProperty(entity,'P1448',year )
end
if (bFoundName) then
return txtName
else
return entity:getLabel( txtLanguageCode )
end
end
end
function getLabelFromEntityId( qid, year )
if (year==nil) then
return mw.wikibase.getLabelByLang(qid, txtLanguageCode )
else
--If available use P2561 (name), prioriticed as it can be edited with greater flexibility
local bFoundName,txtName=getLabelFromNamePropertyId(qid,'P2561',year )
if (not(bFoundName)) then
--If P2561 not available try P1448 (official name)
local bFoundName,txtName=getLabelFromNamePropertyId(qid,'P1448',year )
end
if (bFoundName) then
return txtName
else
txtLabelDirect=mw.wikibase.getLabel(qid, txtLanguageCode )
if (txtLabelDirect) then
return txtLabelDirect
else
return localisation.txtMissing
end
end
end
end
function getLabelFromNamePropertyId(qid, pid,year )
local claims = mw.wikibase.getBestStatements(qid,pid) -- If the club have had many names, loop through to find the one that was used when the player played at the club
if (next(claims)) then
-- Use label as default (if nothing is found that match year-wise)
local txtName=mw.wikibase.getLabelByLang(qid, txtLanguageCode )
-- Loop through names
for key, value in pairs( claims ) do
local yearfrom,yearto,datefrom,dateto = years(value)
local lng=read(value,'language')
if ((year >= yearfrom and year <= yearto) or (yearto=='' and year >= yearfrom)) and (not(lang_denylist[lng]==true)) then
if (lng==txtLocalLanguage) then
txtName=read(value,'text')
if (txtName==nil) then
txtName=txtNotLocalLanguage
else --If name exists in local language return straight away, do not continue looping
return true,txtName
end
else --set name for possible later return, but keep looping
txtName=read(value,'text')
end
end
end
--if name property (P1448 or P2561) is set then set name if one was found, otherwise write an error message
return true,txtName
else
--if property was not set just use usual name
return false,nil
end
end
function getLabelFromEntityId2( qid, asofdate )
if (year==nil) then
return mw.wikibase.getLabelByLang(qid, txtLanguageCode )
else
--If available use P2561 (name), prioriticed as it can be edited with greater flexibility
local bFoundName,txtName=getLabelFromNamePropertyId2(qid,'P2561',asofdate )
if (not(bFoundName)) then
--If P2561 not available try P1448 (official name)
local bFoundName,txtName=getLabelFromNamePropertyId2(qid,'P1448',asofdate )
end
if (bFoundName) then
return txtName
else
return mw.wikibase.getLabelByLang(qid, txtLanguageCode )
end
end
end
function getLabelFromNamePropertyId2(qid, pid,asofdate )
local claims = mw.wikibase.getBestStatements(qid,pid) -- If the club have had many names, loop through to find the one that was used when the player played at the club
if (next(claims)) then
-- Use label as default (if nothing is found that match year-wise)
local txtName=mw.wikibase.getLabelByLang(qid, txtLanguageCode )
-- Loop through names
for key, value in pairs( claims ) do
local yearfrom,yearto,datefrom,dateto = years(value)
local lng=read(value,'language')
if ((asofdate >= datefrom and asofdate <= dateto) or (dateto=='' and asofdate >= datefrom)) and (not(lang_denylist[lng]==true)) then
if (lng==txtLocalLanguage) then
txtName=read(value,'text')
if (txtName==nil) then
txtName=txtNotLocalLanguage
else --If name exists in local language return straight away, do not continue looping
return true,txtName
end
else --set name for possible later return, but keep looping
txtName=read(value,'text')
end
end
end
--if name property (P1448 or P2561) is set then set name if one was found, otherwise write an error message
return true,txtName
else
--if property was not set just use usual name
return false,nil
end
end
--Ersätt med getLabelFromNamePropertyId
function getLabelFromNameProperty(entity, iProperty,year )
local claims = entity:getBestStatements(iProperty) -- If the club have had many names, loop through to find the one that was used when the player played at the club
if (next(claims)) then
-- Use label as default (if nothing is found that match year-wise)
txtName=entity:getLabel( txtLanguageCode )
-- Loop through names
for key, value in pairs( claims ) do
yearfrom,yearto,datefrom,dateto = years(value)
lng=read(value,'language')
if ((year >= yearfrom and year <= yearto) or (yearto=='' and year >= yearfrom)) and (not(lang_denylist[lng]==true)) then
if (lng==txtLocalLanguage) then
txtName=read(value,'text')
if (txtName==nil) then
txtName=txtNotLocalLanguage
else --If name exists in local language return straight away, do not continue looping
return true,txtName
end
else --set name for possible later return, but keep looping
txtName=read(value,'text')
end
end
end
--if name property (P1448 or P2561) is set then set name if one was found, otherwise write an error message
return true,txtName
else
--if property was not set just use usual name
return false,nil
end
end
function readFirstStatementId(entityid,txtProperty)
statements=mw.wikibase.getBestStatements(entityid,txtProperty)
if not(next(statements)==nil) then
if not(statements[1]==nil) then
return read(statements[1],'id')
end
end
return nil
end
function readFirstStatement(entityid,txtProperty)
statements=mw.wikibase.getBestStatements(entityid,txtProperty)
if not(next(statements)==nil) then
if not(statements[1]==nil) then
qid=read(statements[1],'id')
if not (qid==nil) then
return mw.wikibase.getLabelByLang(qid,txtLanguageCode)
end
end
end
return nil
end
--Plocka bort när ersatt av readFirstStatement
function getOneValue(entity,txtProperty)
statements=entity:getBestStatements( txtProperty)
if not(next(statements)==nil) then
if not(statements[1]==nil) then
qid=read(statements[1],'id')
if not (qid==nil) then
return mw.wikibase.getLabel(qid)
end
end
end
return nil
end
--Plocka bort när ersatt av readFirstStatementId
function getOneEntityId(entity,txtProperty)
statements=entity:getBestStatements( txtProperty)
if next(statements)==nil then
return nil
else
return read(statements[1],'id')
end
end
function getQualifier(value,txtQualifierId)
return getProperty(value,txtQualifierId)
end
--Replace with (and move content to) getQualifier, which as a more sensible name
function getProperty(value,txtPropertyId)
if value['qualifiers'] and value['qualifiers'][txtPropertyId] then
txtValue=emptyifNil(mw.wikibase.renderSnak(value['qualifiers'][txtPropertyId][1]))
else
txtValue=nil
end
return txtValue
end
function getQualifierId(value,txtQualifierId)
return getPropertyId(value,txtQualifierId)
end
--Replace with (and move content to) getQualifierId, which as a more sensible name
function getPropertyId(value,txtPropertyId)
if value then
if value['qualifiers'] then
if value['qualifiers'][txtPropertyId] then
txtSnaktype=value['qualifiers'][txtPropertyId][1].snaktype
if (txtSnaktype=="value") then
txtValue=value['qualifiers'][txtPropertyId][1].datavalue.value.id
if (not txtValue) then
txtValue=value['qualifiers'][txtPropertyId][1].datavalue.value.amount
end
else
txtValue="Not Implemented"
end
else
txtValue=nil
end
else
txtValue=nil
end
else
return nil
end
return txtValue
end
--Read a value of a property
function read(data,type)
if (data) then
if (data['mainsnak']) then
return readQualifier(data['mainsnak'],type)
else
return nil
end
else
return nil
end
end
--Read a value of a qualifier
function readQualifier(data,type)
if (data['datavalue']) then
return data['datavalue']['value'][type]
else
return nil
end
end
rm.readtext=function(claims, asofdate)
for key, value in pairs( claims ) do
local yearfrom,yearto,datefrom,dateto = years(value)
local lng=read(value,'language')
if ((asofdate >= datefrom and asofdate <= dateto) or (dateto=='' and asofdate >= datefrom)) and (not(localisation.lang_denylist[lng]==true)) then
if (lng==localisation.txtLocalLanguage) then
txtName=read(value,'text')
if (txtName==nil) then
txtName=localisation.txtNotLocalLanguage
else --If name exists in local language return straight away, do not continue looping
return true,txtName
end
else --set name for possible later return, but keep looping
txtName=read(value,'text')
end
end
end
end
rm.readQualifier=function (value,qfid,type)
if (value.qualifiers and value.qualifiers[qfid]) then
return readQualifier(value.qualifiers[qfid][1],type)
else
return nil
end
end
-- the "qualifiers" and "snaks" field have a respective "qualifiers-order" and "snaks-order" field
-- use these as the second parameter and this function instead of the built-in "pairs" function
-- to iterate over all qualifiers and snaks in the intended order.
local function orderedpairs(array, order)
if not order then return pairs(array) end
-- return iterator function
local i = 0
return function()
i = i + 1
if order[i] then
return order[i], array[order[i]]
end
end
end
function __genOrderedIndex( t )
local orderedIndex = {}
for key in pairs(t) do
table.insert( orderedIndex, key )
end
table.sort( orderedIndex )
return orderedIndex
end
function orderedNext(t, state)
-- Equivalent of the next function, but returns the keys in the alphabetic
-- order. We use a temporary ordered key table that is stored in the
-- table being iterated.
local key = nil
--print("orderedNext: state = "..tostring(state) )
if state == nil then
-- the first time, generate the index
t.__orderedIndex = __genOrderedIndex( t )
key = t.__orderedIndex[1]
else
-- fetch the next value
for i = 1,table.getn(t.__orderedIndex) do
if t.__orderedIndex[i] == state then
key = t.__orderedIndex[i+1]
end
end
end
if key then
return key, t[key]
end
-- no more value to return, cleanup
t.__orderedIndex = nil
return
end
function orderedPairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return orderedNext, t, nil
end
--from http://lua-users.org/wiki/StringRecipes
function url_decode(str)
str = str:gsub("+", " ")
str = str:gsub("%%(%x%x)", function(h)
return string.char(tonumber(h,16))
end)
str = str:gsub("\r\n", "\n")
return str
end
-- from: https://stackoverflow.com/questions/2421695/first-character-uppercase-lua
function firstToUpper(str)
if (str) then
return (str:gsub("^%l", string.upper))
else
return ''
end
end
--from http://lua-users.org/wiki/StringRecipes
function url_encode(str)
if str then
str = str:gsub("\n", "\r\n")
-- str = str:gsub("([^%w %-%_%.%~])", function(c)
-- return ("%%%02X"):format(string.byte(c))
-- end)
str = str:gsub(" ", "+")
end
return str
end
-- Funktion från https://stackoverflow.com/questions/41942289/display-contents-of-tables-in-lua
function tprint (tbl, indent)
if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent-2) .. "}"
return toprint
end
return rm