Module:FilenameChecker

From SW420
Revision as of 14:20, 26 May 2022 by imported>Xd1358 (added .webp check)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Documentation for this module may be created at Module:FilenameChecker/doc

p = {}

local function makeCategoryLink(cat)
	return string.format('[[Category:%s]]', cat)
end

function p.main(frame)
	local title = mw.title.getCurrentTitle()
	if title.namespace ~= 6 then
		return ''
	end
	local ret = {''}
	local filename = title.text
	local filename2 = mw.ustring.lower(filename)
	if mw.ustring.match(filename2, '%.png%.png$') or mw.ustring.match(filename2, '%.jpg%.jpg$') or mw.ustring.match(filename2, '%.jpeg%.jpeg$') or mw.ustring.match(filename2, '%.svg%.svg$') or mw.ustring.match(filename2, '%.ogg%.ogg$') then
		ret[#ret + 1] = makeCategoryLink('Images with double extensions in filename')
	end
	if mw.ustring.match(filename2, '%.jpeg$') then
		ret[#ret + 1] = makeCategoryLink('Images with extended extensions in filename')
	end
	if mw.ustring.match(filename2, '%.webp$') then
		ret[#ret + 1] = makeCategoryLink('Images with .webp extensions to be converted')
	end
	if mw.ustring.match(filename, '%.JPG$') or mw.ustring.match(filename, '%.JPEG$') or mw.ustring.match(filename, '%.PNG$') or mw.ustring.match(filename, '%.SVG$') or mw.ustring.match(filename, '%OGG$')then
		ret[#ret + 1] = makeCategoryLink('Images with capitalized extensions in filename')
	end
	if mw.ustring.match(filename2, 'img') or mw.ustring.match(filename2, 'picture') or mw.ustring.match(filename2, 'screenshot') or mw.ustring.match(filename2, 'screen shot') then
		ret[#ret + 1] = makeCategoryLink('Images with generic strings in filename')
	elseif mw.ustring.match(filename2, 'file') and not (
		mw.ustring.match(filename2, 'files') or
		mw.ustring.match(filename2, 'defile') or
		mw.ustring.match(filename2, 'profile') or
		mw.ustring.match(filename2, 'fact ?file')
	) then
		ret[#ret + 1] = makeCategoryLink('Images with generic strings in filename')
	elseif mw.ustring.match(filename2, 'image') and not (
		mw.ustring.match(filename2, 'images') or
		mw.ustring.match(filename2, 'userimage') or
		mw.ustring.match(filename2, 'imagecaster') or
		mw.ustring.match(filename2, 'imagery') or
		mw.ustring.match(filename2, 'pilgrimage')
	) then
		ret[#ret + 1] = makeCategoryLink('Images with generic strings in filename')
	end
	if mw.ustring.match(filename, "[%/%,%?%,%+%!%(%)’”]") then
		ret[#ret + 1] = makeCategoryLink('Images with annoying characters in filename')
	elseif mw.ustring.match(filename, "[%']") then
		ret[#ret + 1] = makeCategoryLink('Images with apostrophes in filename')
	elseif mw.ustring.match(filename, "[%&]") then
		ret[#ret + 1] = makeCategoryLink('Images with ampersands in filename')
	elseif mw.ustring.match(filename, "[%@]") then
		ret[#ret + 1] = makeCategoryLink('Images with at signs in filename')
	else
		for code in mw.ustring.gcodepoint(filename) do
			if code < 32 or code > 122 then
				ret[#ret + 1] = makeCategoryLink('Images with annoying characters in filename')
				break
			end
		end
	end
	return table.concat(ret)
end

return p