Модуль:OobTitle

Материал из Указатель частей и соединений РККА 1941-1945
Перейти к:навигация, поиск

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

local p = {}
local global = mw.ext.luaglobal
local tools = require( 'Module:Tools' )
local cargo = mw.ext.cargo

local english = mw.getContentLanguage():getCode() == 'en'
local translation, months
if english then
	translation = {
		'Order of battle on ',
		'Order of battle in the Far East on ',
	}
	months = {
		'January',
		'February',
		'March',
		'April',
		'May',
		'June',
		'July',
		'August',
		'September',
		'October',
		'November',
		'December',
	}
else
	translation = {
		'Боевой состав на ',
		'Боевой состав войск Дальнего Востока на ',
	}
end

local dates = '22.06.1941 01.07.1941 10.07.1941 01.08.1941 01.09.1941 01.10.1941 01.11.1941 01.12.1941 '
	.. '01.01.1942 01.02.1942 01.03.1942 01.04.1942 01.05.1942 01.06.1942 01.07.1942 01.08.1942 01.09.1942 01.10.1942 01.11.1942 01.12.1942 '
	.. '01.01.1943 01.02.1943 01.03.1943 01.04.1943 01.05.1943 01.06.1943 01.07.1943 01.08.1943 01.09.1943 01.10.1943 01.11.1943 01.12.1943 '
	.. '01.01.1944 01.02.1944 01.03.1944 01.04.1944 01.05.1944 01.06.1944 01.07.1944 01.08.1944 01.09.1944 01.10.1944 01.11.1944 01.12.1944 '
	.. '01.01.1945 01.02.1945 01.03.1945 01.04.1945 01.05.1945 01.06.1945 01.07.1945 01.08.1945 09.08.1945 03.09.1945'
local LASTDATE = 54

local DV = 50 -- первый боевой состав ДВ

local function oob( dateN, pattern )
	local shift = dateN * 11 - 10
	local dateLink = string.sub( dates, shift, shift + 9 ) -- dateset[dateN]
	local dateShow = dateLink
	if english then
		local d, m, y = string.match( dateLink, '^(%d%d)%.(%d%d)%.(%d%d%d%d)$' )
		dateShow = y .. '-' .. m .. '-' .. d
		dateLink = months[tonumber( m )] .. ' ' .. tonumber( d ) .. ', ' .. y
	end
	local ptn = string.gsub( pattern, '@', dateShow )
	if dateN < DV then
		return 'Project:' .. translation[1] .. dateLink .. '|' .. ptn
	end
	return 'Project:' .. translation[2] .. dateLink .. '|' .. ptn
end

function p.Title( frame )
	local mytitle = mw.title.getCurrentTitle()
	local date = string.match( mytitle.text, '(%d%d%.%d%d%.%d%d%d%d)$' ) or mw.text.trim( frame:getParent().args[1] )
	assert( date, 'Шаблон «Шапка» может использоваться только на сводных страницах боевого состава' )

	local n = ( ( string.find( dates, date, 1, true ) or -10 ) + 10 ) / 11
	assert( n ~= 0, 'Неизвестная дата' )
	global.set( 'UDatePorno', string.sub( '00' .. n, -2, -1 ) )
	local prevOOB, nextOOB
	if n ~= 1 then
--		prevOOB = '<li class="previous">[[' .. oob( n-1, '&larr; @' ) .. ']]</li>'
		prevOOB = '<div class="previous btn btn-outline-secondary">[[' .. oob( n-1, '&larr; @' ) .. ']]</div>'
--	else
--		prevOOB = '<li class="previous disabled">&larr;</li>'
	end
	if n ~= LASTDATE then
--		nextOOB = '<li class="next">[[' .. oob( n+1, '@ &rarr;' ) .. ']]</li>'
		nextOOB = '<div class="next btn btn-outline-secondary pull-right">[[' .. oob( n+1, '@ &rarr;' ) .. ']]</div>'
--	else
--		nextOOB = '<li class="next disabled">&rarr;</li>'
	end
	local seo = frame:callParserFunction{ name = '#seo', args = { '',
				description = 'Боевой состав Советской Армии на ' .. date .. '.',
				keywords = 'боевой состав Советской Армии,Великая Отечественная война,Действующая армия,РККА,' .. date
			}
		}
	return seo .. '<div id="rkka-source"><div class="pager" style="padding-left:0">' .. ( prevOOB or '' ) .. ( nextOOB or '' ) .. '</div><br style=clear:both></div>'
	-- rkka-source is used by common.js
end

function p.Part( frame )
	local args = tools.checkargs( frame:getParent().args, {
			'',
		} )
	local x = tonumber( args[1] )
	assert ( x and x >= 0 and x <= 9, 'Недопустимый номер части таблицы боевого состава' )
	global.set( 'UPartBS', x )
	local tit = mw.title.getCurrentTitle().fullText .. '/' .. x
-- <div class="rkka-editor pull-right">[{{fullurl:{{FULLPAGENAME}}/{{{1|}}}|action=edit}} <i class="fas fa-pencil-alt"></i>]</div>{{ {{FULLPAGENAME}}/{{{1|x}}} }}
	return '<div class="rkka-editor pull-right">['
		.. tostring( mw.uri.fullUrl( tit, { action = 'edit' } ) )
		.. '<i class="fas fa-pencil-alt"></i>]</div>'
		.. frame:expandTemplate{ title = tit }
end

function p.Write( frame )
	local args = tools.checkargs( frame:getParent().args, {
			'',
		} )
	local uDBmode = global.get( 'UDBmode' )
	local mytitle = mw.title.getCurrentTitle()
	local stamp, part = string.match( mytitle.text, ' (%d%d%.%d%d%.%d%d%d%d)/(.)$' )
	if stamp then
		local n = ( ( string.find( dates, stamp, 1, true ) or -10 ) + 10 ) / 11
		assert( n ~= 0, 'Неизвестная дата' )
		global.set( 'UDatePorno', string.sub( '00' .. n, -2, -1 ) )
		global.set( 'UPartBS', tonumber( part ) or '-' )
	end
	local locked = ( mw.title.new( 'РККА:Блокировка БД' ).id or 0 ) ~= 0
--[[
заменяем uDry на uDBmode: nil — никакой записи, 1 — блокировка страницей-семафором, 2 — протокол,
у нас есть типы страниц с шаблоном «Запись»: «пишущие части», «тестовые», «публичные»
и страница-семафор

Шаблон Запись (начало):
- /1 + семафор — nil
- /1 без семафора —  1
- /t - 2
- основная — 3

на основных мы считаем в UDBcount число записей в базу
--]]

	if args[1] == 'конец' then
		if uDBmode == 1 then
			return 	frame:expandTemplate{ title = 'Таблица формирований', args = {
								unit = 'Запись в БД', tab = 'финиш',
							} }
		elseif uDBmode == 2 then
--			return mw.dumpObject( global.get( 'RKKA-log' ) )
			local log, res = global.get( 'RKKA-log' ), {}
			for _, t in ipairs( log ) do
				local row = {}
				table.insert( row, t.unitId or '—' )
				table.insert( row, '[[' .. ( t.unit or '—' ) .. ']]' )
				table.insert( row, '<i>' .. ( t.parentId or '—' ) .. '</i>' )
				table.insert( row, '<i>[[' .. ( t.parent or '—' ) .. ']]</i>' )
				table.insert( row, t.structParentId or '—' )
				table.insert( row, t.addendum or '—' )
				table.insert( row, t.ref or '—' )
				table.insert( row, t.ass or '—' )
				table.insert( row, t.tab or '—' )
				table.insert( row, t.creating or '—' )
				table.insert( res, '<tr><td>' .. table.concat( row, '</td><td>' ) .. '</td></tr>\n' )
			end
			return '<table class="table table-condensed table-hover>'
				.. '<tr><th>Id</th><th>Unit</th><th>pId</th><th>Parent</th><th>strP</th><th>add</th><th>ref</th><th>Ass</th><th>Tab</th><th>Create</th></tr>\n'
				.. table.concat( res ) .. '</table>'
		elseif uDBmode == 3 then
--			do return '(' .. ( global.get( 'UPartBS' ) or ' ((?)) ' ) .. ')<br>' end
--			local tmp = '/' .. global.get( 'UPartBS' )
--			tmp = mw.title.getCurrentTitle() .. '/'
			local dbCount = cargo.query( 'units', 'COUNT(_pageID)=X',
				{ where = '_pageTitle="' .. mw.title.getCurrentTitle().text .. '/' .. global.get( 'UPartBS' ) .. '"' } )
			local luaCount = global.get( 'UDBcount' )
			frame:expandTemplate{ title = 'Таблица контроля БД', args = {
								pagel = luaCount, basel = dbCount[1].X - 2,
							} }
			if  luaCount ~= dbCount[1].X - 2 then
				return '<p class="rkka-editor error">Ошибка! В базе ' .. ( dbCount[1].X - 2 ) .. ' записей вместо ' .. luaCount .. '</p>'
			else
--				return '<p class="rkka-editor">В базе ' .. dbCount[1].X .. ' записей</p>'
			end
			global.set( 'UDBcount', 1 ) --09022019
		else
			return
		end
	end

	-- аргумент не «конец» или uDBmode == 3

	local txt = ''
	if tonumber( part ) then
		if locked then
			uDBmode = nil
			txt = '<p class=error>Запись в базу данных [[РККА:Блокировка БД|<span class=error>заблокирована!</span>]]</p>'
		else
			uDBmode = 1
			frame:expandTemplate{ title = 'Таблица формирований', args = {
								unit = 'Запись в БД', tab = 'старт',
							} }
		end
		txt = txt .. '[[Category:Боевой состав|'
			.. string.sub( stamp, 7, 10 ) .. string.sub( stamp, 4, 5 ) .. string.sub( stamp, 1, 2 )
			.. part .. ']]'
	elseif part then
		uDBmode = 2
	else
		uDBmode = 3
		global.set( 'UDBcount', 0 ) --09022019
	end
	global.set( 'UDBmode', uDBmode )
	return txt
end


return p