Module:Check card number/sandbox
< Module:Check card number
Revision as of 17:02, 26 July 2023 by Dinoguy1000 (talk | contribs) (that's not how you iterate over a table in Lua)
This is the module sandbox page for Module:Check card number (diff). |
Rudimentary Card Number validity check for Template:R from card number.
-- 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( '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 ipairs( 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