模块:沙盒/a2569875/UserPageFunc

维基百科,自由的百科全书
跳到导航 跳到搜索
文档图示 模块文档[创建]
--[[
<includeonly><onlyinclude>{{#invoke:沙盒/a2569875/UserPageFunc|getTemplateVar|:{{FULLPAGENAME}}|Polyhedronbox|Face}}</onlyinclude></includeonly>
]]
local p={}
local lib_arg={};
local lib_para = {}

local num_map, num_str = {1,2,3,4,5,6,7,8,9,[0]=0,[',']=',',['’']=','}, {"0123456789","₀₁₂₃₄₅₆₇₈₉","⁰¹²³⁴⁵⁶⁷⁸⁹"}; for i=1,#num_str do for j=1,mw.ustring.len(num_str[i])do num_map[mw.ustring.sub(num_str[i],j,j)]=j-1 end end 
function p._getPolyItem(entity)
	local poly_item_property = {527,1012,1552,2670,4330}
	local propertyValues = {}
	for it=1,#poly_item_property do
		local temp_data = entity:getAllStatements( 'P' .. poly_item_property[it] )
		for key,value in pairs(temp_data) do
			propertyValues[key] = mw.clone( temp_data[key] )
		end
	end
	return propertyValues
end

function p.loadDimItem(frame)
	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})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = args[1] or args['1']
    local input_dim = -1
    local entity
    input_title = mw.text.trim(mw.ustring.upper(input_title))
   
    if mw.ustring.find(input_title,"^Q%d+$") then
    	input_dim = tonumber(arg[2] or arg['2'] or '') or -2
    	entity = mw.wikibase.getEntity(input_title)
    elseif tonumber(input_title or '') then
    	input_dim = tonumber(input_title)
    	entity = mw.wikibase.getEntity()
    end
	local poly_item = {26382,3064117,2733626,4637223,18028552,18028565,18028567,18028569,18028571,
	[-1]={["item"]=27064338,["default"]=1},
	[0]={["item"]=26401},}
	local propertyValues = p._getPolyItem(entity)
	local body = ''
	local checkpoly = poly_item[tonumber(input_dim)or-2]
	local checkpoly_name = checkpoly
	local default_text = ''
	if type(checkpoly) == type({}) then
		checkpoly_name = checkpoly.item
		default_text = checkpoly.default or default_text
	end
    for it=1,#propertyValues do
    	local checker = ((propertyValues[it].mainsnak.datavalue or {}).value or {}).id
    	
    	if ('Q' .. tostring(checkpoly_name or '')) == (checker or '') then
			local getter = propertyValues[it].qualifiers["P1114"]
			if getter then
				body = body .. tonumber(getter[1].datavalue.value.amount)
			end
			break

    	end
    end
    if mw.text.trim(body) == '' then return default_text end
	return body
end

function p.loadSchlafli(frame)
	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})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = args[1] or args['1']
    local input_format = args.format or '$'
    local input_split = args.split or '、'
    local entity = mw.wikibase.getEntity(input_title or '') or mw.wikibase.getEntity()
    local propertyValues = entity:getAllStatements( 'P3228' )
    local body = ''
    for it=1,#propertyValues do
    	if body ~= '' then body = body .. input_split end
    	local sstr = propertyValues[it].mainsnak.datavalue.value
		sstr = mw.ustring.gsub(sstr, '([^,%{%}/]+)/([^,%{%}/]+)', '<sup>%1</sup>/<sub>%2</sub>')
		sstr = mw.ustring.gsub(sstr,"[,₁₂₃₄₅₆₇₈₉₀]+",function(nstr)
			if mw.ustring.len(nstr) <= 1 and mw.ustring.sub(nstr,1,1) == ',' then return nstr end
			return "<sub>"..mw.ustring.gsub(nstr,".",function(c) return num_map[c] end).."</sub>"
		end)
		sstr = mw.ustring.gsub(sstr,"[’⁰¹²³⁴⁵⁶⁷⁸⁹]+",function(nstr)
			if mw.ustring.len(nstr) <= 1 and mw.ustring.sub(nstr,1,1) == ',' then return nstr end
			return "<sup>"..mw.ustring.gsub(nstr,".",function(c) return num_map[c] end).."</sup>"
		end)
    	body = body .. mw.ustring.gsub(input_format,'%$',sstr)
    end
	return body
end

function p.templateArgWarp(frame, warpargs)
	local args, new_name
    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.
        new_name = frame.args['1'] or frame.args[1] or frame.args.name or frame.args.Name
        local parent = frame:getParent() or {['args']={}}
        args = parent.args
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        if type(frame) == type('string') then new_name = frame
        elseif type(frame) == type({}) then new_name = frame['1'] or frame[1] or frame.name or frame.Name
        end
        args = warpargs or mw.getCurrentFrame().args
    end
    local mTemplateInvocation = require('Module:Template invocation')
	return require('Module:Template invocation').invocation(new_name, args)
end

function p.testFakeStrip()
	return mw.getCurrentFrame():preprocess( "{{{1\127'\"`UNIQ--item-00010000-QINU`\"'\127}}}\127'\"`UNIQ--item-00020000-QINU`\"'\127" )
end

function p.testBreakStrip()
	return mw.ustring.gsub(mw.getCurrentFrame():preprocess(mw.ustring.gsub(mw.getCurrentFrame().args[1] .. "<!--__UINQABCQNIU__-->",'\127','') ),'\127','')
end

local yesno = {}
function p.getJSON(frame)
	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})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_title = args[1] or args['1']
    local key_alias = args['key alias'] or args.key_alias
    if (key_alias or '') ~= '' then
		if type(yesno) ~= type(tonumber) then yesno = require('Module:Yesno') end
		key_alias = yesno(key_alias or 'no')
	end
    local default = args["default"] or ''
    local fullText = mw.title.new( input_title, "Template" ):getContent()
    local jsonText = ''
    if fullText then
    	local checker, begin = mw.ustring.find(fullText, "<%s*source%s*lang%s*=%s*['\"]?[Jj][Ss][Oo][Nn]['\"]?%s*>")
    	local finish, __noop = mw.ustring.find(fullText, "<%s*/%s*source[^>]*>")
    	if checker then
    		jsonText = mw.ustring.sub(fullText,begin+1,finish-1)
    	end
    end

    local JSONobj = mw.text.jsonDecode( jsonText )
    local iterator = 2
	while args[iterator] ~= nil do
		if key_alias then
			local check_key = JSONobj[args[iterator]]
			if type(check_key) == type("string") then
				local check_key_str, find_key_str = mw.ustring.gsub(check_key,"<%s*target%s*>(.-)<%s*/%s*target%s*>", '%1')
				mw.log(args[iterator] .. ', find:' .. check_key_str)
				while find_key_str > 0 and mw.text.trim(check_key_str) ~= '' and JSONobj[check_key_str] ~= nil do
					check_key = JSONobj[check_key_str]
					if type(check_key) == type("string") then
						check_key_str, find_key_str = mw.ustring.gsub(check_key,"<%starget%s>(.-)<%s/%starget%s>", '%1')
						mw.log('   =>\"' .. check_key_str .. '\"')
					else break end
				end
			end
			JSONobj = check_key
		else JSONobj = JSONobj[args[iterator]] end
		if JSONobj == nil and default ~= nil then
			return default
		end
		iterator = iterator + 1
	end
	
	if type(JSONobj) == type({}) then
		return default
	end
	
	return JSONobj
end


function p.allSubst(frame)
	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})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_data = args[1] or args['1']
    local input_title = mw.ustring.gsub(mw.ustring.gsub(mw.ustring.gsub(input_data, 'msgnw:',''), 'safesubst:',''), 'subst:','')
    local check_i, start = mw.ustring.find(input_data, "#invoke:")
    local offset = 1
    if check_i then
    	input_title = 'Module' .. mw.ustring.sub(input_data,start)
    	offset = 2
    end
    local title_obj = mw.title.new(input_title, 'Template' )
    local page_text = mw.text.decode(title_obj:getContent())
    page_text = mw.ustring.gsub(page_text, '<%s*(/?)([a-zA-Z0-9_]+)([^>]*)>', ' {{{$__UINQ-HTML-QNIU__$|%1%2%3}}} ')
    
    page_text = mw.ustring.gsub(page_text, '<%s*/?includeonly[^>]*>', '')
	page_text = mw.ustring.gsub(page_text, '<%s*/?onlyinclude[^>]*>', '')
    page_text = mw.ustring.gsub(page_text, '<%s*/?noinclude[^>]*>', '')
    local num_args = {}
	for key,value in pairs(args) do
		local num = tonumber(key) or 0
		if num then
			if num > offset then
				num_args[key - offset] = value
			end
		else
			num_args[key] = value
		end
	end
	num_args['$__UINQ-HTML-QNIU__$'] = ''
    local subst_frame = working_frame:newChild{ title = title_obj.fullText, args = num_args }
	return subst_frame:preprocess( page_text )
end



function p.unnowiki(frame)
	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})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_n = args[1] or args['1']
	local input_data = "" .. mw.text.unstripNoWiki( input_n )
	if(mw.isSubsting())then 
		return mw.ustring.sub( input_n );
	end
	return working_frame:preprocess( input_data )
end

function p.getTemplateVar(frame)
    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})
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local finding_frame = working_frame:newChild{ title = working_frame:getTitle() .. "/FINDER", working_frame.args }
    local input_n = args[1] or args['1']
    local finder_template = args[2] or args['2'] or ''
    local finder = args[3] or args['3'] or ''
    local it_times = args['times'] or 3
	local src = ' ' .. (finding_frame:newParserValue{ text = "{{msgnw:" .. input_n .. "}}" }):expand()
	if mw.text.trim(finder_template) ~= '' then
		if lib_para._getEscapeString == nil then lib_para = require('Module:TemplateParameters') end
		local unstripped = ' ' .. mw.ustring.gsub(src ,'(&#123;&#123;)%s-(' .. lib_para._getEscapeString(finder_template) .. ')', '%1[[%2]]' );
		unstripped = mw.ustring.gsub(unstripped ,'ref', '[[ref]]' );
		local reexpand = '. ' .. (finding_frame:newParserValue{ text = mw.text.decode( unstripped ) }):expand() .. ' .'

		template_data = mw.ustring.gmatch(reexpand , "{{%s-%[%[.-}}[^\}]")
		local result, iterator = '*', template_data()
		local replace_link = function(text)
			return mw.ustring.gsub(text ,'|', '{{!}}' )
		end
		while iterator do
			local pre_template_data = mw.ustring.gsub(iterator ,'%[%[.-%]%]', replace_link )
			local this_template = mw.text.split(pre_template_data, '|')  or { [1] = pre_template_data }
			local template_name = mw.text.trim(mw.ustring.gsub(this_template[1], '[%[%]{}\n\r\t]', ''))
			local arg_index = 0
			mw.log('pre_template_data',pre_template_data)
			mw.log('template_name',template_name)
			for arg_id, arg_full in pairs(this_template) do
				if arg_index == 0 then arg_index = 1 else
					local checker = mw.ustring.find( arg_full, "=")
					local arg_full_text = arg_full
					local arg_name = arg_index
					if checker then 
						arg_full_text = mw.text.trim(mw.ustring.sub(arg_full, checker+1, -1))
						arg_name = mw.text.trim(mw.ustring.sub(arg_full, 1, checker-1))
					else
						arg_index = arg_index + 1
					end 
					mw.log('arg_name='..arg_name..', '..'arg_value='..arg_full_text)
					
					local tmp_v = 'false';if(finder==arg_name)then tmp_v='true' end
					local tmp_t = 'false';if(finder_template==template_name)then tmp_t='true' end

					if mw.text.trim('' .. finder_template) == template_name and mw.text.trim('' .. finder) == arg_name then
						return mw.text.trim(arg_full_text or '')
					end
				end
			end
			--result = result .. iterator .. '\n*'
			iterator = template_data()
		end
		--return result
	end
	return ''
end
p['生成']=function()return 'jj生成' end
return p;