Module:Check card number/sandbox

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
		table.insert( targets, page.redirectTarget )
		return type(page), page.redirectTarget
	else
		local pageSrc = page:getContent()
		if pageSrc:find( 'disambig' ) then
			for target in pageSrc:gsplit( '%[%[' ) do
				table.insert( targets, pageSrc:match( '([^%]|]-)%]%]' ) or pageSrc:match( '([^%]|]-)|[^%]]-%]%]' ) )
			end
		else
			-- some page that 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 targets do
		local success, targetTitle = pcall( mw.title.new, target )
		if not success or not targetTitle then
			return
		end
		
		local targetSrc = targetTitle:getContent()
		if 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
	return
end

return p