模块:Template:Weighted page size

local p = {}
local lib_va = require('Module:Va')
local yesno = require('Module:Yesno')

local function getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then
		return titleObj
	else
		return nil
	end
end

function p.main( frame )
    local titleText = frame.args.page
    if not titleText then
        return 0
    end

    local title = mw.title.new( titleText )
    local success = xpcall(function()
        if not yesno(frame.args.ignore_redirect) then --查找重新導向是高開銷操作,提供取消的方法
	        title = getTitle( lib_va.redirect_target(titleText) )
	    end
    end,function()end)
	if not success then mw.addWarning("[[Module:Template:Weighted page size]] 超出了模板限制 於標題 \"" .. titleText .. "\" 顯示的結果可能會不正確。") end
    if not title then
        return 0
    end
    local content = title:getContent()
    if not content then
        return 0
    end
    local chars = mw.ustring.len( content )
    return math.floor( chars * 3.7 + 0.5 )
end

return p