Tämä moduuli on osittain suojattu muutoksilta.

Moduuli:Verkkoviite

Wikipediasta
Siirry navigaatioon Siirry hakuun

host_from_url()

Funktio palauttaa url-osoitteen host-osan {{verkkoviite}}-mallineessa käytettäväksi.. Mikäli palautettava teksti alkaa sanalla "www.", niin tämä poistetaan palautettavasta tekstistä.

Parametrit
  • url = url-osoite, josta palautettava arvo luetaan jos site tai publisher -parametreja ei ole määritelty
  • site = teksti joka näytetään jos se on määritelty
  • publisher = mikäli tässä on jokin arvo, niin ei yritetä hakea automaattista arvoa url-parametrista

Esimerkit

url-parametri täytetty, site ja publisher tyhjiä
  • {{#invoke:verkkoviite|host_from_url|url=https://fi.wikipedia.org/wiki/Moduuli:Verkkoviite|site=|publisher=}}
  • fi.wikipedia.org
url-alkaa tekstillä www
  • {{#invoke:verkkoviite|host_from_url|url=https://www.wikipedia.org/wiki/Moduuli:Verkkoviite|site=|publisher=}}
  • wikipedia.org
url ja site -parametrit täytetty, publisher tyhjä
  • {{#invoke:verkkoviite|host_from_url|url=https://fi.wikipedia.org/wiki/Moduuli:Verkkoviite|site=site-arvo|publisher=}}
  • site-arvo
url, site ja publisher -parametrit täytetty
  • {{#invoke:verkkoviite|host_from_url|url=https://fi.wikipedia.org/wiki/Moduuli:Verkkoviite|site=site-arvo|publisher=publisher-arvo}}
  • site-arvo
url on tyhjä, mutta site-parametri on määritelty
  • {{#invoke:verkkoviite|host_from_url|url=|site=site-arvo|publisher=}}
  • site-arvo
url ja publisher -parametrit täytetty. Site-parametri on tyhjä
  • {{#invoke:verkkoviite|host_from_url|url=https://fi.wikipedia.org/wiki/Moduuli:Verkkoviite|site=|publisher=publisher-arvo}}
url ja site ovat tyhjiä
  • {{#invoke:verkkoviite|host_from_url|url=|site=|publisher=}}
url on virheellinen
  • {{#invoke:verkkoviite|host_from_url|url=https:www.wikipedia.org/wiki/Moduuli:Verkkoviite|site=|publisher=}}

local t={}

-- Palauttaa url-osoitteen perusteella arvon sivusto-parametrille
local function get_host_from_url(url, site, publisher)

	-- Mikäli site on määritelty, niin käytetään sitä   
	if site ~= "" then
		return site
	end

	-- Mikäli publisher on määritelty, niin ei tarvita host:ia kertomaan lähdettä   
	if publisher ~= "" then
		-- varmaankin ideana oli palauttaa publisher tässä
		-- vai onko se jätetty pois jostain mystisestä syystä? bugi vai feature?
		return ""
	end

	if url == "" then
		return ""
	end

	local u = mw.uri.new(url)
   
	-- poistetaan "www" alusta
	local ret = ""
	if u.host ~= nil then
		ret = string.gsub( u.host, "^www.", "", 1 )
	end
	return ret
end

-- public interface for currently existing template usage
function t.host_from_url(frame)
   local url=frame.args.url or "";
   local site=frame.args.site or "";   
   local publisher=frame.args.publisher or ""

   return get_host_from_url(url, site, publisher)
end


-- patterns copied from template Onko päiväys ISO 8601 -muodossa?
local function istimeinISO( datestring )
	if not datestring then
		return false
	end
	local result
	result = mw.ustring.match( datestring, "^%s*-?%d%d%d%d%-%d%d%-%d%d%s*$", 1 ) -- 2016-05-05
	if result then 
		return true
	end
	result = mw.ustring.match( datestring, "^%s*-?%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d:%d%d?Z%s*$", 1 ) -- 2016-05-05T10:09:04Z
	if result then 
		return true
	end
	result = mw.ustring.match( datestring, "^%s*-?%d%d%d%d%d%d%d%dT%d%d%d%d%d%d?Z%s*$", 1 ) -- 20160505T100904Z
	if result then 
		return true
	end
	return false
end

local function localizedate( datestring )
	local tmp = datestring
	-- check for non-breakable spaces
	tmp = tmp:gsub( '\194\160', ' ' ):gsub( ' ', ' ' )

	local lang = mw.language.getContentLanguage()
	-- same as #time parser
	return lang:formatDate( "j.n.Y", tmp, true )
end

local function encbrackets( s )
	local tmp = s
	if mw.ustring.find(tmp, '%]') then
		tmp = mw.ustring.gsub( tmp, '%]', "]" )
	end
	if mw.ustring.find(tmp, '%[') then
		tmp = mw.ustring.gsub( tmp, '%[', "[" )
	end
	return tmp
end

local function formatpubmedid( pmid )
	-- some extra validation that id is a number
	local num = tonumber(pmid)
	if num ~= nil then
		return "PubMed:[https://pubmed.ncbi.nlm.nih.gov/".. pmid .. " " .. pmid .."]"
	end
	-- could just give error instead?
	return "PubMed: <span class='error'>Virhe: ".. pmid .."</span> [[Luokka:Sivut, joissa on virheellinen PubMed-tunniste]]"
end

local function formatdoi( doiid )
	local url = '//dx.doi.org/' .. mw.uri.encode( doiid ) 
	local htmlenc = mw.text.encode(doiid)
	if mw.ustring.find(htmlenc, '%]') then
		-- check: what was this case for? should just give error?
		htmlenc = mw.ustring.gsub( htmlenc, '%]', "&rsqb;" )
	end
	return "[[Digital object identifier|doi]]:[" .. url .. " " .. htmlenc .."]"
end

-- Semantic Scholar Corpus ID
local function formats2cid( s2cid )
	-- just a number
	local num = tonumber(s2cid)
	if num ~= nil then
		return "S2CID:[https://api.semanticscholar.org/CorpusID:".. s2cid .. " " .. s2cid .."]"
	end
	-- could just give error instead?
	return "S2CID:<span class='error'>Virhe: ".. s2cid .."</span> [[Luokka:Sivut, joissa on virheellinen S2CID-tunniste]]"
end

local function formatjstor(jstor)
	-- should be number only?
	local jstorid = mw.uri.encode( jstor )
	return '[[JSTOR]]:[http://www.jstor.org/stable/' .. jstorid .. ' ' .. jstorid .. ']'
end

local function formatisbn(isbn)
	return '[[Toiminnot:Kirjalähteet/' .. isbn .. '|ISBN&nbsp;' .. isbn ..']]'
	
	-- change module: can't call with frame, needs access to local method
	--local Isxn = require('Moduuli:ISxN')
	--if Isxn.check_isbn() then
		-- format as valid ISBN
		--return '[[Toiminnot:Kirjalähteet/' .. isbn .. '|ISBN&nbsp;' .. isbn ..']]'
	--end
	-- give error if invalid
	--return "<span class='error'>Virhe: ISBN " .. isbn .. "</span>"
end

local function formatlanguagecode( kielinimi )
	local str = "<span style='font-size: 0.95em; color:#555; position: relative;'>(" .. kielinimi .. ")</span>"
	return str
end

local function verkkoviite( args )
	local wiki = {}	
	local function insert( value )
		wiki[#wiki+1] = value
	end
	insert('<span class="verkkoviite" title="Verkkoviite">')

	local author = args['Tekijä'] or args['author'] or args['tekijä'] or ""
	if author ~= "" then
		insert(author)
		insert(":&#32;")
	else
		local numeroton = false
		local sukunimi = args['Sukunimi'] or args['sukunimi'] or args['last'] or ""
		local etunimi = args['Etunimi'] or args['etunimi'] or args['first'] or ""
		if sukunimi  ~= "" and etunimi  ~= "" then
			insert(sukunimi)
			insert(", ")
			insert(etunimi)
			numeroton = true
		elseif sukunimi ~= "" then
			insert(sukunimi)
			numeroton = true
		elseif etunimi  ~= "" then
			insert(etunimi)
			numeroton = true
		end

		-- numeroidut rivit (jos käännös toisenkielistä wikistä)
		local numeroitu = false
		local rivinro = 1
		while (rivinro <= 9) do
			local numero = tostring(rivinro)
			sukunimi = args['Sukunimi' .. numero] or args['sukunimi' .. numero] or args['last' .. numero] or ""
			etunimi = args['Etunimi' .. numero] or args['etunimi' .. numero] or args['first' .. numero] or ""

			if sukunimi  ~= "" and etunimi  ~= "" then
				if (numeroton == true or rivinro > 1) then
					-- päätä edellinen ennen seuraavaa nimeä
					insert(';&#32;')
				end
				insert(sukunimi)
				insert(", ")
				insert(etunimi)
				numeroitu = true
			end
			rivinro = rivinro +1
		end
		if (numeroton == true or numeroitu == true) then
			-- päätä tekijä(t)
			insert(":&#32;")
		end
	end

	local url = args['Osoite'] or args['url'] or args['osoite'] or args['URL'] or ""
	local archiveurl = args['Arkisto'] or args['archiveurl'] or args['arkisto'] or ""
	local title = args['Nimeke'] or args['Nimike'] or args['title'] or args['Otsikko'] or args['nimeke'] or args['nimike'] or args['otsikko'] or args['Title'] or ""
	if url ~= "" then
		if title ~= "" then
			insert('[')
			insert(url)
			insert(' ')
			insert(title)
			insert(']')
		else
			insert("<span style='color:red; font-weight:bold;'>Määritä nimeke!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]]")
		end
	elseif archiveurl ~= "" then
		-- archived url without a live url
		if title ~= "" then
			insert(title)
		else
			insert("<span style='color:red; font-weight:bold;'>Määritä nimeke!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]]")
		end
	else
		insert("<span style='color:red; font-weight:bold;'>Määritä osoite!</span>[[Luokka:Sivut, joiden viitemallineissa on virheitä]]")
	end

	local tiedostomuoto = args['Tiedostomuoto'] or args['format'] or args['tiedostomuoto'] or ""
	if tiedostomuoto ~= "" then
		insert("&#32;<small>(")
		insert(tiedostomuoto)
		insert(")</small>")
	end

	local selite = args['Selite'] or args['selite'] or ""
	if selite ~= "" then
		insert("&nbsp;(")
		insert(selite)
		insert(")")
	end

	-- same conditions as earlier
	local sitehost = args['Julkaisu'] or args['Sivusto'] or args['site'] or args['work'] or args['julkaisu'] or args['sivusto'] or ""
	local julkaisija = args['Julkaisija'] or args['publisher'] or args['julkaisija'] or ""
	local publisherfromurl = get_host_from_url(url, sitehost, julkaisija)
	if publisherfromurl ~= "" then
		insert("&#32;<i>")
		insert(publisherfromurl)
		insert("</i>.")
	end

	local ajankohta = args['Ajankohta'] or args['date'] or args['ajankohta'] or ""
	if ajankohta ~= "" then
		if istimeinISO(ajankohta) then
			ajankohta = localizedate(ajankohta)
		end
		insert('&#32; ' .. ajankohta .. '.')
	end

	local julkaisupaikka = args['Julkaisupaikka'] or args['julkaisupaikka'] or ""
	if julkaisupaikka ~= "" then
		insert("&#32;")
		insert(julkaisupaikka)

		-- semicolon if place is followed by publisher, otherwise dot
		if julkaisija ~= "" then
			insert("&#58;&#32;")
		else
			insert(".")
		end
	end
	-- publisher
	if julkaisija ~= "" then
		insert("&#32;")
		insert(julkaisija)
		insert(".")
	end

	if archiveurl ~= "" then
		insert("&#32;[")
		insert(archiveurl)
		insert(' Arkistoitu]')
		local arkistoaika = args['Arkistoitu'] or args['archivedate'] or args['arkistoitu'] or ""
		if arkistoaika ~= "" then
			if istimeinISO(arkistoaika) then
				arkistoaika = localizedate(arkistoaika)
			end
			insert(' ' .. arkistoaika .. '.')
		end
	end

	local lainaus = args['Lainaus'] or args['quote'] or args['lainaus'] or ""
	if lainaus ~= "" then
		--<span class="citation">...</span>
		insert('&#32;”')
		insert(lainaus)
		insert('”')
	end

	local pmid = args['Pmid'] or args['pmid'] or ""
	if pmid ~= "" then
		local pubmedlink = formatpubmedid(pmid)
		if pubmedlink ~= "" then
			insert("&#32;")
			insert(pubmedlink)
			insert('.&#32;')
		end
	end
	local doi = args['Doi'] or args['doi'] or ""
	if doi ~= "" then
		local doilink = formatdoi(doi)
		if doilink ~= "" then
			insert("&#32;")
			insert(doilink)
			insert('.&#32;')
		end
	end
	local jstor = args['Jstor'] or args['jstor'] or ""
	if jstor ~= "" then
		local jstorlink = formatjstor(jstor)
		if jstorlink ~= "" then
			insert("&#32;")
			insert(jstorlink)
			insert('.&#32;')
		end
	end

	local s2cid = args['S2CID'] or args['S2cid'] or ""
	if s2cid ~= "" then
		local s2cidlink = formats2cid(s2cid)
		if s2cidlink ~= "" then
			insert("&#32;")
			insert(s2cidlink)
			insert('.&#32;')
		end
	end

	local isbn = args['Isbn'] or args['ISBN'] or ""
	if isbn ~= "" then
		local isbnlink = formatisbn(isbn)
		if isbnlink ~= "" then
			insert("&#32;")
			insert(isbnlink)
			insert('.&#32;')
		end
	end

	local viitattu = args['Viitattu'] or args['Luettu'] or args['accessdate'] or args['viitattu'] or args['luettu'] or ""
	if viitattu ~= "" then
		if istimeinISO(viitattu) then
			viitattu = localizedate(viitattu)
		end
		insert(' Viitattu ' .. viitattu .. '.')
	end

	local kieli = args['Kieli'] or args['language'] or args['kieli'] or ""
	local ietfkielikoodi = args['ietf-kielikoodi'] or ""
	if kieli ~= "" then
		insert('&#32;')
		insert(kieli)
	elseif ietfkielikoodi ~= "" then
		ietfkielikoodi = ietfkielikoodi:lower()
		if ietfkielikoodi == "fi" or ietfkielikoodi == "fin" or ietfkielikoodi == "fi-fi" then
			-- |fi|fin|fi-fi =  -> no change, omitted
		elseif ietfkielikoodi == "en" or ietfkielikoodi == "en-en" then
			-- |en|en-en = {{en}}
			insert("&#32;")
			insert(formatlanguagecode('englanniksi'))
		elseif ietfkielikoodi == "sv" or ietfkielikoodi == "sv-sv" then
			-- |sv|sv-sv = {{sv}}
			insert("&#32;")
			insert(formatlanguagecode('ruotsiksi'))
		else
			-- otherwise: Module:Fr:Langue|codeLangue3
			local Langue = require('Module:Fr:Langue')
			local kielikoodi = Langue.getCodeLangue3fin( ietfkielikoodi )
			if kielikoodi ~= "" then
				local kielinimi = Langue.getLanguageName(kielikoodi)
				if kielinimi ~= "" then
					insert("&#32;")
					insert(formatlanguagecode( kielinimi ))
				else
					insert("&#32;")
					insert(formatlanguagecode( ietfkielikoodi ))
				end
			else
				insert("&#32;")
				insert("<span class='error'>Virhe: kielikoodi " .. ietfkielikoodi .. "</span>")
			end
		end
	end

	insert('</span>')
	return table.concat( wiki )
end

function t.main( frame )
    local origArgs
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs( frame.args ) do
            origArgs = frame.args
            break
        end
    else
        origArgs = frame
    end
    
    local args = {}
    for k, v in pairs( origArgs ) do
        if type( k ) == 'number' or v ~= '' then
            args[ k ] = v
        end
    end
    return verkkoviite( args )
end

return t;