Modul:Wb: Unterschied zwischen den Versionen

Aus STA Dokumentationsplattform
Zur Navigation springen Zur Suche springen
imported>Juergen.Kett
imported>Admin
 
(93 dazwischenliegende Versionen von 3 Benutzern werden nicht angezeigt)
Zeile 5: Zeile 5:
 
p.claims = nil;
 
p.claims = nil;
  
local rowDelA = "<tr>"
+
local statementsTemplate = '\n<table class="statements">$var\n</table>'
local rowDelB = "</tr>\n"
+
local statementTemplate = '\n<tr valign="top" class="statement">$var\n</tr>'
local colDelA = "<td>"
+
local propertyTemplate = '\n<td class="property">$var</td>'
local colDelB = "</td>"
+
local valuesTemplate = '\n<td class="values">$var\n</td>'
local tableDelA = "<table>"
+
local valueTemplate = '\n<div class="value">$var</div>'
local tableDelB = "</table>"
+
local qStatementsTemplate = '<table class="qstatements">$var</table>'
local refUrl = nil
+
local qStatementTemplate = '<tr valign="top" class="qstatement">$var</tr>'
 +
local qPropertyTemplate = '<td valign="top" class="qproperty">$var</td>'
 +
local qValuesTemplate = '<td valign="top" class="qvalues">$var</td>'
 +
local qValueTemplate = '<div class="qvalue">$var</div>'
  
function p.getFormatedStatements(frame)
+
function p.renderEntity(frame)
 
   id = frame.args.id
 
   id = frame.args.id
   rowDelA = frame.args.rowDelA
+
   local propertyIds = nil
   rowDelB = frame.args.rowDelB
+
   local s = frame.args.propertyIds
   colDelA = frame.args.colDelA
+
   if not p._isempty(s) then
  colDelB = frame.args.colDelB
+
      propertyIds = mw.text.split(frame.args.propertyIds,"%s*,%s*") 
  tableDelA = frame.args.tableDelA
+
   end
   tableDelB = frame.args.tableDelB
+
   return p._renderEntity(id, propertyIds)
   return p._getFormatedStatements(id)
 
 
end
 
end
  
function p._getFormatedStatements(id)
+
function p._isempty(s)
 +
  return s == nil or s == ''
 +
end
 +
 
 +
function p._fillTemplate()
 +
  local frame = mw.getCurrentFrame()
 +
  o = frame:expandTemplate{ title = 'Wbtest', args = { id='Q1' } }
 +
  mw.logObject(o)
 +
end
 +
 
 +
function p._getFormatedValues(entityId, propertyId )
 +
  local claim = nil
 +
  -- get claim from wikibase if claims are not cached in p.claims
 +
  if not claims then
 +
    claim = mw.wikibase.getBestStatements( entityId, propertyId )
 +
  else
 +
    claim = p.claims[propertyId]
 +
  end
 +
  if claim then
 +
    return p._renderClaim(propertyId, claim)
 +
  end
 +
end
 +
 
 +
function p._renderEntity(id, propertyIds)
 
   local ret = ""
 
   local ret = ""
 
   local qualifiers = ""
 
   local qualifiers = ""
Zeile 30: Zeile 55:
 
   local value = ""
 
   local value = ""
 
   p.entity = mw.wikibase.getEntity(id)
 
   p.entity = mw.wikibase.getEntity(id)
   p.claims = p.entity["claims"]
+
 
   for key, claimgroup in pairs(p.claims) do
+
  if not p.entity then
    if claimgroup then
+
    return "no entity with id found: " .. id
    prop = mw.wikibase.getLabel(key)
+
  end
        for key2, claim in pairs(claimgroup) do
+
   p.claims = p.entity["claims"]  
          qualifiers=p.getFormatedQualifiers(claim["qualifiers"])
+
   if not propertyIds then
          value = mw.wikibase.renderSnak(claim["mainsnak"])
+
    propertyIds = {}
          ret = ret .. rowDelA .. colDelA .. prop .. colDelB .. colDelA .. value .. colDelB .. colDelA .. tableDelA .. qualifiers .. tableDelB .. colDelB .. rowDelB
+
    for propId in pairs(p.claims) do table.insert(propertyIds, propId) end
        end
+
    propertyIds = mw.wikibase.orderProperties(propertyIds)
    end  
+
  end
 +
  for i, propId in ipairs(propertyIds) do
 +
    valueSnaks = p.claims[propId]
 +
    if valueSnaks then
 +
      ret = ret .. p._renderClaim(propId, valueSnaks)
 +
    end
 
   end
 
   end
 +
 
 +
 +
  ret = string.gsub(statementsTemplate, "$var", ret)
 
   return ret
 
   return ret
 
end
 
end
  
function p.getFormatedQualifiers(snaks)
+
function p._renderClaim(propId, valueSnaks)
 +
    local values = ""
 +
    for key, valueSnak in pairs(valueSnaks) do
 +
        qualifiers = p._renderQualifiers(valueSnak["qualifiers"])
 +
        values = values .. p._renderValue(valueSnak["mainsnak"], qualifiers)
 +
    end
 +
    return p._renderStatement(propId, values)
 +
end
 +
 
 +
function p._renderQualifiers(props)
 +
    if not props then return nil end
 
     local ret = ""
 
     local ret = ""
     local prop = ""
+
     for propId, valueSnaks in pairs(props) do
    if snaks then
+
      local values = ""
      for key, snak in pairs(snaks) do
+
      for key, valueSnak in pairs(valueSnaks) do  
        prop = mw.wikibase.getLabel(key)
+
          values = values .. p._renderQValue(valueSnak)   
        ret = ret .. rowDelA .. colDelA .. prop .. colDelB .. colDelA .. mw.wikibase.renderSnak(snak[1]) .. colDelB .. rowDelB
+
      end
      end
+
      values = string.gsub(qValuesTemplate, "$var", values)
 +
      ret = ret .. p._renderQStatement(propId, values)  
 
     end
 
     end
     return ret ;
+
     return ret
 +
end
 +
 
 +
function p._renderQStatement(propId, values)
 +
  local propLabel = mw.wikibase.getLabel(propId)
 +
  if not propLabel then propLabel = "" end
 +
  local prop = string.gsub(qPropertyTemplate, "$var", propLabel)
 +
  local val = string.gsub(qValuesTemplate, "$var", values)
 +
  return string.gsub(qStatementTemplate, "$var", prop .. val) 
 +
end
 +
 
 +
function p._renderQValue(valueSnak)
 +
  local value = mw.wikibase.renderSnak(valueSnak)
 +
  return string.gsub(qValueTemplate, "$var", value)
 +
end
 +
 
 +
function p._renderStatement(propId, values)
 +
  local propLabel = mw.wikibase.getLabel(propId)
 +
  if not propLabel then propLabel = "" end
 +
  local prop = string.gsub(propertyTemplate, "$var", propLabel)
 +
  local val = string.gsub(valuesTemplate, "$var", values)
 +
  return string.gsub(statementTemplate, "$var", prop .. val) 
 +
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
 
end
  
Zeile 84: Zeile 167:
 
end
 
end
  
p.getLabel = function (frame)
+
 
local id = frame.args.id
 
if id then
 
  return mw.wikibase.getLabel(id)
 
end
 
return ""
 
end
 
  
 
return p
 
return p

Aktuelle Version vom 8. Juni 2020, 13:32 Uhr

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<table class="statements">$var\n</table>'
local statementTemplate = '\n<tr valign="top" class="statement">$var\n</tr>'
local propertyTemplate = '\n<td class="property">$var</td>'
local valuesTemplate = '\n<td class="values">$var\n</td>'
local valueTemplate = '\n<div class="value">$var</div>'
local qStatementsTemplate = '<table class="qstatements">$var</table>'
local qStatementTemplate = '<tr valign="top" class="qstatement">$var</tr>'
local qPropertyTemplate = '<td valign="top" class="qproperty">$var</td>'
local qValuesTemplate = '<td valign="top" class="qvalues">$var</td>'
local qValueTemplate = '<div class="qvalue">$var</div>'

function p.renderEntity(frame)
   id = frame.args.id
   local propertyIds = nil
   local s = frame.args.propertyIds
   if not p._isempty(s) then
      propertyIds = mw.text.split(frame.args.propertyIds,"%s*,%s*")   
   end
   return p._renderEntity(id, propertyIds)
end

function p._isempty(s)
  return s == nil or s == ''
end

function p._fillTemplate()
   local frame = mw.getCurrentFrame()
   o = frame:expandTemplate{ title = 'Wbtest', args = { id='Q1' } }
   mw.logObject(o)
end

function p._getFormatedValues(entityId, propertyId )
  local claim = nil
  -- get claim from wikibase if claims are not cached in p.claims 
  if not claims then
     claim = mw.wikibase.getBestStatements( entityId, propertyId )
  else
     claim = p.claims[propertyId]
  end
  if claim then
     return p._renderClaim(propertyId, claim)
  end
end

function p._renderEntity(id, propertyIds)
  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"] 
  if not propertyIds then
    propertyIds = {}
    for propId in pairs(p.claims) do table.insert(propertyIds, propId) end 
    propertyIds = mw.wikibase.orderProperties(propertyIds)
  end
  for i, propId in ipairs(propertyIds) do
    valueSnaks = p.claims[propId]
    if valueSnaks then
      ret = ret .. p._renderClaim(propId, valueSnaks)
    end
  end
  

  ret = string.gsub(statementsTemplate, "$var", ret) 
  return ret
end

function p._renderClaim(propId, valueSnaks)
     local values = ""
     for key, valueSnak in pairs(valueSnaks) do
        qualifiers = p._renderQualifiers(valueSnak["qualifiers"])
        values = values .. p._renderValue(valueSnak["mainsnak"], qualifiers)
     end
     return p._renderStatement(propId, values) 
end

function p._renderQualifiers(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
       values = string.gsub(qValuesTemplate, "$var", values)
       ret = ret .. p._renderQStatement(propId, values) 
    end
    return ret
end

function p._renderQStatement(propId, values)
  local propLabel = mw.wikibase.getLabel(propId)
  if not propLabel then propLabel = "" end
  local prop = string.gsub(qPropertyTemplate, "$var", propLabel)
  local val = string.gsub(qValuesTemplate, "$var", values)
  return string.gsub(qStatementTemplate, "$var", prop .. val)  
end

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

function p._renderStatement(propId, values)
  local propLabel = mw.wikibase.getLabel(propId)
  if not propLabel then propLabel = "" end
  local prop = string.gsub(propertyTemplate, "$var", propLabel)
  local val = string.gsub(valuesTemplate, "$var", values)
  return string.gsub(statementTemplate, "$var", prop .. val)  
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