Difference between revisions of "Module:Check card number/sandbox"

From Yugipedia
Jump to: navigation, search
(redirectTarget returns a title object, not a string)
(that doesn't work either I guess)
 
(5 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
else
 
else
 
local pageSrc = page:getContent()
 
local pageSrc = page:getContent()
if pageSrc:find( 'disambig' ) then
+
if pageSrc:find( '[Dd]isambig' ) then
for target in pageSrc:gsplit( '%[%[' ) do
+
for target in mw.text.gsplit( pageSrc, '%[%[' ) do
table.insert( targets, pageSrc:match( '([^%]|]-)%]%]' ) or pageSrc:match( '([^%]|]-)|[^%]]-%]%]' ) )
+
table.insert( targets, target:match( '^([^%]|]-)%]%]' ) or target:match( '^([^%]|]-)|[^%]]-%]%]' ) )
 
end
 
end
 
else
 
else
Line 25: Line 25:
 
 
 
local searchStr = ( '%s%s;' ):format ('%s', page.prefixedText:gsub( '%-', '%%-' ) )
 
local searchStr = ( '%s%s;' ):format ('%s', page.prefixedText:gsub( '%-', '%%-' ) )
for target in targets do
+
for _, target in ipairs( targets ) do
 
local success, targetTitle = pcall( mw.title.new, target )
 
local success, targetTitle = pcall( mw.title.new, target )
if not success or not targetTitle then
+
if success and targetTitle then
return
+
local targetSrc = targetTitle:getContent()
end
+
if not targetSrc:find( searchStr ) then
+
return isRedirect and '[[Category:Potentially invalid card number redirects]]' or '[[Category:Card number disambiguation pages to be checked]]'
local targetSrc = targetTitle:getContent()
+
end
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
 
end
 
end

Latest revision as of 17:14, 26 July 2023

-- 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
			-- 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 ipairs( targets ) do
		local success, targetTitle = pcall( mw.title.new, target )
		if success and targetTitle then
			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
	end
	return
end

return p