跳转到内容

模組:Infobox number

被永久保护的模块
维基百科,自由的百科全书
local p={}
local lib_arg={}
local base_data = {unary=1,binary=2,ternary=3,quaternary=4,quinary=5,senary=6,septenary=7,octal=8,nonary=9,denary=10,decimal==10,undecimal=11,duodecimal=12,tridecimal=13,tetradecimal=14,pentadecimal=15,hexadecimal=16,heptadecimal=17,octodecimal=18,enneadecimal=19,vigesimal=20,unvigesimal=21,duovigesimal=22,trivigesimal=23,tetravigesimal=24,pentavigesimal=25,hexavigesimal=26,heptavigesimal=27,septemvigesimal=27,octovigesimal=28,enneavigesimal=29,trigesimal=30,untrigesimal=31,duotrigesimal=32,tritrigesimal=33,tetratrigesimal=34,pentatrigesimal=35,hexatrigesimal=36,heptatrigesimal=37,octotrigesimal=38,quadragesimal=40,duoquadragesimal=42,pentaquadragesimal=45,octoquadragesimal=48,enneaquadragesimal=49,quinquagesimal=50,duoquinquagesimal=52,tetraquinquagesimal=54,hexaquinquagesimal=56,heptaquinquagesimal=57,octoquinquagesimal=58,sexagesimal=60}
local pre60={'一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五','十六','十七','十八','十九','二十','二十一','二十二','二十三','二十四','二十五','二十六','二十七','二十八','二十九','三十','三十一','三十二','三十三','三十四','三十五','三十六','三十七','三十八','三十九','四十','四十一','四十二','四十三','四十四','四十五','四十六','四十七','四十八','四十九','五十','五十一','五十二','五十三','五十四','五十五','五十六','五十七','五十八','五十九','六十','六十一','六十二','六十三','六十四',[0]='零'}
local numberToChinese = require('Module:NumberToChinese')
local aliasesP = {
	coord                   = "P625",
	-----------------------
	image                   = "P18",
	author                  = "P50",
	publisher               = "P123",
	importedFrom            = "P143",
	statedIn                = "P248",
	pages                   = "P304",
	language                = "P407",
	hasPart                 = "P527",
	publicationDate         = "P577",
	startTime               = "P580",
	endTime                 = "P582",
	chapter                 = "P792",
	retrieved               = "P813",
	referenceURL            = "P854",
	sectionVerseOrParagraph = "P958",
	archiveURL              = "P1065",
	title                   = "P1476",
	formatterURL            = "P1630",
	quote                   = "P1683",
	shortName               = "P1813",
	definingFormula         = "P2534",
	archiveDate             = "P2960",
	inferredFrom            = "P3452",
	typeOfReference         = "P3865",
	column                  = "P3903"
}
local special_wikidata_code = {
	['Q68656494'] = {
		title='摩尔斯电码'
	},
	['Q68101340'] = {
		title='古埃及数字',
		rendering=function(text)
			return "<div style='display:inline-block;'>" .. mw.getCurrentFrame():extensionTag{ name = 'hiero', content = text }..'</div>'
		end
	},
	['Q69476427'] = {
		title='玛雅数字',
		rendering=function(text)
			return "[[File:"..text.."|30px]]"
		end
	}
}
local code_prop_blacklist = {"Q29961325", "Q68401769", "Q38918"}
local function includes(t,item)
	for _,value in ipairs(t) do
		if value==item then return true end
	end
	return false
end
function p.codedata(frame)
	-- For calling from #invoke. {{#invoke:Infobox number|nbase}}
	local args, working_frame
	if frame == mw.getCurrentFrame() then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
		args = lib_arg.getArgs(frame, {
			parentFirst=true
		}) --frame
	else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        if type(args) ~= type({}) then args = {frame} end
    end
    working_frame = mw.getCurrentFrame()
    local wikidata_id = args.WikidataID or ''
    if mw.text.trim(wikidata_id) == '' then
    	wikidata_id = mw.wikibase.getEntityIdForCurrentPage() or ''
    end
    if mw.text.trim(wikidata_id) == '' then return '' end --找無維基數據項目
    local i18n = require("Module:Wd/i18n").init(aliasesP)
    local body = ''
    local props = {"P3295","P7415"}
    local codes_entity = {}
    for _,prop in ipairs(props) do
    	local load_prop = mw.wikibase.getAllStatements(wikidata_id, prop)
    	for __,code_item in ipairs(load_prop) do table.insert(codes_entity, code_item)end
    end
    for _,code_item in ipairs(codes_entity) do
    	mw.logObject(code_item)
    	local code_type = (code_item.qualifiers or {})['P3294']
    	if code_type[1] then
    		local code_id = code_type[1].datavalue.value.id
    		local render_func = function(text)return text end
    		local wikidata_code_obj = special_wikidata_code[code_id] or {rendering = render_func}
    		local code_title = mw.wikibase.getSitelink(code_id) or wikidata_code_obj.title
    		if code_title and not includes(code_prop_blacklist,code_id) then
    			local rendering = wikidata_code_obj.rendering or render_func
    			local ret = rendering(code_item.mainsnak.datavalue.value)
				ret = ret .. '<span style="float:right">' .. "[[File:OOjs UI icon edit-ltr-progressive.svg|frameless|text-top|10px|alt=" .. 
					i18n['info']['edit-on-wikidata'] .. "|link=https://www.wikidata.org/wiki/" .. 
					wikidata_id .. "?uselang=zh#" .. code_item.id .. "|" .. i18n['info']['edit-on-wikidata'] .. "]]</span>"
    			body = body .. '<tr><th>[['.. code_title .. ']]</th><td>'..ret..'</td></tr>'
    		end
    	end
    end
    return body
end
function p.nbase(frame)
	-- For calling from #invoke. {{#invoke:Infobox number|nbase}}
	local args, working_frame
	if frame == mw.getCurrentFrame() then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
		args = lib_arg.getArgs(frame, {
			parentFirst=true
		}) --frame
	else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
    end
    working_frame = mw.getCurrentFrame()
    local base_args = {}
    for k,v in pairs(args) do
    	local finder=mw.ustring.lower(k)
		if mw.ustring.find(finder,'[進进][位制]') or mw.ustring.find(finder,'base') then
			if finder~='进位制' and finder~='進位制' then
				local basedata=mw.ustring.gsub(finder,'[進进][位制]','')
				basedata=mw.ustring.gsub(basedata,'base','')
				local base_id = basedata
				xpcall(function()base_id=numberToChinese._numberToChinese(basedata)end,function(err)end)
				if base_id==numberToChinese.NotANumber() then base_id = basedata end
				base_args[base_id] = v
			end
		elseif base_data[finder] then
			local base_id = numberToChinese._numberToChinese(base_data[finder])
			base_args[base_id] = v
		end
    end
    local body = ''
    local test_data = {}
    for i=0,64 do
    	if base_args[pre60[i]] then
    		if mw.text.trim(base_args[pre60[i]]) ~= '' and mw.text.trim(base_args[pre60[i]]) ~= 'hide' then
	    		body = body .. '<tr><th>[['.. pre60[i] .. '进制]]</th><td>'..base_args[pre60[i]]..'</td></tr>'
    		end
    		test_data[pre60[i]]=true
    	end
    end
    for k,v in pairs(base_args) do
    	if not test_data[k] then
    		if mw.text.trim(v) ~= '' and mw.text.trim(v) ~= 'hide' then
    			body = body .. '<tr><th>[['.. k .. '进制]]</th><td>'..v..'</td></tr>'
    		end
    	end
    end
	return body
end
return p