모듈:Ilh

위키백과, 우리 모두의 백과사전.

local ilh = {}
local getArgs
local yesno = require('Module:Yesno')

local args
--local frameArgs
local COMMON_TAIL='</span>'

--derived from Module:Langname
local langdata = mw.loadData('Module:Langname/data')
local lang_name = langdata.lang_name
local lang_autonym = langdata.lang_autonym
local lang_article = langdata.lang_article
local lang_wikipedia = langdata.lang_wikipedia

local function getLanguageName(code)
    return lang_name[code] or getCldrName(code)
end

function ilh.name(frame)
    local code = string.lower(frame.args.code)
    
    return getLanguageName(code) or '언어 오류(' .. code .. ')'
end

-- derived from zh.wikipedia.org
local MODEL={
	 frame_head='<span class="ilh-all %s" data-orig-title="%s" data-lang-code="%s" data-lang-name="%s" data-foreign-title="%s">'
	,frame_tail=COMMON_TAIL
	,page_head='<span class="ilh-page">'
	,page_tail=COMMON_TAIL
	,comment_head='<span class="noprint ilh-comment">('
	,comment_tail=')'..COMMON_TAIL
	,lang_head='<span class="ilh-lang">'
	,lang_tail=COMMON_TAIL
	,colon='<span class="ilh-colon">:</span>'
	,link_head='<span class="ilh-link">'
	,link_body='[[:%s:%s|<span class="ilh-text" lang="%s" dir="auto">%s</span>]]'
	,link_tail=COMMON_TAIL
}
local clazz_pageExist_framehead='ilh-blue'
local TRA_CAT='[[분류:제거된 임시링크를 포함하는 문서]]'

function ilh.main(frame)
	if not getArgs then
		getArgs = require('Module:Arguments').getArgs
	end
	args = getArgs(frame, {parentFirst=true})
	
	return ilh._ilh(arg)
end

function ilh._ilh(arg)
	--frameArgs = getArgs(frame, {frameOnly=true})

	local context={}
	context["isMainPage"]=ilh.isMainPage()
	context["localPage"]=args[1]
	context["foreignPage"]=args[2] or args[1] --{{{2}}}가 전달되지 않을 경우
	context["displayName"]=ilh.displayName(args)
	context["langCode"]=args["lang-code"]
	context["lang"]=getLanguageName(context["langCode"])
	context["nocat"]=yesno( args["nocat"] , false )

	context["isExist"]= (args["$exist$"] and args["$exist$"]==1) or ilh.isExist(context["localPage"])
	
	local curPage_obj=mw.title.getCurrentTitle()
	context["isNoCatWithNamespace"]=curPage_obj:inNamespaces(2,828) --User,Module
	--context["curPageNamespace"]=curPage_obj.namespace

	return (context["isMainPage"] and ilh.onlyLink(context)) or ilh.functionLink(context)
end

function ilh.onlyLink(context)
	return ( context["isExist"] and mw.ustring.format( '[[%s|%s]]', context["localPage"], context["displayName"] ) ) or context["displayName"]
end

function ilh.functionLink(context)
	context["_localPage"]=mw.ustring.gsub(context["localPage"],'"','&quot;')
	context["_foreignPage"]=mw.ustring.gsub(context["foreignPage"],'"','&quot;')
	local need_cat=
				   (not context["nocat"])
				   and
				   (not context["isNoCatWithNamespace"])
				   --[[not (
					   context["curPageNamespace"]==2 --User
					or context["curPageNamespace"]==828 --Module
				   )]]
	--mw.log(context["nocat"])
	--mw.log(context["curPageNamespace"])
	--mw.log(need_cat)

	local output_context={}
	table.insert(output_context,
					mw.ustring.format(MODEL.frame_head ,
						 (context["isExist"] and clazz_pageExist_framehead) or ''
						,context["_localPage"]
						,context["langCode"]
						,context["lang"]
						,context["_foreignPage"]
					)
				)
		table.insert(output_context,MODEL.page_head)
			table.insert(output_context,
							mw.ustring.format('[[:%s|%s]]' ,
								context["localPage"],context["displayName"]
							)
						)
		table.insert(output_context,MODEL.page_tail)
		if context["isExist"] then
			if need_cat then
				table.insert(output_context,TRA_CAT)
			end
		else
			table.insert(output_context,MODEL.comment_head)
				table.insert(output_context,MODEL.lang_head)
					table.insert(output_context,context["lang"])
				table.insert(output_context,MODEL.lang_tail)

				table.insert(output_context,MODEL.colon)

				table.insert(output_context,MODEL.link_head)
					table.insert(output_context,
									mw.ustring.format(MODEL.link_body,
										 context["langCode"]
										,(context["foreignPage"] or context["localPage"])
										,context["langCode"]
										,(context["foreignPage"] or context["localPage"])
									)
								)
				table.insert(output_context,MODEL.link_tail)
			table.insert(output_context,MODEL.comment_tail)
		end
	table.insert(output_context,MODEL.frame_tail)

	return table.concat(output_context,"")
end

function ilh.displayName(args)
	local _d=args["d"]
	local _1=args["1"]
	local _3=args["3"]
	local dpN1=_3 or _d
	return (dpN1 and {dpN1} or {_1})[1]
end

--아래의 경우 더 효율적인 구현이 필요하다.

--홈페이지 확정
--mw 정보를 사용하여 홈페이지 이름 얻기
--language 라이브러리를 사용하여 본 사이트의 묵시적 언어 코드(ko)를 획득한 뒤 메시지의 대응 언어를 결정하고, 전체 홈페이지 이름을 획득합니다.
---(언어만 다른 이름으로 나누어 이름공간을 판단할 수 있으나 번거로움)
--그리고 현재 페이지와 홈 페이지가 일치하는지 판단합니다.
---(방향을 바꿀 계획이었지만 필요하지 않았음)
function ilh.isMainPage()
	local mainpage_msgobj=mw.message.new('Mainpage')
	mainpage_msgobj=mainpage_msgobj:inLanguage(mw.getContentLanguage():getCode())
	local mainPage_obj=mw.title.makeTitle(0,mainpage_msgobj:plain())
	local curpage_obj=mw.title.getCurrentTitle()
	--local curpage_redirectFrom_obj=curpage_obj.redirectTarget
	--[[if curpage_redirectFrom_obj ~=false then
		curpage_obj=curpage_redirectFrom_obj
	end]]
	return mw.title.equals(mainPage_obj,curpage_obj) --and curpage_obj.namespace==4
end

--페이지의 존재를 확정합니다.
---(exists의 복잡도가 크기 때문에 개선 필요)
--메소드(_isExist)를 보호함
--exists가 고비용이기 때문에 ifexist도 고비용 함수입니다.
--ifexist가 한계에 다다르면 false를 기본적으로 반환
--그러나 exists는 오류가 발생하면 바로 interrupt를 발생시킴
--이런 exists와 ifexists의 특성 때문에 별도의 패키지를 제작한 것.
function ilh.isExist(pageName)
	local execStatus,result=pcall(ilh._isExist,pageName)
	
	if execStatus then
		return result
	else
		return false
	end
end
--실제 메소드
function ilh._isExist(pageName)
	local localPage_obj=mw.title.makeTitle(0,pageName)
	return localPage_obj.exists
end
--end

return ilh