Module:Check card number

From Yugipedia
Jump to: navigation, search

-- super rudimentary check for potentially invalid card numbers

local p = {}

function p.check()
	local page = mw.title.getCurrentTitle()
	local isRedirect = page.isRedirect
	local targets = {}
	if isRedirect then
		local target = page.redirectTarget
		if target then
			table.insert( targets, target.prefixedText )
		end
	else
		local pageSrc = page:getContent()
		if pageSrc:find( '[Dd]isambig' ) then
			for target in mw.text.gsplit( pageSrc, '%[%[' ) do
				table.insert( targets, target:match( '^([^%]|]-)%]%]' ) or target:match( '^([^%]|]-)|[^%]]-%]%]' ) )
			end
		else
			-- current page isn't a redirect or a dabpage, just return without doing anything
			return
		end
	end
	
	local searchStr = ( '%s%s;' ):format( '%s', page.prefixedText:gsub( '%-', '%%-' ) )
	for _, target in ipairs( targets ) do
		local success, targetTitle = pcall( mw.title.new, target )
		if success and targetTitle then
			local targetSrc = targetTitle:getContent()
			if not targetSrc or not targetSrc:find( searchStr ) then
				return isRedirect and '[[Category:Potentially invalid card number redirects]]' or '[[Category:Card number disambiguation pages to be checked]]'
			end
		end
	end
	return
end

return p