Модуль:Поиск персоны: различия между версиями

Материал из Кадровый состав НКВД 1935-1939
Перейти к навигации Перейти к поиску
Нет описания правки
Нет описания правки
(не показано 8 промежуточных версий этого же участника)
Строка 2: Строка 2:
local global = mw.ext.luaglobal
local global = mw.ext.luaglobal
local tools = require( 'Module:Tools' )
local tools = require( 'Module:Tools' )
local cq = mw.ext.cargo.query
global.strict( true )
global.strict( true )


Строка 19: Строка 20:
end
end
end
end
fio[4] = nil
 
if fio[5] then
if fio[5] then
return '<p class=error>Невозможно разобрать название статьи. Это точно ФИО?</p>'
return '<p class=error>Невозможно разобрать. Это точно ФИО?</p>'
end
end
return table.concat( fio, ' ' )
return table.concat( fio, ' ' )
end
end


function pp.makeP( unknow )
local fio = mw.text.trim( unknow ) .. ' '
fio = mw.ustring.gsub( fio, '([%.,])([^ ])', '%1 %2')
fio = mw.ustring.gsub( fio, '  +', ' ')
fio = mw.text.split( fio, ' ', true )
if string.sub( fio[1], -1, -1 ) ~= ',' then
fio[1] = fio[1] .. ','
end
if #fio > 4 or ( fio[4] or '' ) ~= '' then
return nil
end
fio[#fio] = nil
if #fio > 1 and string.sub( fio[2], -1, -1 ) == '.' then
fio[2] = string.sub( fio[2], 1, -2 ) .. '%'
end
if #fio > 2 and string.sub( fio[3], -1, -1 ) == '.' then
fio[3] = string.sub( fio[3], 1, -2 ) .. '%'
end
local res = table.concat( fio, ' ' )
if #fio < 3 and string.sub( res, -1, -1 ) ~= '%' then
res = res .. ' %'
end
return res
end
function pp.test()
return pp.makeP('Иванов Иван Иванович') .. '\n'
.. pp.makeP('Иванов, Иван Иванович') .. '\n'
.. pp.makeP('Иванов, Иван ') .. '\n'
.. pp.makeP('Иванов, Иван И.') .. '\n'
.. pp.makeP('Иванов, И. Иванович') .. '\n'
.. pp.makeP('Иванов, И.И.') .. '\n'
.. pp.makeP('Иванов, И.') .. '\n'
.. pp.makeP('Иванов') .. '\n'
end
pp[''] = function ( frame )
pp[''] = function ( frame )
local ourName = mw.title.getCurrentTitle()
local ourName = mw.title.getCurrentTitle()
Строка 34: Строка 71:
end
end


local fio = makePattern( ourName )
local fio = pp.makeP( ourName )
-- local queryT = frame:callParserFunction{ name = '#cargo_query', args = { '',
if not fio then
-- tables = 'names',
return  '<p class=error>Невозможно разобрать. Это точно ФИО?</p>'
-- where = 'name LIKE "' .. fio .. '"',
end
-- fields = 'name,disambig,CONCAT(names._pageName)=pn',
 
-- limit = '999',
local query = cq( 'names', 'name,disambig,_pageName', {
-- format = 'native'
-- } }
-- local query = mw.ext.cargo.get()
local query = tools.cq( 'names', 'name,disambig,_pageName', {
where = 'name LIKE "' .. fio .. '"',
where = 'name LIKE "' .. fio .. '"',
limit = '999',
limit = '999',
} )
} )
 
 
local out = {}
local out = {}
if not query then
if #query == 0 then
return 'В базе не найдено людей с похожим именем (' .. fio .. ')'
return 'В базе не найдено людей с похожим именем (' .. fio .. ')'
end
end
Строка 66: Строка 99:
table.insert( out, fio )
table.insert( out, fio )
end
end
return 'Возможно, вас интересует одна из следующих персоналий: '
return 'Возможно, вас интересует одна из следующих персоналий: ' -- .. '(' .. fio .. '/' .. #query .. ') '
.. table.concat( out, ' &bull; ' )
.. table.concat( out, ' &bull; ' )


end
end
return pp
return pp

Версия от 21:02, 27 января 2018

Для документации этого модуля может быть создана страница Модуль:Поиск персоны/doc

local pp = {}
local global = mw.ext.luaglobal
local tools = require( 'Module:Tools' )
local cq = mw.ext.cargo.query
global.strict( true )

function pp.makePattern( ourName )
	local fio = mw.text.split( ourName, '[%. ]+' )
	if #fio == 1 then
		fio[1] = fio[1] .. ', %'
	else
		if mw.ustring.sub( fio[1], -1, -1) ~= ',' then
			fio[1] = fio[1] .. ','
		end
		if mw.ustring.len( fio[2] ) == 1 then
			fio[2] = fio[2] .. '%'
		end
		if fio[3] and mw.ustring.len( fio[3] ) == 1 then
			fio[3] = fio[3] .. '%'
		end
	end

	if fio[5] then
		return '<p class=error>Невозможно разобрать. Это точно ФИО?</p>'
	end
	return table.concat( fio, ' ' )
end

function pp.makeP( unknow )
	local fio = mw.text.trim( unknow ) .. ' '
	fio = mw.ustring.gsub( fio, '([%.,])([^ ])', '%1 %2')
	fio = mw.ustring.gsub( fio, '  +', ' ')
	fio = mw.text.split( fio, ' ', true )
	if string.sub( fio[1], -1, -1 ) ~= ',' then
		fio[1] = fio[1] .. ','
	end
	if #fio > 4 or ( fio[4] or '' ) ~= '' then
		return nil
	end
	fio[#fio] = nil
	if #fio > 1 and string.sub( fio[2], -1, -1 ) == '.' then
		fio[2] = string.sub( fio[2], 1, -2 ) .. '%'
	end
	if #fio > 2 and string.sub( fio[3], -1, -1 ) == '.' then
		fio[3] = string.sub( fio[3], 1, -2 ) .. '%'
	end
	local res = table.concat( fio, ' ' )
	if #fio < 3 and string.sub( res, -1, -1 ) ~= '%' then
		res = res .. ' %'
	end

	return res
end

function pp.test()
	return pp.makeP('Иванов Иван Иванович') .. '\n'
		.. pp.makeP('Иванов, Иван Иванович') .. '\n'
		.. pp.makeP('Иванов, Иван ') .. '\n'
		.. pp.makeP('Иванов, Иван И.') .. '\n'
		.. pp.makeP('Иванов, И. Иванович') .. '\n'
		.. pp.makeP('Иванов, И.И.') .. '\n'
		.. pp.makeP('Иванов, И.') .. '\n'
		.. pp.makeP('Иванов') .. '\n'
end
pp[''] = function ( frame )
	local ourName = mw.title.getCurrentTitle()
	if ourName.text == ourName.subpageText then
		ourName = ourName.text
	else
		return 'Это подстраница с дополнительными материалами (' .. ourName.baseText .. ')'
	end

	local fio = pp.makeP( ourName )
	if not fio then
		return  '<p class=error>Невозможно разобрать. Это точно ФИО?</p>'
	end

	local query = cq( 'names', 'name,disambig,_pageName', {
		where = 'name LIKE "' .. fio .. '"',
		limit = '999',
	} )


	local out = {}
	if #query == 0 then
		return 'В базе не найдено людей с похожим именем (' .. fio .. ')'
	end
	for  _, pers in ipairs( query ) do
		local fio
		if pers.name == pers._pageName then
			fio = '[[' .. pers._pageName .. ']]'
		elseif pers.name .. ' ' .. pers.disambig == pers._pageName then
			fio = '[[' .. pers._pageName .. '|' .. pers.name .. ' <span class="nkvd-idx">' .. pers.disambig .. '</span>]]'
		elseif pers.disambig == '' then
			fio = pers.name .. ' (<i>он же</i> [[' .. pers._pageName .. ']])'
		else
			fio = pers.name .. ' <span class="nkvd-idx">' .. pers.disambig .. '</span> (<i>он же</i> [[' .. pers._pageName .. ']])'
		end
		table.insert( out, fio )
	end
	return 'Возможно, вас интересует одна из следующих персоналий: ' -- .. '(' .. fio .. '/' .. #query .. ') '
		.. table.concat( out, ' &bull; ' )

end
return pp