وحدة:Cite/نسخة

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

local p = {}
local cite_functions = require("Module:Cite/functions")
local wikidata = require("وحدة:Wikidata2/نسخة").formatStatementsFromLua
local wikidata2 = require("وحدة:Wikidata2/نسخة").formatEntityId2 -- table
local quall = require("Module:Cite/quall")

function p.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

local function P1629(pid)
    local Label = wikidata2(pid, {nolink = "true", noref="true"}).value
	local pro = wikidata({property="P1629", nolink="true", entityId=pid, firstvalue="true", noref="true", label=Label})
	return pro
end

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

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

local function GetValueFromEntityId(claims, qid, pid, text, first)
	local Pattern = "autourl"
	if pid == "P345" then
		Pattern = ""
	end
	local url = wikidata({property=pid, firstvalue=first, noref="true", pattern=Pattern, modifytime="longdate"}, claims)
	if not url or url == "" then
		url = ValueFromEntityId(qid, pid, first)
	end
	if pid == "P345" then
		url = cite_functions.make_P345_link_o(url)
	end
	return url
end

local function GetItemValue(items, claims, pid, text, firstvalue)
	local pattern = "autourl"
	local prop = wikidata({property=pid, firstvalue=(firstvalue or ""), pattern=pattern, noref="true", modifytime="longdate", separator="، و", conjunction="، و"
		}, claims)
    
	if not prop then
		prop = ValueFromItem(items, pid, (firstvalue or ""))
	end

	if prop and prop ~= "" then
		if text and text ~= "" then
			prop = text .. prop
		end
	end
	return prop
end

local function Getauthor(items, claims)
	local P50 = GetItemValue(items, claims, "P50", "","","")
	local P2093 = GetItemValue(items, claims, "P2093", "","","")

	if P50 and P50 ~= "" and P2093 and P2093 ~= "" then
		return P50 .. "، و" .. P2093

	elseif P2093 and P2093 ~= "" then
		return P2093

	elseif P50 and P50 ~= "" then
		return P50
	end
end

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

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

--[[
  جلب أي وصلة مرجع من الخواص مثل
	 P1065, P854
--]]
local function getLink(items, claims, pid, text)
	local links = wikidata({property=pid, pattern="true", noref="true", firstvalue="t"}, claims)
	if not text or text == "" then
		text = "|url ="
	end
	if links and links ~= "" then
		return text .. links
	end
end

local function get_Link_from_item(items, claims, pid)
	local links = GetItemValue(items, claims, pid, "", "true")
	if links and links ~= "" then
		return "|url =" .. links
	end
end

--[[
  جلب أي تاريخ من الخواص مثل
	 P813, P577
--]]
local function getdate(items, claims, pid, text)
	local date = {}
	local datetime = wikidata({property=pid, modifytime="longdate", enlabelcate="true"}, claims)
	for i, item in pairs(items) do
		local datetime1 = wikidata({entityId=item, property=pid, 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 getwhatever(items, claims, pid, title)
	local a = P1629(pid)
	if pid == "P143" then
		a = ""
	end
	if pid == "P248" then
		a = ""
	end

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

	local s = {}
	local ss = wikidata({property=pid, conjunction=", "}, claims)
	for i, item in pairs(items) do
		local sss = wikidata({entityId=item, property=pid, 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, claims, qid, pid)
	if
		claims[pid] and claims[pid][1] and claims[pid][1].datavalue and
			claims[pid][1].datavalue.value["numeric-id"]
	 then
		house = claims[pid][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(claims, qid, v[1], (v[2] or ""), "true")
				if b and b ~= "" then
					return b
				end
			end
		end
	end
end

local function Getlanguage(items, claims)
	lang_prop = {"P364", "P407"}
	local qid = ""
	for _, prop in pairs(lang_prop) do
        if claims[prop] then
            qid = wikidata({property=prop, firstvalue="t", formatting="raw", noref="true"}, claims)
            if qid and qid ~= "" then
                break
            end
		end
	end
	if qid == "" then
		for i, item in pairs(items) do
			qid = wikidata({entityId=item, property="P364", firstvalue="t", formatting="raw", noref="true"})
			if qid and qid ~= "" then
				break
			end
		end
	end
	local lang = ""
	if qid and qid ~= "" then
        mw.log(qid)
		lang = wikidata({entityId=qid, property="P218", noref="true", firstvalue="t"})
		if lang and lang ~= "" then
			lang = "|language =" .. lang
		end
	end
	return lang
end

local function gettherest(items, claims, qid)
	local s = {}
	for i, j in pairs(claims) do
		for k, l in pairs(cite_functions.Known_properties) do
			if i == l then
				return ""
			end
		end
		local entity1 = mw.wikibase.getEntityObject(i)
		if (entity1.datatype == "string" or entity1.datatype == "external-id") then
			local vaa = GetValueFromEntityId(claims, qid, i, "", "true")
			if vaa and vaa ~= "" then
				return "|url=" .. vaa
			end
		end
	end
end

local function gettitle(items, claims)
	local title = GetItemValue(items, claims, "P1476", "", "")
	local subtitle = GetItemValue(items, claims, "P1680", "", "")
	if not title or title == "" then
		title = subtitle
	elseif subtitle and subtitle ~= "" then
		
		title = title .. ": " .. subtitle
	end
	if title then
		title = mw.ustring.gsub(title, "|", "/")
		title = "|title = " .. title
	end
	return title
end

function numbers(items, claims)
	local rows = {}
	rows.section = GetNumberFromItem(items, claims, "P958", "|section = ", "")
	rows.volume = GetNumberFromItem(items, claims, "P478", "|volume =", "yes")
	rows.page = GetNumberFromItem(items, claims, "P304", "|page	= ", "yes")
	rows.issue = GetNumberFromItem(items, claims, "P433", "|issue	 =", "yes")
	rows.chapter = GetNumberFromItem(items, claims, "P792", "|chapter =", "yes")
	rows.edition = GetNumberFromItem(items, claims, "P393", "|edition =", "yes")

	for i, item in pairs(items) do
		if item == "Q36578" then
			rows.edition = nil
			break
		end
	end

	local field = {}
	for _, line in pairs(rows) do
		if line and line ~= "" then
			table.insert(field, line)
		end
	end

	return table.concat(field, "\n")
end

function doipmid(items, claims)
	local doi = GetNumberFromItem(items, claims, "P356", "|doi =", "true") -- Digital Object Identifier
	local pmid = GetNumberFromItem(items, claims, "P698", "|pmid =", "true") -- Identifier for journal articles/abstracts in PubMed
	local pmc = GetNumberFromItem(items, claims, "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
	if #field > 0 then
		local magazine = GetItemValue(items, claims, "P1433", "|magazine =", " ")
		if magazine and magazine ~= "" then
			table.insert(field, magazine)
		end
	end
	return table.concat(field, "\n")
end

local function Getquall(items, claims, qid)
	local quall = getsomequall(items, claims, qid, "P143")
	local quall1 = getsomequall(items, claims, qid, "P248")
	if not quall or quall == "" then
		quall = quall1
	end
	if quall and quall ~= "" then
		return "|url=" .. quall
	end
end

function getMultiLink(items, claims, qid)
	local link = getLink(items, claims, "P854", "|url =")
	if not link or link == "" then
		link = gettherest(items, claims, qid)
	end
	if not link or link == "" then
		link = Getquall(items, claims, qid)
	end
	if not link or link == "" then
		link = get_Link_from_item(items, claims, "P854")
	end
	if not link or link == "" then
		link = get_Link_from_item(items, claims, "P953")
	end
	local accessdate = GetItemValue(items, claims, "P813", "|access-date = ", "true")

	if link and link ~= "" then
		if accessdate and accessdate ~= "" then
			link = link .. "\n" .. accessdate
		end
	end

	return link
end

function getarchiveLink(items, claims, qid)
	local archive = getLink(items, claims, "P1065", "|archiveurl =") --
	local archivedate = GetItemValue(items, claims, "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 getreftable(items, claims, options)
	local qid = options.entityId or options.qid
    mw.log("getreftable qid: " .. qid)
	local rows = {}
	rows.author = p.authorlist(Getauthor(items, claims), "author")
	rows.editor = p.authorlist(GetItemValue(items, claims, "P98", "", ""), "editor")
	rows.translator = p.authorlist(GetItemValue(items, claims, "P655", "", ""), "translator")

	rows.title = gettitle(items, claims)

	rows.publisher = GetItemValue(items, claims, "P123", "|publisher = ")
	 --publisher
	rows.publication_place = GetItemValue(items, claims, "P291", "|publication-place = ")
	rows.publishdate = GetItemValue(items, claims, "P577", "|publication-date =", "true") -- تاريخ النشر:

	rows.creator = ""
	 --GetItemValue(items, claims, 'P170', 'مخترع')
	rows.language = Getlanguage(items, claims)

	rows.doipmid = doipmid(items, claims)
	rows.number  = numbers(items, claims)
	rows.import  = getwhatever(items, claims, "P143") or getwhatever(items, claims, "P248")

	--rows.import    = getwhatever(items, claims, "P143", '|work =' )
	--rows.import1   = getwhatever(items, claims, "P248", '|work=' )
	rows.MultiLink   = getMultiLink(items, claims, qid)
	rows.archiveLink = getarchiveLink(items, claims, qid)

	--rows.license = GetItemValue(items, claims, 'P275', 'رخصة')
	rows.isbn    = getISBN(items, claims) --0

	rows.oclc    = GetNumberFromItem(items, claims, "P243", "|OCLC=", "yes")
	rows.asin    = GetNumberFromItem(items, claims, "P5749", "|asin=", "yes")
	rows.arxiv   = GetNumberFromItem(items, claims, "P818", "|arxiv=", "yes")
	rows.biorxiv = GetNumberFromItem(items, claims, "P3951", "|biorxiv=", "yes")
	rows.issn    = GetNumberFromItem(items, claims, "P236", "|issn=", "yes")
	rows.jstor   = GetNumberFromItem(items, claims, "P888", "|jstor=", "yes")
	rows.lccn    = GetNumberFromItem(items, claims, "P1144", "|lccn=", "yes")

	rows.mr    = GetNumberFromItem(items, claims, "P889", "|mr=", "yes")
	rows.ol    = GetNumberFromItem(items, claims, "P648", "|ol=", "yes")
	rows.osti  = GetNumberFromItem(items, claims, "P3894", "|osti=", "yes")
	rows.rfc   = GetNumberFromItem(items, claims, "P892", "|rfc=", "yes")
	rows.ssrn  = GetNumberFromItem(items, claims, "P893", "|ssrn=", "yes")
	rows.s2cid = GetNumberFromItem(items, claims, "P8299", "|s2cid=", "yes")
	rows.zbl   = GetNumberFromItem(items, claims, "P894", "|zbl=", "yes")
	rows.quote = GetItemValue(items, claims, "P1683", "|quote =", " ")

	for i, item in pairs(items) do
		if item == "Q36578" then
			rows.author = nil
			break
		end
	end

	local field = {}

	for _, line in pairs(rows) do
		if line and line ~= "" then
			table.insert(field, line)
		end
	end

	local openn = "{{cite web\n"
	local closee = "\n}}"

	if (rows.doipmid and rows.doipmid ~= "") then
		openn = "{{Cite magazine\n"
	elseif (rows.isbn and rows.isbn ~= "") then
		openn = "{{cite book\n"
	elseif (rows.asin and rows.asin ~= "") then
		openn = "{{cite journal\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, claims)
	-- function to ignore Sister projects
	local val = "false"
	local s = {}
	for i, item in pairs(items) do
		function qoo(item, pid)
			return wikidata({entityId = item, property=pid, 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" or -- Human is not a direct reference
				wik2 == "Q184188" or -- Also Canton
				wik2 == "Q1250464"
		 then
			val = "true" -- Also places
		end
	end
	return val
end

function p.citeitem(claims, hash, options, notag)
	if not claims then
        mw.log("no claims")
		return nil
	end
    local items = {}

    if claims.P248 then
        for j, prop in pairs(claims.P248) do
            if prop.snaktype == "value" then
                table.insert(items, "Q" .. prop.datavalue.value["numeric-id"])
            end
        end
    elseif claims.P143 then
        for j, prop in pairs(claims.P143) do
            if prop.snaktype == "value" then
                table.insert(items, "Q" .. prop.datavalue.value["numeric-id"])
            end
        end
    end
    
	local blacklist1 = findblacklist(items, claims)
	if blacklist1 == "true" then
        mw.log("blacklist1")
		return nil
	end
	local refrence = getreftable(items, claims, options)
	if refrence and refrence ~= "" then
		if notag and notag ~= "" then
			refrence = refrence
		else
			refrence = mw.getCurrentFrame():extensionTag("ref", refrence)
		end
	end
	return refrence
end

function p.reflist(frame)
    local args = frame.args
	local list = mw.text.split(args.item, " ")
	local s = {}
	for i, qid in pairs(list) do
		qid = mw.text.trim(qid)
        mw.log("qid:" .. qid)
        args.qid = qid
		local item = mw.wikibase.getEntityObject(qid)
		local str = p.citeitem(item.claims, "", 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 args = frame.args
    local qid = args.item or args.qid
    args.qid = qid
	local item = mw.wikibase.getEntityObject(qid)
	if not item then
		return "no item "
	end
	local fi = p.citeitem(item.claims, "", args, "t")
	if args.nowiki then
		return mw.getCurrentFrame():extensionTag("pre", mw.ustring.gsub(fi, "\n", ""))
	else
		return frame:preprocess(fi)
	end
end

function p.cite_property(frame)
	local qid = frame.args.item
	local item = mw.wikibase.getEntityObject(qid)

	if not item then
		return "no item "
	end
	local prop = mw.text.split(frame.args.property, ",")
	local s = {}
	for i, pid in pairs(prop) do
		mw.log("prop:" .. pid)
		local ref = GetValueFromEntityId(item.claims, qid, pid, "", "true")
		if ref and ref ~= "" then
			ref = "|url=" .. ref
			ref = "{{مرجع ويب\n" .. ref .. "\n}}"
			if frame.args.tag and frame.args.tag ~= "" then
				ref = mw.getCurrentFrame():extensionTag("ref", ref)
			end
			table.insert(s, ref)
		end
	end
	local temp = table.concat(s, "\n")
	if not temp or temp == "" then
		return ""
	end
	if frame.args.nowiki then
		return mw.getCurrentFrame():extensionTag("pre", mw.ustring.gsub(temp, "\n", ""))
	else	
		return frame:preprocess(temp)
	end
end

return p