Модуль:Sources-title

Поделись знанием:
Перейти к: навигация, поиск
 Документация

Возвращает название источника для включения в виде комментария в гаджетах вставки источника (см. MediaWiki:Gadget-wefsources.js)

Тесты [ править ]

2 tests failed.

test_Sources:

Text Expected Actual
N {{#invoke:Sources-title | renderSourceTitle | Q21725116}} Handbook of Applied Cryptography (unspecified title)
N {{#invoke:Sources-title | renderSourceTitle | Q27056247}} A New Generation Cryptographic Technique // IJCTE (unspecified title)



Во избежание поломок страниц, использующих данный модуль, желательно экспериментировать в Песочнице для модулей.

local p = {};
local u = require('Module:Sources-utils')

function p.renderSourceTitle( frame )
	p.currentFrame = frame;
	return p.renderSourceTitleImpl( mw.text.trim( frame.args[1] ) );
end

function p.renderSourceTitleImpl( entityId )
	local snaks = {};
	snaks.P248 = { toWikibaseEntityIdSnak( 'P248', entityId ) };

	local rendered = renderSourceTitle_1( mw.wikibase.getEntity(), { snaks = snaks } );
	if ( rendered ) then return rendered.text end;
end

function renderSourceTitle_1( currentEntity, reference )
	if ( not reference.snaks ) then return nil; end

	-- контекст, содержит также кеш элементов
	local context = {
		cache = {},
	}

	-- данные в простом формате, согласованном с модулями формирования библиографического описания
	local data = {};

    -- забрать данные из reference
    populateDataFromClaims( context, nil, reference.snaks, data )
	expandSpecials( context, currentEntity, reference, data );

	local sourceEntity = nil;
	if ( data.sourceId ) then
		sourceEntity = getEntity( context, data.sourceId );
		if ( sourceEntity ) then
			populateSourceDataImpl( context, sourceEntity, data );
		end
	end

	if ( data.publication ) then
		expandPublication( context, sourceEntity, data );
	end

	if ( next( data ) == nil ) then
		return nil;
	end

	local rendered = renderSourceTitle_2( context, data );
	if ( mw.ustring.len( rendered.text ) == 0 ) then
		return nil;
	end

	return rendered;
end

local options_commas = { separator = ', ', conjunction = ', ', format = function( src ) return src end, nolinks = true, preferids = false };
local options_commas_short = { separator = ', ', conjunction = ', ', format = function( src ) return src end, nolinks = true, preferids = false, short = true };

function renderSourceTitle_2( context, src )
	context.lang = getLangCode( getSingle( src.lang ) ) or i18nDefaultLanguage;
	src.title = src.title or getSingle( src.url ) or '\'\'(unspecified title)\'\''

	local result = '';
 	if ( src.part ) then
		result = result .. toString( context, src.part, options_commas );
		result = result .. ' // ' .. toString( context, src.title, options_commas );
	else
		result = result .. toString( context, src.title, options_commas );
 	end

	if ( src.subtitle ) then
		result = result .. ": " .. toString( context, src.subtitle, options_commas );
	end

	if ( src.publication ) then
		if ( type( src.publication.title or '') ~= 'string' ) then error('type of src.publication.title is not string but ' .. type( src.publication.title ) ) end;

		result = result .. ' // ' .. toString( context, src.publication, options_commas_short );
		if ( src.publication.subtitle ) then
			result = result .. ': ' .. toString( context, src.publication.subtitle, options_commas_short );
		end
	end

	return {text = result, code = src.code};
end

return p;