Dokumentācijas ikona Moduļa dokumentācija[izveidot]
local messages = {
	notFormat = "Lapas ar IMDb veidni, kam ir nederīga vērtība";
	wdDiff = 	"P345: nesakrīt ar vērtību Vikidatos";
	notWD =		"P345: nav Vikidatos";
	noValue = 	"Lapas ar IMDb veidni, kam ir nederīga vērtība"
}

local p = {}
local categories = {}
local entity = mw.wikibase.getEntityObject()
local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs
local title = mw.title.getCurrentTitle()

function getPrefix(value)
	result = string.sub( value, 1,2 )
	
	return result
end

function formatValue(str, len)
	firsttwo = getPrefix(str)
	if firsttwo=='tt' or firsttwo=='nm' or firsttwo=='ch' or firsttwo=='co' or firsttwo=='ev' then
		str = string.sub(str, 3)
	end
	
    return string.rep('0', len - #str) .. str
end

function checkFormat(fullprofile)
	firsttwo = getPrefix(fullprofile)
	if firsttwo ~= 'ev' then
		formatmatch = string.match(formatValue(fullprofile, 7), "^%d%d%d%d%d%d%d$")--(tt|nm|ch|co|ev)
	else
		formatmatch = string.match(fullprofile, "^ev%d%d%d%d%d%d%d/?%d?%d?%d?%d?$")--@todo: fix!
	end
	
	if not formatmatch then
		table.insert(categories, messages.notFormat)
	end
	
    return link
end

function makeLink(fullprofile)
	firsttwo = getPrefix(fullprofile)
	--@todo: pārveidot par switch?
	if firsttwo=='tt' then link = "http://www.imdb.com/title/" .. fullprofile .. "/"
		elseif firsttwo=='nm' then link = "http://www.imdb.com/name/" .. fullprofile .. "/"
		elseif firsttwo=='ch' then link = "http://www.imdb.com/character/" .. fullprofile .. "/"
		elseif firsttwo=='co' then link = "http://www.imdb.com/company/" .. fullprofile .. "/"
		elseif firsttwo=='ev' then link = "http://www.imdb.com/event/" .. fullprofile .. "/"
			--elseif ielikt kaut kādu kategoriju/parastu saiti
	end
	
    return link
end

function final(link, name)
	firsttwo = getPrefix(link)
	
	if firsttwo ~= 'tt' and firsttwo~='nm' and firsttwo~='ch' and firsttwo~='co' and firsttwo~='ev' then notvalid = true end
	
	if notvalid then result = table.insert(categories, messages.noValue) else
		result = '[' .. makeLink(link) .. ' ' .. name .. '] ' .. mw.getCurrentFrame():expandTemplate{ title="en ikona" }
	end
	
	return result
end

function p._main(args)
	local value
	local param = args[1] or args.id
	local nowikidata = yesno(args.nowd, false)
	local pagetype = args.type or ''--ch/tt etc.
	local name = args.name or "''IMDb'' profils"
	local section = args.section or ''
	local sect = string.lower(section)
	
	if param then
		value = pagetype .. formatValue(param, 7)
	else
		if nowikidata == true then
			table.insert(categories, messages.noValue)
		end
	end

	if sect == 'award' or sect == 'awards' then
			value = value .. '/awards'--@todo: fix (noslēdzošā slīpsvītra)
			name = args.name or "Balvas"
		elseif sect == 'ep' then
			value = value .. '/episodes'
			name = args.name or "Sērijas"
	end
	
	if entity then
		if entity.claims["P345"] then
			local mainsnak = entity.claims["P345"][1].mainsnak
		
			if mainsnak.snaktype == 'value' then
				wdvalue = mainsnak.datavalue.value
				if not value then value = wdvalue elseif wdvalue ~= value and nowikidata~=true then
					table.insert(categories, messages.wdDiff)
				end
				
			end
		else
			table.insert(categories, messages.notWD)
		end
	end

	if value and makeLink(value) then
		finallink = final(value,name)
		checkFormat(value)
	end
	
	if title.nsText == '' then
		for i, cat in ipairs(categories) do
			categories[i] = string.format('[[Kategorija:%s]]', cat)
			--categories[i] = cat
		end
		
		finalCategories = table.concat(categories)
	end

	return (finallink or value or '') .. (finalCategories or '')
end

function p.main(frame)
	local args = getArgs(frame, {
		wrappers = "Veidne:IMDB",
					"Veidne:IMDB filma",
					"Veidne:IMDB sērija",
					"Veidne:IMDB sērijas",
					"Veidne:IMDB tēls",
					"Veidne:IMDB uzņēmums",
					"Veidne:IMDb profils"
})
	return p._main(args)
end

return p