Moduuli:Viralliset kotisivut

Wikipediasta
Siirry navigaatioon Siirry hakuun



local p = {}

local function addLinkback(str, id, property)
	if not id then
		id = mw.wikibase.getEntityIdForCurrentPage()
	end
	if not id then
		return str
	end
	if type(property) == 'table' then
		property = property[1]
	end
	if type(id) == 'table' then
		id = id.id
	end
	
	local class = ''
	if property then
		class = 'wd_' .. string.lower(property)
	end
	local title = "View and modify data on Wikidata"
	local icon = '[%s [[File:Blue pencil.svg|%s|10px|baseline|link=]] ]'
	local url = mw.uri.fullUrl('d:' .. id) -- removed queryparameter "uselang=fr"
	url.fragment = property
	url = tostring(url)
	local v = mw.html.create('span')
		:addClass(class)
		:wikitext(str)
		:tag('span')
			:addClass('noprint plainlinks wikidata-linkback')
			:css('padding-left', '.5em')
			:wikitext(icon:format(url, title))
		:allDone()
	return tostring(v)
end


-- Print p as json
local function dumpJson(p)
    return mw.text.jsonEncode(p)
end

-- Read arguments as flat array
local function parseArgs(frame)
	local args = {}
	for key, value in pairs(frame:getParent().args) do args[key] = value end
	for key, value in pairs(frame.args) do args[key] = value end
	return args
end

-- Get default lang => url pair
local function getBestLangUrl(langurls)

   -- Q1412 = "Finnish"
   if langurls["Q1412"] then
       return langurls["Q1412"]

   -- Q9027 = "Swedish"
   elseif langurls["Q9027"] then
       return langurls["Q9027"]

   -- Q1860 = "English"
   elseif langurls["Q1860"] then
       return langurls["Q1860"]
   else
       -- Default, return just first url
       for languagename, languagevalue in pairs(langurls) do
           return languagevalue
       end
   end
   return ""
end

-- Return P856 values in format
-- { "Q9027" : "https://ateneum.fi/sv", "Q1412" :"https://ateneum.fi/fi", "Q1860": "https://ateneum.fi/en" }

local function getP856url(entity_id)
   local urls=mw.wikibase.getBestStatements(entity_id, "P856")
   local ret={}
   for p, claim in pairs(urls) do
       url=claim.mainsnak.datavalue.value
       if claim.qualifiers then 
           for q, qualifier in pairs(claim.qualifiers) do
               if qualifier[1]["snaktype"]=="value" 
                 and qualifier[1]["datatype"]=="wikibase-item"
                 and qualifier[1]["property"]=="P407" 
                 then
                       lang=qualifier[1]["datavalue"]["value"]["id"] or "default"
               else
                   lang="default"  
               end
               if lang and url then
                   ret[lang]=url
               end
           end
       else
           ret["default"]=url
       end 
   end
   return ret
end


-- Remove unneeded prefixes, cut string to maxlength 

local  function makeWikiUrlWithShortLabel(url, maxlength)
    local shortLabel = url
    local replaceList = {"https://www.", "http://www.", "https://", "http://", "/$"}

    for t, replaceText in pairs(replaceList) do
        shortLabel=string.gsub(shortLabel, replaceText, "")
    end

    if string.len(shortLabel)> maxlength then
        shortLabel=string.sub(shortLabel, 1, maxlength) .."..."
    end

    local ret="[" .. url .." " .. shortLabel .."]"
    return ret
end

-- Externally called function
-- {{#invoke:Viralliset kotisivut|getUrl|entity_id=Q33}}

function p.getUrl(frame)
   args=parseArgs(frame)
   local ret=""
   local defaultvalue=mw.text.trim(args[1] or '')
   local entity_id = args['entity_id'] or mw.wikibase.getEntityIdForCurrentPage()

   if defaultvalue=="-" then
       return ""
   end

   if defaultvalue~='' then
       return defaultvalue
   end

   if not entity_id then
      return ""
   end

   local langurls = getP856url(entity_id)
   local bestLangUrl = getBestLangUrl(langurls)
   if bestLangUrl ~= '' then
       local wikiUrlWithShortLabel=makeWikiUrlWithShortLabel(bestLangUrl, 30)
       local str = wikiUrlWithShortLabel .. "[[Luokka:Artikkelit, joiden virallinen kotisivu tulee Wikidatasta]]"
       ret = addLinkback(str, entity_id, "P856")
   end

   return ret
end

return p