Öppna huvudmenyn

Dokumentationen för denna modul kan skapas på Modul:Cite/dok

--/-- Creates a timeline for an item based on various properties
local propsamling = {"P143", "P304", "P50", "P212", "P957", "P854", "P478", "P98", "P393", "P1476", "P357", "P123", "P577", "P248", "P433", "P275", "P698", "P356", "P958", "P1433", "P1680", 'P1683', 'P387', 'P518'}

local p = {}
local olampliga = {'Q11032', -- tidning
					'Q1110794', -- dagstidning
					'Q1002697', -- periodisk litteratur
					'Q2016362'} -- morgontidning
				
local wikidata = require('Modul:Wikidata2').formatStatementsFromLua
local wikidata2 = require('Modul:Wikidata2').formatEntityId

local function itemcheck(items)
	if items then
		for i, item in pairs(items) do
			local typer = wikidata({entityId = item, property = 'P31', noref = 'true', raw = 'true'})
			if typer then
				for j, typ in pairs(typer) do
					for k, olamplig in pairs(olampliga) do
						if typ.item == olamplig then
							return '[[Kategori:Sidor med mindre lämplig användning av P248]]'
						end
					end
				end
			end
		end
	end
end

local function helpednumber(items, property)
	for i, item in pairs(items) do
		local ednumber = wikidata({entityId = item, property = property, enbarten = 'yes'})
		if ednumber then 
			return ednumber 
		end
	end
end

local function getPage(items, refsnak)
	local page = wikidata({property = 'P304', enbarten = 'true'}, refsnak)
	if not page then
		page = helpednumber(items, "P304")
	end
	if page then
		return 's. ' .. page
	end
end


local function getarticle(items, refsnak)
	local s = {}
	for i, item in pairs(items) do
		local s2 = wikidata({entityId = item, property = 'P958', noref = 'true'})
		if s2 then table.insert(s, '”' .. s2 .. '”') end
	end
	local s3 = wikidata({property = 'P958'}, refsnak)
	if s3 then table.insert(s, '”' .. s3 .. '”') end

	return table.concat(s, ', ')
end


local function getquote(items, refsnak)
	local s = {}
	for i, item in pairs(items) do
		local s2 = wikidata({entityId = item, property = 'P1683', noref = 'true'})
		if s2 then table.insert(s, '”' .. s2 .. '”') end
	end
	local s3 = wikidata({property = 'P1683'}, refsnak)
	if s3 then table.insert(s, '”' .. s3 .. '”') end
	for i, item in pairs(items) do
		local s2 = wikidata({entityId = item, property = 'P387', noref = 'true'})
		if s2 then table.insert(s, '”' .. s2 .. '”') end
	end
	local s4 = wikidata({property = 'P387'}, refsnak)
	if s4 then table.insert(s, '”' .. s4 .. '”') end

	return table.concat(s, ', ')
end

local function getauthor(items, refsnak)
	local s = {}
	for i, item in pairs(items) do
		if item ~= 'Q36578' then
			table.insert(s, wikidata({entityId = item, property = 'P50', noref = 'true', conjunction = ' & ', novalue = ''}))
		end
	end
	table.insert(s, wikidata({property = 'P50', conjunction = ' & ', novalue = ''}, refsnak))
	return table.concat(s, ', ')
end

local function getappliestopart(items, refsnak)
	local s = {}
	for i, item in pairs(items) do
		table.insert(s, wikidata({entityId = item, property = 'P518', noref = 'true'}))
	end
	table.insert(s, wikidata({property = 'P518'}, refsnak))
	return table.concat(s, ', ')
end

local function getISBN(items, refsnak)
	local ISBN = nil
	for i, item in pairs(items) do
		ISBN = wikidata({entityId = item, property = 'P212', enbarten = 'yes', noref = 'true'}) --ISBN 13
		if ISBN then return mw.getCurrentFrame():preprocess('{{ISBN|' .. ISBN .. '}}') end
	end
	ISBN = wikidata({property = 'P212', enbarten = 'true'}, refsnak)
	if ISBN then return mw.getCurrentFrame():preprocess('{{ISBN|' .. ISBN .. '}}') end 
	for i, item in pairs(items) do
		ISBN = wikidata({entityId = item, property = 'P957', enbarten = 'yes', noref = 'true'}) --ISBN 10
		if ISBN  then return mw.getCurrentFrame():preprocess('{{ISBN|' .. ISBN .. '}}') end
	end
	ISBN = wikidata({property = 'P957', enbarten = 'true'}, refsnak)
	if ISBN  then return mw.getCurrentFrame():preprocess('{{ISBN|' .. ISBN .. '}}') end
	return nil
end

local function getLink(items, refsnak )
	local links = {}
	for i, item in pairs(items) do
		local p854 = wikidata({entityId = item, property = 'P854', label = 'läs online', noref = 'true'}) or wikidata({entityId = item, property = 'P953', label = 'läs online', noref = 'true'})
		table.insert(links, p854)
	end
	table.insert(links, wikidata({property = 'P854', label = 'läs online'}, refsnak))
	return table.concat(links)
end

local function getHamtdatum(items, refsnak )
	local hamtdatum = {}
	for i, item in pairs(items) do
		table.insert(hamtdatum, wikidata({entityId = item, property = 'P813', noref = 'true', modifytime = 'longdate' }))
	end
	table.insert(hamtdatum, wikidata({property = 'P813', modifytime = 'longdate'}, refsnak))
	local tot =  table.concat(hamtdatum)
	if tot and tot ~= '' then
		return 'läst: ' .. tot
	end
end

local function getVolume(items, refsnak )
	local page = wikidata({property = 'P478', enbarten = 'true'}, refsnak) or helpednumber(items, "P478")
	if page then
		return 'vol. ' .. page
	end
end

local function getMost(items, refsnak, property )
	local A = wikidata({property = 'P1629', entityId = property, enbarten = 'true', noref = 'true', label = wikidata2(property, {nolink = 'true', noref = 'true'}).value}) 
	if property == 'P1810' then  -- ett plåster för att hantera property P1810, ska troligen utökas till fler properties
		A = ' ' .. A
	end
	local B = {wikidata({property = property, enbarten = 'true', pattern = 'auktoritetsdata2'}, refsnak)}	
	for i, item in pairs(items) do
		table.insert(B, wikidata({entityId = item, property = property, pattern = 'auktoritetsdata2', noref = 'true'}))
		--TODO: detta verkar skriva sönder pattern för huvudmodulen Wikidata2
	end
	if #B > 0 then
		local res = A .. ': ' .. table.concat(B)
		if property == 'P1810' then  -- normalisera stil för namn så att endast förstabokstaven är versal
			for k in  string.gmatch(res, '[A-ZÅÄÖ][A-ZÅÄÖ]+') do
				res = res:gsub(k,string.sub(k,1,1)..string.lower(string.sub(k,2)):gsub('Å','å'):gsub('Ä','ä'):gsub('Ö','ö'))
			end
		end
		return res
	end
end

local function getEditor(items, refsnak )
	local s = {}
	for i, item in pairs(items) do
		table.insert(s, wikidata({entityId = item, property = 'P98', noref = 'true', conjunction = ' & '}))
	end
	table.insert(s, wikidata({property = 'P98', conjunction = ' & '}, refsnak))
	local tot = table.concat(s, ', ')
	if tot and tot ~= '' then
		return tot .. ' (red.)'
	end
end

local function getedition(items, refsnak )
	local ordning = {'första', 'andra', 'tredje', 'fjärde', 'femte', 'sjätte', 'sjunde', 'åttonde', 'nionde', 'tionde', '11:e', '12:e'}
	local ednumber = wikidata({property = 'P393', enbarten = 'true'}, refsnak)
	if not ednumber then
		ednumber = helpednumber(items, "P393")
	end
	
	if not ednumber then 
		return nil
	end
	if not tonumber(ednumber) then
		return ednumber .. ' utgåvan'
	end
	if ordning[tonumber(ednumber)] then
		return ordning[tonumber(ednumber)] .. ' utgåvan'
	end
	local sist = string.sub(ednumber, string.len(ednumber))
	if sist == '1' or sist == '2' then
		return ednumber .. ':a utgåvan'
	else
		return ednumber .. ':e utgåvan'
	end
	
end

local function getsubtitle(items, refsnak)-- needs i18n some languages do not use this format
	local s = {}
	table.insert(s, wikidata({property = 'P1680', enbarten = 'true'}, refsnak))
	for i, item in pairs( items ) do
		local lab = wikidata({property = 'P1680', entityId = item, enbarten = 'true', noref = 'true'})
		local sitelink = mw.wikibase.sitelink( item )
		if sitelink and lab then
			table.insert(s, '[[:' .. sitelink .. '|' .. lab .. ']]')
		elseif lab then
			table.insert(s, lab)
		end
	end
	tot = table.concat(s, ', ')
	if tot and tot ~= '' then
		return tot
	else
		return nil
	end
end


local function gettitle(items, refsnak)-- needs i18n some languages do not use this format
	local subtitle = getsubtitle(items, refsnak)
	local s = {}
	local titel = wikidata({property = 'P1476', enbarten = 'true'}, refsnak) or wikidata({property = 'P357', enbarten = 'true'}, refsnak)
	if titel then
		local link = wikidata({property='P854', enbarten = 'true'}, refsnak)
		if link then
			table.insert(s, '[' .. link .. ' ' .. titel .. ']')
		else
			table.insert(s, titel)
		end
	end
	local language = nil
	local sprakparentes = ''
	if refsnak["P1476"] and refsnak["P1476"][1].datavalue.value.language then
		language = refsnak["P1476"][1].datavalue.value.language
		if language and language ~= 'sv' and language ~= 'und' then
			sprakparentes = ' (på ' .. mw.language.fetchLanguageName(language, 'sv') .. ')'
		end
	end
	for i, item in pairs( items ) do
		local lab = wikidata({property = 'P1476', entityId = item, enbarten = 'true', noref = 'true'}) or wikidata({property = 'P357', entityId = item, enbarten = 'true', noref = 'true'})
		if not lab then 
			lab = wikidata2(item, {nolink = 'true', noref = 'true'}).value
		end
		local sitelink = mw.wikibase.sitelink( item )
		if sitelink then
			table.insert(s, '[[:' .. sitelink .. '|' .. lab .. ']]')
		else
			table.insert(s, lab)
		end
	end
	tot = table.concat(s, ', ')
	if tot and tot ~= '' then
		if subtitle and subtitle ~= '' then
			return '<i>' .. tot .. ' : ' .. subtitle .. '</i>'
		else
			return '<i>' .. tot .. '</i>' .. sprakparentes
		end
	else
		return nil
	end
end

local function getissue(items, refsnak)
	local issue = wikidata({property = 'P433', enbarten = 'true'}, refsnak)
	if not issue then
		issue = helpednumber(items, "P433")
	end
	if issue then
		return issue
	end
end

local function getpublisher(items, refsnak )
	local s = {}
	for i, item in pairs(items) do
		table.insert(s, wikidata({entityId = item, property = 'P123', noref = 'true'}))
	end
	table.insert(s, wikidata({property = 'P123'}, refsnak))
	local tot = table.concat(s, ', ')
	if tot and tot ~= '' then
		return tot
	end
end

local function getpublishedin(items, refsnak )
	local s = {}
	for i, item in pairs(items) do
		table.insert(s, wikidata({entityId = item, property = 'P1433', noref = 'true'}))
	end
	table.insert(s, wikidata({property = 'P1433'}, refsnak))
	local tot = table.concat(s, ', ')
	if tot and tot ~= '' then
		return tot
	end
end

local function getPublishdate(items, refsnak )
	local publishdate = {}
	for i, item in pairs(items) do
		table.insert(publishdate, wikidata({entityId = item, property = 'P577', noref = 'true', modifytime = 'longdate' }))
	end
	table.insert(publishdate, wikidata({property = 'P577', modifytime = 'longdate'}, refsnak))
	local tot =  table.concat(publishdate)
	if tot and tot ~= '' then
		return tot
	end
end

local function getwhatever(items, refsnak, property)
	local a = wikidata({property = 'P1629', entityId = property, enbarten = 'true', noref = 'true', label = wikidata2(property, {nolink = 'true', noref = 'true'}).value}) 
	if property == "P143" then a = '[[Kategori:Artiklar med importerade Wikidatavärden]]hämtat från' end
	local s = {}
	for i, item in pairs(items) do
		table.insert(s, wikidata({entityId = item, property = property, noref = 'true', conjunction = ', '}))
	end
	table.insert(s, wikidata({property = property, conjunction = ', '}, refsnak))
	if #s > 0 then
		return a .. ': ' .. table.concat(s, ', ')
	end
end

local function gettherest(items, refsnak)
	local s = {}
	for i, j in pairs(refsnak) do
		finns = false
		for k, l in pairs(propsamling) do
			if i == l then 
				finns = true
			end
		end
		if not finns then
			local entity = mw.wikibase.getEntityObject( i )
			if ( entity.datatype == 'string' or entity.datatype == 'external-id' ) and entity.claims and entity.claims.P1629 then
				table.insert(s, getMost(items, refsnak, i))
			end
		end
	end
	if #s > 0 then
		return table.concat(s,', ')
	end
end

local function citebook(item, lang, page)
	local publishdate = getpublishdate(item )
	local author = getauthor(item )
	local editor = getEditor(item )
	local title = getbooktitle(item )
	local publisher = getpublisher(item )
	local publishedin = getpublishedin(item )
	local edition = getedition(item )
	local isbn = getISBN(item )
	local doi = getDoi(item )
	local pmid = getPmid(item )
	local link = getLink(item )
	local volume = getVolume(item )
	local pagenumber = formatpage(page, lang)
	local license = getlicense(item )

	local fields = {}
	table.insert(fields, author)
	table.insert(fields, editor)
	table.insert(fields, title)
	table.insert(fields, edition)
	table.insert(fields, volume)
	table.insert(fields, publisher)
	table.insert(fields, publishedin)
	table.insert(fields, publishdate)
	table.insert(fields, pagenumber)
	table.insert(fields, isbn)
	table.insert(fields, doi)
	table.insert(fields, pmid)
	table.insert(fields, link)
	table.insert(fields, license)
	return linguistic.conj(fields, lang, "comma")
end

local function citearticle(item, lang, page)

	local fields = {}
	table.insert(fields, author)
	table.insert(fields, title)
	table.insert(fields, publisher)
	table.insert(fields, publishedin)
	table.insert(fields, journal)
	table.insert(fields, volume)
	table.insert(fields, issue)
	table.insert(fields, publishdate)
	table.insert(fields, pagenumber)
	table.insert(fields, doi)
	table.insert(fields, pmid)
	table.insert(fields, link)
	table.insert(fields, license)
	return linguistic.conj(fields, lang, "comma")
end

function p.citeitem(items, refsnak, options)
	if not items and not refsnak then return nil end
	if type(items) == 'string' then
		items = {items}
	end

	local author = getauthor(items, refsnak )
	local appliestopart = getappliestopart(items, refsnak)
	local editor = getEditor(items, refsnak )
	local article = getarticle(items, refsnak )
	local title = gettitle(items, refsnak )
	local publisher = getpublisher(items, refsnak )
	local publishedin = getpublishedin(items, refsnak )
	local publishdate = getPublishdate(items, refsnak )
	local edition = getedition(items, refsnak )
	local issue = getissue(items, refsnak )
	local page = getPage(items, refsnak )
	local isbn = getISBN(items, refsnak )
	local doi = getMost(items, refsnak, "P356" )
	local pmid = getMost(items, refsnak, "P698" )
	local import = getwhatever(items, refsnak, "P143" )
	local link = getLink(items, refsnak )
	local hamtdatum = getHamtdatum(items, refsnak )
	local volume = getVolume(items, refsnak )
	local license = getwhatever(items, refsnak, "P275" )
	local therest = gettherest(items, refsnak)
	local citatet = getquote(items, refsnak)
	local checket = itemcheck(items)
	if title and title ~= '' and subtitle and subtitle ~= '' then 
		--title = title .. ' : ' .. subtitle
	end

	local field = {}
	local barap854 = false -- Flagga för att se om "bara" P854 används i källan
	if import and import ~= '' then table.insert(field, import) end
	if author and author ~= '' then table.insert(field, author) end
	if editor and editor ~= '' then table.insert(field, editor) end
	if article and article ~= '' then table.insert(field, article) end
	if title and title ~= '' then table.insert(field, title) end
	if edition and edition ~= '' then table.insert(field, edition) end
	if volume and volume ~= '' then table.insert(field, volume) end
	if issue and issue ~= '' then table.insert(field, issue) end
	if publisher and publisher ~= '' then table.insert(field, publisher) end
	if publishedin and publishedin ~= '' then table.insert(field, publishedin) end
	if publishdate and publishdate ~= '' then table.insert(field, publishdate) end
	if appliestopart and appliestopart ~= '' then table.insert(field, appliestopart) end
	if page and page ~= '' then table.insert(field, page) end
	if isbn and isbn ~= '' then table.insert(field, isbn) end
	if doi and doi ~= '' then table.insert(field, doi) end
	if pmid and pmid ~= '' then table.insert(field, pmid) end
	if therest and therest ~= '' then table.insert(field, therest) end
	if link and link ~= '' then 
		if #field < 1 then
			barap854 = true
		end
		if not title or title == '' then
			table.insert(field, link)
		end
	end
	if hamtdatum and hamtdatum ~= '' then table.insert(field, hamtdatum) end
	if license and license ~= '' then table.insert(field, license) end
	if citatet and citatet ~= '' then table.insert(field, citatet) end
	if checket and checket ~= '' then table.insert(field, checket) end
	if barap854 then table.insert(field, '[[Kategori:Artiklar med svaga Wikidatabaserade källhänvisningar]]') end
	return table.concat(field, ', ')
end

function p.cite(frame)
	local lang = frame.args.lang
	if not lang or lang == '' then
		lang = frame:preprocess('{{int:lang}}')
	end
	local page = frame.args.page
	if page == '' then
		page = nil
	end
	return p.citeitem(frame.args.item, lang, page)
end


return p