Модуль:Sandbox/Avsolov/Heritage

Материал из Wikivoyage

Для документации этого модуля может быть создана страница Модуль:Sandbox/Avsolov/Heritage/doc

local wiki = {
  langcode = mw.language.getContentLanguage().code
}

local i18n = {
  ["labels"] = {
    ["en"] = {
      ["Lost"] = "Lost",
      ["Upload photo"] = "Upload photo",
    },
    ["ru"] = {
      ["Lost"] = "Утрачен",
      ["Upload photo"] = "Загрузить фото",
    }
  }
}

local p = {}

function p.monument(frame)
  local id = frame.args[1]
  local entity = mw.wikibase.getEntity(id)
  local langcode = frame.args[2] or frame:preprocess('{{int:lang}}')
  local language = mw.getLanguage(langcode)

  local name = entity.labels[langcode] or entity.labels[wiki.langcode]
  local knid = entity:formatStatements('P1483')
  knid.rawvalue = ''
  if #knid.value == 0 then knid.value= 'N/A' else knid.rawvalue = entity.claims['P1483'][1]['mainsnak']['datavalue']['value'] end
  knid.label = mw.wikibase.getLabelByLang('P1483', langcode) or mw.wikibase.getLabelByLang('P1483', wiki.langcode)
  local knidnew = entity:formatStatements('P5381')
  if #knidnew.value == 0 then knidnew.value= 'N/A' end
  knidnew.label = mw.wikibase.getLabelByLang('P5381', langcode) or mw.wikibase.getLabelByLang('P5381', wiki.langcode)

  local image = entity.claims['P18']
  if image then image = image[1]['mainsnak']['datavalue']['value'] else image = 'Village without photo.svg' end

  local typ = '[[File:PorticoIcon.svg|20px|%s|link=]]'
  local typ_id = 'Q811165'
  local map_marker = 'town-hall'
  for i,v in ipairs(entity.claims['P31']) do
    if v['mainsnak']['datavalue']['value']['id'] == 'Q1081138' then
      typ_id = 'Q1081138'
      map_marker = 'embassy'
      typ = '[[File:HistoryIcon.svg|20px|%s|link=]]'
    end
    if v['mainsnak']['datavalue']['value']['id'] == 'Q839954' then
      typ_id = 'Q839954'
      map_marker = 'campsite'
      typ = '[[File:ArcheologyIcon.svg|20px|%s|link=]]'
    end
    if v['mainsnak']['datavalue']['value']['id'] == 'Q4989906' then
      typ_id = 'Q4989906'
      map_marker = 'monument'
      typ = '[[File:MonumentIcon.svg|20px|%s|link=]]'
    end
  end
  local typ_label = mw.wikibase.getLabelByLang(typ_id, langcode) or mw.wikibase.getLabelByLang(typ_id, wiki.langcode)
  typ = string.format(typ, typ_label)

  local municipality = entity.claims['P131']
  if municipality then
    local munid = entity.claims['P131'][1]['mainsnak']['datavalue']['value']['id']
    municipality = mw.wikibase.getLabelByLang(munid, langcode) or mw.wikibase.getLabelByLang(munid, wiki.langcode)
    if municipality then municipality = string.format('[[:d:%s|%s]]', munid, municipality) end
  end
  if not municipality then municipality = 'N/A' end
  local location = entity.claims['P276']
  if location then
    local locid = entity.claims['P276'][1]['mainsnak']['datavalue']['value']['id']
    location = mw.wikibase.getLabelByLang(locid, langcode) or mw.wikibase.getLabelByLang(locid, wiki.langcode)
    if location then municipality = string.format('%s, [[:d:%s|%s]]', municipality, locid, location) end
  end
  local directions = entity.claims['P2795']
  local address = nil
  local address_fallback = ''
  if directions then
    for i,v in ipairs(directions) do
      if v['mainsnak']['datavalue']['value']['language'] == langcode then address = v['mainsnak']['datavalue']['value']['text'] end
      if v['mainsnak']['datavalue']['value']['language'] == wiki.langcode then address_fallback = v['mainsnak']['datavalue']['value']['text'] end
    end
  end
  if not address then address = address_fallback end
  if #address ~= 0 then municipality = municipality .. ', ' end

  local coords_text = '<span title="N/A" style="padding-left:4px; padding-right:6px;">[[File:Map pin icon.svg|12px|link=]]</span>'
  local coords = entity.claims['P625']
  if coords then
    coords = coords[1]['mainsnak']['datavalue']['value']
    coords_text = string.format('<span title="%s" style="padding-left:2px;"><maplink group="listing" text="" latitude="%s" longitude="%s" zoom="17">{"type":"Feature","properties":{"title":"%s","description":"%s<br>[[File:%s|border|150x150px|center|%s]]","marker-symbol":"%s","marker-color":"0050d0","marker-size":"medium"},"geometry":{"type":"Point","coordinates":[%s,%s]}}</maplink></span>', mw.wikibase.getLabelByLang('P625', langcode) or mw.wikibase.getLabelByLang('P625', wiki.langcode), coords.latitude, coords.longitude, name.value, knid.rawvalue, image, image, map_marker, coords.longitude, coords.latitude)
  end

  local description = language:ucfirst(typ_label) .. '. '
  local text_color = 'black'
  local inception = entity:formatPropertyValues('P571')
  inception.label = mw.wikibase.getLabelByLang('P571', langcode) or mw.wikibase.getLabelByLang('P571', wiki.langcode)
  if #inception.value ~= 0 then description = description .. string.format('<span title="%s">%s</span>%s ', inception.label, inception.value, string.byte(inception.value, -1) == 46 and '' or '.') end
  
  local loss = entity:formatPropertyValues('P576')
  if #loss.value ~= 0 then
    text_color = 'gray'
    loss.label = mw.wikibase.getLabelByLang('P576', langcode) or mw.wikibase.getLabelByLang('P576', wiki.langcode)
    description = description .. string.format('<span title="%s">%s: %s</span>.', loss.label, (i18n.labels[langcode] or i18n.labels[wiki.langcode])["Lost"], loss.value)
  end

  local refs = entity.claims['P1435'][1]['references']
  if refs then
    for i,v in ipairs(refs) do
      p_key, p_val = next(v['snaks'])
      if p_key == 'P854' or p_key == 'P4656' then description = description .. string.format('<ref name="%s">%s</ref>', p_val[1]['datavalue']['value'], p_val[1]['datavalue']['value']) end
      if p_key == 'P248' then description = description .. string.format('<ref name="%s">[[:d:%s|%s]]</ref>', p_val[1]['datavalue']['value']['id'], p_val[1]['datavalue']['value']['id'], mw.wikibase.getLabelByLang(p_val[1]['datavalue']['value']['id'], langcode) or mw.wikibase.getLabelByLang(p_val[1]['datavalue']['value']['id'], wiki.langcode)) end
    end
  end

  local upload_link = string.format('https://commons.wikimedia.org/w/index.php?title=Special:UploadWizard&campaign=%s&id=%s&id2=&description=%s&categories=%s', 'wlm-ru', mw.uri.encode(knid.rawvalue), 'description', 'cat')

  return frame:preprocess(string.format([==[
{| class="monument" border=0 style="font-size:97%%; width:100%%; color:%s" 
|- valign="top"
| width="160px" rowspan="2" | {{Якорь|%s}}[[File:%s|border|150x150px|center|%s]]
| style="background-color:#F8F8F8; padding-left:10px;" valign="middle" rowspan="2" | %s <span class="monument-name" style="font-size:115%%; font-weight:bold">%s</span><br>%s%s%s<br>%s
| style="text-align:left; width: 150px; background-color:#F0E68C; font-size: 93%%; padding:10px;" valign="top" | <abbr title="%s" class="plainlinks">[[File:Wikivoyage-logo.svg|15px|link=]]&nbsp;%s</abbr><br><abbr title="%s" class="plainlinks">[[File:Ministry_of_Culture_RF.png|15px|link=]]&nbsp;%s</abbr><br><abbr title="%s">[[File:Wikidata-logo.svg|15px|link=]]&nbsp;[[:d:%s|<span title="%s">%s</span>]]</abbr>
|-
| style="text-align:center; background-color:#FFFACD;" | <span class="plainlinks">[%s %s]</span>&nbsp;[[File:Wiki_Loves_Monuments_Logo_notext.svg|30px|%s|link=%s]]
|-
|}
]==], text_color, knid.rawvalue, image, image, typ, name.value, coords_text, municipality, address, description, knid.label, knid.value, knidnew.label, knidnew.value, '{{int:wikibase-listdatavaluetypes-name-wikibase-entityid}}', id, '{{int:wikibase-listdatavaluetypes-name-wikibase-entityid}}', id, upload_link, (i18n.labels[langcode] or i18n.labels[wiki.langcode])["Upload photo"], (i18n.labels[langcode] or i18n.labels[wiki.langcode])["Upload photo"], upload_link))

end

return p