Module:Wikidata item

From Wikipedia

Module:Wikidata item/doc

This module formats itemlinks (and also other values)

Sitelink exists No Sitelink exists
Label exists [[Sitelink|Label]] Label
No Label exsists [[Sitelink]] (Blank)

Parameter

  • first parameter: property (like p36 for capital)

-- get label and sitelink to result in [[ sitelink | label ]]
local p = {}

function p.link(frame)
   
  local entity = mw.wikibase.getEntityObject()
  local var1 = string.lower(frame.args[1])
  if entity.claims[var1] == nil then
    return ""
  end
  if entity.claims[var1][0] == nil then
      return ""
  end  
  local separator=""
  local result = ""
  for i=0, 999 do 
      if entity.claims[var1][i] == nil then
          return result
      end
      if entity.claims[var1][i].qualifiers then
          -- skip qualifiers
      else  
          if not ( type (entity.claims[var1][i].mainsnak.datavalue.value) == "table" ) then
              -- this is not an item
              result = result .. separator .. entity.claims[var1][i].mainsnak.datavalue.value
          else
              -- this is an item
              local q=  "Q" .. entity.claims[var1][i].mainsnak.datavalue.value["numeric-id"]
              local sitelink = mw.wikibase.sitelink(q)
              local label    = mw.wikibase.label(q)
              -- format [[sitelink|label]] 
              if sitelink == nil then
                  if label == nil then
                      -- blank
                  else
                      result = result .. separator .. label
                  end
              else
                  if label == nil then
                      result  = result .. separator .. "[[" .. sitelink .. "]]"
                  else
                      result  = result .. separator .. "[[" .. sitelink .. "|" .. label .. "]]"
                  end
              end
          end
      end
      separator = ", "
  end
  return result
end

return p