Модуль:Sources

Версия от 23:53, 5 октября 2016; />Vlsergey (Новая страница: «Test»)

Принцип работы модуля

Данный модуль генерирует текст, используемый в сносках, ссылающихся на элемент викиданных.

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

Ошибка скрипта: Модуля «Sources/тесты» не существует.


Служебные подмодули

Используемые параметры Викиданных

Свойство Комментарий
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value). используется для указания названия статьи в энциклопедии
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value). если требуется переопределить название из метки элемента
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).
Ошибка Lua в Модуль:WD на строке 450: attempt to index field 'wikibase' (a nil value).

Функции

Внешние

Внешние функции принимают объекты типа фрейм и предназначены для вызова из других модулей или через функцию парсера {{#invoke:}}.

Прямое обращение к функциям модулей в статьях крайне нежелательно! Используйте для этих целей подходящие шаблоны.

p.renderSource(frame)

Выдаёт вики-текст ссылки на заданный источник для подстановки внутрь сноски или списка литературы. См. шаблоны {{source}} и {{ВД-Источник}}, использующие данную функцию. Поддерживает следующие аргументы:

  • frame.args[1] — анонимный аргумент, задающий идентификатор объекта на викиданных, по которому нужно сгенерировать ссылку. Например, Q20750516.
  • frame.args['ref'] — задаёт метку ref, которую в дальнейшем можно будет использовать в шаблонах типа {{sfn}}.
  • frame.args['ref-year'] — задаёт метку ref-year, которая используется аналогично метке ref.
  • frame.args['part'] — дополнительный аргумент, позволяющий уточнить часть источника, на которую идёт ссылка (например, главу в книге).
  • frame.args['parturl'] — ссылка, которую следует поставить на часть, описанную предыдущим аргументом.
  • frame.args['pages'] — конкретные страницы в источнике, на которые ведётся ссылка.
  • frame.args['url'] — позволяет явно указать, какую ссылку нужно будет проставить на источник.
  • frame.args['volume'] — позволяет явно указать том источника, на который идёт ссылка.
  • frame.args['issue'] — позволяет явно указать выпуск источника, на который идёт ссылка.

Пробрасывание большей части аргументов происходит в utils.copyArgsToSnaks. Сам переданный фрейм сохраняется в p.currentFrame для дальнейшего использования, а на основе переданных аргументов функцией artificialSnaks создаются искусственные снеки, которые ссылаются на источник, указанный в frame.args[1], через свойства P248 (stated in) и P805 (statement is subject of). Затем данные передаются в renderReferenceImpl для дальнейшей обработки.

p.renderReference(frame, currentEntity, reference)

Выдаёт вики-текст готовой сноски на заданный источник. Поддерживает те же аргументы, что и p.renderSource, кроме ref и ref-year. См. шаблоны {{source-ref}} и {{ВД-Сноска}}, использующие данную функцию. Также используется в Модуль:Wikidata для отображения ссылок, указанных возле утверждений на викиданных. Если currentEntity и reference отсутствуют, создаются искусственные снеки с помощью функции artificialSnaks, после чего они передаются в renderReferenceImpl. Если вики-текст для сноски был успешно сгенерирован, он оборачивается в тэг <ref> с помощью frame:extensionTag, при этом имя для сноски генерируется путём хеширования её вики-текста через mw.hash.hashValue. Статьи, с такими сносками помещаются в Категория:ЭАНМ:Статьи с источниками из Викиданных.

Внутренние

tokenizeName(fullName)

Преобразует полное имя в пару {фамилии через пробел, инициалы имён через пробел}. Реализована в виде разбора случаев, которые можно встретить на викиданных:

  1. Фамилия, Имя
  2. Фамилия, Имя Имя
  3. Фамилия Фамилия, Имя
  4. Имя Имя оглы Фамилия
  5. Имя Имя де Фамилия
  6. Имя … Имя Фамилия (хотя бы одно и не более четырёх единичных имён)

Здесь имя, в отличие от фамилии, может являться инициалом. Если ни один из форматов выше не выполнен, возвращает полное имя без изменений.

personNameToAuthorName(fullName)

Преобразует полное имя в формат Фамилия И. О. с помощью tokenizeName.

personNameToResponsibleName(fullName)

Преобразует полное имя в формат И. О. Фамилия с помощью tokenizeName.

getPeopleAsWikitext(context, value, options)

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

  1. separator — разделитель в списке;
  2. conjunction — разделитель перед последним элементом списка;
  3. format — функция, преобразующая имена к некоторому нормализованному виду (например, personNameToAuthorName);
  4. nolinks — логическое значение, должно быть истинным если проставление ссылок нежелательно;
  5. preferids — логическое значение, должно быть истинным если нужно вернуть id с викиданных, а не имена.

Если в списке больше maxAuthors (на текущий момент 10) людей, заменяет остальных на и др. или его аналоги (если в контексте указан язык, то используется i18nEtAl[context.lang], иначе используется i18nEtAlDefault).

appendProperty(result, context, src, conjunctor, property, url)

Приписывает src[property] к result, разделяя их строкой, записанной в conjunctor. Если возможно, оформляет его ссылкой на src[url].

generateAuthorLinks(context, src)

Возвращает список авторов src.author, оформленный через getPeopleAsWikitext и обрамлённый в <i class="wef_low_priority_links"></i>.

appendTitle(result, context, src)

Дописывает к result строку src.part // src.title либо только src.title если src.part не указан. Если возможно, обрамляет src.part (или src.title если src.part не указан) в src.url.

appendLanguage(result, context, src)

Если context.lang отличается от i18nDefaultLanguage (в нашем разделе русский), то указание об этом приписывается к result с помощью Модуль:Languages в формате {{ref-lang}}.

appendSubtitle(result, context, src)

Дописывает к result строку : src.subtitle если src.subtitle определён.

appendOriginalTitle(result, context, src)

Дописывает к result строку  = src.originaltitle если src.originaltitle определён.

appendPublication(result, context, src)

Дописывает к result строку  // src.publication: src.publication.subtitle если определён src.publication.subtitle, либо  // src.publication если определён только src.publication.

appendEditor(result, context, src)

Дописывает к result строку  / prefix src.editor если определён src.editor, где prefix определяется по context.lang (по умолчанию, под ред.).

appendEdition(result, context, src)

Дописывает к result строку  — src.edition если src.edition определён.

appendPublicationData(result, context, src)

Добавляет к result строку вида  — src.place: src.publisher, src.year. если хотя бы один из указанных параметров определён. Неуказанная часть опускается вместе с соответствующей пунктуацией. В частности, двоеточие ставится только если указано src.place и хотя бы что-то из src.publisher и src.year, запятая ставится только если указаны и src.publisher, и src.year. Тире и точка ставятся если указан хотя бы один из параметров.

appendVolumeAndIssue(result, context, src)

Добавляет к result строку виду  — letter_vol src.volume, letter_iss src.issue. если хотя бы один из указанных параметров определён. Запятая ставится если указаны оба параметра. letter_vol и letter_iss определяются по context.lang (например, Т. и вып. для русских текстов, Vol. и Iss. для английских).

appendPages(result, context, src)

Добавляет к result строку вида  — letter src.pages. если src.pages определён, при этом в качестве разделителя в src.pages, если это диапозон страниц, используется символ «—», а letter определяется исходя из context.lang (например, P. для английского и С. для русского).

appendNumberOfPages(result, context, src)

Добавляет к result строку вида  — src.numberOfPages letter если src.numberOfPages определён. При этом letter определяется из context.lang (p. для английского и с. для русского).

appendBookSeries(result, context, src)

Добавляет к result строку вида  — (src.bookSeries; letter_vol src.bookSeriesVolume, letter_iss src.bookSeriesIssue) если src.bookSeries определено. Точка с запятой ставится только если определено src.bookSeriesVolume или src.bookSeriesIssue, запятая ставится если определены оба параметра. letter_vol и letter_iss определяются из context.lang, аналогично тому, как это делается в appendVolumeAndIssue.

appendBookSeries(result, context, src)

Добавляет к result информацию из src.tirage если тот определён. Формат определяется из context.lang, для английского это  — ed. size: src.tirage, а для русского  — src.tirage экз..

appendIdentifiers(result, context, src)

Добавляет к result идентификаторы ISBN, ISSN, DOI, PMID и arXiv если те определены. Идентификаторы приписываются через тире, более точный формат определён в таблицах options_commas, options_issn, options_doi, options_pmid и options_arxiv.

appendSourceId(result, context, src)

Оборачивает result в <span class="wikidata_cite citetype" data-entity-id="src.sourceId"></span>, где citetyle это src.type если это поле определено и citetype_unknown в противном случае.

appendAccessDate(result, context, src)

Добавляет к result строку виду <small>Проверено dd month yyyy.</small>, где dd, month и yyyy берутся из src.accessdate если данное поле определено.

populateUrl(context, src)

Если src.url не определено, но src.sourceId известен, пытается присвоить в src.url ссылку на викитеку.

populateYear(src)

Если src.year не определён, пытается заполнить его из src.dateOfPublication и src.dateOfCreation.

populateTitle(src)

Если src.title не определён, пытается присвоить ему src.url, если и это не получается, то присваивает ''(unspecified title)''.

renderSource(context, src)

Внутренняя функция, генерирующая текст, который будет отображаться в сноске. Действует следующим образом:

  1. Записывает src.lang в context.lang (или i18nDefaultLanguage если src.lang записать не получилось).
  2. Вызывает populateUrl, populateTitle и populateYear.
  3. Заводит переменную result, изначально равную generateAuthorLinks(context, src).
  4. .Последовательно применяет к result функции appendTitle—appendAccessDate, при этом блок appendEditor—appendAccessDate дополнительно обрамляется в &lt;span class="wef_low_priority_links"&gt;&lt;/span&gt;

artificialSnaks(args)

Создаёт искусственные снеки, которые ссылаются на источник с идентификатором frame.args[1] через свойства P248 (stated in) и P805 (statement is subject of), а также пробрасывает в них аргументы (том, выпуск и т. д.).


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

local i18nDefaultLanguage = 'ru';

local i18nEtAlDefault = ' et al.';
local i18nEtAl = {
	ru	= ' и др.',
}

local i18nEditors = {
	fr	= '',
	de	= 'Hrsg.: ',
	es	= '',
	en	= '',
	it	= '',
	ru	= 'под ред. ',
}

local i18nVolume = {
	fr	= 'Vol.',
	es	= 'Vol.',
	en	= 'Vol.',
	it	= 'Vol.',
	ru	= 'Т.',
}
local i18nIssue = {
	en	= 'Iss.',
	ru	= 'вып.',
}
local i18nPages = {
	fr = 'P.',
	de = 'S.',
	es = 'P.',
	en = 'P.',
	it = 'P.',
	ru = 'С.',
}

local i18nNumberOfPages = {
	en = 'p.',
	ru = 'с.',
}

local monthg = {'января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', "сентября", "октября", "ноября", "декабря"};

local PREFIX_CITEREF = "CITEREF_";

local options_arxiv = { separator = '; ', conjunction = '; ', format = function( id ) return '[http://arxiv.org/abs/' .. id .. ' arXiv:' .. id .. ']' end, nolinks = true, preferids = false };
local options_doi = { separator = '; ', conjunction = '; ', format = function( doi ) return '[http://dx.doi.org/' .. doi .. ' doi:' .. doi .. ']' end, nolinks = true, preferids = false };
local options_issn = { separator = '; ', conjunction = '; ', format = function( issn ) return '[https://www.worldcat.org/issn/' .. issn .. ' ' .. issn .. ']' end, nolinks = true, preferids = false };

local options_commas = { separator = ', ', conjunction = ', ', format = function( src ) return src end, nolinks = false, preferids = false };
local options_commas_short = { separator = ', ', conjunction = ', ', format = function( src ) return src end, nolinks = false, preferids = false, short = true };
local options_commas_nolinks = { separator = ', ', conjunction = ', ', format = function( src ) return src end, nolinks = true, preferids = false };
local options_commas_it = { separator = ', ', conjunction = ', ', format = function( src ) return "''" .. src .. "''" end, nolinks = false, preferids = false };
local options_commas_it_short = { separator = ', ', conjunction = ', ', format = function( src ) return "''" .. src .. "''" end, nolinks = false, preferids = false, short = true };
local options_commas_it_nolinks = { separator = ', ', conjunction = ', ', format = function( src ) return "''" .. src .. "''" end, nolinks = true , preferids = false };
local options_citetypes = { separator = ' ', conjunction = ' ', format = function( src ) return 'citetype_' .. src end, nolinks = true , preferids = true };

function renderSource( context, src )
	context.lang = getLangCode( getSingle( src.lang ) ) or i18nDefaultLanguage;

	preprocessPlaces( src, context.lang );

	src.title = src.title or getSingle( src.url ) or '\'\'(unspecified title)\'\''

	if ( src.sourceId and not src.url ) then
		local entity = getEntity( context, src.sourceId );
		if ( entity.sitelinks and entity.sitelinks[ context.lang .. 'wikisource'] ) then
			src.url = ':' .. context.lang .. ':s:' .. entity.sitelinks[ context.lang .. 'wikisource' ].title;
		end
	end

	if ( not src.year and src.dateOfPublication ) then
		local date = getSingle( src.dateOfPublication );
		src.year = mw.ustring.sub( date, 2, 5 );
	end

	if ( not src.year and src.dateOfCreation ) then
		local date = getSingle( src.dateOfCreation );
		src.year = mw.ustring.sub( date, 2, 5 );
	end

	local result;
	if ( src.author ) then
		result = getPeopleAsAuthorWikitext( context, src.author, options_commas );
	end
	if ( not isEmpty( result )) then
		result = '<i class="wef_low_priority_links">' .. result .. '</i> ';
	else
		result = '';
	end
 
 	if ( src.part ) then
 		if ( src.url ) then
			result = result .. wrapInUrl( src.url, toString( context, src.part, options_commas_nolinks ) );
		else
			result = result .. toString( context, src.part, options_commas );
		end
		result = result .. ' // ' .. toString( context, src.title, options_commas );
	else
		-- title only
 		if ( src.url ) then
			result = result .. wrapInUrl( src.url, toString( context, src.title, options_commas_nolinks ) );
		else
			result = result .. toString( context, src.title, options_commas );
		end
 	end

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

	if ( src.originaltitle ) then
		result = result .. ' = ' .. toString( context, src.originaltitle, 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_it_short );
		if ( src.publication.subtitle ) then
			result = result .. ': ' .. toString( context, src.publication.subtitle, options_commas_it_short );
		end
	end

	result = result .. '<span class="wef_low_priority_links">';

	if ( src.editor ) then
		local prefix = i18nEditors[ context.lang ] or i18nEditors[ i18nDefaultLanguage ];
		result = result .. ' / ' .. prefix .. toString( context, src.editor, options_commas );
	end

	if ( src.edition ) then
		result = result .. ' — ' .. toString( context, src.edition, options_commas );
	end

	if ( src.place or src.publisher or src.year ) then
		result = result .. ' — ';
		if ( src.place ) then
			result = result .. toString( context, src.place, options_commas_short );
			if ( src.publisher or src.year ) then
				result = result .. ': ';
			end
		end
		if ( src.publisher ) then
			result = result .. toString( context, src.publisher, options_commas_short );
			if ( src.year ) then
				result = result .. ', ';
			end
		end
		if ( src.year ) then
			result = result .. toString( context, src.year, options_commas );
		end
		result = result .. '.';
	end
 
 	if ( src.volume or src.issue ) then
 		result = result .. ' — ';
		if ( src.volume ) then
			local letter = i18nVolume[ context.lang ] or i18nVolume[ i18nDefaultLanguage ];
			result = result .. letter .. '&nbsp;' .. toString( context, src.volume, options_commas );
			if ( src.issue ) then
				local letter = i18nIssue[ context.lang ] or i18nIssue[ i18nDefaultLanguage ];
				result = result .. ', ' .. letter .. '&nbsp;' .. toString( context, src.issue, options_commas ) .. '.';
			else
				result = result .. '.';
			end
		else
			local letter = i18nIssue[ context.lang ] or i18nIssue[ i18nDefaultLanguage ];
			result = result .. letter .. '&nbsp;' .. toString( context, src.issue, options_commas ) .. '.';
		end
 	end

	if ( src.pages ) then
		local letter = i18nPages[ context.lang ] or i18nPages[ i18nDefaultLanguage ];
		result = result .. ' — ' .. letter .. '&nbsp;' .. toString( context, src.pages, options_commas )  .. '.';
	end

	if ( src.numberOfPages ) then
		local letter = i18nNumberOfPages[ context.lang ] or i18nNumberOfPages[ i18nDefaultLanguage ];
		result = result .. ' — ' .. toString( context, src.numberOfPages, options_commas ) .. '&nbsp;' .. letter;
	end

	if ( src.bookSeries ) then
		result = result .. ' — (' .. toString( context, src.bookSeries, options_commas )

	 	if ( src.bookSeriesVolume or src.bookSeriesIssue ) then
	 		result = result .. '; ';
			if ( src.bookSeriesVolume ) then
				local letter = i18nVolume[ context.lang ] or i18nVolume[ i18nDefaultLanguage ];
				result = result .. letter .. '&nbsp;' .. toString( context, src.bookSeriesVolume, options_commas );
				if ( src.bookSeriesIssue ) then
					local letter = i18nIssue[ context.lang ] or i18nIssue[ i18nDefaultLanguage ];
					result = result .. ', ' .. letter .. '&nbsp;' .. toString( context, src.bookSeriesIssue, options_commas );
				else
					result = result;
				end
			else
				local letter = i18nIssue[ context.lang ] or i18nIssue[ i18nDefaultLanguage ];
				result = result .. letter .. '&nbsp;' .. toString( context, src.bookSeriesIssue, options_commas );
			end
	 	end

		result = result .. ')';
	end

	if ( src.isbn ) then
		result = result .. ' — ISBN ' .. toString( context, src.isbn, options_commas );
	end

	if ( src.issn ) then
		result = result .. ' — ISSN ' .. toString( context, src.issn, options_issn );
	end

	if ( src.doi ) then
		result = result .. ' — ' .. toString( context, src.doi, options_doi );
	end

	if ( src.arxiv ) then
		result = result .. ' — ' .. toString( context, src.arxiv, options_arxiv );
	end

	if ( src.sourceId ) then
		if ( src.type and src.sourceId ) then
			-- wrap into span to target from JS
			result = '<span class="wikidata_cite ' .. toString( context, src.type, options_citetypes ) .. '" data-entity-id="' .. getSingle( src.sourceId ) .. '">' .. result .. '</span>'
		else
			result = '<span class="wikidata_cite citetype_unknown" data-entity-id="' .. getSingle( src.sourceId ) .. '">' .. result .. '</span>'
		end
	end

	if ( src.accessdate ) then
			local date = getSingle( src.accessdate );
			local pattern = "(%-?%d+)%-(%d+)%-(%d+)T";
			local y, m, d = mw.ustring.match( date , pattern );
			y,m,d = tonumber(y),tonumber(m),tonumber(d);
			result = result .. " <small>Проверено " .. tostring(d) .. " " .. monthg[m]  .. " " .. tostring(y) .. ".</small>";
	end

	result = result .. '</span>';

    -- append invisible links to all elements used by source for tracking purposes
    local result = result .. '<div style="display:none">';
	for key, entity in pairs( context.cache ) do
		result = result .. '<a href="https://wikidata.org' ..  mw.uri.localUrl('Track:' .. key).path .. '"></a>';
	end
    result = result ..'</div>'

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

function renderShortReference( src )
	context = {
		cache = {},
		lang = getSingle( src.lang ) or i18nDefaultLanguage;
	};
	src.title = src.title or '\'\'(unspecified title)\'\''

	local result = '[[#' .. PREFIX_CITEREF .. src.code .. '|';
	if ( src.author ) then
		result = result .. toString( context, src.author, options_authors_nolinks );
	else
		result = result .. toString( context, src.title, options_commas_it_nolinks );
	end
	result = result .. ']]'

	if ( src.year ) then
		result = result .. ', ' .. toString( context, src.year, options_commas );
	end

	if ( src.volume ) then
		local letter = i18nVolume[ context.lang ] or i18nVolume[ i18nDefaultLanguage ];
		result = result .. ' — ' .. letter .. '&nbsp;' .. toString( context, src.volume, options_commas ) .. '.';
	end

	if ( src.issue ) then
		local letter = i18nIssue[ context.lang ] or i18nIssue[ i18nDefaultLanguage ];
		result = result .. ' — ' .. letter .. '&nbsp;' .. toString( context, src.issue, options_commas ) .. '.';
	end
 
	if ( src.pages ) then
		local letter = i18nPages[ context.lang ] or i18nPages[ i18nDefaultLanguage ];
		result = result .. ' — ' .. letter .. '&nbsp;' .. toString( context, src.pages, options_commas )  .. '.';
	end
 
end

function getPeopleAsAuthorWikitext( context, value, options )
	if ( type( value ) == 'string' ) then
		return personNameToAuthorName( value );
	elseif ( type( value ) == 'table' ) then
		if ( value.id ) then
			-- this is link
			if ( options.preferids ) then
				return value.id;
			else
				if ( options.nolinks ) then
					return getPersonNameAsAuthorLabel( context, value.id, value.label, options );
				else
					return getPersonNameAsAuthorWikitext( context, value.id, value.label, options );
				end
			end
		end

		local resultList = {};
		for i, tableValue in pairs( value ) do
			local nextWikitext = getPeopleAsAuthorWikitext( context, tableValue, options );
			if ( not isEmpty( nextWikitext ) ) then
				table.insert( resultList, nextWikitext );
				if ( #resultList == 4 ) then
					-- even 4 is too much, but we preserve 4th to mark that "it's more than 3"
					break;
				end
			end
		end

		local resultWikitext = '';
		for i, wikitext in pairs( resultList ) do
			if ( i == 4 ) then
				resultWikitext = resultWikitext .. ( i18nEtAl[ context.lang ] or i18nEtAlDefault );
				break;
			end
			if ( i ~= 1 ) then
				resultWikitext = resultWikitext .. ', ';
			end
			resultWikitext = resultWikitext .. wikitext;
		end

		return resultWikitext;
	end

	return options.format( '(unknown type)' );
end

function getPersonNameAsAuthorWikitext( context, entityId, customLabel, options )
	local personNameAsAuthor = getPersonNameAsAuthorLabel( context, entityId, customLabel, options);
	if ( personNameAsAuthor == nil ) then
		return nil;
	end

	local link = getElementLink( context, entityId, nil );
	return wrapInUrl( link, personNameAsAuthor );
end

function getPersonNameAsAuthorLabel( context, entityId, providedLabel, options )
	-- would custom label provided we don't need to check entity at all
	if ( not isEmpty( providedLabel ) ) then
		mw.log( 'Custom label provided for ' .. entityId );
		return personNameToAuthorName( providedLabel );
	end

	local entity = getEntity( context, entityId );
	if ( not entity ) then return '\'\'(entity ' .. entityId .. ' is missing)\'\'' end;
	if ( not isInstanceOf( entity, 'Q5' ) ) then
		mw.log( 'Entity ' .. entityId .. ' is not a person' );
		return nil;
	end

	local personName = nil;
	-- support only labels so far
	if ( entity.labels[ context.lang ] ) then
		personName = entity.labels[ context.lang ].value;
		mw.log('Got person name of ' .. entityId .. ' from label: «' .. personName .. '»' )
	end

	if ( isEmpty( personName ) ) then
		return '\'\'(not translated to ' .. context.lang .. ')\'\'';
	else
		return personNameToAuthorName( personName );
	end
end

function personNameToAuthorName( fullName )
	if ( not fullName ) then return fullName; end

	local f, i, o = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)\,%s(%a[%a\-]*)%s(%a[%a\-]*)%s*$' );
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Fa, I. O.» match' );
		return f .. '&nbsp;'
			.. mw.ustring.sub( i, 1, 1 ) .. '.&nbsp;'
			.. mw.ustring.sub( o, 1, 1 ) .. '.';
	end

	local f1, f2, i = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a[%a\-]*)\,%s(%a[%a\-]*)%s*$' );
	if ( f1 ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Fa Fa, I» match' );
		return f1 .. '&nbsp;' .. f2 .. '&nbsp;'
			.. mw.ustring.sub( i, 1, 1 ) .. '.';
	end

	local i, o, f = mw.ustring.match( fullName, '^%s*(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I. O. Fa» match' );
		return f .. '&nbsp;' .. i .. '.&nbsp;' .. o .. '.';
	end

	local i1, i2, i3, f = mw.ustring.match( fullName, '^%s*(%a)\.%s(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I. O. ?. Fa» match' );
		return f .. '&nbsp;' .. i1 .. '.&nbsp;' .. i2 .. '.&nbsp;' .. i3 .. '.';
	end

    -- Joel J. P. C. Rodrigues
	local i1, i2, i3, i4, f = mw.ustring.match( fullName, '^%s*(%a)[%a\-]+%s(%a)\.%s(%a)\.%s(%a)\.%s(%a[%a\-]+)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «I. O. ?. Fa» match' );
		return f .. '&nbsp;' .. i1 .. '.&nbsp;' .. i2 .. '.&nbsp;' .. i3 .. '.&nbsp;' .. i4 .. '.';
	end

	local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a)\.%s(%a[%a\-]*)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im O. Fa» match' );
		return f .. '&nbsp;' .. mw.ustring.sub( i, 1, 1 ) .. '.&nbsp;' .. o .. '.';
	end

	local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]*)%s(%a[%a\-]*)%s(%a[%a\-]*)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im Ot Fa» match' );
		return f .. '&nbsp;' .. mw.ustring.sub( i, 1, 1 ) .. '.&nbsp;' .. mw.ustring.sub( o, 1, 1 ) .. '.';
	end

	local i, o, f = mw.ustring.match( fullName, '^%s*(%a[%a\-]+)%s(%a[%a\-]+)%s+оглы%s+(%a[%a\-]+)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im Ot оглы Fa» match' );
		return f .. '&nbsp;' .. mw.ustring.sub( i, 1, 1 ) .. '.&nbsp;' .. mw.ustring.sub( o, 1, 1 ) .. '.';
	end

	local i, f = mw.ustring.match( fullName, '^%s*(%a[%a\-\']+)%s(%a[%a\-\']+)%s*$');
	if ( f ) then
		mw.log( 'personNameToAuthorName: «' .. fullName .. '»: have «Im Fa» match' );
		return f .. '&nbsp;' .. mw.ustring.sub( i, 1, 1 ) .. '.';
	end

	mw.log( 'Unmatched any pattern: «' .. fullName .. '»' );
	return fullName;
end

function p.renderSource( frame )
	p.currentFrame = frame;

	local arg = frame.args[1];
	local refAnchor = frame.args['ref'];
	local refAnchorYear = frame.args['ref-year'];
	local args = {};
	args.refAnchor = frame.args['ref'];
	args.refAnchorYear = frame.args['ref-year'];
	args.part = frame.args['part'];
	args.pages = frame.args['pages'];

	return p.renderSourceImpl( mw.text.trim( arg ), args );
end

function p.renderSourceImpl( entityId, args )
	args = args or {};

	local snaks = {};
	snaks.P248 = { toWikibaseEntityIdSnak( 'P248', entityId ) };
	copyArgsToSnaks( args, snaks );

	local rendered = renderReferenceImpl( mw.wikibase.getEntity(), { snaks = snaks }, args.refAnchor, args.refAnchorYear );
	if ( rendered ) then return rendered.text end;
end

function p.renderReference( frame, currentEntity, reference )
	p.currentFrame = frame;

	-- template call
	if ( frame and not currentEntity and not reference ) then
		local args = frame.args;
		if ( #frame.args == 0 ) then
			args = frame:getParent().args;
		end

		local snaks = {};

		if ( args[1] ) then
			snaks.P248 = { toWikibaseEntityIdSnak( "P248", args[1] ) };
		end
		copyArgsToSnaks( args, snaks );

		currentEntity = mw.wikibase.getEntity();
		reference = { snaks = snaks };
	end

	local rendered = renderReferenceImpl( currentEntity, reference );

	if ( not rendered ) then
		return '';
	end

	local result;
	local code = rendered.code or rendered.text;
	result = frame:extensionTag( 'ref', rendered.text, {name = code} ) .. '[[Category:ЭАНМ:Статьи с источниками из Викиданных]]';

	return result;
end

function renderReferenceImpl( currentEntity, reference, refAnchor, refAnchorYear )
	if ( not reference.snaks ) then
		return nil;
	end

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

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

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

	-- update ref name with ref-specific properties
	if ( data.code ) then
		if ( data.part ) then data.code = data.code .. '-' .. getSingle( data.part ) end
		if ( data.pages ) then data.code = data.code .. '-' .. getSingle( data.pages ) end
		if ( data.volume ) then data.code = data.code .. '-' .. getSingle( data.volume ) end
		if ( data.issue ) then data.code = data.code .. '-' .. getSingle( data.issue ) end
		if ( data.url ) then data.code = data.code .. '-' .. getSingle( data.url ) end
	end

	expandSpecials( currentEntity, reference, data );

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

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

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

	local rendered;
	if ( p.short ) then
		rendered = renderShortReference( data );
		if ( mw.ustring.len( rendered.text ) == 0 ) then
			return nil;
		end

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

		if ( refAnchor ) then
			local anchorValue = 'CITEREF' .. refAnchor .. ( coalesce( refAnchorYear, data.year ) or '' );
			rendered.text = '<span class="citation" id="' .. mw.uri.anchorEncode( anchorValue ) .. '">' .. rendered.text .. '</span>';
		end
	end

	return rendered;
end

return p;