Modulis:Filmu ārējās saites

Dokumentācijas ikona Moduļa dokumentācija[izveidot]
function imdbLink( id )
    if not string.match( id, '^t?t?%d+$' ) then
        return false
    end
    if string.find( id, 'tt', 1, true ) then
        id = string.gsub( id, 'tt', '' )
    end
    return 'http://www.imdb.com/title/tt' .. id .. '/'
end

function tcmdbLink( id )
    if not string.match( id, '^%d+$' ) then
        return false
    end
    return 'http://tcmdb.com/title/title.jsp?stid=' .. id
end

function mojoLink( id )
    if not string.match( id, '^[a-z0-9][-a-z0-9]*$' ) then
        return false
    end
    return 'http://boxofficemojo.com/movies/?id=' .. id .. '.htm'--
end

function rottenLink( id )
--[[
    if not string.match( id, "^m/[0-9A-Za-z][-0-9A-Za-z_']*$" ) then
        return false
    end
]]
    return 'http://www.rottentomatoes.com/' .. id
end

function allmovieLink( id )
    if not string.match( id, '^v[1-9][0-9]*$' ) then
        return false
    end
    return 'http://www.allmovie.com/movie/' .. id--v[1-9][0-9]*
end

function metacriticLink( id )
--[[
    if not string.match( id, '^m?o?v?i?e?/?[-a-z0-9!+_]+$' ) then
        return false
    end
    if string.find( id, '^movie/' ) then
        id = string.gsub( id, 'movie/', '', 1 )
    end
]]
    return 'http://www.metacritic.com/' .. id--(game/[-a-z0-9]{2,16}|tv|movie|music(/[-a-z0-9!+_]+)?)/[-a-z0-9!+_]+(/season-\d+)?/*
end

function tvLink( id )
    return 'http://www.tv.com/' .. id ..'/'
end

--[==[function bbfcLink( id )
    if not string.match( id, '^[A-Z0-9]+$' ) then
        return false
    end
    return 'http://www.bbfc.co.uk/' .. id .. '/'
end]==]

function append(str, c, length)
    while str:len() < length do
        str = c .. str
    end
    return str
end

function getIdsFromWikidata( item, property )
    local ids = {}
    if not item.claims[property] then
        return ids
    end
    for _, statement in pairs( item.claims[property] ) do
		if statement.mainsnak.datavalue then
			table.insert( ids, statement.mainsnak.datavalue.value )
		end
    end
    return ids
end

function createRow( id, label, rawValue, link, lang )
    if link then
        return '* [' .. link .. ' ' .. label .. '] ' .. lang
    else
        return '* <span class="error">' .. id .. ' identifikators ' .. rawValue .. ' nav derīgs.</span>[[Kategorija:Raksti ar nederīgiem ārējo saišu identifikatoriem]]'
    end
end

--In this order: name of the parameter, label, propertyId in Wikidata, formatting function, language
local conf = {
    { 'imdb', "''IMDb'' profils", 345, imdbLink, 'en' },
    { 'allmovie', "''AllMovie'' profils", 1562, allmovieLink, 'en' },
    { 'turner', "''Turner Classic Movies'' profils", 2631, tcmdbLink, 'en' },
    { 'tv', "''TV.com'' profils", 2638, tvLink, 'en' },
    { 'metacritic', "''Metacritic'' profils", 1712, metacriticLink, 'en' },
    { 'mojo', "''Box Office Mojo'' profils", 1237, mojoLink, 'en' },
    { 'rotten', "''Rotten Tomatoes'' profils", 1258, rottenLink, 'en' },
   -- { 'bbfc', "BBFC profils", 0, bbfcLink, 'en' },
}
-- parametru kā pēdējo likt, lai var ielikt arī alias

local p = {}

function p.main( frame )
    local parentArgs = frame:getParent().args
    --Create rows
    local elements = {}


    --Wikidata fallback if requested
	local item = mw.wikibase.getEntityObject()
	if item and item.claims then
		for _, params in pairs( conf ) do
			params[6] = ''
			if params[3] ~= 0 then
				local val = parentArgs[params[1]]
				if not val or val == '' then
					local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
					if wikidataIds[1] then
						parentArgs[params[1]] = wikidataIds[1]
					end
				else
					params[6] = '[[Category:Raksti ar lokālajiem ārējo saišu veidnes parametriem]]'
				end
			end
		end
	end

	--Configured rows
	local rct = 0
	for k, params in pairs( conf ) do
		local val = parentArgs[params[1]]
		if val and val ~= '' then
			local icon = ''
			if params[5] and params[5] ~= '' and params[5] ~= 'lv' then
				icon = frame:expandTemplate{ title = params[5] .. " ikona" }
			end
			table.insert( elements, createRow( params[1], params[2], val, params[4]( val ), icon ) .. params[6] )
			rct = rct + 1
		end
	end

	if rct == 0 then
		return '[[Kategorija:Raksti ar tukšu filmu ārējo saišu veidni]]'
	else
		return table.concat( elements, "\n" )
	end
end

return p