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

Материал из Wikivoyage

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

local lang_list = {'en', 'de', 'ru', 'pl', 'uk', 'es', 'fr', 'it', 'he', 'fa', 'el', 'fi', 'nl', 'zh', 'pt', 'ro', 'sv', 'vi'}
local wikis = {voy = 'wikivoyage', w = 'wiki'}

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

local p = {}

function p.table(frame)
  if not wikis[frame.args[1]] then error('Unknown interwiki id') end
  local title = mw.wikibase.getEntity(frame.args[2])
  local language = mw.getLanguage(wiki.langcode)
  local commonscat = nil
  local lang_cnt = {}
  if title.claims['P373'] then commonscat = title.claims['P373'][1]['mainsnak']['datavalue']['value'] end
  if commonscat then commonscat = 'Category:' .. commonscat else commonscat = title:getSitelink('commonswiki') end
  if commonscat then commonscat = string.format('[[File:Commons-logo.svg|13px|link=//commons.wikimedia.org/wiki/%s]]', mw.uri.encode(commonscat, "PATH")) else commonscat = '' end
  local result = string.format('{|class="wikitable"\n|-\n! [[:d:%s|%s]] !! %s ', frame.args[2], title.labels[wiki.langcode].value, commonscat)
  for j,l in ipairs(lang_list) do
    local sitelink = title:getSitelink(l .. wikis[frame.args[1]])
    if sitelink then sitelink = string.format('[[:%s:%s:%s|%s]]', frame.args[1], l, sitelink, l) else sitelink = l end
    lang_cnt[l] = 0
    result = result .. ' !! ' .. sitelink
  end
  result = result .. ' !! Σ\n|-\n'
  local i = 3

  while frame.args[i] do
    local entity = mw.wikibase.getEntity(frame.args[i])
    local name = entity.labels[wiki.langcode]
    local commons = nil
    if entity.claims['P373'] then commons = entity.claims['P373'][1]['mainsnak']['datavalue']['value'] end
    if commons then commons = 'Category:' .. commons else commons = entity:getSitelink('commonswiki') end
    if commons then commons = string.format('[[File:Commons-logo.svg|13px|link=//commons.wikimedia.org/wiki/%s|Commons category]]', mw.uri.encode(commons, "PATH")) else commons = '' end
    result = result .. string.format('| [[d:%s|%s]] || %s ', frame.args[i], name.value, commons) 
    local row_sum = 0
    for j,l in ipairs(lang_list) do
      local sitelink = entity:getSitelink(l .. wikis[frame.args[1]])
      if sitelink then
        result = result .. string.format(' || [[:%s:%s:%s|+]]', frame.args[1], l, sitelink)
        row_sum = row_sum + 1
        lang_cnt[l] = lang_cnt[l] + 1
      else 
        result = result .. ' || - ' 
      end
    end
    result = result .. string.format('|| %d \n|-\n', row_sum)
    i = i + 1
  end

  local row_sum = 0
  result = result .. "\n|-\n| '''Σ''' || "
  for j,l in ipairs(lang_list) do
    result = result .. string.format(' || %s', lang_cnt[l])
    row_sum = row_sum + lang_cnt[l]
  end
  result = result .. string.format("|| '''%d'''\n|-\n|}\n", row_sum)
  return result
end

return p