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

وحدة:ملعب/حبيشان/Citation Doc

من ويكيبيديا، الموسوعة الحرة
local basic_arguments_t =  {
    "osti-access",
    "ContributorList-Mask",
    "Scale",
    "Place",
    "ASIN",
    "TransChapter",
    "ContributorList-Last",
    "ChapterURL",
    "Agency",
    "URL",
    "AuthorList-Last",
    "Edition",
    "TranslatorList-Link",
    "JSTOR",
    "ArchiveDate",
    "TranslatorList-First",
    "s2cid-access",
    "ChapterFormat",
    "ISSN",
    "PublicationPlace",
    "hdl-access",
    "OCLC",
    "Mode",
    "ChapterUrlAccess",
    "DisplayEditors",
    "ScriptPeriodical",
    "ArchiveURL",
    "Language",
    "OL",
    "Time",
    "Format",
    "AuthorList-First",
    "PublisherName",
    "DisplayAuthors",
    "AccessDate",
    "Chapter",
    "InterviewerList-Mask",
    "Encyclopedia",
    "Pages",
    "ARXIV",
    "ContributorList-First",
    "PMC",
    "ArchiveFormat",
    "TitleLink",
    "Others",
    "TranslatorList-Last",
    "TitleNote",
    "Date",
    "EditorList-Link",
    "AuthorList-Link",
    "InterviewerList-Last",
    "Page",
    "Series",
    "BIBCODE",
    "Veditors",
    "Title",
    "isbn",
    "PublicationDate",
    "DisplayContributors",
    "Periodical",
    "HDL",
    "ZBL",
    "Issue",
    "QuotePage",
    "S2CID",
    "Ref",
    "Authors",
    "PMID",
    "DOI",
    "DisplayInterviewers",
    "BIORXIV",
    "NoPP",
    "MR",
    "Collaboration",
    "OSTI",
    "TransTitle",
    "bibcode-access",
    "LCCN",
    "ContributorList-Link",
    "Via",
    "EditorList-Mask",
    "InterviewerList-First",
    "EditorList-Last",
    "EISSN",
    "SSRN",
    "TitleType",
    "Volume",
    "UrlStatus",
    "TransPeriodical",
    "Vauthors",
    "TransQuote",
    "EditorList-First",
    "ScriptQuote",
    "OrigDate",
    "TranslatorList-Mask",
    "Year",
    "MEDRXIV",
    "AuthorList-Mask",
    "Section",
    "NoTracking",
    "DoiBroken",
    "ASINTLD",
    "ScriptChapter",
    "Minutes",
    "ssrn-access",
    "ArticleNumber",
    "CITESEERX",
    "JFM",
    "InterviewerList-Link",
    "RFC",
    "ScriptTitle",
    "SBN",
    "doi-access",
    "Number",
    "ID",
    "Quote",
    "At",
    "TimeCaption",
    "UrlAccess",
    "QuotePages",
    "ISMN",
    "ISBN",
    "DF",
    "NameListStyle",
    "DisplayTranslators",
    "PostScript",
    "Embargo",
    "ol-access",
    "jstor-access",
}
local document_arguments_t =  {
    "osti-access",
    "TranslatorList-Last",
    "Place",
    "TitleType",
    "PublisherName",
    "ScriptQuote",
    "TranslatorList-First",
    "TransQuote",
    "TranslatorList-Link",
    "OrigDate",
    "BIBCODE",
    "Language",
    "DOI",
    "TranslatorList-Mask",
    "DoiBroken",
    "doi-access",
    "ID",
    "HDL",
    "ZBL",
    "QuotePage",
    "TitleLink",
    "JFM",
    "ScriptTitle",
    "NoPP",
    "MR",
    "OSTI",
    "bibcode-access",
    "QuotePages",
    "hdl-access",
}
local document_numbered_arguments_t =  {
    "TranslatorList-Link",
    "TranslatorList-First",
    "TranslatorList-Mask",
    "TranslatorList-Last",
}
local limited_basic_arguments_t =  {
    "AuthorList-Last",
    "Page",
    "Quote",
    "Date",
    "AuthorList-Link",
    "AuthorList-First",
    "Year",
    "DisplayAuthors",
    "Language",
    "TransTitle",
    "Pages",
    "Title",
    "DF",
    "Mode",
    "NameListStyle",
    "PostScript",
    "Ref",
    "Authors",
    "Vauthors",
    "AuthorList-Mask",
    "Collaboration",
    "At",
    "NoTracking",
}
local limited_numbered_arguments_t =  {
    "AuthorList-Link",
    "AuthorList-Mask",
    "AuthorList-First",
    "AuthorList-Last",
}
local numbered_arguments_t =  {
    "TranslatorList-Link",
    "EditorList-First",
    "AuthorList-First",
    "EditorList-Link",
    "ContributorList-Last",
    "AuthorList-Last",
    "EditorList-Last",
    "TranslatorList-First",
    "InterviewerList-Link",
    "InterviewerList-Mask",
    "TranslatorList-Last",
    "ContributorList-Link",
    "InterviewerList-First",
    "TranslatorList-Mask",
    "InterviewerList-Last",
    "AuthorList-Link",
    "ContributorList-First",
    "AuthorList-Mask",
    "ContributorList-Mask",
    "EditorList-Mask",
}
local preprint_arguments_t =  {
    ["arxiv"] = {
        "ARXIV",
        "Class",
    },
    ["biorxiv"] = {
        "BIORXIV",
    },
    ["citeseerx"] = {
        "CITESEERX",
    },
    ["medrxiv"] = {
        "MEDRXIV",
    },
    ["ssrn"] = {
        "ssrn-access",
        "SSRN",
    },
}
local unique_arguments_t =  {
    ["audio-visual"] = {
        "TranscriptFormat",
        "Transcript",
        "TranscriptURL",
    },
    ["conference"] = {
        "BookTitle",
        "ConferenceFormat",
        "ConferenceURL",
        "Conference",
    },
    ["episode"] = {
        "TranscriptFormat",
        "Date",
        "Season",
        "Transcript",
        "Network",
        "TitleLink",
        "SeriesLink",
        "Authors",
        "TranscriptURL",
        "Station",
        "SeriesNumber",
    },
    ["mailinglist"] = {
        "Periodical",
    },
    ["map"] = {
        "Sections",
        "MapURL",
        "Sheet",
        "Cartography",
        "TransMap",
        "Sheets",
        "Inset",
        "Map",
        "MapFormat",
        "ScriptMap",
        "MapUrlAccess",
    },
    ["newsgroup"] = {
        "Newsgroup",
        "MessageID",
    },
    ["report"] = {
        "Docket",
    },
    ["serial"] = {
        "Authors",
        "Episode",
        "Date",
        "Network",
        "TitleLink",
        "Station",
        "SeriesLink",
    },
    ["speech"] = {
        "ConferenceURL",
        "ConferenceFormat",
        "Conference",
    },
    ["thesis"] = {
        "Degree",
        "Docket",
    },
}

local td = mw.text.jsonDecode(mw.title.new ('Module:Citation/CS1/td.json'):getContent());
local conf = require('Module:Citation/CS1/Configuration/ملعب');
local translated =  {'label', 'description','example','default' }
local boolprops = {'suggested', 'required', 'deprecated'}
local order_n = { "الأول", "الثاني", "الثالث", "الرابع", "الخامس", "السادس", "السابع", "الثامن","التاسع"}
local templates = {
	['استشهاد'] = 'citation',
	['استشهاد بأرخايف'] = 'arxiv',
	['cite chemrxiv'] = 'chemrxiv',
	['استشهاد بوثيقة'] = 'document',
	['استشهاد بتدوين صوتي'] = 'podcast',
	['cite ssrn'] = 'ssrn',
	["استشهاد بخبر"] = "news",
	["استشهاد بدورية محكمة"] = "journal",
	["استشهاد ويب"] = "web",
	["استشهاد بوسائط مرئية ومسموعة"] = "audio-visual",
	["استشهاد ببيان صحفي"] = "pressrelease",
	["استشهاد بمجموعة أخبار"] = "newsgroup",
	["استشهاد بمنشورات مؤتمر"] = "conference",
	["استشهاد بموسوعة"] = "encyclopaedia",
	["استشهاد بخريطة"] = "map",
	["استشهاد بحلقة"] = "episode",
	["استشهاد بمقابلة"] = "interview",
	["استشهاد بقائمة بريدية"] = "mailinglist",
	["استشهاد بأطروحة"] = "thesis",
	["استشهاد بتقرير"] = "report",
	["استشهاد بكتاب"] = "book",
	["استشهاد بتقرير فني"] = "techreport",
	["استشهاد بمجلة"] = "magazine",
	["استشهاد بمسلسل"] = "serial",
	["استشهاد بإشارة توضيحية"] = "sign",
	["استشهاد بخطاب"] = "speech",
	["استشهاد بتسجيلات"] = "AV-media-notes",
	["استشهاد ببيوركسيف"] = "biorxiv",
	["Cite ssrn"] = "ssrn",
	["Cite medRxiv"] = "medrxiv",
}
	local custom_access = {}
	for _,v in pairs(conf.id_handlers) do
		if v.custom_access then
			table.insert(custom_access,v.custom_access)
		end
	end

local limited_templates = {'arxiv', 'biorxiv', 'citeseerx', 'medrxiv', 'ssrn'}

local function in_array (needle, haystack)
	if needle == nil then
		return false;
	end
	for n, v in ipairs (haystack) do
		if v == needle then
			return n;
		end
	end
	return false;
end


local function template_data_json_get (template)
	local json = mw.title.new (template):getContent() or '';					-- get the content of the article or ''; new pages edited w/ve do not have 'content' until saved; ve does not preview; phab:T221625
	json = json:match ('<[Tt]emplate[Dd]ata>(.-)</[Tt]emplate[Dd]ata>');		-- remove everything exept the content of the TemplatData tags
	return json and mw.text.jsonDecode (json);									-- decode the json string and return as a table; nil if not found
end

local function merge(t1,t2,lang,atype,copyother)
	for k,v in pairs(t2) do
		if k == 'label' or  k == 'description'  or  k == 'example'  or  k == 'default' then
			if not t1[k] then 
				t1[k] ={[atype] = {[lang]=v}} 
			elseif (not t1[k][atype] and t1[k].default and ((t1[k].default.en == v) or (t1[k].default[lang] == v)))
				or (t1[k][atype] and (t1[k][atype].en == v)) then
				-- do nothing
			elseif not t1[k][atype] then
				t1[k][atype] = {[lang]=v}
			else
				t1[k][atype][lang] = v
			end
		elseif copyother==2 or (copyother==1 and k ~= 'aliases') then
			t1[k] = v
		end
	end
	return t1
end

function main(frame)
	local templatear = mw.title.getCurrentTitle().baseText
	local template = templates[templatear] 
	local r = {}
	local pa = {}
	local ids = {}
	if template == nil then
		return error("بيانات القالب غير موجودة")
	end
	
	local function tinsert(ot,insrt)
		if type(insrt) == 'table' then
			for _,v in pairs(insrt) do
				if not in_array(v,ot) then
					table.insert(ot,v)
				end
			end
		elseif not in_array(insrt,ot) then
			table.insert(ot,insrt)
		end
		return ot
	end

	if preprint_arguments_t[template] or template == 'document' then
		pa = limited_basic_arguments_t
		pa = tinsert(pa,limited_numbered_arguments_t)
		if preprint_arguments_t[template] then
			pa = tinsert(pa,preprint_arguments_t[template])
		else
			pa = tinsert(pa,document_arguments_t)
			pa = tinsert(pa,document_numbered_arguments_t)
		end
	else
		pa = basic_arguments_t
		pa = tinsert(pa,numbered_arguments_t)
		
		if unique_arguments_t[template] then
			pa=tinsert(pa,unique_arguments_t[template])
		end

		local i
		if in_array(template, conf.templates_not_using_page) then
			i = in_array('Page',pa)
			if i then table.remove(pa,i) end
			i = in_array('Pages',pa)
			if i then table.remove(pa,i) end
		end
		i = in_array('Volume',pa)
		if i and not in_array(template, conf.templates_using_volume) then
			table.remove(pa,i)
		end
		i = in_array('Issue', pa)
		if i and not in_array(template, conf.templates_using_volume) then
			table.remove(pa,i)
		end

	end
	mw.logObject(pa);
	r.description = td.description[template] and (td.description[template].ar or td.description[template].en) or (td.description.default.ar .. templatear)
	r.format = "inline"
	local posource = (td.paramOrder[template] and template) or (td.paramOrder.default and "default")
	r.paramOrder = {}	
	if posource then
		for _,v in ipairs(td.paramOrder[posource]) do
			vv= v:gsub("%d$","")
			if in_array(vv,pa) and td.params[vv] then
				table.insert(r.paramOrder,v)
			end
		end
	end
	r.params = {}
	
	for _,k in pairs(pa) do
		if not td.params[k] then
			mw.log('no param: ' .. k)
		else
			local oaliases
			oaliases = conf.aliases[k] or (conf.id_handlers[k] and conf.id_handlers[k].parameters) or (in_array(k,custom_access) and k) or nil
			if oaliases==nil then
				--mw.logObject(k,"no aliases for ")			
				return error("no aliases for " .. k)
			end
			local ic=td.params[k].numbered and 9 or 1
			ids[k] = (type(oaliases)=="table") and oaliases[1] or oaliases
			for i =1,ic do
				local ak,aliases,ki
				if td.params[k].numbered then
					aliases = {}
					for _,vv in ipairs(oaliases) do
						if i==1 then
							local txt = mw.ustring.gsub(vv, "#", "")
							table.insert(aliases, txt)
						end
						local txt = mw.ustring.gsub(vv, "#", tostring(i))
						table.insert(aliases, txt)
					end
					ki=k .. (i==1 and "" or tostring(i))
				else
					ki=k
					aliases=oaliases
				end

				if type(aliases) == "table" then
					ak=aliases[1]
					if #aliases>1 then
						ak=table.remove(aliases,1)
					else
						aliases=nil
					end
				else
					ak=aliases
					aliases=nil
				end

				r.params[ak] = {}
				local j=in_array(ki, r.paramOrder)
				if j then
					r.paramOrder[j]=ak
				else
					table.insert(r.paramOrder,ak)
				end

				if aliases then
					r.params[ak].aliases = aliases
				end

				for kk,v in pairs(td.params[k]) do
					if kk == "numbered" then
						-- do nothing
					elseif type(v) ~= "table" then
						r.params[ak][kk] = v
					elseif in_array(kk, boolprops) then
						if i==1 then
							r.params[ak][kk] = v[template] or v.default or nil
						end
					elseif not in_array(kk, translated) then
						r.params[ak][kk] = v[template] or v.default or v
					elseif in_array(kk, translated) then
						local vv = v.ar or v.en
						r.params[ak][kk] = vv[template] or vv.default or vv
						if td.params[k].numbered then
							r.params[ak][kk]= mw.ustring.gsub(r.params[ak][kk], "$1", (i>1 and (" " .. order_n[i]) or ""))
						end
					end
				end
			end
		end
	end
	--check
	for k,_ in pairs(r.params) do
		if not in_array(k,r.paramOrder) then
			mw.log(k .. ": not in order")
		end
	end
	for _,k in pairs(r.paramOrder) do
		if not r.params[k] then
			mw.log(k .. ": not in param")
		end
	end
	
	if td.maps.citoid[template] then
		r.maps = {citoid=td.maps.citoid[template]}
		for k,v in 	pairs(r.maps.citoid) do
			if in_array(k,{"author","editor","translator"}) then
				local idf = ids[k:gsub("^%l", string.upper) .. "List-First"]
				local idl = ids[k:gsub("^%l", string.upper) .. "List-Last"]
				r.maps.citoid[k] = {{(idf:gsub("#","")),(idl:gsub("#",""))}}
				for i = 2,9 do
					table.insert(r.maps.citoid[k], {(idf:gsub("#",i)),(idl:gsub("#",i))})
				end
			else
				if ids[v] then
					r.maps.citoid[k] = ids[v]
				elseif type(v)=="table" then
					for kk,vv in pairs(v) do
						r.maps.citoid[k][kk] = ids[vv] or vv:lower()
					end
				else
					mw.log("citoid: " .. k .. " not found")
				end
			end
		end
	end
	--mw.logObject(mw.text.jsonEncode( r, mw.text.JSON_PRETTY))
	return mw.text.jsonEncode( r, mw.text.JSON_PRETTY) 
end

local function check_no_default(frame)
	mw.log("incheck")
	for k,v in pairs(td.params) do
		for kk,vv in pairs(v) do
			if type(vv) =="table" then
				if in_array(kk, translated) then 
					if not vv.en then
						mw.log("no en: " .. k .. "." .. kk);
					elseif not vv.en.default then
						mw.log("==" .. k .. "." .. kk);
						local mm ,mr ,ms={},0;
						
						for kkk,vvv in pairs(vv.en) do
							mm[vvv] = (mm[vvv] and (mm[vvv] +1)) or 1
							if mm[vvv]>mr then
								mr=mm[vvv]
								ms=vvv
							end
						end
						for kkk,vvv in pairs(vv.en) do
							if vvv==ms then
								vv.en[kkk] = nil
							end
						end
						vv.en.default=ms
					end
				elseif vv.default==nil then
					mw.log(k .. "." .. kk)
				end
			end
		end
	end
	return '<div dir="ltr">\n' ..  mw.text.jsonEncode( td.params, mw.text.JSON_PRETTY) .. '\n</div>\n'
end

local function setargs(frame)
	local conf = require('Module:Citation/CS1/Configuration');
	local wl = require('Module:Citation/CS1/Whitelist');
	local function getOrign( aparam )
		for k,v in pairs(conf.aliases) do
			if type(v) =="table" then
				for _,vv in pairs(v) do
					if aparam == vv or aparam == vv:gsub('#', '') then
						return k;
					end
				end
			else
				if aparam == v then
					return k;
				end
			end
		end
		for k,v in pairs(conf.id_handlers) do
			for _,vv in pairs(v.parameters) do
				if aparam == vv then
					return k;
				end
			end
			if aparam == v.custom_access then
				return aparam;
			end
		end
		return error(aparam .. ": not found")
	end
	
	local function fetchTable(t)
		local r={}
		for k,v in pairs(t) do
			if type(v) == "table" then
				r[k]= fetchTable(v)
			else
				local okk=getOrign(k)
				if okk and not in_array(okk,r) then
					table.insert(r,okk)
				end
			end
		end
		return r
	end
	
	return mw.logObject(fetchTable({
		basic_arguments_t = basic_arguments_t,
		numbered_arguments_t = numbered_arguments_t,
		preprint_arguments_t = preprint_arguments_t,
		limited_basic_arguments_t = limited_basic_arguments_t,
		limited_numbered_arguments_t = limited_numbered_arguments_t,
		unique_arguments_t = unique_arguments_t,
		document_arguments_t = document_arguments_t,
		document_numbered_arguments_t = document_numbered_arguments_t
		
	}))
end

local function setcoid(frame)
	local conf = require('Module:Citation/CS1/Configuration');
	-- local wl = require('Module:Citation/CS1/Whitelist');
	local coid = mw.text.jsonDecode(doc)
	local function getOrign( aparam )
		
		for k,v in pairs(conf.aliases) do
			if type(v) =="table" then
				for _,vv in pairs(v) do
					if aparam == vv or aparam == vv:gsub('#', '') then
						return k;
					end
				end
			else
				if aparam == v then
					return k;
				end
			end
		end
		for k,v in pairs(conf.id_handlers) do
			for _,vv in pairs(v.parameters) do
				if aparam == vv then
					return k;
				end
			end
			if aparam == v.custom_access then
				return aparam;
			end
		end
		return error(aparam .. ": not found")
	end
	
	local function fetchTable(t)
		local r={}
		for k,v in pairs(t) do
			if in_array(k,{"author","editor","translator"}) then
				r[k]=""
			elseif type(v) == "table" then
				r[k]= fetchTable(v)
			else
				r[k] = getOrign(v)
			end
		end
		return r
	end
	
	return  mw.text.jsonEncode( fetchTable(coid), mw.text.JSON_PRETTY)
end


local function exjson(frame)
	local td = mw.loadJsonData('Module:Citation/CS1/td.json');
	local translated =  {'label', 'description','example','default' }
	local pa = {}
	for k,v in pairs(td['params-numbered']) do
		pa[k] = {}
		for kk,vv in pairs(v) do
			if not in_array(kk, translated) then
				pa[k][kk] = vv
			else
				pa[k][kk] = {}
				for kkk,vvv in pairs(vv) do
					for kkkk,vvvv in pairs(vvv) do
						if not pa[k][kk][kkkk] then
							pa[k][kk][kkkk] = {[kkk] = vvvv}
						else
							pa[k][kk][kkkk][kkk] = vvvv
						end
					end
				end
			end
		end
	end
	return '<div dir="ltr">\n' ..  mw.text.jsonEncode( pa, mw.text.JSON_PRETTY) .. '\n</div>\n'
end

local function check(frame)
	local td = mw.loadJsonData('Module:Citation/CS1/td.json');
	for k,v in pairs(td.params) do
		if not conf.aliases[k] and not conf.id_handlers[k] and not in_array(k,custom_access) then
			mw.log(k .. " in json not conf" )
		end
	end
	for k,v in pairs(conf.aliases) do
		if not td.params[k] then
			mw.log(k .. " in conf not json" )
		end
	end

end
return {
	main = main
}