Modul:Wb

Aus STA Dokumentationsplattform
Version vom 30. Mai 2020, 22:40 Uhr von imported>Juergen.Kett
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:Wb/Doku erstellt werden

local p = {}; 

p.id = nil;
p.entity = nil;
p.claims = nil;

local statementsTemplate = '\n<div class="statements">$var\n</div>'
local statementTemplate = '\n<div class="statement">$var\n</div>'
local valuesTemplate = '\n<div class="values">$var\n</div>'
local valueTemplate = '\n<div class="value">$var</div>'
local propertyTemplate = '\n<div class="property">$var</div>'
local qStatementsTemplate = '<div class="qstatements">$var</div>'
local qStatementTemplate = '<div class="qstatement">$var</div>'
local qValuesTemplate = '<div class="qvalues">$var</div>'
local qValueTemplate = '<div class="qvalue">$var</div>'

function p.getFormatedStatements(frame)
   id = frame.args.id
   return p._getFormatedStatements(id)
end

function p._getFormatedStatements(id)
  local ret = ""
  local qualifiers = ""
  local prop = ""
  local value = ""
  p.entity = mw.wikibase.getEntity(id)
  
  if not p.entity then
     return "no entity with id found: " .. id
  end
  
  p.claims = p.entity["claims"]
  
  for propId, valueSnaks in pairs(p.claims) do
     local values = ""
     for key, valueSnak in pairs(valueSnaks) do
        qualifiers = p.getFormatedQualifiers(valueSnak["qualifiers"])
        values = values .. p.renderValue(valueSnak["mainsnak"], qualifiers)
     end
     ret = ret .. p.renderStatement(propId, values, statementTemplate) 
  end
  ret = string.gsub(statementsTemplate, "$var", ret) 
  return ret
end

function p.getFormatedQualifiers(props)
    if not props then return nil end
    local ret = ""

    for propId, valueSnaks in pairs(props) do
       local values = ""
       for key, valueSnak in pairs(valueSnaks) do   
          values = values .. p.renderQValue(valueSnak)
       end
       ret = ret .. p.renderStatement(propId, values, qStatementTemplate) 
    end
    return ret
end

function p.renderStatement(propId, values, template)
  local propLabel = mw.wikibase.getLabel(propId)
  if not propLabel then propLabel = "" end
  local prop = string.gsub(propertyTemplate, "$var", propLabel)
  return string.gsub(template, "$var", prop .. values)  
end

function p.renderQValue(valueSnak)
  local value = mw.wikibase.renderSnak(valueSnak)
  return string.gsub(qValueTemplate, "$var", value) 
end

function p.renderValue(valueSnak, qualifiers)
  if not qualifiers then 
     qualifiers = "" 
  else
     qualifiers = string.gsub(qStatementsTemplate, "$var", qualifiers)
  end
  local value = mw.wikibase.renderSnak(valueSnak)
  return string.gsub(valueTemplate, "$var", value .. qualifiers) 
end

p.getLabel = function (frame)
 local id = frame.args.id
 if id then 
   return mw.wikibase.getLabel(id)
 end
 return ""
end

-- wird nicht mehr benötigt
function p.nextStatement() 
  p.property = p.keys[p.index]
  mw.log(p.property)
  if p.property then
    claim = p.claims[p.property]
    snak = claim[1]["mainsnak"] 
    p.value = mw.wikibase.renderSnak(snak)
    p.index = p.index + 1
  end
end

-- wird nicht mehr benötigt
function p.initEntity(qid)
  p.entity = mw.wikibase.getEntity(qid)
  p.claims = p.entity["claims"]
  -- p.i, p.v = next(p.claims, nil) 
  -- mw.logObject(p.entity)
  p.keys = {}
  p.index=1
  local i = 1
  for k in pairs(p.claims) do  
     p.keys[i]=k
     i = i + 1
  end
  mw.logObject(p.keys)
end



return p