Jump to content

Module:Check DYK hook

Permanently protected module
From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 12:31, 29 November 2020 (fix whitespace trimming; only trim whitespace in p.isValidHook, not in the Lua p._isValidHook interface; add a "hook" keyword parameter; and check the type of the input in p._isValidHook). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

-- This module performs validation checks for [[WP:DYK]] hooks

local libraryUtil = require('libraryUtil')
local p = {}

local validationPatternGroups = {
	{
		-- Check that hooks start with three periods, followed by an acceptable
		-- follow-on word.
		"^%.%.%. *that",
		"^%.%.%. *about",	
	},
	{
		-- Check that hooks end with a question mark, or another acceptable
		-- phrase.
		[[.%?%]*'*"?$]],
		[[.&#63;</span>%]*'*"?$]],
		"[Yy]ou probably did%.+$",
	}
}

function p._isValidHook(hook)
	-- Whether the given hook is valid.
	-- We use the patterns in the validationPatternGroups table to find whether
	-- a hook is valid or not. Hooks are treated as valid if they match at least
	-- one pattern from each group.
	libraryUtil.checkType("_isValidHook", 1, hook, "string")
	for _, patternGroup in ipairs(validationPatternGroups) do
		local found = false
		for _, pattern in ipairs(patternGroup) do
			if mw.ustring.find(hook, pattern) then
				found = true
				break
			end
		end
		if not found then
			return false
		end
	end
	return true
end

function p.isValidHook(frame)
	local args = frame.args
	local hook = args.hook or args[1]
	if not hook then
		error("No hook specified")
	end
	hook = hook:match('^%s*(.-)%s*$') -- Trim whitespace
	if p._isValidHook(hook) then
		return "yes"
	else
		return ""
	end
end

return p