Module:MediaDateCheck

From SW420
Jump to navigation Jump to search

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

local p = {}

local function makeCategoryLink(cat)
	-- "Category" is split out here so that the module isn't put into the
	-- category "%s" when the page is saved.
	return string.format('[[%s:%s]]', 'Category', cat)
end

function p.main(frame)
	if mw.title.getCurrentTitle().namespace ~= 0 then
		return ''
	end
	local isbnCheck = false
	local audiobook = false
    for _, k in ipairs(frame.args) do
    	if k == "isbn" then
            isbnCheck = true
        elseif k == "audiobook" then
        	audiobook = true
        end
    end

	local missing_isbn = false
	local has_isbn = false
	local canceled = false
	local missing = 0
	local unreferenced = 0
	for k, v in pairs(frame:getParent().args) do
		if k == "isbn" then
			if v and string.len(v) > 0 then
				has_isbn = true
			elseif isbnCheck then
				missing_isbn = true
			end
		elseif k == "canceled" and v and string.len(v) > 0 then
			canceled = true
		elseif k == "publication_date" or k == "publication date" or k == "released" or k == "release_date" or k == "release date" or k == "first_date" or k == "first date" or k == "last_date" or k == "last_date" then
			if not v then
				missing = missing + 1
			elseif string.len(v) > 0 and not string.find(v, 'ref') then
				unreferenced = unreferenced + 1
			end
		end
	end

	local isbn_cat = ''
	if has_isbn then
		isbn_cat = makeCategoryLink("Media with defined ISBN")
	elseif missing_isbn and not canceled and audiobook then
		isbn_cat = makeCategoryLink("Audiobooks without ISBN")
	elseif missing_isbn and not canceled then
		isbn_cat = makeCategoryLink("Media without ISBN")
	end
	
	local date_cat = ''
	if 0 < missing then
		date_cat = makeCategoryLink('Missing publication date parameter')
	elseif 0 < unreferenced then
		date_cat = makeCategoryLink('Media without referenced publication dates')
	end
	
	return string.format('%s%s', date_cat, isbn_cat)
end

return p