跳转到内容

模組:DYKCount/sandbox

维基百科,自由的百科全书
local p = {}

local function count_pattern(title, pattern, offset)
	offset = tonumber(offset or 0) or 0
	-- Known Issue: type(mw.ustring) ~= "string"
	local src = type(title) ~= "string" and title:getContent() or mw.title.new(title):getContent()
    if src then
		-- [[mw:Extension:Scribunto/Lua_reference_manual/zh#ustring匹配模式]] 避免已知限制:最大长度
    	local _, count_id = string.gsub(src, pattern, "")
		return count_id + offset
    end
    return offset
end

-- [[T:DYKButton]]
function p.find_dyk_edit_by_hN(frame) 
	local pattern = "\n==+ *([^=]-) *==+" -- [[:w:Special:Redirect/revision/1363707408#L-70]]
    local title = mw.title.new(mw.text.trim(frame.args[1] or frame.args['1']))
    local offset = tonumber(frame.args[2] or frame.args['2'] or 0) or 0
	return title:fullUrl( {
		action='edit',
		section=count_pattern(title, pattern, offset) - 1 -- 最后章节:{{進行中的內容評選}}
	}, 'relative' ) -- .. '&summary=提交新的新条目推荐评选提名'
end

-- function p.find_dyk_edit_by_entry(frame) 
-- 	local pattern = "{{%s*DYKEntry%s*|.-timestamp%s*="
-- end

-- [[T:User DYK]]
function p.getDYKCount(frame)
    local input_name = mw.text.trim(frame.args[1] or frame.args['1'] or '')
	local src = mw.title.new(input_name):getContent() or ''
	-- 默认直接返回 0 而不报错
	-- if not src then return "<span class='error'>" .. input_name .. "页面不存在或为空</span>" end
	return mw.ustring.match(src, "{{%s-[Pp]roduceEncouragement%s-|%s-count%s-=%s-(%d+)%s-}}" ) or 0
end

return p