Մոդուլ:Wikidata/link

Վիքիպեդիայից՝ ազատ հանրագիտարանից

Предназначен для преобразования ID из свойств Викиданных в работающие ссылки. Работает совместно с {{Wikidata/link}}.


p = {};

function p.fromTemplate( frame )
	local args = frame.args;
	return p.generateLink( args[1], args[2], args['title'] );
end

function p.fromModule( context, options, id )
	return p.generateLink( options['property'], id, options['title'], context, options );
end

function p.generateLink( propertyId, id, title, context, options )
	if p[propertyId] then
		return p[propertyId]( context, options, id );
	end

	local pattern = p.findPattern( propertyId )
	if pattern then
		local link = mw.ustring.gsub( pattern, '$1', id );

		if not title or title == '' then
			title = id;
		end

		return '[' .. link .. ' ' .. title .. ']';
	end

	return id
end

function p.p212( context, options, id )
	return '[[Special:Booksources/' .. id .. '|' .. id .. ']]';
end

function p.p267( context, options, id )
	local frame = mw.getCurrentFrame();
	return frame:expandTemplate{ title = 'АТХ', args = { id } };
end

function p.p345( context, options, id )
	local prefix = options and options.prefix
    local number = id
    if not string.match( id, '^%d' ) then
    	prefix = string.sub( id, 1, 2 )
    	number = string.sub( id, 3 )
	end
    local label = 'ID ' .. number
    if prefix == 'ch' then
		return '[[IMDbCharacter:' .. number .. '|' .. label .. ']]'
    end
    if prefix == 'co' then
		return '[[IMDbCompany:' .. number .. '|' .. label .. ']]'
    end
    if prefix == 'nm' then
		return '[[IMDbName:' .. number .. '|' .. label .. ']]'
    end
    if prefix == 'tt' then
		return '[[IMDbTitle:' .. number .. '|' .. label .. ']]'
    end

	return id;
end

function p.p721( context, options, id )
	local label = '';
    for i = mw.ustring.len( id ), 1, -3 do
    	if ( i ~= mw.ustring.len( id ) ) then
    		label = ' ' .. label;
    	end
    	if ( i - 2 <= 0 ) then
        	label = mw.ustring.sub( id, 0, i ) .. label;
    	else
        	label = mw.ustring.sub( id, i - 2, i ) .. label;
        end
    end
	return '[http://classif.spb.ru/classificators/view/okt.php?st=A&kr=1&kod=' .. id .. ' ' .. label .. ']';
end

function p.p764( context, options, id )
	local label = '';
    for i = mw.ustring.len( id ), 1, -3 do
    	if ( i ~= mw.ustring.len( id ) ) then
    		label = ' ' .. label;
    	end
    	if ( i - 2 <= 0 ) then
        	label = mw.ustring.sub( id, 0, i ) .. label;
    	else
        	label = mw.ustring.sub( id, i - 2, i ) .. label;
        end
    end
	return '[http://classif.spb.ru/classificators/view/tma.php?st=A&kr=1&kod=' .. id .. ' ' .. label .. ']';
end

function p.p957( context, options, id )
	return '[[Special:Booksources/' .. id .. '|' .. id .. ']]';
end

function p.findPattern( property )
	local entity = mw.wikibase.getEntity( property:upper() );
	if entity then
		local statements = entity:getBestStatements( 'P1630' );
		for _, statement in pairs( statements ) do
			if statement.mainsnak.snaktype == 'value' then
				return statement.mainsnak.datavalue.value;
			end
		end
	end
	return nil;
end

return p;