跳转到内容

模組:DataPage/sandbox

维基百科,自由的百科全书
local p = {}
local yesno = require('Module:Yesno')

function p.getDisambiguationData(test_str)

	local start_iterator = 0
	while start_iterator >= 0 do
		start_pos = start_iterator
		start_iterator = mw.ustring.find( test_str, " (", start_iterator + 1, true )
		
		if start_iterator == nil then
        	start_iterator = -1
		end
		
	end
	
	local end_iterator = 0
	while end_iterator >= 0 do
		end_pos = end_iterator
		end_iterator = mw.ustring.find( test_str, ')', end_iterator + 1, true )
		if end_iterator == nil then
        	end_iterator = -1
		end
	end

	if (start_pos < 0) or (end_pos < 0) or (end_pos <= start_pos) then
		return {test_str , ''}
	end
	return {mw.ustring.sub(test_str, 1, start_pos - 1) , mw.ustring.sub(test_str, start_pos + 1, end_pos)}
end

function p.getWithoutDisambiguation(frame)
	-- For calling from #invoke.
	local pframe = frame:getParent()
	args = {}
	for k, v in pairs( frame.args ) do
		args[k] = v;	   
	end
	return p.getDisambiguationData(args)[1]
end

function p.getWithDisambiguation(frame)
	-- For calling from #invoke.
	local pframe = frame:getParent()
	args = {}
	for k, v in pairs( frame.args ) do
		args[k] = v;	   
	end
	return p.getDisambiguationData(args)[2]
end

function p.getDataPageNameByArg(frame)
	-- For calling from #invoke.
	local pframe = frame:getParent()
	args = {}
	for k, v in pairs( frame.args ) do
		args[k] = v;	   
	end
	
	isHans = "no"
	no_disambig = "no"
	
	if (args[1] and args[1]  ~= '') then 
		page_name = args[1]
	end
	
	if (args["isHans"] and args["isHans"]  ~= '') then 
		isHans = args["isHans"]
	end
	
	if (args["NoDisambig"] and args["NoDisambig"]  ~= '') then 
		no_disambig = args["NoDisambig"]
	end

	return p.getDataPageName(page_name, yesno(is_hans), yesno(no_disambig))
end

function p.getDataPageName(page_name, is_hans, no_disambig)

	datapage_str = "性質表"
	if is_hans then
		datapage_str = "性质表"
	end
	
	disambiguation_data = p.getDisambiguationData(page_name)
	if disambiguation_data[2] == '' then
		return disambiguation_data[1] .. datapage_str
	end
	
	if no_disambig then
		return disambiguation_data[1] .. datapage_str
	end
	
	return disambiguation_data[1] .. datapage_str .. ' ' .. disambiguation_data[2]
end

function p.testDataPageNameExist(frame)
	-- For calling from #invoke.
	local pframe = frame:getParent()
	args = {}
	for k, v in pairs( frame.args ) do
		args[k] = v;	   
	end
	
	if (args[1] and args[1]  ~= '') then 
		page_name = args[1]
	else
		return ''	
	end
	
	return p.getDataPageNameExist(page_name)
end

function p.getDataPageNameExist(page_name)
	disambiguation_data = p.getDisambiguationData(page_name)
	
	pagename = disambiguation_data[1]
	disambigname = disambiguation_data[2]
	
	name_array = { " (數據頁)", " (数据页)", "/數據頁", "/数据页", 
		"性質表" .. ' ' .. disambiguation_data[2], "性质表" .. ' ' .. disambiguation_data[2],
		"性質表", "性质表"}
	
	for i = 1,8 do
		test_pagename = pagename .. name_array[i]
		if mw.title.new( test_pagename , mw.site.namespaces.displayName ).exists then
			return test_pagename
		end
	end
	
	return ''
end

return p