MODULE:SearchParser

Documentation for this module may be created at MODULE:SearchParser/doc

local p = {}

local letterToNs = {
	Q = 0,
	P = 120,
	L = 146,
	      E = 640,
}

local function strafter(arg, pre)
	local length = mw.ustring.len(pre)
	if mw.ustring.sub(arg, 1, length) == pre then
		return mw.ustring.sub(arg, length + 1)
	end
	return nil
end

local function booleanOutput(val)
	local map = {[true] = '1', [false] = ''}
	return map[val]
end

local function namespaceIdFromEntityId(entityId)
	if not mw.wikibase.isValidEntityId(entityId) then
		return nil
	end
	local first = entityId:sub(1, 1):upper()
	return letterToNs[first]
end

function p.isEntityId(frame)
	return booleanOutput(mw.wikibase.isValidEntityId(frame.args[1]))
end

function p.isEntityIdForNamespace(frame)
	local entityId = frame.args[1]
	local namespace = tonumber(frame.args[2])
	return booleanOutput(namespace == namespaceIdFromEntityId(entityId))
end

function p.getNamespaceFromEntityId(frame)
	return namespaceIdFromEntityId(frame.args[1])
end

local function _getLemmaFromInput(arg)
	for _, pre in ipairs{ "L:", "l:", "Lexeme:", "lexeme:" } do
		local lemma = strafter(arg, pre)
		if lemma then
			-- avoid T14974 when * is at the beginning
			-- if it causes problems, replace with string.gsub(lemma, '^*', '*', 1)
			return lemma
		end
	end
	return nil
end

function p.getUnescapedLemmaFromInput(frame)
	return _getLemmaFromInput(mw.text.unstripNoWiki(frame.args[1]))
end

function p.getLemmaFromInput(frame)
	local arg = _getLemmaFromInput(mw.text.unstripNoWiki(frame.args[1]))
	if arg then
		return mw.text.nowiki(arg)
	end
	return nil
end

function p.urlencode(frame)
	local arg = mw.text.unstripNoWiki(frame.args[1])
	-- some rare inputs ("-{", "}-", "<script>", etc.) will have HTML entities
	-- use arg = mw.text.decode(arg) if needed
	return mw.uri.encode(arg, frame.args[2])
end

return p