모듈:FemiwikiBlog

최근 편집: 2019년 1월 3일 (목) 22:40

이 모듈에 대한 설명문서는 모듈:FemiwikiBlog/설명문서에서 만들 수 있습니다

local p = {}

--- 최근 블로그 포스트들을 보여줍니다.
-- @param frame.arg['root'] 포스트들의 공통된 문서 제목 부분, '/'는 포함 안 함.
-- @param frame.arg['limit'] 보여줄 최다 포스트 수, 없으면 5
-- @return string wikitext
function p.renderPosts( frame )
	local limit = tonumber( frame.args['limit'] ) or 5;

	local titleText = frame.args['root']
	local lastId = tonumber( frame:expandTemplate{
		title = titleText,
		args = { ['find-last-id'] = 1 }
	} )
	
	local posts = ''
	local count = 1;
	for id=lastId, 1, -1 do
		titleText = frame.args['root'] .. '/' .. id
		if mw.title.new( titleText ).exists then
			posts = posts .. frame:expandTemplate{ title = titleText }
			
			count = count + 1
			if ( count > limit ) then break end
		end
    end
    
    local ul = mw.html.create( 'ul' ):addClass( 'blog-posts' ):wikitext( posts )
    return tostring( ul )
end

--- 마지막 포스트의 Id를 찾습니다.
-- @param frame.args['root'] 포스트들의 공통된 문서 제목 부분, '/'는 포함 안 함.
-- @param frame.args['approximate-number-of-posts'] ID에 대해 순차탐색을 시작할 수.
--	해당 ID에 이미 포스트가 있다면 증가하면서 순차탐색을,
--	없을 경우 감소시키면서 순차탐색을 하며,
--	0일 경우 0부터 1씩 증가하면서 찾습니다.
-- @return number 마지막 포스트의 id
function p.findLastId( frame )
	local root = frame.args['root']
	local start = tonumber( frame.args['approximate-number-of-posts'] ) or 0
	local limit = 20
	
	if root == nil then error('root is must declared') end
	
	if start == 0 then
		for id=start, start + limit, 1 do
			local titleText = frame.args['root'] .. '/' .. id
			if not mw.title.new( titleText ).exists then
				return id - 1
			end
		end
		
		error("last id was not found")
	else
		local titleText = frame.args['root'] .. '/' .. start
		
		-- /start 문서가 이미 있다면 어디까지 있는지 증가하면서 순차탐색합니다.
		if mw.title.new( titleText ).exists then
			for id=start + 1, start + limit, 1 do
				local titleText = frame.args['root'] .. '/' .. id
				if not mw.title.new( titleText ).exists then
					return id - 1
				end
			end
			
			error("last id was not found")
		-- /start 문서가 아직 없다면 어디까지 없는지 감소하면서 순차탐색합니다.
		else
			for id=start -1, 1, -1 do
				local titleText = frame.args['root'] .. '/' .. id
				if mw.title.new( titleText ).exists then
					return id
				end
			end
			
			return 0
		end
	end
end

return p