Перейти к содержанию

Модуль:Natural monument wikidata

Материал из Wikivoyage

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

local p = {}

function p.monument(frame)
  local markergroup = {['Q46169'] = 'see', ["Q728904"] = 'see', ['Q16653233'] = 'do', ['Q7212557'] = 'go', ['Q23790'] = 'buy', ['Q167346'] = 'eat', ['Q272231'] = 'sleep', ['Q179049'] = 'vicinity'}
  local pArgs = frame:getParent().args
  local lang = pArgs['lang'] or 'ru' 
  local country = pArgs['country']

  local translations = mw.loadData("Module:Natural monument wikidata/translation")

  local llanguage = 'en'
  local campaign = ''
  local noupload = 'yes'
  local dcat = ''

  function list_check(t, x)
    for _, y in ipairs(t) do
      if x == y then
        return true
      end
    end
    return false
  end

  function find_by_wdid(t, wd)
    for a, b in ipairs(t) do
       if b.wdid == wd then
          return a
       end
    end
    return nil
  end

  local countriesinfo = mw.ext.data.get("Sandbox/Bok/config.tab")
  for i, v in ipairs(countriesinfo.data) do
    if v[1] == country then
      llanguage = v[2]
      campaign = v[3]
      noupload = v[5]
      dcat = v[4]  
    end
  end  

  local mwl = mw.language.new( lang )
  local sort = pArgs['sort'] or 'no'
  local sections = pArgs['sections'] or ''
   
  local wdids = pArgs['wdid']
  wdids = mw.text.split( wdids, ",", true)
  local wdids_for_map = ""
  
  values = {}
  
  for i, wdid in ipairs(wdids) do
    wdids_for_map = wdids_for_map .. "wd:" .. wdid .. " "
    values[i] = {}
    local parts =  mw.wikibase.getBestStatements(wdid, 'P361')
    values[i]['complex'] = ''
    values[i]['main_complex'] = ''
    for j, complex in ipairs(parts) do
      if list_check(wdids, complex['mainsnak']['datavalue']['value']['id']) then
        values[i]['complex'] = complex['mainsnak']['datavalue']['value']['id']
      end
    end
	values[i]['wdid'] = wdid
    values[i]['name'] = mw.wikibase.getLabelByLang(wdid, lang) or mw.wikibase.getLabelByLang(wdid, "ru") or mw.wikibase.getLabelByLang(wdid, "en") or mw.wikibase.getLabelByLang(wdid, llanguage) or ''
    values[i]['wiki'] = mw.wikibase.getSitelink(wdid, lang .. 'wiki') or ''
    values[i]['wikivoyage'] = mw.wikibase.getSitelink(wdid, 'ruwikivoyage') or '' -- не переведен
    values[i]['wdpa'] = mw.wikibase.getBestStatements(wdid, 'P809')
    values[i]['wdpa'] = (values[i]['wdpa'][1] and values[i]['wdpa'][1]['mainsnak']['datavalue']['value']) or ''
    values[i]['image'] = mw.wikibase.getBestStatements(wdid, 'P18')
    values[i]['image'] = (values[i]['image'][1] and values[i]['image'][1]['mainsnak']['datavalue']['value']) or ''
    values[i]['commonscat'] = mw.wikibase.getBestStatements(wdid, 'P373')
    values[i]['commonscat'] = (values[i]['commonscat'][1] and values[i]['commonscat'][1]['mainsnak']['datavalue']['value']) or '' 
    local coords = mw.wikibase.getBestStatements(wdid, 'P625')
    values[i]['lat'] = (coords[1] and coords[1]['mainsnak']['datavalue']['value'].latitude) or ''
    values[i]['long'] = (coords[1] and coords[1]['mainsnak']['datavalue']['value'].longitude) or ''
    values[i]['area'] = mw.wikibase.getBestStatements(wdid, 'P2046')
	if values[i]['area'][1] then
	  local area_n = mwl:formatNum(tonumber(values[i]['area'][1]['mainsnak']['datavalue']['value']['amount']))
	  local p5061 = mw.wikibase.getBestStatements(string.gsub(values[i]['area'][1]['mainsnak']['datavalue']['value']['unit'], "http://www.wikidata.org/entity/", ""), 'P5061')
	  local area_u = ''
	  for k, v in ipairs(p5061) do
	    if (v['mainsnak']['datavalue']['value']['language'] == lang or (area_u == '' and v['mainsnak']['datavalue']['value']['language'] == 'en')) then
		  area_u = v['mainsnak']['datavalue']['value']['text']
		end
      end
      values[i]['area'] = area_n .. ' ' .. area_u	  
	else
	  values[i]['area'] = ''
	end
    values[i]['iucntype'] = mw.wikibase.getBestStatements(wdid, 'P814')
    values[i]['iucntype'] = (values[i]['iucntype'][1] and mw.wikibase.getLabelByLang(values[i]['iucntype'][1]['mainsnak']['datavalue']['value']['id'], lang)) or ''	
    local p17 = mw.wikibase.getBestStatements(wdid, 'P17')
    p17 = (p17[1] and p17[1]['mainsnak']['datavalue']['value']['id']) or ''
    values[i]['municipality'] = mw.wikibase.getBestStatements(wdid, 'P131') or ''
    if values[i]['municipality'] and values[i]['municipality'] ~= '' then
      values[i]['municipality'] = (values[i]['municipality'][1] and mw.wikibase.getLabelByLang(values[i]['municipality'][1]['mainsnak']['datavalue']['value']['id'], lang)) or ''
      values[i]['first_level'] = values[i]['municipality'] or ''
      local x = mw.wikibase.getBestStatements(wdid, 'P131') or ''
      x = x[1] and mw.wikibase.getBestStatements(x[1]['mainsnak']['datavalue']['value']['id'], 'P131')
      while x and next(x) and x[1]['mainsnak']['datavalue']['value']['id'] ~= p17 do
	    values[i]['first_level'] = mw.wikibase.getLabelByLang(x[1]['mainsnak']['datavalue']['value']['id'], lang) or ''
        values[i]['municipality'] =  values[i]['first_level'] .. ", " .. values[i]['municipality'] or ''
        x = mw.wikibase.getBestStatements(x[1]['mainsnak']['datavalue']['value']['id'], 'P131')
      end
    end
    values[i]['website'] = mw.wikibase.getBestStatements(wdid, 'P856')
    values[i]['website'] = (values[i]['website'][1] and values[i]['website'][1]['mainsnak']['datavalue']['value']) or '' 
    local p31 = mw.wikibase.getBestStatements(wdid, 'P31')
    values[i]['m_type'] = ''
    for k, v in ipairs(p31) do
      local x = v.mainsnak.datavalue.value.id
	  if x == 'Q46169' or x == 'Q16653233' or x == 'Q7212557' or x == 'Q23790' or x == 'Q167346' or x == 'Q272231' or x == 'Q179049' or x == 'Q728904' then
	    values[i]['m_type'] = x
	  end
    end
    if values[i]['m_type'] ~= '' then 
      values[i]['markergroup'] = markergroup[values[i]['m_type']] or 'other'
      values[i]['m_type'] = mw.wikibase.getLabelByLang(values[i]['m_type'], lang) or ''
    end   
    if values[i]['markergroup'] == '' then 
      values[i]['markergroup'] = 'other'
    end
	local p1435 = mw.wikibase.getBestStatements(wdid, 'P1435')
	values[i]['unesco'] = ''
    values[i]['unesco_actual'] = ''
	values[i]['ramsar'] = ''
	for k, v in ipairs(p1435) do
	    local x = v.mainsnak.datavalue.value.id
	  	if x == 'Q19683138' then
		  values[i]['ramsar'] = mw.wikibase.getLabelByLang(x, lang) or ''
		end
		if x == 'Q9259' or x == 'Q43113623' or x == 'Q1459900' then
          if x == 'Q9259' or x =='Q43113623' then
            values[i]['unesco_actual'] = 'true'
          end
		  values[i]['unesco'] = mw.wikibase.getLabelByLang(x, lang) or ''
		  if x == 'Q43113623' and v['qualifiers']['P361'] then
		    values[i]['unesco'] = values[i]['unesco'] .. ' «' .. (mw.wikibase.getLabelByLang(v['qualifiers']['P361'][1]['datavalue']['value']['id'], lang) or '') .. '»'
		  end 
		end
	end
    values[i]['first_level_sort'] = values[i]['first_level']
    values[i]['name_sort'] = values[i]['name']
  end

  local t = frame:extensionTag{ name = 'maplink', content = [=[ {
  "type": "ExternalData",
  "service": "geoshape",
  "query": "SELECT ?id (?idLabel as ?title) (sample(?image) as ?image) ('#88ff88' as ?fill) ('' as ?description) WHERE {VALUES ?id {]=] .. wdids_for_map .. [=[} OPTIONAL { ?id wdt:P18 ?image. } SERVICE wikibase:label { bd:serviceParam wikibase:language ']=] .. lang .. [=[' . ?id rdfs:label ?idLabel.} } GROUP BY ?id ?idLabel" }]=], args = { ["class"] = "no-icon", ["text"] = "", ["group"] = "other" } } .. '\n'

  for i, m in ipairs(values) do
    if m.complex and m.complex ~= '' then
      local cm = find_by_wdid(values, m.complex)
      if cm and cm ~= '' then
        values[i].first_level_sort = values[cm].first_level_sort
        values[i].name_sort = values[cm].name_sort .. '#' .. m.name_sort
        values[cm].main_complex = 'true'
      end
    end
  end
  

  local csection = ''
  if sort and sort == "yes" then
    function comp(a, b)
	  if a.first_level_sort < b.first_level_sort or (a.first_level_sort == b.first_level_sort and a.name_sort < b.name_sort) then
	    return true
	  else
	    return false
	  end
	end
    table.sort( values, comp )
  end
  
  for i, m in ipairs(values) do
    if sections and sections == 'yes' and m['first_level_sort'] ~= csection then
      t = t .. "\n== " .. m['first_level_sort'] .. " =="
	  csection = m['first_level_sort']
	end
-- Шапка
   t = t .. "\n" .. [[{| id=]] .. m.wdid .. [[ class="monument" border=0 style="font-size:97%;width:100%;"
 |- valign="top" ]]
 
-- Изображение
    local imagelink = '[[File:Natural landmark without photo.svg|border|150x150px|center|' .. translations['nophotoLabel'][lang] ..']]'
    if m.image and m.image ~= '' then
      imagelink = string.format('[[File:%s|150x150px|center|%s]]', m.image, m.name or translations['photoLabel'][lang]) 
    end 
    t = t .. '\n| width="160px" | ' .. imagelink 
  
-- Название
    local background_color = ''
    if m.main_complex and m.main_complex == 'true' then
      background_color = "#BAFFC1;"
    elseif m.complex and m.complex ~= '' then
      background_color = "#E1FFE4;"
    else
      background_color = "#F8F8F8;"
    end
    t = t .. '\n| style=" background-color:' .. background_color.. ' padding-left:10px;" valign="middle" | '
    if m.unesco_actual and m.unesco_actual == 'true' then
      t = t .. "[[File:Swedish world heritage sign.PNG|18px|link=]] "
    end
    if m.main_complex and m.main_complex == 'true' then
      t = t .. '[[File:Location dot darkslategray.svg|10px|' .. translations['complexLabel'][lang] .. '|link=]]&nbsp;'
    end
    t = t .. '<span class="monument-name" style="font-size:115%; font-weight:bold">'
    if m.wikivoyage and m.wikivoyage ~= '' then
      t = t .. '[[File:Wikivoyage-logo.svg|15px]]&nbsp;[[' .. m.wikivoyage .. '|' .. m.name .. ']]'
    else
      t = t .. m.name
    end
    t = t .. '</span><br>'
  
-- Расположение:
    t = t .. "''" .. translations['locationLabel'][lang] .. ":'' " .. (m.municipality or '')
  
-- Площадь:
    if m.area and m.area ~= '' then
      t = t.. string.format("&nbsp;&nbsp;&nbsp;&nbsp;''" .. translations['areaLabel'][lang] .. ":'' %s", m.area)
    end
    t = t .. '<br>'

-- Ссылки
    if m.lat and m.long and m.lat~= '' and m.long ~= '' then
      t = t .. "''" .. translations['mapLabel'][lang] .. ":''&nbsp;"
      t = t .. frame:expandTemplate{ title = 'marker', args = { ["lat"] = m.lat, ["long"] = m.long, ["name"] = mw.text.encode(m.name), ["image"] = m.image, ["type"] = m.markergroup or 'other' } }
      t = t .. "&nbsp;&nbsp;&nbsp;&nbsp;"
    end

    t = t .. "''" .. translations['linksLabel'][lang] .. ":''&nbsp;"
    if m.wiki and m.wiki ~= '' then
      t = t .. '&nbsp;[[File:Wikipedia-logo-v2.svg|19px|link=//' .. lang .. '.wikipedia.org/wiki/' .. mw.uri.encode(m.wiki, "PATH") .. '|' .. translations['wikipediaLabel'][lang] .. ']]'
    end
    if m.commonscat and m.commonscat ~= '' then
      t = t .. '&nbsp;[[File:Commons-logo.svg|13px|link=//commons.wikimedia.org/wiki/Category:' .. mw.uri.encode(m.commonscat, "PATH") .. '|' .. translations['commonsLabel'][lang] .. ']]'
    end
    if m.wdid and m.wdid ~= '' then
      t = t .. '&nbsp;[[File:Wikidata-logo-without-paddings.svg|18px|link=//www.wikidata.org/wiki/' .. mw.uri.encode(m.wdid, "PATH") .. '|' .. translations['wikidataLabel'][lang] .. ']]'
    end
    if m.wdpa and m.wdpa ~= '' then
      t = t .. '&nbsp;[[File:P geography 3 b.png|22px|link=//protectedplanet.net/' .. mw.uri.encode(m.wdpa, "PATH") .. '|' .. translations['wdpaLabel'][lang] .. ']]'
    end
    if m.website and m.website ~= '' then
      t = t .. '&nbsp;[[File:P geography 3 b.png|22px|link=' .. m.website .. '|' .. translations['siteLabel'][lang] .. ']]'
    end
    t = t .. "<br>"
   
-- Описание
    t = t .. "''" .. translations['descriptionLabel'][lang] .. ":'' "
    if m.iucntype ~= "" then
      t = t .. string.gsub(m.iucntype, ": ", " (") .. '). ' 
    elseif m.m_type ~= "" then 
      t = t .. m.m_type .. '. '	
    end
    if m.ramsar and m.ramsar ~= "" then
      t = t .. mwl:ucfirst(m.ramsar) .. '. '
    end  
    if m.unesco and m.unesco ~= "" then
      t = t .. mwl:ucfirst(m.unesco) .. '. '
    end 
 
-- Ссылка на загрузку
    if noupload ~= 'yes' then
      t = t .. '\n| class="plainlinks" style="width:10%; text-align:center; vertical-align:middle; background-color:#FFFACD; padding:0 1em" '
      if not m.commonscat or m.commonscat == '' then
        m.commonscat = dcat or ''
      end
      t = t .. "| [//commons.wikimedia.org/w/index.php?title=Special:UploadWizard&campaign=" .. mw.uri.encode(campaign, "PATH")
      t = t .. [[&id=]] .. mw.uri.encode(m.wdid or '', "PATH")
      t = t .. [[&caption=]] .. mw.uri.encode((m.name .. ": " .. m.municipality), "PATH")
      t = t .. [[&description=]] .. mw.uri.encode((m.name .. ": " .. m.municipality), "PATH")
      t = t .. [[&categories=]] .. mw.uri.encode(m.commonscat, "PATH")
      t = t .. '&uselang=' .. lang .. ' <span class="mw-ui-button">' .. translations['uploadLinkLabel'][lang] .. '</span>]'
    end
    t = t .. "\n|}<hr>"
  end	
  
  return t
end

return p;