انتقل إلى المحتوى

وحدة:Cite/1

من ويكيبيديا، الموسوعة الحرة
--/-- 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'}

local p = {}
local wikidata = require('وحدة:Wikidata2').formatStatementsFromLua
local wikidata2 = require('وحدة:Wikidata2').formatEntityId

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

local function getPage(items, refsnak)
	local page = wikidata({property = 'P304', firstvalue= '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 = ' & '}))
		end
	end
	table.insert(s, wikidata({property = 'P50', conjunction = ' & '}, 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', firstvalue= 'yes', noref = 'true'}) --ISBN 13
		if ISBN then return 'ISBN ' .. ISBN end
	end
	ISBN = wikidata({property = 'P212', firstvalue= 'true'}, refsnak)
	if ISBN then return 'ISBN ' .. ISBN end 
	for i, item in pairs(items) do
		ISBN = wikidata({entityId = item, property = 'P957', firstvalue= 'yes', noref = 'true'}) --ISBN 10
		if ISBN  then return 'ISBN ' .. ISBN end
	end
	ISBN = wikidata({property = 'P957', firstvalue= 'true'}, refsnak)
	if ISBN  then return 'ISBN ' .. ISBN end
	return nil
end

local function getLink(items, refsnak )
	local links = {}
	for i, item in pairs(items) do
		table.insert(links , wikidata({entityId = item, property = 'P854', label = 'وصلة مرجع', noref = 'true'}))
	end
	table.insert(links , wikidata({property = 'P854', label = 'وصلة مرجع'} , 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 'تاريخ الاطلاع: ' .. tot
	end
end

local function getVolume(items, refsnak )
	local page = wikidata({property = 'P478', firstvalue= '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, firstvalue= 'true', noref = 'true', label = wikidata2(property, {nolink = 'true', noref = 'true'}).value}) 
	local B = {wikidata({property = property, firstvalue= 'true', pattern = 'autourl2'}, refsnak)}	
	for i, item in pairs(items) do
		table.insert(B, wikidata({entityId = item, property = property, pattern = 'autourl2', noref = 'true'}))
	end
	if #B > 0 then
		return A .. ': ' .. table.concat(B)
	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 = {'الأول', 'الثاني', 'الثالث', 'الرابع', 'الخامس', 'السادس', 'السابع', 'الثامن', 'التاسع', 'العاشر', 'الحادي عشر', 'الثاني عشر'}
	local ednumber = wikidata({property = 'P393', firstvalue= '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', firstvalue= 'true'}, refsnak))
	for i, item in pairs( items ) do
		local lab = wikidata({property = 'P1680', entityId = item, firstvalue= '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 = {}
	table.insert(s, wikidata({property = 'P1476', firstvalue= 'true'}, refsnak) or wikidata({property = 'P357', firstvalue= 'true'}, refsnak))
	for i, item in pairs( items ) do
		local lab = wikidata({property = 'P1476', entityId = item, firstvalue= 'true', noref = 'true'}) or wikidata({property = 'P357', entityId = item, firstvalue= '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>'
		end
	else
		return nil
	end
end

local function getissue(items, refsnak)
	local issue = wikidata({property = 'P433', firstvalue= '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, firstvalue= 'true', noref = 'true', label = wikidata2(property, {nolink = 'true', noref = 'true'}).value}) 
	if property == "P143" then a = 'مستورد من [[تصنيف:صفحات بها مراجع مستورد من ويكي]]' 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 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)
	if title and title ~= '' and subtitle and subtitle ~= '' then 
		--title = title .. ' : ' .. subtitle
	end
	
	field = {}
	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 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 table.insert(field, link) 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
	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