مۆدیوول:Cite/Copy

لە ئینسایکڵۆپیدیای ئازادی ویکیپیدیاوە
بەڵگەدارکردنی مۆدیوول[دروست بکە]
--/-- Creates a timeline for an item based on various properties 
local Known_properties = {"P304",  "P50", "P957",  "P478", "P98", "P393", "P1476", "P123", "P577", "P433", "P275", "P698", "P356", "P958", "P1433",  "P1680", "P1683", "P1065" ,"P212","P854","P170","P248"
		,"P143","P364","P813"} --,"P345","P536","P227"
local p = {}
local wikidata = require('مۆدیوول:Wikidata2/Copy').formatStatementsFromLua
local wikidata2 = require('مۆدیوول:Wikidata2/Copy').formatEntityId2  -- table
local quall =  require('Module:Cite/quall')

local function P1629(property,text)
		local pro = wikidata({property = 'P1629', nolink = 'true', entityId = property, firstvalue = 'true', noref = 'true'
		, label = wikidata2(property, {nolink = 'true', noref = 'true'}).value}) 
			if not text or text == '' then text = pro end 
		if text then 
			return text 
		end
end

local function urltitle(url,title,exsisttitle)
	local final = ''
	if url  and url ~= '' then
		if title  and title  ~='' then
			final = '|url =' .. url .. '\n|title =' ..title 
		else
			final =  '|url =' .. url .. '\n|title =' 
		end
		if exsisttitle and exsisttitle~= '' 
			then final = '|url =' .. url  
		end
	end
	return final
end

local function ValueFromItem(items, property,firstvalue)
	local pattern = 'autourl'
	if property =='P212' or property =='P957' then pattern = nil end -- no automatic link for ISBN
	for i, item in pairs(items) do
		ednumber = wikidata({entityId = item, property = property , firstvalue = firstvalue, pattern =pattern
			, noref = 'true', modifytime = 'longdate',enlabelcate ='true',separator='، و',conjunction='، و'})
	end
	return ednumber 
end

local function make_P345_link_o( s )
	if not s or s == "" then return "" end
	local IMDB_Table = {
	['tt'] = 'https://www.imdb.com/title/$1/',
	['co'] = 'https://www.imdb.com/search/title?companies=$1',
	['ev'] = 'https://www.imdb.com/event/$1/',
	['nm'] = 'https://www.imdb.com/name/$1/',
	['ni'] = 'https://www.imdb.com/news/$1/',
	}
	local s2 = s:sub(1,2)
	--mw.log("s2" .. s2 )
	if IMDB_Table[s2] then 
		local fo = mw.ustring.gsub( IMDB_Table[s2], '$1', s )
		--return '[' .. fo .. ' ' .. fo .. ']' 
		return fo
	end
	return s
end

local function ValueFromEntityId(entity, property,firstvalue)
	local pattern = 'autourl'
	if property == "P345" then pattern = "" end
	local val = wikidata({entityId = entity, property = property, firstvalue = firstvalue, pattern = pattern
		, noref = 'true', modifytime = 'longdate',enlabelcate ='true'})
	if property == "P345" then val = make_P345_link_o(val) end
	if val then 
		return val 
	end
end

local function GetValueFromEntityId(items, snak,entity, property , text , firstvalue,exsisttitle)
	local A = mw.wikibase.label( property ) --P1629(property,text)
	local Pattern = "autourl"
	if property == "P345" then Pattern = "" end
	
	local prop = wikidata({property = property, firstvalue = firstvalue, noref = 'true', pattern = Pattern, modifytime = 'longdate'}, snak)
	if not prop or prop == '' then prop = ValueFromEntityId(entity, property,firstvalue) end
	if property == "P345" then prop = make_P345_link_o(prop) end
	
	if not text or text == '' then text = A end
	local final = urltitle(prop,text,exsisttitle)
	return final
end

local function GetItemValue(items, snak, property , text,firstvalue)
	local pattern = 'autourl'
	--	if property =='P212' or property =='P957' then pattern = nil end -- no automatic link for ISBN
	local prop = wikidata({property = property, firstvalue = (firstvalue or ''), pattern = pattern, noref = 'true', modifytime = 'longdate'
		,separator='، و',conjunction='، و'}, snak)
		if not prop then prop = ValueFromItem(items, property,(firstvalue or '')) end
	
	if prop and prop ~= '' then
		if text and text ~= '' then prop  = text .. prop end
	end
	return prop
end

local function GetNumberFromItem(items, snak, property , text,firstvalue)
	local prop = wikidata({property = property, firstvalue = 't'}, snak)
	for i, item in pairs(items) do
		ednumber = wikidata({entityId = item, property = property , firstvalue = 'true', noref = 'true'})
	end
		if not prop then prop = ednumber  end
	if prop and prop ~= '' then
		if text and text ~= '' then prop  = text .. prop end
	end
	return prop
end

local function getISBN(items, snak)
	local ISBN13 = GetNumberFromItem(items, snak, 'P212' ,'|ISBN=','yes') --ISBN 13
	local ISBN10 = GetNumberFromItem(items, snak, 'P957' ,'|ISBN=','yes')  --ISBN 10
		if not ISBN13 or ISBN13 =='' then ISBN13 = ISBN10 end
	return ISBN13  
end

--[[
  جلب أي وصلة مرجع من الخواص مثل
     P1065,P854
--]]

local function getLink(items, snak, property,text,exsisttitle)
	local links = wikidata({property = property, pattern = 'true', noref = 'true' , firstvalue = "t"}, snak)
	if not text or text =='' then text = '|url	=' end
	if links and links ~= '' then
		if exsisttitle and exsisttitle ~= '' 
			then
		return text .. links
			else
		 return text .. links .. '\n|title	='  
 		end
	end
end

local function get_Link_from_item(items, snak, property,text,exsisttitle)
	local links = GetItemValue(items, snak, property ,'','true')
	if links and links ~= '' then
		if exsisttitle and exsisttitle ~= '' 
			then
		return '|url	=' .. links
			else
		 return '|url	=' .. links .. '\n|title	='  
 		end
	end
end

--[[
  جلب أي تاريخ من الخواص مثل
     P813,P577
--]]

local function getdate(items, snak, property,text)
	local date = {}
	local datetime = wikidata({property = property, modifytime = 'longdate',enlabelcate ='true' }, snak)
	for i, item in pairs(items) do
		local datetime1 = wikidata({entityId = item, property =property, noref = 'true',enlabelcate ='true', modifytime = 'longdate' })
		if not datetime or datetime == '' then
			datetime = datetime1
		end
		table.insert(date, datetime)
	end
	local tot = table.concat(date, ' ') --  — 
	if tot and tot ~= '' then
		 if text then
		 	return text .. tot --text ..': '.. tot
	 			else return tot
	 		end
	end
end
local function getedition(items, snak )
	local ordning = {'یەکەم', 'دووەم', 'سێیەم', 'چوارەم', 'پێنجەم', 'شەشەم', 'حەوتەم', 'ھەشتەم', 'نۆیەم', 'دەیەم', 'یانزەیەم', 'دوانزەیەم'}

	local ednumber = wikidata({property = 'P393', firstvalue = 'true'}, snak)
	if not ednumber then
		ednumber = ValueFromItem(items, "P393")
	end
	if not ednumber then doo = nil end
	if not tonumber(ednumber) then
		 doo =  ednumber  -- ' الطبعة ' .. ednumber 
	end
	if ordning[tonumber(ednumber)] then
		 doo =   ordning[tonumber(ednumber)]  --' الطبعة ' .. ordning[tonumber(ednumber)] 
	end
	if doo and doo ~= '' then
		return '|edition = '.. doo
end
	
end

local function getwhatever(items, snak, property, title)
	local a = P1629(property,'')
	if property == "P143" then a = '' end
	if property == "P248" then a = '' end

	if a and a ~= '' then 
		a = a  .. ': ' --title .. a 
	end

	local s = {}
		local ss = wikidata({property = property, conjunction = ', '}, snak)
		for i, item in pairs(items) do
			local sss = wikidata({entityId = item, property = property, noref = 'true', conjunction = ', '})
		if not ss or ss == '' then
			ss = sss
		end
			table.insert(s, ss )
		end
	fi =  table.concat(s, ', ')
	if fi and fi ~=''
		then f =  '|work =' .. (a or  '') .. fi
	end
	if #s > 0 then
		return f
	end
end

--[[
   يجلب خواص معينة عند وجود مراجع محددة مسبقاً
--]]

local function getsomequall(items, snak , entity , property,exsisttitle)
	if snak[property]
	and snak[property][1]
	and snak[property][1].datavalue
	and snak[property][1].datavalue.value['numeric-id']
	then 
		house = snak[property][1].datavalue.value['numeric-id'] 
		local l10n = quall[house]
		if l10n then
			local q = {}
			for _, v in pairs(l10n) do
				mw.log("finding " .. v[1] .. "house" .. house )
				b = GetValueFromEntityId(items, snak,entity, v[1],(v[2] or ''),'true',exsisttitle)
				if b and b ~= '' 
					then table.insert( q, b ) 
				end
			end
			t =  table.concat( q )
			if t and t ~= '' then
				return t
			end
		end
	end
end

local function Getlanguage(items, snak)
	local prop = wikidata({property = 'P364', firstvalue = 't', formatting   = 'raw', noref = 'true'}, snak)
		for i, item in pairs(items) do
			ednumber = wikidata({entityId = item, property = 'P364' , firstvalue = 't',  formatting   = 'raw' , noref = 'true'})
		end
		if not prop then prop = ednumber end
	tt = wikidata({entityId = prop, property = 'P218' , noref = 'true', firstvalue = 't'})
	if tt and tt ~= '' then
		 tt  = '|language 	=' .. tt 
	end
	return tt
end

local function gettherest(items, snak, entity,exsisttitle)
	local s = {}
	for i, j in pairs(snak) do
		finns = false
		for k, l in pairs(Known_properties) do
			if i == l then 
				finns = true
			end
		end
		if not finns then
			local entity1 = mw.wikibase.getEntityObject( i )
			if ( entity1.datatype == 'string' or entity1.datatype == 'external-id' ) --and entity1.claims and entity.claims.P1629 
			then
			local vaa =  GetValueFromEntityId(items, snak,entity, i, '' , 'true',exsisttitle )
				table.insert(s, vaa) 
			end
		end
	end
 sss = table.concat(s)
	if #s > 0 then
		return s[1]
	end
end
--[[
local function citearticle(item, lang, page)
	local author = getauthor(item, lang)
	local title = getarticletitle(item, lang)
	local publisher = getpublisher(item, lang)
	local publishdate = getpublishdate(item, lang)
	local pagenumber = formatpage(page, lang)
	local journal = getjournal(item, lang)
	local issue = getissue(item, lang)
	local doi = getDoi(item, lang)
	local pmid = getPmid(item, lang)
	local link = getLink(item, lang)
	local volume = getVolume(item, lang)
	local pagenumber = formatpage(page, lang)
	local license = getlicense(item, lang)
	
	local field = {}
	table.insert(field, author)
	table.insert(field, title)
	table.insert(field, publisher)
	table.insert(field, journal)
	table.insert(field, volume)
	table.insert(field, issue)
	table.insert(field, publishdate)
	table.insert(field, pagenumber)
	table.insert(field, doi)
	table.insert(field, pmid)
	table.insert(field, link)
	table.insert(field, license)
	return linguistic.conj(field, lang, "comma")
end--]]

		
local function gettitle(items, snak)
	local title = GetItemValue(items, snak, 'P1476' ,  '|title = ' ,' ')
	local subtitle = GetItemValue(items, snak, 'P1680' ,  '|title = ' ,' ')
		if not title or title =='' then title = subtitle end
	return title  
end

function numbers( items , snak)
	local section = GetNumberFromItem(items, snak, 'P958' , '|section = ','') --0
	local volume  = GetNumberFromItem(items, snak, 'P478' ,'|volume	=','yes') --المجلد: 
	local page = GetNumberFromItem(items, snak, 'P304' ,'|page	= ','yes')
	local issue = GetNumberFromItem(items, snak, 'P433' ,'|issue	=','yes') --العدد: 

	field = {}
		if section and section ~= '' then 	table.insert(field, section)	end
		if volume and volume ~= '' then 	table.insert(field, volume)	end
		if page and page ~= '' then 	table.insert(field, page)	end
		if issue and issue ~= '' then 	table.insert(field, issue)	end
	return table.concat(field, '\n')
end

function doipmid( items , snak)
	local doi = GetNumberFromItem(items, snak,'P356','|doi	=','true') -- Digital Object Identifier
	local pmid = GetNumberFromItem(items, snak,'P698','|pmid	=','true')-- Identifier for journal articles/abstracts in PubMed
	local pmc = GetNumberFromItem(items, snak,'P932','|pmc	=','true')-- identifier issued by PubMed Central
	field = {}
	if doi and doi ~= '' then 	table.insert(field, doi)	end
	if pmid and pmid ~= '' then 	table.insert(field, pmid)	end
	if pmc and pmc ~= '' then 	table.insert(field, pmc)	end
	return table.concat(field, '\n')
end

local function Getquall(items, snak , entity, title)
	local quall = getsomequall(items, snak , entity , "P143",title)
	local quall1 = getsomequall(items, snak , entity , "P248" ,title)
	if not quall or quall =='' then quall = quall1 end
	return quall
end

function getMultiLink(items, snak , entity ,title)
	local link = getLink(items, snak, 'P854', '|url =', title) -- 
	local link1 = get_Link_from_item(items, snak, 'P854', '|url =', title) -- 
	local therest = gettherest(items, snak, entity,title)
	local quall =Getquall(items, snak , entity, title)
	local fulltext = get_Link_from_item(items, snak, 'P953', '|url =', title) -- 
	local accessdate =GetItemValue(items, snak, 'P813' ,'|access-date    = ','true')

	field = {}
	
	if link and link ~= '' --We don't need to search for some links if they here already
		then newtab = link
	elseif therest and therest ~= '' 
		then newtab = therest 
	elseif quall and quall ~= '' 
		then newtab = quall 
	elseif link1 and link1 ~= '' 
		then newtab = link1  -- last thing we want to see
	elseif fulltext and fulltext ~= '' 
		then newtab = fulltext
	end
	if newtab and newtab ~= '' then
		table.insert(field, newtab) 
		if accessdate and accessdate ~= '' then table.insert(field, accessdate)  end
	end
	return table.concat(field, '\n')
end

function getarchiveLink(items, snak , entity)
	local archive = getLink(items, snak, 'P1065','|archiveurl =',title) -- 
	local archivedate =GetItemValue(items, snak, 'P2960' ,'|archive-date    = ','true')
	field = {}
	if archive and archive ~= '' then 
		table.insert(field, archive) 
		if archivedate and archivedate ~= '' then table.insert(field, archivedate)  end
	end
	return table.concat(field, ' ')
end

function authorlist(auth,name)
	if not auth or auth == '' then return nil end
	local list = mw.text.split( auth, '، و')
	local s = {}
	for i, auth in pairs(list) do
		local str = '|' .. name .. i .. '=' .. auth
	if str then table.insert(s, str) end
	end
	return table.concat(s, '\n')
end

function getreftable( items , snak , hash , options )
    local entity = options.entityId
	local openn = '{{بیرخستنەوەی وێب\n'
    local closee = '\n}}'
	local author = authorlist(GetItemValue(items, snak, 'P50','',''),'author') --GetItemValue(items, snak, 'P50','|author =','') -- المؤلف: 
	local editor =  authorlist(GetItemValue(items, snak, 'P98','',''),'editor') -- GetItemValue(items, snak, 'P98','|editor =')
	local title = gettitle(items, snak)
	local publisher = GetItemValue(items, snak, 'P123','|publisher      = ')--publisher
	local creator = ''--GetItemValue(items, snak, 'P170','مخترع')
	local language  = Getlanguage(items, snak)

	--local publishedin = GetItemValue(items, snak, 'P1433','|journal =نشر في')
	local publishdate = GetItemValue(items, snak, 'P577' ,'|publication-date =','true') -- تاريخ النشر: 
	
	local edition = getedition(items, snak ) --0
	local doipmid = doipmid( items , snak)
	local number = numbers( items , snak)
	local import = getwhatever(items, snak, "P143" ) or getwhatever(items, snak, "P248" )

--	local import = getwhatever(items, snak, "P143",'|work =' )
--	local import1 = getwhatever(items, snak, "P248" ,'|work=' )
	local MultiLink = getMultiLink(items, snak , entity ,title)
	local archiveLink = getarchiveLink(items, snak , entity)

	local license = GetItemValue(items, snak, 'P275','مۆڵەتنامە')
	local isbn = getISBN(items, snak ) --0
	local quote = GetItemValue(items, snak, 'P1683' ,  '|quote =' ,' ') --  إقتباس: 
	--if title and title ~= '' and subtitle and subtitle ~= '' then 
		--title = title .. ' : ' .. subtitle
	--end
	for i, item in pairs(items) do
			if item == 'Q36578'
			then 
				edition ,author  = nil,nil
			end
	end

	field = {}
	if import and import ~= '' then table.insert(field, import) end
--	if import1 and import1 ~= '' then table.insert(field, import1) end

	if MultiLink and MultiLink ~= '' then table.insert(field, MultiLink)  end
	if archiveLink and archiveLink ~= '' then table.insert(field, archiveLink)  end

	if author and author ~= '' then table.insert(field, author) end
	if editor and editor ~= '' then table.insert(field, editor) end
	if title and title ~= '' then table.insert(field, title) end
	if number and number ~= '' then 	table.insert(field, number)	end
	if isbn and isbn ~= '' then 	table.insert(field, isbn)	end

	if edition and edition ~= '' then table.insert(field, edition) end
	if publisher and publisher ~= '' then table.insert(field, publisher) end
	if creator and creator ~= '' then table.insert(field, creator) end

	if language and language ~= '' then 	table.insert(field, language)	end
	if publishedin and publishedin ~= '' then table.insert(field, publishedin) end
	if publishdate and publishdate ~= '' then table.insert(field, publishdate) end
	if doipmid and doipmid ~= '' then 	table.insert(field, doipmid)	end
--	if license and license ~= '' then table.insert(field, license) end
	if quote and quote ~= '' then table.insert(field, quote) end
	--table.insert(field, closee)
	--if archive and archive ~= '' then table.insert(field, archive) end
	--return table.concat(field, ' — ')
	if doipmid and doipmid ~= '' then openn =  '{{Cite magazine\n' 
	elseif author and author ~= '' or editor and editor ~= '' then openn =  '{{Cite book\n' 
		end
	
	local doo =  table.concat(field, '\n')
	if doo and doo ~= '' then 
		doo = openn .. doo .. closee
		final = doo --mw.getCurrentFrame():extensionTag( 'ref', doo)
		--	final = mw.getCurrentFrame():extensionTag( 'ref', doo, {name = hash..'fg'} )
	end
	return	final
end

local function findblacklist(items, snak)
	 -- function to ignore Sister projects
	local val = 'false'
	local s = {}
		for i, item in pairs(items) do
			function qoo(item,property)
				return wikidata({entityId = item, property = property , noref = 'true',firstvalue='1',formatting = 'raw'})
			end
			local wiki = qoo(item, 'P1800') or qoo(item, 'P1566') or qoo(item,'P21') -- all wikimedia site ,GeoNames ,gender
			local wik2 = qoo(item, 'P31')
			local blackk = {"Q20651139"
				,  "Q465"
				, "Q15241312" 
				, 'Q52'  -- Wikipedia 
				} 
			for k, b in pairs(blackk) do
				if item == b then  val = 'true' end
			end
			if wiki and wiki  ~= ''
				then val = 'true'
				elseif wik2 == 'Q5' -- Human is not a direct reference
					or wik2 == 'Q184188'  -- Also Canton
					or wik2 == 'Q1250464' then val = 'true' -- Also places
			end
	end
	return val
end

function  FindTypeOfRefrence( items , snak , hash , options )
	--	local refrence,Type,book  = '','',''  -- if item has "edition of" is a book, else an article, needs a better solution

	local s = {}
		for i, item in pairs(items) do
			local Aut = wikidata({entityId = item, property = 'P31', noref = 'true',formatting='raw'}) 
			 Aut2 = wikidata({entityId = item, property = 'P212', noref = 'true'}) -- ISBN-13
			 Aut3 =  wikidata({entityId = item, property = 'P957', noref = 'true'}) -- ISBN-10
			 Aut4 =  wikidata({entityId = item, property = 'P393', noref = 'true'}) 
			--local Aut = wikidata({entityId = item, property = 'P50', noref = 'true', conjunction = ' & '})
				table.insert(s, Aut)
		end
	local author =  table.concat(s, ' — ')
	if author and author ~= ''   then
	if author == 'Q571' or author == 'Q8261' or author == 'Q5292'
		then itemtype = 'book' 
			elseif Aut2 and Aut2 ~= '' or Aut3 and Aut3 ~= '' or Aut4 and Aut4 ~= ''  then itemtype = 'book'  end --	 itemtype = 'book'
		else itemtype= 'other' 
	end
	if itemtype == 'book' 
		then refrence= citebook( items , snak , hash , options )
		else refrence= getreftable( items , snak , hash , options )

	end
	return refrence
end	

function p.citeitem( items , snak , hash , options , notag)
	if not items and not snak then return nil end
	if type(items) == 'string' then
		items = {items}
	end
	local blacklist1 = findblacklist(items, snak)
	if blacklist1 == 'true' then return nil end
	--local findtype = FindTypeOfRefrence( items , snak , hash , options )
	local refrence = getreftable( items , snak , hash , options )
	if refrence and refrence ~='' then
		if notag and notag ~='' then
		refrence = refrence
		else
		refrence = mw.getCurrentFrame():extensionTag( 'ref', refrence) --, {name = hash} 
		end
		end
	return refrence
end

function p.reflist(frame)
	local list = mw.text.split( frame.args.item, ' ')
	local s = {}
	for i, j in pairs(list) do
    	local item = mw.wikibase.getEntityObject( mw.text.trim(j) )
		local str = p.citeitem( {mw.text.trim(j)} , item.claims, mw.text.trim(j)..i , frame.args , 't')
		if str then table.insert(s, '*'.. str) end
	end
	fii = table.concat(s, '\n')
	return frame:preprocess(fii ) 
end

function p.cite(frame)
    	local item = mw.wikibase.getEntityObject( frame.args.item )
    	if not item then return 'no item ' end
	fii = p.citeitem( {frame.args.item} , item.claims, frame.args.item, frame.args , 't')
	return frame:preprocess(fii) 
end

return p